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, 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, XLogRecPtr lsn, RepOriginId origin_id)
 
void xact_redo (XLogReaderState *record)
 
bool IsSubTransactionAssignmentPending (void)
 
void MarkSubTransactionAssigned (void)
 

Variables

int DefaultXactIsoLevel = XACT_READ_COMMITTED
 
int XactIsoLevel
 
bool DefaultXactReadOnly = false
 
bool XactReadOnly
 
bool DefaultXactDeferrable = false
 
bool XactDeferrable
 
int synchronous_commit = SYNCHRONOUS_COMMIT_ON
 
TransactionId CheckXidAlive = InvalidTransactionId
 
bool bsysscan = false
 
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 229 of file xact.c.

Referenced by EstimateTransactionStateSpace(), and SerializeTransactionState().

Typedef Documentation

◆ SerializedTransactionState

◆ SubXactCallbackItem

◆ TBlockState

typedef enum TBlockState TBlockState

◆ TransactionState

Definition at line 211 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 153 of file xact.c.

154 {
155  /* not-in-transaction-block states */
156  TBLOCK_DEFAULT, /* idle */
157  TBLOCK_STARTED, /* running single-query transaction */
158 
159  /* transaction block states */
160  TBLOCK_BEGIN, /* starting transaction block */
161  TBLOCK_INPROGRESS, /* live transaction */
162  TBLOCK_IMPLICIT_INPROGRESS, /* live transaction after implicit BEGIN */
163  TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
164  TBLOCK_END, /* COMMIT received */
165  TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
166  TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
167  TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
168  TBLOCK_PREPARE, /* live xact, PREPARE received */
169 
170  /* subtransaction states */
171  TBLOCK_SUBBEGIN, /* starting a subtransaction */
172  TBLOCK_SUBINPROGRESS, /* live subtransaction */
173  TBLOCK_SUBRELEASE, /* RELEASE received */
174  TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
175  TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
176  TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
177  TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
178  TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
179  TBLOCK_SUBABORT_RESTART /* failed subxact, ROLLBACK TO received */
180 } TBlockState;
TBlockState
Definition: xact.c:153

◆ TransState

enum TransState
Enumerator
TRANS_DEFAULT 
TRANS_START 
TRANS_INPROGRESS 
TRANS_COMMIT 
TRANS_ABORT 
TRANS_PREPARE 

Definition at line 137 of file xact.c.

138 {
139  TRANS_DEFAULT, /* idle */
140  TRANS_START, /* transaction starting */
141  TRANS_INPROGRESS, /* inside a valid transaction */
142  TRANS_COMMIT, /* commit in progress */
143  TRANS_ABORT, /* abort in progress */
144  TRANS_PREPARE /* prepare in progress */
145 } TransState;
TransState
Definition: xact.c:137

Function Documentation

◆ AbortCurrentTransaction()

void AbortCurrentTransaction ( void  )

Definition at line 3220 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(), ReorderBufferImmediateInvalidation(), ReorderBufferProcessTXN(), and SnapBuildClearExportedSnapshot().

3221 {
3223 
3224  switch (s->blockState)
3225  {
3226  case TBLOCK_DEFAULT:
3227  if (s->state == TRANS_DEFAULT)
3228  {
3229  /* we are idle, so nothing to do */
3230  }
3231  else
3232  {
3233  /*
3234  * We can get here after an error during transaction start
3235  * (state will be TRANS_START). Need to clean up the
3236  * incompletely started transaction. First, adjust the
3237  * low-level state to suppress warning message from
3238  * AbortTransaction.
3239  */
3240  if (s->state == TRANS_START)
3241  s->state = TRANS_INPROGRESS;
3242  AbortTransaction();
3244  }
3245  break;
3246 
3247  /*
3248  * If we aren't in a transaction block, we just do the basic abort
3249  * & cleanup transaction. For this purpose, we treat an implicit
3250  * transaction block as if it were a simple statement.
3251  */
3252  case TBLOCK_STARTED:
3254  AbortTransaction();
3257  break;
3258 
3259  /*
3260  * If we are in TBLOCK_BEGIN it means something screwed up right
3261  * after reading "BEGIN TRANSACTION". We assume that the user
3262  * will interpret the error as meaning the BEGIN failed to get him
3263  * into a transaction block, so we should abort and return to idle
3264  * state.
3265  */
3266  case TBLOCK_BEGIN:
3267  AbortTransaction();
3270  break;
3271 
3272  /*
3273  * We are somewhere in a transaction block and we've gotten a
3274  * failure, so we abort the transaction and set up the persistent
3275  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3276  */
3277  case TBLOCK_INPROGRESS:
3279  AbortTransaction();
3280  s->blockState = TBLOCK_ABORT;
3281  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3282  break;
3283 
3284  /*
3285  * Here, we failed while trying to COMMIT. Clean up the
3286  * transaction and return to idle state (we do not want to stay in
3287  * the transaction).
3288  */
3289  case TBLOCK_END:
3290  AbortTransaction();
3293  break;
3294 
3295  /*
3296  * Here, we are already in an aborted transaction state and are
3297  * waiting for a ROLLBACK, but for some reason we failed again! So
3298  * we just remain in the abort state.
3299  */
3300  case TBLOCK_ABORT:
3301  case TBLOCK_SUBABORT:
3302  break;
3303 
3304  /*
3305  * We are in a failed transaction and we got the ROLLBACK command.
3306  * We have already aborted, we just need to cleanup and go to idle
3307  * state.
3308  */
3309  case TBLOCK_ABORT_END:
3312  break;
3313 
3314  /*
3315  * We are in a live transaction and we got a ROLLBACK command.
3316  * Abort, cleanup, go to idle state.
3317  */
3318  case TBLOCK_ABORT_PENDING:
3319  AbortTransaction();
3322  break;
3323 
3324  /*
3325  * Here, we failed while trying to PREPARE. Clean up the
3326  * transaction and return to idle state (we do not want to stay in
3327  * the transaction).
3328  */
3329  case TBLOCK_PREPARE:
3330  AbortTransaction();
3333  break;
3334 
3335  /*
3336  * We got an error inside a subtransaction. Abort just the
3337  * subtransaction, and go to the persistent SUBABORT state until
3338  * we get ROLLBACK.
3339  */
3340  case TBLOCK_SUBINPROGRESS:
3343  break;
3344 
3345  /*
3346  * If we failed while trying to create a subtransaction, clean up
3347  * the broken subtransaction and abort the parent. The same
3348  * applies if we get a failure while ending a subtransaction.
3349  */
3350  case TBLOCK_SUBBEGIN:
3351  case TBLOCK_SUBRELEASE:
3352  case TBLOCK_SUBCOMMIT:
3354  case TBLOCK_SUBRESTART:
3358  break;
3359 
3360  /*
3361  * Same as above, except the Abort() was already done.
3362  */
3363  case TBLOCK_SUBABORT_END:
3367  break;
3368  }
3369 }
void AbortCurrentTransaction(void)
Definition: xact.c:3220
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192
TransState state
Definition: xact.c:191
static void CleanupTransaction(void)
Definition: xact.c:2799
static void AbortSubTransaction(void)
Definition: xact.c:4934
static void AbortTransaction(void)
Definition: xact.c:2611
static void CleanupSubTransaction(void)
Definition: xact.c:5088

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4590 {
4592 
4593  /* Ensure we're not running in a doomed memory context */
4594  AtAbort_Memory();
4595 
4596  /*
4597  * Get out of any transaction or nested transaction
4598  */
4599  do
4600  {
4601  switch (s->blockState)
4602  {
4603  case TBLOCK_DEFAULT:
4604  if (s->state == TRANS_DEFAULT)
4605  {
4606  /* Not in a transaction, do nothing */
4607  }
4608  else
4609  {
4610  /*
4611  * We can get here after an error during transaction start
4612  * (state will be TRANS_START). Need to clean up the
4613  * incompletely started transaction. First, adjust the
4614  * low-level state to suppress warning message from
4615  * AbortTransaction.
4616  */
4617  if (s->state == TRANS_START)
4618  s->state = TRANS_INPROGRESS;
4619  AbortTransaction();
4621  }
4622  break;
4623  case TBLOCK_STARTED:
4624  case TBLOCK_BEGIN:
4625  case TBLOCK_INPROGRESS:
4628  case TBLOCK_END:
4629  case TBLOCK_ABORT_PENDING:
4630  case TBLOCK_PREPARE:
4631  /* In a transaction, so clean up */
4632  AbortTransaction();
4635  break;
4636  case TBLOCK_ABORT:
4637  case TBLOCK_ABORT_END:
4638 
4639  /*
4640  * AbortTransaction is already done, still need Cleanup.
4641  * However, if we failed partway through running ROLLBACK,
4642  * there will be an active portal running that command, which
4643  * we need to shut down before doing CleanupTransaction.
4644  */
4645  AtAbort_Portals();
4648  break;
4649 
4650  /*
4651  * In a subtransaction, so clean it up and abort parent too
4652  */
4653  case TBLOCK_SUBBEGIN:
4654  case TBLOCK_SUBINPROGRESS:
4655  case TBLOCK_SUBRELEASE:
4656  case TBLOCK_SUBCOMMIT:
4658  case TBLOCK_SUBRESTART:
4661  s = CurrentTransactionState; /* changed by pop */
4662  break;
4663 
4664  case TBLOCK_SUBABORT:
4665  case TBLOCK_SUBABORT_END:
4667  /* As above, but AbortSubTransaction already done */
4668  if (s->curTransactionOwner)
4669  {
4670  /* As in TBLOCK_ABORT, might have a live portal to zap */
4675  }
4677  s = CurrentTransactionState; /* changed by pop */
4678  break;
4679  }
4680  } while (s->blockState != TBLOCK_DEFAULT);
4681 
4682  /* Should be out of all subxacts now */
4683  Assert(s->parent == NULL);
4684 
4685  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4686  AtCleanup_Memory();
4687 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void AtCleanup_Memory(void)
Definition: xact.c:1843
void AtAbort_Portals(void)
Definition: portalmem.c:779
TBlockState blockState
Definition: xact.c:192
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
static void CleanupTransaction(void)
Definition: xact.c:2799
struct TransactionStateData * parent
Definition: xact.c:208
SubTransactionId subTransactionId
Definition: xact.c:188
static void AbortSubTransaction(void)
Definition: xact.c:4934
static void AtAbort_Memory(void)
Definition: xact.c:1753
static void AbortTransaction(void)
Definition: xact.c:2611
#define Assert(condition)
Definition: c.h:804
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:975
static void CleanupSubTransaction(void)
Definition: xact.c:5088

◆ AbortSubTransaction()

static void AbortSubTransaction ( void  )
static

Definition at line 4934 of file xact.c.

References AbortBufferIO(), AfterTriggerEndSubXact(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubAbort_childXids(), AtSubAbort_Memory(), AtSubAbort_Notify(), AtSubAbort_Portals(), AtSubAbort_ResourceOwner(), AtSubAbort_smgr(), AtSubAbort_Snapshot(), CallSubXactCallbacks(), 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(), ResetLogicalStreamingState(), 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().

4935 {
4937 
4938  /* Prevent cancel/die interrupt while cleaning up */
4939  HOLD_INTERRUPTS();
4940 
4941  /* Make sure we have a valid memory context and resource owner */
4944 
4945  /*
4946  * Release any LW locks we might be holding as quickly as possible.
4947  * (Regular locks, however, must be held till we finish aborting.)
4948  * Releasing LW locks is critical since we might try to grab them again
4949  * while cleaning up!
4950  *
4951  * FIXME This may be incorrect --- Are there some locks we should keep?
4952  * Buffer locks, for example? I don't think so but I'm not sure.
4953  */
4954  LWLockReleaseAll();
4955 
4958  AbortBufferIO();
4959  UnlockBuffers();
4960 
4961  /* Reset WAL record construction state */
4963 
4964  /* Cancel condition variable sleep */
4966 
4967  /*
4968  * Also clean up any open wait for lock, since the lock manager will choke
4969  * if we try to wait for another lock before doing this.
4970  */
4971  LockErrorCleanup();
4972 
4973  /*
4974  * If any timeout events are still active, make sure the timeout interrupt
4975  * is scheduled. This covers possible loss of a timeout interrupt due to
4976  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4977  * We delay this till after LockErrorCleanup so that we don't uselessly
4978  * reschedule lock or deadlock check timeouts.
4979  */
4981 
4982  /*
4983  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4984  * handler. We do this fairly early in the sequence so that the timeout
4985  * infrastructure will be functional if needed while aborting.
4986  */
4988 
4989  /*
4990  * check the current transaction state
4991  */
4992  ShowTransactionState("AbortSubTransaction");
4993 
4994  if (s->state != TRANS_INPROGRESS)
4995  elog(WARNING, "AbortSubTransaction while in %s state",
4997 
4998  s->state = TRANS_ABORT;
4999 
5000  /*
5001  * Reset user ID which might have been changed transiently. (See notes in
5002  * AbortTransaction.)
5003  */
5005 
5006  /* Forget about any active REINDEX. */
5008 
5009  /* Reset logical streaming state. */
5011 
5012  /* Exit from parallel mode, if necessary. */
5013  if (IsInParallelMode())
5014  {
5016  s->parallelModeLevel = 0;
5017  }
5018 
5019  /*
5020  * We can skip all this stuff if the subxact failed before creating a
5021  * ResourceOwner...
5022  */
5023  if (s->curTransactionOwner)
5024  {
5025  AfterTriggerEndSubXact(false);
5031  s->parent->subTransactionId);
5033 
5034  /* Advertise the fact that we aborted in pg_xact. */
5035  (void) RecordTransactionAbort(true);
5036 
5037  /* Post-abort cleanup */
5040 
5042  s->parent->subTransactionId);
5043 
5046  false, false);
5048  s->parent->subTransactionId);
5049  AtEOSubXact_Inval(false);
5052  false, false);
5055  false, false);
5056  AtSubAbort_smgr();
5057 
5058  AtEOXact_GUC(false, s->gucNestLevel);
5059  AtEOSubXact_SPI(false, s->subTransactionId);
5061  s->parent->subTransactionId);
5063  s->parent->subTransactionId);
5065  s->parent->subTransactionId);
5067  AtEOSubXact_PgStat(false, s->nestingLevel);
5069  }
5070 
5071  /*
5072  * Restore the upper transaction's read-only state, too. This should be
5073  * redundant with GUC's cleanup but we may as well do it for consistency
5074  * with the commit case.
5075  */
5077 
5079 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1798
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3223
void ResetReindexState(int nestLevel)
Definition: index.c:4042
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void pgstat_report_wait_end(void)
Definition: wait_event.h:278
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:607
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1909
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:393
void pgstat_progress_end_command(void)
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:504
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:133
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:970
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
bool IsInParallelMode(void)
Definition: xact.c:1012
void ConditionVariableCancelSleep(void)
static void AtSubAbort_Memory(void)
Definition: xact.c:1773
void XLogResetInsertion(void)
Definition: xloginsert.c:208
void ResetLogicalStreamingState(void)
Definition: logical.c:1793
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5350
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6230
SubTransactionId subTransactionId
Definition: xact.c:188
sigset_t UnBlockSig
Definition: pqsignal.c:22
FullTransactionId fullTransactionId
Definition: xact.c:187
static void AtSubAbort_childXids(void)
Definition: xact.c:1811
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2472
void LockErrorCleanup(void)
Definition: proc.c:716
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:3979
void AtSubAbort_Notify(void)
Definition: async.c:1810
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:16417
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:3008
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3626
bool XactReadOnly
Definition: xact.c:78
int parallelModeLevel
Definition: xact.c:205
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:486
static const char * TransStateAsString(TransState state)
Definition: xact.c:5453
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:975
void AtSubAbort_smgr(void)
Definition: storage.c:914
bool prevXactReadOnly
Definition: xact.c:202
void AbortBufferIO(void)
Definition: bufmgr.c:4477
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4988
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1064
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:131
#define elog(elevel,...)
Definition: elog.h:232
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4177
void LWLockReleaseAll(void)
Definition: lwlock.c:1902
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1215
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1641

◆ AbortTransaction()

static void AbortTransaction ( void  )
static

Definition at line 2611 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(), ResetLogicalStreamingState(), 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().

2612 {
2614  TransactionId latestXid;
2615  bool is_parallel_worker;
2616 
2617  /* Prevent cancel/die interrupt while cleaning up */
2618  HOLD_INTERRUPTS();
2619 
2620  /* Make sure we have a valid memory context and resource owner */
2621  AtAbort_Memory();
2623 
2624  /*
2625  * Release any LW locks we might be holding as quickly as possible.
2626  * (Regular locks, however, must be held till we finish aborting.)
2627  * Releasing LW locks is critical since we might try to grab them again
2628  * while cleaning up!
2629  */
2630  LWLockReleaseAll();
2631 
2632  /* Clear wait information and command progress indicator */
2635 
2636  /* Clean up buffer I/O and buffer context locks, too */
2637  AbortBufferIO();
2638  UnlockBuffers();
2639 
2640  /* Reset WAL record construction state */
2642 
2643  /* Cancel condition variable sleep */
2645 
2646  /*
2647  * Also clean up any open wait for lock, since the lock manager will choke
2648  * if we try to wait for another lock before doing this.
2649  */
2650  LockErrorCleanup();
2651 
2652  /*
2653  * If any timeout events are still active, make sure the timeout interrupt
2654  * is scheduled. This covers possible loss of a timeout interrupt due to
2655  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2656  * We delay this till after LockErrorCleanup so that we don't uselessly
2657  * reschedule lock or deadlock check timeouts.
2658  */
2660 
2661  /*
2662  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2663  * handler. We do this fairly early in the sequence so that the timeout
2664  * infrastructure will be functional if needed while aborting.
2665  */
2667 
2668  /*
2669  * check the current transaction state
2670  */
2671  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2672  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2673  elog(WARNING, "AbortTransaction while in %s state",
2675  Assert(s->parent == NULL);
2676 
2677  /*
2678  * set the current transaction state information appropriately during the
2679  * abort processing
2680  */
2681  s->state = TRANS_ABORT;
2682 
2683  /*
2684  * Reset user ID which might have been changed transiently. We need this
2685  * to clean up in case control escaped out of a SECURITY DEFINER function
2686  * or other local change of CurrentUserId; therefore, the prior value of
2687  * SecurityRestrictionContext also needs to be restored.
2688  *
2689  * (Note: it is not necessary to restore session authorization or role
2690  * settings here because those can only be changed via GUC, and GUC will
2691  * take care of rolling them back if need be.)
2692  */
2694 
2695  /* Forget about any active REINDEX. */
2697 
2698  /* Reset logical streaming state. */
2700 
2701  /* If in parallel mode, clean up workers and exit parallel mode. */
2702  if (IsInParallelMode())
2703  {
2704  AtEOXact_Parallel(false);
2705  s->parallelModeLevel = 0;
2706  }
2707 
2708  /*
2709  * do abort processing
2710  */
2711  AfterTriggerEndXact(false); /* 'false' means it's abort */
2712  AtAbort_Portals();
2713  smgrDoPendingSyncs(false, is_parallel_worker);
2714  AtEOXact_LargeObject(false);
2715  AtAbort_Notify();
2716  AtEOXact_RelationMap(false, is_parallel_worker);
2717  AtAbort_Twophase();
2718 
2719  /*
2720  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2721  * far as assigning an XID to advertise). But if we're inside a parallel
2722  * worker, skip this; the user backend must be the one to write the abort
2723  * record.
2724  */
2725  if (!is_parallel_worker)
2726  latestXid = RecordTransactionAbort(false);
2727  else
2728  {
2729  latestXid = InvalidTransactionId;
2730 
2731  /*
2732  * Since the parallel leader won't get our value of XactLastRecEnd in
2733  * this case, we nudge WAL-writer ourselves in this case. See related
2734  * comments in RecordTransactionAbort for why this matters.
2735  */
2737  }
2738 
2739  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2740 
2741  /*
2742  * Let others know about no transaction in progress by me. Note that this
2743  * must be done _before_ releasing locks we hold and _after_
2744  * RecordTransactionAbort.
2745  */
2746  ProcArrayEndTransaction(MyProc, latestXid);
2747 
2748  /*
2749  * Post-abort cleanup. See notes in CommitTransaction() concerning
2750  * ordering. We can skip all of it if the transaction failed before
2751  * creating a resource owner.
2752  */
2753  if (TopTransactionResourceOwner != NULL)
2754  {
2755  if (is_parallel_worker)
2757  else
2759 
2762  false, true);
2763  AtEOXact_Buffers(false);
2764  AtEOXact_RelationCache(false);
2765  AtEOXact_Inval(false);
2769  false, true);
2772  false, true);
2773  smgrDoPendingDeletes(false);
2774 
2775  AtEOXact_GUC(false, 1);
2776  AtEOXact_SPI(false);
2777  AtEOXact_Enum();
2779  AtEOXact_Namespace(false, is_parallel_worker);
2780  AtEOXact_SMgr();
2781  AtEOXact_Files(false);
2783  AtEOXact_HashTables(false);
2784  AtEOXact_PgStat(false, is_parallel_worker);
2785  AtEOXact_ApplyLauncher(false);
2787  }
2788 
2789  /*
2790  * State remains TRANS_ABORT until CleanupTransaction().
2791  */
2793 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:766
void ResetReindexState(int nestLevel)
Definition: index.c:4042
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:612
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1785
static TransactionState CurrentTransactionState
Definition: xact.c:250
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:1007
static void pgstat_report_wait_end(void)
Definition: wait_event.h:278
uint32 TransactionId
Definition: c.h:587
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:607
XLogRecPtr XactLastRecEnd
Definition: xlog.c:344
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3078
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
PGPROC * MyProc
Definition: proc.c:68
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2579
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3571
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:16385
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:654
void pgstat_progress_end_command(void)
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:504
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3041
void AtAbort_Portals(void)
Definition: portalmem.c:779
TBlockState blockState
Definition: xact.c:192
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:133
void pgstat_report_xact_timestamp(TimestampTz tstamp)
TransState state
Definition: xact.c:191
void AtEOXact_SMgr(void)
Definition: smgr.c:678
bool IsInParallelMode(void)
Definition: xact.c:1012
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4892
void XLogResetInsertion(void)
Definition: xloginsert.c:208
void ResetLogicalStreamingState(void)
Definition: logical.c:1793
struct TransactionStateData * parent
Definition: xact.c:208
void AtAbort_Twophase(void)
Definition: twophase.c:317
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2385
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6230
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
void LockErrorCleanup(void)
Definition: proc.c:716
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3979
static void AtAbort_Memory(void)
Definition: xact.c:1753
void AtAbort_Notify(void)
Definition: async.c:1720
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2701
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:804
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:486
static const char * TransStateAsString(TransState state)
Definition: xact.c:5453
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:371
void AtEOXact_MultiXact(void)
Definition: multixact.c:1686
void AtEOXact_RelationMap(bool isCommit, bool isParallelWorker)
Definition: relmapper.c:476
void AbortBufferIO(void)
Definition: bufmgr.c:4477
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:131
#define elog(elevel,...)
Definition: elog.h:232
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:680
void LWLockReleaseAll(void)
Definition: lwlock.c:1902
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1883
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1234
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4109
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1641
LocalTransactionId lxid
Definition: proc.h:143

◆ AssignTransactionId()

static void AssignTransactionId ( TransactionState  s)
static

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

570 {
571  bool isSubXact = (s->parent != NULL);
572  ResourceOwner currentOwner;
573  bool log_unknown_top = false;
574 
575  /* Assert that caller didn't screw up */
578 
579  /*
580  * Workers synchronize transaction state at the beginning of each parallel
581  * operation, so we can't account for new XIDs at this point.
582  */
584  elog(ERROR, "cannot assign XIDs during a parallel operation");
585 
586  /*
587  * Ensure parent(s) have XIDs, so that a child always has an XID later
588  * than its parent. Mustn't recurse here, or we might get a stack
589  * overflow if we're at the bottom of a huge stack of subtransactions none
590  * of which have XIDs yet.
591  */
592  if (isSubXact && !FullTransactionIdIsValid(s->parent->fullTransactionId))
593  {
594  TransactionState p = s->parent;
595  TransactionState *parents;
596  size_t parentOffset = 0;
597 
598  parents = palloc(sizeof(TransactionState) * s->nestingLevel);
599  while (p != NULL && !FullTransactionIdIsValid(p->fullTransactionId))
600  {
601  parents[parentOffset++] = p;
602  p = p->parent;
603  }
604 
605  /*
606  * This is technically a recursive call, but the recursion will never
607  * be more than one layer deep.
608  */
609  while (parentOffset != 0)
610  AssignTransactionId(parents[--parentOffset]);
611 
612  pfree(parents);
613  }
614 
615  /*
616  * When wal_level=logical, guarantee that a subtransaction's xid can only
617  * be seen in the WAL stream if its toplevel xid has been logged before.
618  * If necessary we log an xact_assignment record with fewer than
619  * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
620  * for a transaction even though it appears in a WAL record, we just might
621  * superfluously log something. That can happen when an xid is included
622  * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
623  * xl_standby_locks.
624  */
625  if (isSubXact && XLogLogicalInfoActive() &&
627  log_unknown_top = true;
628 
629  /*
630  * Generate a new FullTransactionId and record its xid in PG_PROC and
631  * pg_subtrans.
632  *
633  * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
634  * shared storage other than PG_PROC; because if there's no room for it in
635  * PG_PROC, the subtrans entry is needed to ensure that other backends see
636  * the Xid as "running". See GetNewTransactionId.
637  */
638  s->fullTransactionId = GetNewTransactionId(isSubXact);
639  if (!isSubXact)
641 
642  if (isSubXact)
645 
646  /*
647  * If it's a top-level transaction, the predicate locking system needs to
648  * be told about it too.
649  */
650  if (!isSubXact)
652 
653  /*
654  * Acquire lock on the transaction XID. (We assume this cannot block.) We
655  * have to ensure that the lock is assigned to the transaction's own
656  * ResourceOwner.
657  */
658  currentOwner = CurrentResourceOwner;
660 
662 
663  CurrentResourceOwner = currentOwner;
664 
665  /*
666  * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
667  * top-level transaction we issue a WAL record for the assignment. We
668  * include the top-level xid and all the subxids that have not yet been
669  * reported using XLOG_XACT_ASSIGNMENT records.
670  *
671  * This is required to limit the amount of shared memory required in a hot
672  * standby server to keep track of in-progress XIDs. See notes for
673  * RecordKnownAssignedTransactionIds().
674  *
675  * We don't keep track of the immediate parent of each subxid, only the
676  * top-level transaction that each subxact belongs to. This is correct in
677  * recovery only because aborted subtransactions are separately WAL
678  * logged.
679  *
680  * This is correct even for the case where several levels above us didn't
681  * have an xid assigned as we recursed up to them beforehand.
682  */
683  if (isSubXact && XLogStandbyInfoActive())
684  {
686  nUnreportedXids++;
687 
688  /*
689  * ensure this test matches similar one in
690  * RecoverPreparedTransactions()
691  */
693  log_unknown_top)
694  {
695  xl_xact_assignment xlrec;
696 
697  /*
698  * xtop is always set by now because we recurse up transaction
699  * stack to the highest unassigned xid and then come back down
700  */
701  xlrec.xtop = GetTopTransactionId();
703  xlrec.nsubxacts = nUnreportedXids;
704 
705  XLogBeginInsert();
706  XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
708  nUnreportedXids * sizeof(TransactionId));
709 
710  (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
711 
712  nUnreportedXids = 0;
713  /* mark top, not current xact as having been logged */
715  }
716  }
717 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
uint32 TransactionId
Definition: c.h:587
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
TransactionId xtop
Definition: xact.h:198
TransactionId GetTopTransactionId(void)
Definition: xact.c:410
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
void pfree(void *pointer)
Definition: mcxt.c:1169
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:46
#define XLogLogicalInfoActive()
Definition: xlog.h:183
struct TransactionStateData * parent
Definition: xact.c:208
#define MinSizeOfXactAssignment
Definition: xact.h:203
FullTransactionId fullTransactionId
Definition: xact.c:187
#define IsParallelWorker()
Definition: parallel.h:61
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:340
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:432
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:153
void XactLockTableInsert(TransactionId xid)
Definition: lmgr.c:599
#define XLogStandbyInfoActive()
Definition: xlog.h:180
static int nUnreportedXids
Definition: xact.c:247
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
#define Assert(condition)
Definition: c.h:804
static TransactionStateData TopTransactionStateData
Definition: xact.c:237
void RegisterPredicateLockingXid(TransactionId xid)
Definition: predicate.c:1941
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
FullTransactionId GetNewTransactionId(bool isSubXact)
Definition: varsup.c:50
static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: xact.c:248
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void AssignTransactionId(TransactionState s)
Definition: xact.c:569
void SubTransSetParent(TransactionId xid, TransactionId parent)
Definition: subtrans.c:74
void XLogBeginInsert(void)
Definition: xloginsert.c:135

◆ AtAbort_Memory()

static void AtAbort_Memory ( void  )
static

Definition at line 1753 of file xact.c.

References MemoryContextSwitchTo(), and TopMemoryContext.

Referenced by AbortOutOfAnyTransaction(), and AbortTransaction().

1754 {
1755  /*
1756  * Switch into TransactionAbortContext, which should have some free space
1757  * even if nothing else does. We'll work in this context until we've
1758  * finished cleaning up.
1759  *
1760  * It is barely possible to get here when we've not been able to create
1761  * TransactionAbortContext yet; if so use TopMemoryContext.
1762  */
1763  if (TransactionAbortContext != NULL)
1765  else
1767 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:290
MemoryContext TopMemoryContext
Definition: mcxt.c:48

◆ AtAbort_ResourceOwner()

static void AtAbort_ResourceOwner ( void  )
static

Definition at line 1785 of file xact.c.

References CurrentResourceOwner, and TopTransactionResourceOwner.

Referenced by AbortTransaction().

1786 {
1787  /*
1788  * Make sure we have a valid ResourceOwner, if possible (else it will be
1789  * NULL, which is OK)
1790  */
1792 }
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146

◆ AtCCI_LocalCache()

static void AtCCI_LocalCache ( void  )
static

Definition at line 1476 of file xact.c.

References AtCCI_RelationMap(), and CommandEndInvalidationMessages().

Referenced by CommandCounterIncrement().

1477 {
1478  /*
1479  * Make any pending relation map changes visible. We must do this before
1480  * processing local sinval messages, so that the map changes will get
1481  * reflected into the relcache when relcache invals are processed.
1482  */
1484 
1485  /*
1486  * Make catalog changes visible to me for the next command.
1487  */
1489 }
void CommandEndInvalidationMessages(void)
Definition: inval.c:1152
void AtCCI_RelationMap(void)
Definition: relmapper.c:439

◆ AtCleanup_Memory()

static void AtCleanup_Memory ( void  )
static

Definition at line 1843 of file xact.c.

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

Referenced by AbortOutOfAnyTransaction(), and CleanupTransaction().

1844 {
1846 
1847  /*
1848  * Now that we're "out" of a transaction, have the system allocate things
1849  * in the top memory context instead of per-transaction contexts.
1850  */
1852 
1853  /*
1854  * Clear the special abort context for next time.
1855  */
1856  if (TransactionAbortContext != NULL)
1858 
1859  /*
1860  * Release all transaction-local memory.
1861  */
1862  if (TopTransactionContext != NULL)
1864  TopTransactionContext = NULL;
1865  CurTransactionContext = NULL;
1867 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
MemoryContext TopTransactionContext
Definition: mcxt.c:53
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
static MemoryContext TransactionAbortContext
Definition: xact.c:290
struct TransactionStateData * parent
Definition: xact.c:208
MemoryContext curTransactionContext
Definition: xact.c:195
MemoryContext TopMemoryContext
Definition: mcxt.c:48
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:804

◆ AtCommit_Memory()

static void AtCommit_Memory ( void  )
static

Definition at line 1495 of file xact.c.

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

Referenced by CommitTransaction(), and PrepareTransaction().

1496 {
1497  /*
1498  * Now that we're "out" of a transaction, have the system allocate things
1499  * in the top memory context instead of per-transaction contexts.
1500  */
1502 
1503  /*
1504  * Release all transaction-local memory.
1505  */
1506  Assert(TopTransactionContext != NULL);
1508  TopTransactionContext = NULL;
1509  CurTransactionContext = NULL;
1511 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
MemoryContext TopTransactionContext
Definition: mcxt.c:53
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
MemoryContext curTransactionContext
Definition: xact.c:195
MemoryContext TopMemoryContext
Definition: mcxt.c:48
#define Assert(condition)
Definition: c.h:804

◆ AtStart_Cache()

static void AtStart_Cache ( void  )
static

Definition at line 1086 of file xact.c.

References AcceptInvalidationMessages().

Referenced by StartTransaction().

1087 {
1089 }
void AcceptInvalidationMessages(void)
Definition: inval.c:725

◆ AtStart_Memory()

static void AtStart_Memory ( void  )
static

Definition at line 1095 of file xact.c.

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

Referenced by StartTransaction().

1096 {
1098 
1099  /*
1100  * If this is the first time through, create a private context for
1101  * AbortTransaction to work in. By reserving some space now, we can
1102  * insulate AbortTransaction from out-of-memory scenarios. Like
1103  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1104  * size, so that space will be reserved immediately.
1105  */
1106  if (TransactionAbortContext == NULL)
1109  "TransactionAbortContext",
1110  32 * 1024,
1111  32 * 1024,
1112  32 * 1024);
1113 
1114  /*
1115  * We shouldn't have a transaction context already.
1116  */
1117  Assert(TopTransactionContext == NULL);
1118 
1119  /*
1120  * Create a toplevel context for the transaction.
1121  */
1124  "TopTransactionContext",
1126 
1127  /*
1128  * In a top-level transaction, CurTransactionContext is the same as
1129  * TopTransactionContext.
1130  */
1133 
1134  /* Make the CurTransactionContext active. */
1136 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define AllocSetContextCreate
Definition: memutils.h:173
MemoryContext TopTransactionContext
Definition: mcxt.c:53
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
static MemoryContext TransactionAbortContext
Definition: xact.c:290
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
MemoryContext curTransactionContext
Definition: xact.c:195
MemoryContext TopMemoryContext
Definition: mcxt.c:48
#define Assert(condition)
Definition: c.h:804

◆ AtStart_ResourceOwner()

static void AtStart_ResourceOwner ( void  )
static

Definition at line 1142 of file xact.c.

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

Referenced by StartTransaction().

1143 {
1145 
1146  /*
1147  * We shouldn't have a transaction resource owner already.
1148  */
1150 
1151  /*
1152  * Create a toplevel resource owner for the transaction.
1153  */
1154  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1155 
1159 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:147
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
ResourceOwner curTransactionOwner
Definition: xact.c:196
#define Assert(condition)
Definition: c.h:804
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:428

◆ AtSubAbort_childXids()

static void AtSubAbort_childXids ( void  )
static

Definition at line 1811 of file xact.c.

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

Referenced by AbortSubTransaction().

1812 {
1814 
1815  /*
1816  * We keep the child-XID arrays in TopTransactionContext (see
1817  * AtSubCommit_childXids). This means we'd better free the array
1818  * explicitly at abort to avoid leakage.
1819  */
1820  if (s->childXids != NULL)
1821  pfree(s->childXids);
1822  s->childXids = NULL;
1823  s->nChildXids = 0;
1824  s->maxChildXids = 0;
1825 
1826  /*
1827  * We could prune the unreportedXids array here. But we don't bother. That
1828  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1829  * would likely introduce more CPU time into the more common paths, so we
1830  * choose not to do that.
1831  */
1832 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TransactionId * childXids
Definition: xact.c:197
void pfree(void *pointer)
Definition: mcxt.c:1169

◆ AtSubAbort_Memory()

static void AtSubAbort_Memory ( void  )
static

Definition at line 1773 of file xact.c.

References Assert, and MemoryContextSwitchTo().

Referenced by AbortSubTransaction().

1774 {
1776 
1778 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:290
#define Assert(condition)
Definition: c.h:804

◆ AtSubAbort_ResourceOwner()

static void AtSubAbort_ResourceOwner ( void  )
static

Definition at line 1798 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, and TransactionStateData::curTransactionOwner.

Referenced by AbortSubTransaction().

1799 {
1801 
1802  /* Make sure we have a valid ResourceOwner */
1804 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
ResourceOwner curTransactionOwner
Definition: xact.c:196

◆ AtSubCleanup_Memory()

static void AtSubCleanup_Memory ( void  )
static

Definition at line 1879 of file xact.c.

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

Referenced by CleanupSubTransaction().

1880 {
1882 
1883  Assert(s->parent != NULL);
1884 
1885  /* Make sure we're not in an about-to-be-deleted context */
1888 
1889  /*
1890  * Clear the special abort context for next time.
1891  */
1892  if (TransactionAbortContext != NULL)
1894 
1895  /*
1896  * Delete the subxact local memory contexts. Its CurTransactionContext can
1897  * go too (note this also kills CurTransactionContexts from any children
1898  * of the subxact).
1899  */
1900  if (s->curTransactionContext)
1902  s->curTransactionContext = NULL;
1903 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
static MemoryContext TransactionAbortContext
Definition: xact.c:290
struct TransactionStateData * parent
Definition: xact.c:208
MemoryContext curTransactionContext
Definition: xact.c:195
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:804

◆ AtSubCommit_childXids()

static void AtSubCommit_childXids ( void  )
static

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

1552 {
1554  int new_nChildXids;
1555 
1556  Assert(s->parent != NULL);
1557 
1558  /*
1559  * The parent childXids array will need to hold my XID and all my
1560  * childXids, in addition to the XIDs already there.
1561  */
1562  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1563 
1564  /* Allocate or enlarge the parent array if necessary */
1565  if (s->parent->maxChildXids < new_nChildXids)
1566  {
1567  int new_maxChildXids;
1568  TransactionId *new_childXids;
1569 
1570  /*
1571  * Make it 2x what's needed right now, to avoid having to enlarge it
1572  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1573  * is what ensures that we don't need to worry about integer overflow
1574  * here or in the calculation of new_nChildXids.)
1575  */
1576  new_maxChildXids = Min(new_nChildXids * 2,
1577  (int) (MaxAllocSize / sizeof(TransactionId)));
1578 
1579  if (new_maxChildXids < new_nChildXids)
1580  ereport(ERROR,
1581  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1582  errmsg("maximum number of committed subtransactions (%d) exceeded",
1583  (int) (MaxAllocSize / sizeof(TransactionId)))));
1584 
1585  /*
1586  * We keep the child-XID arrays in TopTransactionContext; this avoids
1587  * setting up child-transaction contexts for what might be just a few
1588  * bytes of grandchild XIDs.
1589  */
1590  if (s->parent->childXids == NULL)
1591  new_childXids =
1593  new_maxChildXids * sizeof(TransactionId));
1594  else
1595  new_childXids = repalloc(s->parent->childXids,
1596  new_maxChildXids * sizeof(TransactionId));
1597 
1598  s->parent->childXids = new_childXids;
1599  s->parent->maxChildXids = new_maxChildXids;
1600  }
1601 
1602  /*
1603  * Copy all my XIDs to parent's array.
1604  *
1605  * Note: We rely on the fact that the XID of a child always follows that
1606  * of its parent. By copying the XID of this subtransaction before the
1607  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1608  * all XIDs already in the array belong to subtransactions started and
1609  * subcommitted before us, so their XIDs must precede ours.
1610  */
1612 
1613  if (s->nChildXids > 0)
1614  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1615  s->childXids,
1616  s->nChildXids * sizeof(TransactionId));
1617 
1618  s->parent->nChildXids = new_nChildXids;
1619 
1620  /* Release child's array to avoid leakage */
1621  if (s->childXids != NULL)
1622  pfree(s->childXids);
1623  /* We must reset these to avoid double-free if fail later in commit */
1624  s->childXids = NULL;
1625  s->nChildXids = 0;
1626  s->maxChildXids = 0;
1627 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
MemoryContext TopTransactionContext
Definition: mcxt.c:53
uint32 TransactionId
Definition: c.h:587
#define Min(x, y)
Definition: c.h:986
int errcode(int sqlerrcode)
Definition: elog.c:698
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:197
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
struct TransactionStateData * parent
Definition: xact.c:208
FullTransactionId fullTransactionId
Definition: xact.c:187
#define MaxAllocSize
Definition: memutils.h:40
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
int errmsg(const char *fmt,...)
Definition: elog.c:909
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863

◆ AtSubCommit_Memory()

static void AtSubCommit_Memory ( void  )
static

Definition at line 1522 of file xact.c.

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

Referenced by CommitSubTransaction().

1523 {
1525 
1526  Assert(s->parent != NULL);
1527 
1528  /* Return to parent transaction level's memory context. */
1531 
1532  /*
1533  * Ordinarily we cannot throw away the child's CurTransactionContext,
1534  * since the data it contains will be needed at upper commit. However, if
1535  * there isn't actually anything in it, we can throw it away. This avoids
1536  * a small memory leak in the common case of "trivial" subxacts.
1537  */
1539  {
1541  s->curTransactionContext = NULL;
1542  }
1543 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
bool MemoryContextIsEmpty(MemoryContext context)
Definition: mcxt.c:458
struct TransactionStateData * parent
Definition: xact.c:208
MemoryContext curTransactionContext
Definition: xact.c:195
#define Assert(condition)
Definition: c.h:804

◆ AtSubStart_Memory()

static void AtSubStart_Memory ( void  )
static

Definition at line 1170 of file xact.c.

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

Referenced by StartSubTransaction().

1171 {
1173 
1174  Assert(CurTransactionContext != NULL);
1175 
1176  /*
1177  * Create a CurTransactionContext, which will be used to hold data that
1178  * survives subtransaction commit but disappears on subtransaction abort.
1179  * We make it a child of the immediate parent's CurTransactionContext.
1180  */
1182  "CurTransactionContext",
1185 
1186  /* Make the CurTransactionContext active. */
1188 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define AllocSetContextCreate
Definition: memutils.h:173
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
MemoryContext curTransactionContext
Definition: xact.c:195
#define Assert(condition)
Definition: c.h:804

◆ AtSubStart_ResourceOwner()

static void AtSubStart_ResourceOwner ( void  )
static

Definition at line 1194 of file xact.c.

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

Referenced by StartSubTransaction().

1195 {
1197 
1198  Assert(s->parent != NULL);
1199 
1200  /*
1201  * Create a resource owner for the subtransaction. We make it a child of
1202  * the immediate parent's resource owner.
1203  */
1204  s->curTransactionOwner =
1206  "SubTransaction");
1207 
1210 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:147
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
ResourceOwner curTransactionOwner
Definition: xact.c:196
struct TransactionStateData * parent
Definition: xact.c:208
#define Assert(condition)
Definition: c.h:804
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:428

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4049 of file xact.c.

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

Referenced by exec_simple_query().

4050 {
4052 
4053  /*
4054  * If we are in STARTED state (that is, no transaction block is open),
4055  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
4056  * block.
4057  *
4058  * For caller convenience, we consider all other transaction states as
4059  * legal here; otherwise the caller would need its own state check, which
4060  * seems rather pointless.
4061  */
4062  if (s->blockState == TBLOCK_STARTED)
4064 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

Definition at line 4417 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(), ReorderBufferImmediateInvalidation(), and ReorderBufferProcessTXN().

4418 {
4420 
4421  /*
4422  * Workers synchronize transaction state at the beginning of each parallel
4423  * operation, so we can't account for new subtransactions after that
4424  * point. We might be able to make an exception for the type of
4425  * subtransaction established by this function, which is typically used in
4426  * contexts where we're going to release or roll back the subtransaction
4427  * before proceeding further, so that no enduring change to the
4428  * transaction state occurs. For now, however, we prohibit this case along
4429  * with all the others.
4430  */
4431  if (IsInParallelMode())
4432  ereport(ERROR,
4433  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4434  errmsg("cannot start subtransactions during a parallel operation")));
4435 
4436  switch (s->blockState)
4437  {
4438  case TBLOCK_STARTED:
4439  case TBLOCK_INPROGRESS:
4441  case TBLOCK_END:
4442  case TBLOCK_PREPARE:
4443  case TBLOCK_SUBINPROGRESS:
4444  /* Normal subtransaction start */
4445  PushTransaction();
4446  s = CurrentTransactionState; /* changed by push */
4447 
4448  /*
4449  * Savepoint names, like the TransactionState block itself, live
4450  * in TopTransactionContext.
4451  */
4452  if (name)
4454  break;
4455 
4456  /* These cases are invalid. */
4457  case TBLOCK_DEFAULT:
4458  case TBLOCK_BEGIN:
4460  case TBLOCK_SUBBEGIN:
4461  case TBLOCK_SUBRELEASE:
4462  case TBLOCK_SUBCOMMIT:
4463  case TBLOCK_ABORT:
4464  case TBLOCK_SUBABORT:
4465  case TBLOCK_ABORT_END:
4466  case TBLOCK_SUBABORT_END:
4467  case TBLOCK_ABORT_PENDING:
4469  case TBLOCK_SUBRESTART:
4471  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4473  break;
4474  }
4475 
4478 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
MemoryContext TopTransactionContext
Definition: mcxt.c:53
void CommitTransactionCommand(void)
Definition: xact.c:2949
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:46
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5400
#define ereport(elevel,...)
Definition: elog.h:157
static void PushTransaction(void)
Definition: xact.c:5121
void StartTransactionCommand(void)
Definition: xact.c:2848
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1286
#define elog(elevel,...)
Definition: elog.h:232

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

Definition at line 3647 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 apply_handle_prepare_internal(), and standard_ProcessUtility().

3648 {
3650 
3651  switch (s->blockState)
3652  {
3653  /*
3654  * We are not inside a transaction block, so allow one to begin.
3655  */
3656  case TBLOCK_STARTED:
3657  s->blockState = TBLOCK_BEGIN;
3658  break;
3659 
3660  /*
3661  * BEGIN converts an implicit transaction block to a regular one.
3662  * (Note that we allow this even if we've already done some
3663  * commands, which is a bit odd but matches historical practice.)
3664  */
3666  s->blockState = TBLOCK_BEGIN;
3667  break;
3668 
3669  /*
3670  * Already a transaction block in progress.
3671  */
3672  case TBLOCK_INPROGRESS:
3674  case TBLOCK_SUBINPROGRESS:
3675  case TBLOCK_ABORT:
3676  case TBLOCK_SUBABORT:
3677  ereport(WARNING,
3678  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3679  errmsg("there is already a transaction in progress")));
3680  break;
3681 
3682  /* These cases are invalid. */
3683  case TBLOCK_DEFAULT:
3684  case TBLOCK_BEGIN:
3685  case TBLOCK_SUBBEGIN:
3686  case TBLOCK_END:
3687  case TBLOCK_SUBRELEASE:
3688  case TBLOCK_SUBCOMMIT:
3689  case TBLOCK_ABORT_END:
3690  case TBLOCK_SUBABORT_END:
3691  case TBLOCK_ABORT_PENDING:
3693  case TBLOCK_SUBRESTART:
3695  case TBLOCK_PREPARE:
3696  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3698  break;
3699  }
3700 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5400
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ BlockStateAsString()

static const char * BlockStateAsString ( TBlockState  blockState)
static

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

5401 {
5402  switch (blockState)
5403  {
5404  case TBLOCK_DEFAULT:
5405  return "DEFAULT";
5406  case TBLOCK_STARTED:
5407  return "STARTED";
5408  case TBLOCK_BEGIN:
5409  return "BEGIN";
5410  case TBLOCK_INPROGRESS:
5411  return "INPROGRESS";
5413  return "IMPLICIT_INPROGRESS";
5415  return "PARALLEL_INPROGRESS";
5416  case TBLOCK_END:
5417  return "END";
5418  case TBLOCK_ABORT:
5419  return "ABORT";
5420  case TBLOCK_ABORT_END:
5421  return "ABORT_END";
5422  case TBLOCK_ABORT_PENDING:
5423  return "ABORT_PENDING";
5424  case TBLOCK_PREPARE:
5425  return "PREPARE";
5426  case TBLOCK_SUBBEGIN:
5427  return "SUBBEGIN";
5428  case TBLOCK_SUBINPROGRESS:
5429  return "SUBINPROGRESS";
5430  case TBLOCK_SUBRELEASE:
5431  return "SUBRELEASE";
5432  case TBLOCK_SUBCOMMIT:
5433  return "SUBCOMMIT";
5434  case TBLOCK_SUBABORT:
5435  return "SUBABORT";
5436  case TBLOCK_SUBABORT_END:
5437  return "SUBABORT_END";
5439  return "SUBABORT_PENDING";
5440  case TBLOCK_SUBRESTART:
5441  return "SUBRESTART";
5443  return "SUBABORT_RESTART";
5444  }
5445  return "UNRECOGNIZED";
5446 }

◆ CallSubXactCallbacks()

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

Definition at line 3626 of file xact.c.

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

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

3629 {
3630  SubXactCallbackItem *item;
3631 
3632  for (item = SubXact_callbacks; item; item = item->next)
3633  item->callback(event, mySubid, parentSubid, item->arg);
3634 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:314
struct SubXactCallbackItem * next
Definition: xact.c:309
SubXactCallback callback
Definition: xact.c:310

◆ CallXactCallbacks()

static void CallXactCallbacks ( XactEvent  event)
static

Definition at line 3571 of file xact.c.

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

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

3572 {
3573  XactCallbackItem *item;
3574 
3575  for (item = Xact_callbacks; item; item = item->next)
3576  item->callback(event, item->arg);
3577 }
struct XactCallbackItem * next
Definition: xact.c:297
void * arg
Definition: xact.c:299
XactCallback callback
Definition: xact.c:298
static XactCallbackItem * Xact_callbacks
Definition: xact.c:302

◆ CheckTransactionBlock()

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

Definition at line 3464 of file xact.c.

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

Referenced by RequireTransactionBlock(), and WarnNoTransactionBlock().

3465 {
3466  /*
3467  * xact block already started?
3468  */
3469  if (IsTransactionBlock())
3470  return;
3471 
3472  /*
3473  * subtransaction?
3474  */
3475  if (IsSubTransaction())
3476  return;
3477 
3478  /*
3479  * inside a function call?
3480  */
3481  if (!isTopLevel)
3482  return;
3483 
3484  ereport(throwError ? ERROR : WARNING,
3485  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3486  /* translator: %s represents an SQL statement name */
3487  errmsg("%s can only be used in transaction blocks",
3488  stmtType)));
3489 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool IsTransactionBlock(void)
Definition: xact.c:4693
#define ERROR
Definition: elog.h:46
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
bool IsSubTransaction(void)
Definition: xact.c:4766
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ CleanupSubTransaction()

static void CleanupSubTransaction ( void  )
static

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

5089 {
5091 
5092  ShowTransactionState("CleanupSubTransaction");
5093 
5094  if (s->state != TRANS_ABORT)
5095  elog(WARNING, "CleanupSubTransaction while in %s state",
5097 
5099 
5102  if (s->curTransactionOwner)
5104  s->curTransactionOwner = NULL;
5105 
5107 
5108  s->state = TRANS_DEFAULT;
5109 
5110  PopTransaction();
5111 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void PopTransaction(void)
Definition: xact.c:5181
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:147
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:737
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5350
SubTransactionId subTransactionId
Definition: xact.c:188
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1088
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1879
static const char * TransStateAsString(TransState state)
Definition: xact.c:5453
#define elog(elevel,...)
Definition: elog.h:232

◆ CleanupTransaction()

static void CleanupTransaction ( void  )
static

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

2800 {
2802 
2803  /*
2804  * State should still be TRANS_ABORT from AbortTransaction().
2805  */
2806  if (s->state != TRANS_ABORT)
2807  elog(FATAL, "CleanupTransaction: unexpected state %s",
2809 
2810  /*
2811  * do abort cleanup processing
2812  */
2813  AtCleanup_Portals(); /* now safe to release portal memory */
2814  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2815 
2816  CurrentResourceOwner = NULL; /* and resource owner */
2819  s->curTransactionOwner = NULL;
2822 
2823  AtCleanup_Memory(); /* and transaction memory */
2824 
2827  s->nestingLevel = 0;
2828  s->gucNestLevel = 0;
2829  s->childXids = NULL;
2830  s->nChildXids = 0;
2831  s->maxChildXids = 0;
2832  s->parallelModeLevel = 0;
2833 
2836 
2837  /*
2838  * done with abort processing, set current transaction state back to
2839  * default
2840  */
2841  s->state = TRANS_DEFAULT;
2842 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
static TransactionState CurrentTransactionState
Definition: xact.c:250
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:147
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:737
static void AtCleanup_Memory(void)
Definition: xact.c:1843
int nParallelCurrentXids
Definition: xact.c:122
void AtCleanup_Portals(void)
Definition: portalmem.c:856
TransactionId * childXids
Definition: xact.c:197
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
#define FATAL
Definition: elog.h:49
SubTransactionId subTransactionId
Definition: xact.c:188
FullTransactionId fullTransactionId
Definition: xact.c:187
int parallelModeLevel
Definition: xact.c:205
static const char * TransStateAsString(TransState state)
Definition: xact.c:5453
#define InvalidSubTransactionId
Definition: c.h:593
#define InvalidFullTransactionId
Definition: transam.h:56
#define elog(elevel,...)
Definition: elog.h:232
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1006

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1021 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(), ATAddCheckConstraint(), ATExecAddColumn(), ATExecAlterColumnType(), ATExecCmd(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropExpression(), ATExecDropIdentity(), ATExecSetCompression(), ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), ATParseTransformCmd(), ATRewriteTables(), AttachPartitionEnsureIndexes(), btadjustmembers(), CloneFkReferencing(), CommitSubTransaction(), CommitTransactionCommand(), copy_table_data(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyActionTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), CreateTriggerFiringOn(), DefineCollation(), DefineDomain(), DefineQueryRewrite(), DefineRelation(), DefineVirtualRelation(), deleteOneObject(), DelRoleMems(), DetachPartitionFinalize(), DropClonedTriggersFromPartition(), DropRole(), end_replication_step(), 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(), finish_heap_swap(), fmgr_sql(), hashadjustmembers(), 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_index(), reindex_relation(), ReindexRelationConcurrently(), RelationSetNewRelfilenode(), RemoveRoleFromObjectPolicy(), RenumberEnumType(), replorigin_create(), replorigin_drop_guts(), ri_PerformCheck(), SetDefaultACL(), SetMatViewPopulatedState(), shdepReassignOwned(), SPI_cursor_open_internal(), standard_ProcessUtility(), StoreConstraints(), StorePartitionBound(), tryAttachPartitionForeignKey(), vacuum(), and validatePartitionedIndex().

1022 {
1023  /*
1024  * If the current value of the command counter hasn't been "used" to mark
1025  * tuples, we need not increment it, since there's no need to distinguish
1026  * a read-only command from others. This helps postpone command counter
1027  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
1028  */
1030  {
1031  /*
1032  * Workers synchronize transaction state at the beginning of each
1033  * parallel operation, so we can't account for new commands after that
1034  * point.
1035  */
1037  elog(ERROR, "cannot start commands during a parallel operation");
1038 
1039  currentCommandId += 1;
1041  {
1042  currentCommandId -= 1;
1043  ereport(ERROR,
1044  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1045  errmsg("cannot have more than 2^32-2 commands in a transaction")));
1046  }
1047  currentCommandIdUsed = false;
1048 
1049  /* Propagate new command ID into static snapshots */
1051 
1052  /*
1053  * Make any catalog changes done by the just-completed command visible
1054  * in the local syscache. We obviously don't need to do this after a
1055  * read-only command. (But see hacks in inval.c to make real sure we
1056  * don't think a command that queued inval messages was read-only.)
1057  */
1058  AtCCI_LocalCache();
1059  }
1060 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:46
#define IsParallelWorker()
Definition: parallel.h:61
#define InvalidCommandId
Definition: c.h:604
#define ereport(elevel,...)
Definition: elog.h:157
static void AtCCI_LocalCache(void)
Definition: xact.c:1476
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:490
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
static CommandId currentCommandId
Definition: xact.c:257
static bool currentCommandIdUsed
Definition: xact.c:258

◆ CommitSubTransaction()

static void CommitSubTransaction ( void  )
static

Definition at line 4826 of file xact.c.

References AfterTriggerEndSubXact(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubCommit_childXids(), AtSubCommit_Memory(), AtSubCommit_Notify(), AtSubCommit_Portals(), AtSubCommit_smgr(), AtSubCommit_Snapshot(), CallSubXactCallbacks(), CommandCounterIncrement(), CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, TransactionStateData::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().

4827 {
4829 
4830  ShowTransactionState("CommitSubTransaction");
4831 
4832  if (s->state != TRANS_INPROGRESS)
4833  elog(WARNING, "CommitSubTransaction while in %s state",
4835 
4836  /* Pre-commit processing goes here */
4837 
4839  s->parent->subTransactionId);
4840 
4841  /* If in parallel mode, clean up workers and exit parallel mode. */
4842  if (IsInParallelMode())
4843  {
4845  s->parallelModeLevel = 0;
4846  }
4847 
4848  /* Do the actual "commit", such as it is */
4849  s->state = TRANS_COMMIT;
4850 
4851  /* Must CCI to ensure commands of subtransaction are seen as done */
4853 
4854  /*
4855  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4856  * perform that step, if required, as part of the atomic update of the
4857  * whole transaction tree at top level commit or abort.
4858  */
4859 
4860  /* Post-commit cleanup */
4863  AfterTriggerEndSubXact(true);
4868  s->parent->subTransactionId);
4870 
4872  s->parent->subTransactionId);
4873 
4876  true, false);
4878  s->parent->subTransactionId);
4879  AtEOSubXact_Inval(true);
4880  AtSubCommit_smgr();
4881 
4882  /*
4883  * The only lock we actually release here is the subtransaction XID lock.
4884  */
4888 
4889  /*
4890  * Other locks should get transferred to their parent resource owner.
4891  */
4894  true, false);
4897  true, false);
4898 
4899  AtEOXact_GUC(true, s->gucNestLevel);
4902  s->parent->subTransactionId);
4904  s->parent->subTransactionId);
4906  s->parent->subTransactionId);
4908  AtEOSubXact_PgStat(true, s->nestingLevel);
4910 
4911  /*
4912  * We need to restore the upper transaction's read-only state, in case the
4913  * upper is read-write while the child is read-only; GUC will incorrectly
4914  * think it should leave the child state in place.
4915  */
4917 
4921  s->curTransactionOwner = NULL;
4922 
4924 
4925  s->state = TRANS_DEFAULT;
4926 
4927  PopTransaction();
4928 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3223
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void AtSubCommit_Memory(void)
Definition: xact.c:1522
static void PopTransaction(void)
Definition: xact.c:5181
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:147
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1909
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:737
void AtSubCommit_smgr(void)
Definition: storage.c:894
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:393
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:949
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static void AtSubCommit_childXids(void)
Definition: xact.c:1551
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
bool IsInParallelMode(void)
Definition: xact.c:1012
struct TransactionStateData * parent
Definition: xact.c:208
void AtSubCommit_Notify(void)
Definition: async.c:1740
static void ShowTransactionState(const char *str)
Definition: xact.c:5350
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6230
SubTransactionId subTransactionId
Definition: xact.c:188
FullTransactionId fullTransactionId
Definition: xact.c:187
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2472
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:16417
void CommandCounterIncrement(void)
Definition: xact.c:1021
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:3008
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3626
bool XactReadOnly
Definition: xact.c:78
int parallelModeLevel
Definition: xact.c:205
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:486
static const char * TransStateAsString(TransState state)
Definition: xact.c:5453
bool prevXactReadOnly
Definition: xact.c:202
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4988
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1064
#define elog(elevel,...)
Definition: elog.h:232
void XactLockTableDelete(TransactionId xid)
Definition: lmgr.c:616
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4177
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1215
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:941

◆ CommitTransaction()

static void CommitTransaction ( void  )
static

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

2074 {
2076  TransactionId latestXid;
2077  bool is_parallel_worker;
2078 
2079  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2080 
2081  /* Enforce parallel mode restrictions during parallel worker commit. */
2082  if (is_parallel_worker)
2084 
2085  ShowTransactionState("CommitTransaction");
2086 
2087  /*
2088  * check the current transaction state
2089  */
2090  if (s->state != TRANS_INPROGRESS)
2091  elog(WARNING, "CommitTransaction while in %s state",
2093  Assert(s->parent == NULL);
2094 
2095  /*
2096  * Do pre-commit processing that involves calling user-defined code, such
2097  * as triggers. SECURITY_RESTRICTED_OPERATION contexts must not queue an
2098  * action that would run here, because that would bypass the sandbox.
2099  * Since closing cursors could queue trigger actions, triggers could open
2100  * cursors, etc, we have to keep looping until there's nothing left to do.
2101  */
2102  for (;;)
2103  {
2104  /*
2105  * Fire all currently pending deferred triggers.
2106  */
2108 
2109  /*
2110  * Close open portals (converting holdable ones into static portals).
2111  * If there weren't any, we are done ... otherwise loop back to check
2112  * if they queued deferred triggers. Lather, rinse, repeat.
2113  */
2114  if (!PreCommit_Portals(false))
2115  break;
2116  }
2117 
2118  /*
2119  * The remaining actions cannot call any user-defined code, so it's safe
2120  * to start shutting down within-transaction services. But note that most
2121  * of this stuff could still throw an error, which would switch us into
2122  * the transaction-abort path.
2123  */
2124 
2127 
2128  /* If we might have parallel workers, clean them up now. */
2129  if (IsInParallelMode())
2130  AtEOXact_Parallel(true);
2131 
2132  /* Shut down the deferred-trigger manager */
2133  AfterTriggerEndXact(true);
2134 
2135  /*
2136  * Let ON COMMIT management do its thing (must happen after closing
2137  * cursors, to avoid dangling-reference problems)
2138  */
2140 
2141  /*
2142  * Synchronize files that are created and not WAL-logged during this
2143  * transaction. This must happen before AtEOXact_RelationMap(), so that we
2144  * don't see committed-but-broken files after a crash.
2145  */
2146  smgrDoPendingSyncs(true, is_parallel_worker);
2147 
2148  /* close large objects before lower-level cleanup */
2149  AtEOXact_LargeObject(true);
2150 
2151  /*
2152  * Insert notifications sent by NOTIFY commands into the queue. This
2153  * should be late in the pre-commit sequence to minimize time spent
2154  * holding the notify-insertion lock. However, this could result in
2155  * creating a snapshot, so we must do it before serializable cleanup.
2156  */
2157  PreCommit_Notify();
2158 
2159  /*
2160  * Mark serializable transaction as complete for predicate locking
2161  * purposes. This should be done as late as we can put it and still allow
2162  * errors to be raised for failure patterns found at commit. This is not
2163  * appropriate in a parallel worker however, because we aren't committing
2164  * the leader's transaction and its serializable state will live on.
2165  */
2166  if (!is_parallel_worker)
2168 
2169  /* Prevent cancel/die interrupt while cleaning up */
2170  HOLD_INTERRUPTS();
2171 
2172  /* Commit updates to the relation map --- do this as late as possible */
2173  AtEOXact_RelationMap(true, is_parallel_worker);
2174 
2175  /*
2176  * set the current transaction state information appropriately during
2177  * commit processing
2178  */
2179  s->state = TRANS_COMMIT;
2180  s->parallelModeLevel = 0;
2181 
2182  if (!is_parallel_worker)
2183  {
2184  /*
2185  * We need to mark our XIDs as committed in pg_xact. This is where we
2186  * durably commit.
2187  */
2188  latestXid = RecordTransactionCommit();
2189  }
2190  else
2191  {
2192  /*
2193  * We must not mark our XID committed; the parallel leader is
2194  * responsible for that.
2195  */
2196  latestXid = InvalidTransactionId;
2197 
2198  /*
2199  * Make sure the leader will know about any WAL we wrote before it
2200  * commits.
2201  */
2203  }
2204 
2205  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2206 
2207  /*
2208  * Let others know about no transaction in progress by me. Note that this
2209  * must be done _before_ releasing locks we hold and _after_
2210  * RecordTransactionCommit.
2211  */
2212  ProcArrayEndTransaction(MyProc, latestXid);
2213 
2214  /*
2215  * This is all post-commit cleanup. Note that if an error is raised here,
2216  * it's too late to abort the transaction. This should be just
2217  * noncritical resource releasing.
2218  *
2219  * The ordering of operations is not entirely random. The idea is:
2220  * release resources visible to other backends (eg, files, buffer pins);
2221  * then release locks; then release backend-local resources. We want to
2222  * release locks at the point where any backend waiting for us will see
2223  * our transaction as being fully cleaned up.
2224  *
2225  * Resources that can be associated with individual queries are handled by
2226  * the ResourceOwner mechanism. The other calls here are for backend-wide
2227  * state.
2228  */
2229 
2230  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2231  : XACT_EVENT_COMMIT);
2232 
2235  true, true);
2236 
2237  /* Check we've released all buffer pins */
2238  AtEOXact_Buffers(true);
2239 
2240  /* Clean up the relation cache */
2241  AtEOXact_RelationCache(true);
2242 
2243  /*
2244  * Make catalog changes visible to all backends. This has to happen after
2245  * relcache references are dropped (see comments for
2246  * AtEOXact_RelationCache), but before locks are released (if anyone is
2247  * waiting for lock on a relation we've modified, we want them to know
2248  * about the catalog change before they start using the relation).
2249  */
2250  AtEOXact_Inval(true);
2251 
2253 
2256  true, true);
2259  true, true);
2260 
2261  /*
2262  * Likewise, dropping of files deleted during the transaction is best done
2263  * after releasing relcache and buffer pins. (This is not strictly
2264  * necessary during commit, since such pins should have been released
2265  * already, but this ordering is definitely critical during abort.) Since
2266  * this may take many seconds, also delay until after releasing locks.
2267  * Other backends will observe the attendant catalog changes and not
2268  * attempt to access affected files.
2269  */
2270  smgrDoPendingDeletes(true);
2271 
2272  /*
2273  * Send out notification signals to other backends (and do other
2274  * post-commit NOTIFY cleanup). This must not happen until after our
2275  * transaction is fully done from the viewpoint of other backends.
2276  */
2277  AtCommit_Notify();
2278 
2279  /*
2280  * Everything after this should be purely internal-to-this-backend
2281  * cleanup.
2282  */
2283  AtEOXact_GUC(true, 1);
2284  AtEOXact_SPI(true);
2285  AtEOXact_Enum();
2287  AtEOXact_Namespace(true, is_parallel_worker);
2288  AtEOXact_SMgr();
2289  AtEOXact_Files(true);
2291  AtEOXact_HashTables(true);
2292  AtEOXact_PgStat(true, is_parallel_worker);
2293  AtEOXact_Snapshot(true, false);
2294  AtEOXact_ApplyLauncher(true);
2296 
2297  CurrentResourceOwner = NULL;
2299  s->curTransactionOwner = NULL;
2302 
2303  AtCommit_Memory();
2304 
2307  s->nestingLevel = 0;
2308  s->gucNestLevel = 0;
2309  s->childXids = NULL;
2310  s->nChildXids = 0;
2311  s->maxChildXids = 0;
2312 
2315 
2316  /*
2317  * done with commit processing, set current transaction state back to
2318  * default
2319  */
2320  s->state = TRANS_DEFAULT;
2321 
2323 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:766
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:612
static TransactionState CurrentTransactionState
Definition: xact.c:250
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:1007
uint32 TransactionId
Definition: c.h:587
static void AtCommit_Memory(void)
Definition: xact.c:1495
XLogRecPtr XactLastRecEnd
Definition: xlog.c:344
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3078
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
PGPROC * MyProc
Definition: proc.c:68
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2579
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:147
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:737
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3571
int nParallelCurrentXids
Definition: xact.c:122
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:16385
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:654
static TransactionId RecordTransactionCommit(void)
Definition: xact.c:1226
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3041
TBlockState blockState
Definition: xact.c:192
TransactionId * childXids
Definition: xact.c:197
void PreCommit_CheckForSerializationFailure(void)
Definition: predicate.c:4831
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:133
void pgstat_report_xact_timestamp(TimestampTz tstamp)
void PreCommit_Notify(void)
Definition: async.c:895
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
void AtEOXact_SMgr(void)
Definition: smgr.c:678
bool IsInParallelMode(void)
Definition: xact.c:1012
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4892
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5350
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2385
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6230
#define InvalidTransactionId
Definition: transam.h:31
SubTransactionId subTransactionId
Definition: xact.c:188
FullTransactionId fullTransactionId
Definition: xact.c:187
#define WARNING
Definition: elog.h:40
void ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
Definition: parallel.c:1518
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4836
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:804
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:486
static const char * TransStateAsString(TransState state)
Definition: xact.c:5453
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:371
void AtEOXact_MultiXact(void)
Definition: multixact.c:1686
void AtEOXact_RelationMap(bool isCommit, bool isParallelWorker)
Definition: relmapper.c:476
void EnterParallelMode(void)
Definition: xact.c:979
#define InvalidSubTransactionId
Definition: c.h:593
bool PreCommit_Portals(bool isPrepare)
Definition: portalmem.c:675
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:16285
#define InvalidFullTransactionId
Definition: transam.h:56
void AtCommit_Notify(void)
Definition: async.c:1002
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:131
#define elog(elevel,...)
Definition: elog.h:232
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:680
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1883
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1234
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4109
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1006
LocalTransactionId lxid
Definition: proc.h:143

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2949 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(), AllTablesyncsReady(), apply_handle_commit_internal(), apply_handle_commit_prepared(), apply_handle_prepare(), apply_handle_prepare_internal(), apply_handle_rollback_prepared(), apply_handle_stream_abort(), apply_handle_stream_prepare(), apply_handle_stream_stop(), ApplyWorkerMain(), ATExecDetachPartition(), 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(), ProcessIncomingNotify(), ReindexMultipleInternal(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), vacuum(), and vacuum_rel().

2950 {
2952 
2953  if (s->chain)
2955 
2956  switch (s->blockState)
2957  {
2958  /*
2959  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2960  * StartTransactionCommand didn't set the STARTED state
2961  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2962  * by EndParallelWorkerTransaction(), not this function.
2963  */
2964  case TBLOCK_DEFAULT:
2966  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2968  break;
2969 
2970  /*
2971  * If we aren't in a transaction block, just do our usual
2972  * transaction commit, and return to the idle state.
2973  */
2974  case TBLOCK_STARTED:
2977  break;
2978 
2979  /*
2980  * We are completing a "BEGIN TRANSACTION" command, so we change
2981  * to the "transaction block in progress" state and return. (We
2982  * assume the BEGIN did nothing to the database, so we need no
2983  * CommandCounterIncrement.)
2984  */
2985  case TBLOCK_BEGIN:
2987  break;
2988 
2989  /*
2990  * This is the case when we have finished executing a command
2991  * someplace within a transaction block. We increment the command
2992  * counter and return.
2993  */
2994  case TBLOCK_INPROGRESS:
2996  case TBLOCK_SUBINPROGRESS:
2998  break;
2999 
3000  /*
3001  * We are completing a "COMMIT" command. Do it and return to the
3002  * idle state.
3003  */
3004  case TBLOCK_END:
3007  if (s->chain)
3008  {
3009  StartTransaction();
3011  s->chain = false;
3013  }
3014  break;
3015 
3016  /*
3017  * Here we are in the middle of a transaction block but one of the
3018  * commands caused an abort so we do nothing but remain in the
3019  * abort state. Eventually we will get a ROLLBACK command.
3020  */
3021  case TBLOCK_ABORT:
3022  case TBLOCK_SUBABORT:
3023  break;
3024 
3025  /*
3026  * Here we were in an aborted transaction block and we just got
3027  * the ROLLBACK command from the user, so clean up the
3028  * already-aborted transaction and return to the idle state.
3029  */
3030  case TBLOCK_ABORT_END:
3033  if (s->chain)
3034  {
3035  StartTransaction();
3037  s->chain = false;
3039  }
3040  break;
3041 
3042  /*
3043  * Here we were in a perfectly good transaction block but the user
3044  * told us to ROLLBACK anyway. We have to abort the transaction
3045  * and then clean up.
3046  */
3047  case TBLOCK_ABORT_PENDING:
3048  AbortTransaction();
3051  if (s->chain)
3052  {
3053  StartTransaction();
3055  s->chain = false;
3057  }
3058  break;
3059 
3060  /*
3061  * We are completing a "PREPARE TRANSACTION" command. Do it and
3062  * return to the idle state.
3063  */
3064  case TBLOCK_PREPARE:
3067  break;
3068 
3069  /*
3070  * We were just issued a SAVEPOINT inside a transaction block.
3071  * Start a subtransaction. (DefineSavepoint already did
3072  * PushTransaction, so as to have someplace to put the SUBBEGIN
3073  * state.)
3074  */
3075  case TBLOCK_SUBBEGIN:
3078  break;
3079 
3080  /*
3081  * We were issued a RELEASE command, so we end the current
3082  * subtransaction and return to the parent transaction. The parent
3083  * might be ended too, so repeat till we find an INPROGRESS
3084  * transaction or subtransaction.
3085  */
3086  case TBLOCK_SUBRELEASE:
3087  do
3088  {
3090  s = CurrentTransactionState; /* changed by pop */
3091  } while (s->blockState == TBLOCK_SUBRELEASE);
3092 
3095  break;
3096 
3097  /*
3098  * We were issued a COMMIT, so we end the current subtransaction
3099  * hierarchy and perform final commit. We do this by rolling up
3100  * any subtransactions into their parent, which leads to O(N^2)
3101  * operations with respect to resource owners - this isn't that
3102  * bad until we approach a thousands of savepoints but is
3103  * necessary for correctness should after triggers create new
3104  * resource owners.
3105  */
3106  case TBLOCK_SUBCOMMIT:
3107  do
3108  {
3110  s = CurrentTransactionState; /* changed by pop */
3111  } while (s->blockState == TBLOCK_SUBCOMMIT);
3112  /* If we had a COMMIT command, finish off the main xact too */
3113  if (s->blockState == TBLOCK_END)
3114  {
3115  Assert(s->parent == NULL);
3118  if (s->chain)
3119  {
3120  StartTransaction();
3122  s->chain = false;
3124  }
3125  }
3126  else if (s->blockState == TBLOCK_PREPARE)
3127  {
3128  Assert(s->parent == NULL);
3131  }
3132  else
3133  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3135  break;
3136 
3137  /*
3138  * The current already-failed subtransaction is ending due to a
3139  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3140  * examine the parent (which could be in any of several states).
3141  */
3142  case TBLOCK_SUBABORT_END:
3145  break;
3146 
3147  /*
3148  * As above, but it's not dead yet, so abort first.
3149  */
3154  break;
3155 
3156  /*
3157  * The current subtransaction is the target of a ROLLBACK TO
3158  * command. Abort and pop it, then start a new subtransaction
3159  * with the same name.
3160  */
3161  case TBLOCK_SUBRESTART:
3162  {
3163  char *name;
3164  int savepointLevel;
3165 
3166  /* save name and keep Cleanup from freeing it */
3167  name = s->name;
3168  s->name = NULL;
3169  savepointLevel = s->savepointLevel;
3170 
3173 
3174  DefineSavepoint(NULL);
3175  s = CurrentTransactionState; /* changed by push */
3176  s->name = name;
3177  s->savepointLevel = savepointLevel;
3178 
3179  /* This is the same as TBLOCK_SUBBEGIN case */
3183  }
3184  break;
3185 
3186  /*
3187  * Same as above, but the subtransaction had already failed, so we
3188  * don't need AbortSubTransaction.
3189  */
3191  {
3192  char *name;
3193  int savepointLevel;
3194 
3195  /* save name and keep Cleanup from freeing it */
3196  name = s->name;
3197  s->name = NULL;
3198  savepointLevel = s->savepointLevel;
3199 
3201 
3202  DefineSavepoint(NULL);
3203  s = CurrentTransactionState; /* changed by push */
3204  s->name = name;
3205  s->savepointLevel = savepointLevel;
3206 
3207  /* This is the same as TBLOCK_SUBBEGIN case */
3211  }
3212  break;
3213  }
3214 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define AssertState(condition)
Definition: c.h:807
void CommitTransactionCommand(void)
Definition: xact.c:2949
void DefineSavepoint(const char *name)
Definition: xact.c:4096
TBlockState blockState
Definition: xact.c:192
static void PrepareTransaction(void)
Definition: xact.c:2332
#define ERROR
Definition: elog.h:46
static void CommitSubTransaction(void)
Definition: xact.c:4826
#define FATAL
Definition: elog.h:49
static void CleanupTransaction(void)
Definition: xact.c:2799
struct TransactionStateData * parent
Definition: xact.c:208
void SaveTransactionCharacteristics(void)
Definition: xact.c:2929
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5400
static void AbortSubTransaction(void)
Definition: xact.c:4934
void CommandCounterIncrement(void)
Definition: xact.c:1021
static void AbortTransaction(void)
Definition: xact.c:2611
static void StartTransaction(void)
Definition: xact.c:1914
#define Assert(condition)
Definition: c.h:804
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2937
static void CleanupSubTransaction(void)
Definition: xact.c:5088
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:232
static void CommitTransaction(void)
Definition: xact.c:2073
static void StartSubTransaction(void)
Definition: xact.c:4789

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

4097 {
4099 
4100  /*
4101  * Workers synchronize transaction state at the beginning of each parallel
4102  * operation, so we can't account for new subtransactions after that
4103  * point. (Note that this check will certainly error out if s->blockState
4104  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4105  * below.)
4106  */
4107  if (IsInParallelMode())
4108  ereport(ERROR,
4109  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4110  errmsg("cannot define savepoints during a parallel operation")));
4111 
4112  switch (s->blockState)
4113  {
4114  case TBLOCK_INPROGRESS:
4115  case TBLOCK_SUBINPROGRESS:
4116  /* Normal subtransaction start */
4117  PushTransaction();
4118  s = CurrentTransactionState; /* changed by push */
4119 
4120  /*
4121  * Savepoint names, like the TransactionState block itself, live
4122  * in TopTransactionContext.
4123  */
4124  if (name)
4126  break;
4127 
4128  /*
4129  * We disallow savepoint commands in implicit transaction blocks.
4130  * There would be no great difficulty in allowing them so far as
4131  * this module is concerned, but a savepoint seems inconsistent
4132  * with exec_simple_query's behavior of abandoning the whole query
4133  * string upon error. Also, the point of an implicit transaction
4134  * block (as opposed to a regular one) is to automatically close
4135  * after an error, so it's hard to see how a savepoint would fit
4136  * into that.
4137  *
4138  * The error messages for this are phrased as if there were no
4139  * active transaction block at all, which is historical but
4140  * perhaps could be improved.
4141  */
4143  ereport(ERROR,
4144  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4145  /* translator: %s represents an SQL statement name */
4146  errmsg("%s can only be used in transaction blocks",
4147  "SAVEPOINT")));
4148  break;
4149 
4150  /* These cases are invalid. */
4151  case TBLOCK_DEFAULT:
4152  case TBLOCK_STARTED:
4153  case TBLOCK_BEGIN:
4155  case TBLOCK_SUBBEGIN:
4156  case TBLOCK_END:
4157  case TBLOCK_SUBRELEASE:
4158  case TBLOCK_SUBCOMMIT:
4159  case TBLOCK_ABORT:
4160  case TBLOCK_SUBABORT:
4161  case TBLOCK_ABORT_END:
4162  case TBLOCK_SUBABORT_END:
4163  case TBLOCK_ABORT_PENDING:
4165  case TBLOCK_SUBRESTART:
4167  case TBLOCK_PREPARE:
4168  elog(FATAL, "DefineSavepoint: unexpected state %s",
4170  break;
4171  }
4172 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
MemoryContext TopTransactionContext
Definition: mcxt.c:53
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:46
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5400
#define ereport(elevel,...)
Definition: elog.h:157
static void PushTransaction(void)
Definition: xact.c:5121
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1286
#define elog(elevel,...)
Definition: elog.h:232

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4074 of file xact.c.

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

Referenced by exec_simple_query().

4075 {
4077 
4078  /*
4079  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4080  * allowing CommitTransactionCommand to commit whatever happened during
4081  * the implicit transaction block as though it were a single statement.
4082  *
4083  * For caller convenience, we consider all other transaction states as
4084  * legal here; otherwise the caller would need its own state check, which
4085  * seems rather pointless.
4086  */
4089 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5338 of file xact.c.

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

Referenced by ParallelWorkerMain().

5339 {
5343 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:804
static void CommitTransaction(void)
Definition: xact.c:2073

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

3768 {
3770  bool result = false;
3771 
3772  switch (s->blockState)
3773  {
3774  /*
3775  * We are in a transaction block, so tell CommitTransactionCommand
3776  * to COMMIT.
3777  */
3778  case TBLOCK_INPROGRESS:
3779  s->blockState = TBLOCK_END;
3780  result = true;
3781  break;
3782 
3783  /*
3784  * We are in an implicit transaction block. If AND CHAIN was
3785  * specified, error. Otherwise commit, but issue a warning
3786  * because there was no explicit BEGIN before this.
3787  */
3789  if (chain)
3790  ereport(ERROR,
3791  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3792  /* translator: %s represents an SQL statement name */
3793  errmsg("%s can only be used in transaction blocks",
3794  "COMMIT AND CHAIN")));
3795  else
3796  ereport(WARNING,
3797  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3798  errmsg("there is no transaction in progress")));
3799  s->blockState = TBLOCK_END;
3800  result = true;
3801  break;
3802 
3803  /*
3804  * We are in a failed transaction block. Tell
3805  * CommitTransactionCommand it's time to exit the block.
3806  */
3807  case TBLOCK_ABORT:
3809  break;
3810 
3811  /*
3812  * We are in a live subtransaction block. Set up to subcommit all
3813  * open subtransactions and then commit the main transaction.
3814  */
3815  case TBLOCK_SUBINPROGRESS:
3816  while (s->parent != NULL)
3817  {
3818  if (s->blockState == TBLOCK_SUBINPROGRESS)
3820  else
3821  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3823  s = s->parent;
3824  }
3825  if (s->blockState == TBLOCK_INPROGRESS)
3826  s->blockState = TBLOCK_END;
3827  else
3828  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3830  result = true;
3831  break;
3832 
3833  /*
3834  * Here we are inside an aborted subtransaction. Treat the COMMIT
3835  * as ROLLBACK: set up to abort everything and exit the main
3836  * transaction.
3837  */
3838  case TBLOCK_SUBABORT:
3839  while (s->parent != NULL)
3840  {
3841  if (s->blockState == TBLOCK_SUBINPROGRESS)
3843  else if (s->blockState == TBLOCK_SUBABORT)
3845  else
3846  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3848  s = s->parent;
3849  }
3850  if (s->blockState == TBLOCK_INPROGRESS)
3852  else if (s->blockState == TBLOCK_ABORT)
3854  else
3855  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3857  break;
3858 
3859  /*
3860  * The user issued COMMIT when not inside a transaction. For
3861  * COMMIT without CHAIN, issue a WARNING, staying in
3862  * TBLOCK_STARTED state. The upcoming call to
3863  * CommitTransactionCommand() will then close the transaction and
3864  * put us back into the default state. For COMMIT AND CHAIN,
3865  * error.
3866  */
3867  case TBLOCK_STARTED:
3868  if (chain)
3869  ereport(ERROR,
3870  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3871  /* translator: %s represents an SQL statement name */
3872  errmsg("%s can only be used in transaction blocks",
3873  "COMMIT AND CHAIN")));
3874  else
3875  ereport(WARNING,
3876  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3877  errmsg("there is no transaction in progress")));
3878  result = true;
3879  break;
3880 
3881  /*
3882  * The user issued a COMMIT that somehow ran inside a parallel
3883  * worker. We can't cope with that.
3884  */
3886  ereport(FATAL,
3887  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3888  errmsg("cannot commit during a parallel operation")));
3889  break;
3890 
3891  /* These cases are invalid. */
3892  case TBLOCK_DEFAULT:
3893  case TBLOCK_BEGIN:
3894  case TBLOCK_SUBBEGIN:
3895  case TBLOCK_END:
3896  case TBLOCK_SUBRELEASE:
3897  case TBLOCK_SUBCOMMIT:
3898  case TBLOCK_ABORT_END:
3899  case TBLOCK_SUBABORT_END:
3900  case TBLOCK_ABORT_PENDING:
3902  case TBLOCK_SUBRESTART:
3904  case TBLOCK_PREPARE:
3905  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3907  break;
3908  }
3909 
3911  s->blockState == TBLOCK_END ||
3912  s->blockState == TBLOCK_ABORT_END ||
3914 
3915  s->chain = chain;
3916 
3917  return result;
3918 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
#define ERROR
Definition: elog.h:46
#define FATAL
Definition: elog.h:49
struct TransactionStateData * parent
Definition: xact.c:208
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5400
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 979 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

980 {
982 
983  Assert(s->parallelModeLevel >= 0);
984 
985  ++s->parallelModeLevel;
986 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:804

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5215 of file xact.c.

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

Referenced by InitializeParallelDSM().

5216 {
5217  TransactionState s;
5218  Size nxids = 0;
5220 
5221  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5222  {
5224  nxids = add_size(nxids, 1);
5225  nxids = add_size(nxids, s->nChildXids);
5226  }
5227 
5228  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5229 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
uint32 TransactionId
Definition: c.h:587
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
struct TransactionStateData * parent
Definition: xact.c:208
FullTransactionId fullTransactionId
Definition: xact.c:187
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
#define SerializedTransactionStateHeaderSize
Definition: xact.c:229
size_t Size
Definition: c.h:540

◆ ExitParallelMode()

void ExitParallelMode ( void  )

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

993 {
995 
996  Assert(s->parallelModeLevel > 0);
998 
999  --s->parallelModeLevel;
1000 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
bool ParallelContextActive(void)
Definition: parallel.c:990
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:804

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1071 of file xact.c.

References forceSyncCommit.

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

1072 {
1073  forceSyncCommit = true;
1074 }
static bool forceSyncCommit
Definition: xact.c:280

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 761 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

Referenced by ATRewriteTable(), CatalogTuplesMultiInsertWithInfo(), CopyFrom(), create_edata_for_relation(), GetSnapshotData(), GetSnapshotDataReuse(), 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().

762 {
763  /* this is global to a transaction, not subtransaction-local */
764  if (used)
765  {
766  /*
767  * Forbid setting currentCommandIdUsed in a parallel worker, because
768  * we have no provision for communicating this back to the leader. We
769  * could relax this restriction when currentCommandIdUsed was already
770  * true at the start of the parallel operation.
771  */
773  currentCommandIdUsed = true;
774  }
775  return currentCommandId;
776 }
#define IsParallelWorker()
Definition: parallel.h:61
#define Assert(condition)
Definition: c.h:804
static CommandId currentCommandId
Definition: xact.c:257
static bool currentCommandIdUsed
Definition: xact.c:258

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 496 of file xact.c.

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

497 {
499 
502  return s->fullTransactionId;
503 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
FullTransactionId fullTransactionId
Definition: xact.c:187
static void AssignTransactionId(TransactionState s)
Definition: xact.c:569

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 514 of file xact.c.

References TransactionStateData::fullTransactionId.

515 {
517 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
FullTransactionId fullTransactionId
Definition: xact.c:187

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 807 of file xact.c.

References stmtStartTimestamp.

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

808 {
809  return stmtStartTimestamp;
810 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:268

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 455 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

Referenced by IsSubTransactionAssignmentPending(), RecordTransactionAbort(), ReorderBufferProcessTXN(), and XLogRecordAssemble().

456 {
458 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define XidFromFullTransactionId(x)
Definition: transam.h:48
FullTransactionId fullTransactionId
Definition: xact.c:187

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

Definition at line 798 of file xact.c.

References xactStartTimestamp.

Referenced by GetCurrentTimeUsec(), GetSQLCurrentTimestamp(), GetSQLLocalTimestamp(), InitializeParallelDSM(), now(), pg_timezone_abbrevs(), pg_timezone_names(), and timetz_zone().

799 {
800  return xactStartTimestamp;
801 }
static TimestampTz xactStartTimestamp
Definition: xact.c:267

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 819 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

820 {
821  if (xactStopTimestamp != 0)
822  return xactStopTimestamp;
823  return GetCurrentTimestamp();
824 }
static TimestampTz xactStopTimestamp
Definition: xact.c:269
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 541 of file xact.c.

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

Referenced by xid_age().

542 {
544  static TransactionId stablexid = InvalidTransactionId;
545 
546  if (lxid != MyProc->lxid)
547  {
548  lxid = MyProc->lxid;
549  stablexid = GetTopTransactionIdIfAny();
550  if (!TransactionIdIsValid(stablexid))
551  stablexid = ReadNextTransactionId();
552  }
553 
554  Assert(TransactionIdIsValid(stablexid));
555 
556  return stablexid;
557 }
uint32 TransactionId
Definition: c.h:587
PGPROC * MyProc
Definition: proc.c:68
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:316
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:589
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:425
#define Assert(condition)
Definition: c.h:804
#define InvalidLocalTransactionId
Definition: lock.h:69
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:143

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 467 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

Referenced by pg_current_xact_id().

468 {
472 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
static TransactionStateData TopTransactionStateData
Definition: xact.c:237
static void AssignTransactionId(TransactionState s)
Definition: xact.c:569

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 483 of file xact.c.

References XactTopFullTransactionId.

Referenced by pg_current_xact_id_if_assigned().

484 {
486 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

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

411 {
415 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static TransactionStateData TopTransactionStateData
Definition: xact.c:237
static void AssignTransactionId(TransactionState s)
Definition: xact.c:569

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3502 of file xact.c.

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

Referenced by vacuum().

3503 {
3504  /*
3505  * Return true on same conditions that would make
3506  * PreventInTransactionBlock error out
3507  */
3508  if (IsTransactionBlock())
3509  return true;
3510 
3511  if (IsSubTransaction())
3512  return true;
3513 
3514  if (!isTopLevel)
3515  return true;
3516 
3519  return true;
3520 
3521  return false;
3522 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192
bool IsTransactionBlock(void)
Definition: xact.c:4693
bool IsSubTransaction(void)
Definition: xact.c:4766

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 6106 of file xact.c.

References TransactionStateData::assigned, GetCurrentTransactionIdIfAny(), IsSubTransaction(), IsTransactionState(), TransactionIdIsValid, and XLogLogicalInfoActive.

Referenced by MarkSubTransactionAssigned(), and XLogRecordAssemble().

6107 {
6108  /* wal_level has to be logical */
6109  if (!XLogLogicalInfoActive())
6110  return false;
6111 
6112  /* we need to be in a transaction state */
6113  if (!IsTransactionState())
6114  return false;
6115 
6116  /* it has to be a subtransaction */
6117  if (!IsSubTransaction())
6118  return false;
6119 
6120  /* the subtransaction has to have a XID assigned */
6122  return false;
6123 
6124  /* and it should not be already 'assigned' */
6126 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define XLogLogicalInfoActive()
Definition: xlog.h:183
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:455
bool IsTransactionState(void)
Definition: xact.c:371
bool IsSubTransaction(void)
Definition: xact.c:4766
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ IsTransactionBlock()