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 3210 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().

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4580 {
4582 
4583  /* Ensure we're not running in a doomed memory context */
4584  AtAbort_Memory();
4585 
4586  /*
4587  * Get out of any transaction or nested transaction
4588  */
4589  do
4590  {
4591  switch (s->blockState)
4592  {
4593  case TBLOCK_DEFAULT:
4594  if (s->state == TRANS_DEFAULT)
4595  {
4596  /* Not in a transaction, do nothing */
4597  }
4598  else
4599  {
4600  /*
4601  * We can get here after an error during transaction start
4602  * (state will be TRANS_START). Need to clean up the
4603  * incompletely started transaction. First, adjust the
4604  * low-level state to suppress warning message from
4605  * AbortTransaction.
4606  */
4607  if (s->state == TRANS_START)
4608  s->state = TRANS_INPROGRESS;
4609  AbortTransaction();
4611  }
4612  break;
4613  case TBLOCK_STARTED:
4614  case TBLOCK_BEGIN:
4615  case TBLOCK_INPROGRESS:
4618  case TBLOCK_END:
4619  case TBLOCK_ABORT_PENDING:
4620  case TBLOCK_PREPARE:
4621  /* In a transaction, so clean up */
4622  AbortTransaction();
4625  break;
4626  case TBLOCK_ABORT:
4627  case TBLOCK_ABORT_END:
4628 
4629  /*
4630  * AbortTransaction is already done, still need Cleanup.
4631  * However, if we failed partway through running ROLLBACK,
4632  * there will be an active portal running that command, which
4633  * we need to shut down before doing CleanupTransaction.
4634  */
4635  AtAbort_Portals();
4638  break;
4639 
4640  /*
4641  * In a subtransaction, so clean it up and abort parent too
4642  */
4643  case TBLOCK_SUBBEGIN:
4644  case TBLOCK_SUBINPROGRESS:
4645  case TBLOCK_SUBRELEASE:
4646  case TBLOCK_SUBCOMMIT:
4648  case TBLOCK_SUBRESTART:
4651  s = CurrentTransactionState; /* changed by pop */
4652  break;
4653 
4654  case TBLOCK_SUBABORT:
4655  case TBLOCK_SUBABORT_END:
4657  /* As above, but AbortSubTransaction already done */
4658  if (s->curTransactionOwner)
4659  {
4660  /* As in TBLOCK_ABORT, might have a live portal to zap */
4665  }
4667  s = CurrentTransactionState; /* changed by pop */
4668  break;
4669  }
4670  } while (s->blockState != TBLOCK_DEFAULT);
4671 
4672  /* Should be out of all subxacts now */
4673  Assert(s->parent == NULL);
4674 
4675  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4676  AtCleanup_Memory();
4677 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void AtCleanup_Memory(void)
Definition: xact.c:1843
void AtAbort_Portals(void)
Definition: portalmem.c:774
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:2789
struct TransactionStateData * parent
Definition: xact.c:208
SubTransactionId subTransactionId
Definition: xact.c:188
static void AbortSubTransaction(void)
Definition: xact.c:4924
static void AtAbort_Memory(void)
Definition: xact.c:1753
static void AbortTransaction(void)
Definition: xact.c:2601
#define Assert(condition)
Definition: c.h:804
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:970
static void CleanupSubTransaction(void)
Definition: xact.c:5078

◆ AbortSubTransaction()

static void AbortSubTransaction ( void  )
static

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

4925 {
4927 
4928  /* Prevent cancel/die interrupt while cleaning up */
4929  HOLD_INTERRUPTS();
4930 
4931  /* Make sure we have a valid memory context and resource owner */
4934 
4935  /*
4936  * Release any LW locks we might be holding as quickly as possible.
4937  * (Regular locks, however, must be held till we finish aborting.)
4938  * Releasing LW locks is critical since we might try to grab them again
4939  * while cleaning up!
4940  *
4941  * FIXME This may be incorrect --- Are there some locks we should keep?
4942  * Buffer locks, for example? I don't think so but I'm not sure.
4943  */
4944  LWLockReleaseAll();
4945 
4948  AbortBufferIO();
4949  UnlockBuffers();
4950 
4951  /* Reset WAL record construction state */
4953 
4954  /* Cancel condition variable sleep */
4956 
4957  /*
4958  * Also clean up any open wait for lock, since the lock manager will choke
4959  * if we try to wait for another lock before doing this.
4960  */
4961  LockErrorCleanup();
4962 
4963  /*
4964  * If any timeout events are still active, make sure the timeout interrupt
4965  * is scheduled. This covers possible loss of a timeout interrupt due to
4966  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4967  * We delay this till after LockErrorCleanup so that we don't uselessly
4968  * reschedule lock or deadlock check timeouts.
4969  */
4971 
4972  /*
4973  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4974  * handler. We do this fairly early in the sequence so that the timeout
4975  * infrastructure will be functional if needed while aborting.
4976  */
4978 
4979  /*
4980  * check the current transaction state
4981  */
4982  ShowTransactionState("AbortSubTransaction");
4983 
4984  if (s->state != TRANS_INPROGRESS)
4985  elog(WARNING, "AbortSubTransaction while in %s state",
4987 
4988  s->state = TRANS_ABORT;
4989 
4990  /*
4991  * Reset user ID which might have been changed transiently. (See notes in
4992  * AbortTransaction.)
4993  */
4995 
4996  /* Forget about any active REINDEX. */
4998 
4999  /* Reset logical streaming state. */
5001 
5002  /* Exit from parallel mode, if necessary. */
5003  if (IsInParallelMode())
5004  {
5006  s->parallelModeLevel = 0;
5007  }
5008 
5009  /*
5010  * We can skip all this stuff if the subxact failed before creating a
5011  * ResourceOwner...
5012  */
5013  if (s->curTransactionOwner)
5014  {
5015  AfterTriggerEndSubXact(false);
5021  s->parent->subTransactionId);
5023 
5024  /* Advertise the fact that we aborted in pg_xact. */
5025  (void) RecordTransactionAbort(true);
5026 
5027  /* Post-abort cleanup */
5030 
5032  s->parent->subTransactionId);
5033 
5036  false, false);
5038  s->parent->subTransactionId);
5039  AtEOSubXact_Inval(false);
5042  false, false);
5045  false, false);
5046  AtSubAbort_smgr();
5047 
5048  AtEOXact_GUC(false, s->gucNestLevel);
5049  AtEOSubXact_SPI(false, s->subTransactionId);
5051  s->parent->subTransactionId);
5053  s->parent->subTransactionId);
5055  s->parent->subTransactionId);
5057  AtEOSubXact_PgStat(false, s->nestingLevel);
5059  }
5060 
5061  /*
5062  * Restore the upper transaction's read-only state, too. This should be
5063  * redundant with GUC's cleanup but we may as well do it for consistency
5064  * with the commit case.
5065  */
5067 
5069 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1798
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3201
void ResetReindexState(int nestLevel)
Definition: index.c:4212
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void pgstat_report_wait_end(void)
Definition: wait_event.h:277
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:590
#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:394
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:121
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:196
void ResetLogicalStreamingState(void)
Definition: logical.c:1763
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5340
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6235
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:2448
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:3995
void AtSubAbort_Notify(void)
Definition: async.c:1846
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:16152
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2922
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3616
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:5443
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:970
void AtSubAbort_smgr(void)
Definition: storage.c:898
bool prevXactReadOnly
Definition: xact.c:202
void AbortBufferIO(void)
Definition: bufmgr.c:4493
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4838
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1011
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:119
#define elog(elevel,...)
Definition: elog.h:232
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4131
void LWLockReleaseAll(void)
Definition: lwlock.c:1903
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1204
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1641

◆ AbortTransaction()

static void AbortTransaction ( void  )
static

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

2602 {
2604  TransactionId latestXid;
2605  bool is_parallel_worker;
2606 
2607  /* Prevent cancel/die interrupt while cleaning up */
2608  HOLD_INTERRUPTS();
2609 
2610  /* Make sure we have a valid memory context and resource owner */
2611  AtAbort_Memory();
2613 
2614  /*
2615  * Release any LW locks we might be holding as quickly as possible.
2616  * (Regular locks, however, must be held till we finish aborting.)
2617  * Releasing LW locks is critical since we might try to grab them again
2618  * while cleaning up!
2619  */
2620  LWLockReleaseAll();
2621 
2622  /* Clear wait information and command progress indicator */
2625 
2626  /* Clean up buffer I/O and buffer context locks, too */
2627  AbortBufferIO();
2628  UnlockBuffers();
2629 
2630  /* Reset WAL record construction state */
2632 
2633  /* Cancel condition variable sleep */
2635 
2636  /*
2637  * Also clean up any open wait for lock, since the lock manager will choke
2638  * if we try to wait for another lock before doing this.
2639  */
2640  LockErrorCleanup();
2641 
2642  /*
2643  * If any timeout events are still active, make sure the timeout interrupt
2644  * is scheduled. This covers possible loss of a timeout interrupt due to
2645  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2646  * We delay this till after LockErrorCleanup so that we don't uselessly
2647  * reschedule lock or deadlock check timeouts.
2648  */
2650 
2651  /*
2652  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2653  * handler. We do this fairly early in the sequence so that the timeout
2654  * infrastructure will be functional if needed while aborting.
2655  */
2657 
2658  /*
2659  * check the current transaction state
2660  */
2661  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2662  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2663  elog(WARNING, "AbortTransaction while in %s state",
2665  Assert(s->parent == NULL);
2666 
2667  /*
2668  * set the current transaction state information appropriately during the
2669  * abort processing
2670  */
2671  s->state = TRANS_ABORT;
2672 
2673  /*
2674  * Reset user ID which might have been changed transiently. We need this
2675  * to clean up in case control escaped out of a SECURITY DEFINER function
2676  * or other local change of CurrentUserId; therefore, the prior value of
2677  * SecurityRestrictionContext also needs to be restored.
2678  *
2679  * (Note: it is not necessary to restore session authorization or role
2680  * settings here because those can only be changed via GUC, and GUC will
2681  * take care of rolling them back if need be.)
2682  */
2684 
2685  /* Forget about any active REINDEX. */
2687 
2688  /* Reset logical streaming state. */
2690 
2691  /* If in parallel mode, clean up workers and exit parallel mode. */
2692  if (IsInParallelMode())
2693  {
2694  AtEOXact_Parallel(false);
2695  s->parallelModeLevel = 0;
2696  }
2697 
2698  /*
2699  * do abort processing
2700  */
2701  AfterTriggerEndXact(false); /* 'false' means it's abort */
2702  AtAbort_Portals();
2703  smgrDoPendingSyncs(false, is_parallel_worker);
2704  AtEOXact_LargeObject(false);
2705  AtAbort_Notify();
2706  AtEOXact_RelationMap(false, is_parallel_worker);
2707  AtAbort_Twophase();
2708 
2709  /*
2710  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2711  * far as assigning an XID to advertise). But if we're inside a parallel
2712  * worker, skip this; the user backend must be the one to write the abort
2713  * record.
2714  */
2715  if (!is_parallel_worker)
2716  latestXid = RecordTransactionAbort(false);
2717  else
2718  {
2719  latestXid = InvalidTransactionId;
2720 
2721  /*
2722  * Since the parallel leader won't get our value of XactLastRecEnd in
2723  * this case, we nudge WAL-writer ourselves in this case. See related
2724  * comments in RecordTransactionAbort for why this matters.
2725  */
2727  }
2728 
2729  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2730 
2731  /*
2732  * Let others know about no transaction in progress by me. Note that this
2733  * must be done _before_ releasing locks we hold and _after_
2734  * RecordTransactionAbort.
2735  */
2736  ProcArrayEndTransaction(MyProc, latestXid);
2737 
2738  /*
2739  * Post-abort cleanup. See notes in CommitTransaction() concerning
2740  * ordering. We can skip all of it if the transaction failed before
2741  * creating a resource owner.
2742  */
2743  if (TopTransactionResourceOwner != NULL)
2744  {
2745  if (is_parallel_worker)
2747  else
2749 
2752  false, true);
2753  AtEOXact_Buffers(false);
2754  AtEOXact_RelationCache(false);
2755  AtEOXact_Inval(false);
2759  false, true);
2762  false, true);
2763  smgrDoPendingDeletes(false);
2764 
2765  AtEOXact_GUC(false, 1);
2766  AtEOXact_SPI(false);
2767  AtEOXact_Enum();
2769  AtEOXact_Namespace(false, is_parallel_worker);
2770  AtEOXact_SMgr();
2771  AtEOXact_Files(false);
2773  AtEOXact_HashTables(false);
2774  AtEOXact_PgStat(false, is_parallel_worker);
2775  AtEOXact_ApplyLauncher(false);
2777  }
2778 
2779  /*
2780  * State remains TRANS_ABORT until CleanupTransaction().
2781  */
2783 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:767
void ResetReindexState(int nestLevel)
Definition: index.c:4212
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:596
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1785
static TransactionState CurrentTransactionState
Definition: xact.c:250
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:952
static void pgstat_report_wait_end(void)
Definition: wait_event.h:277
uint32 TransactionId
Definition: c.h:587
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:590
XLogRecPtr XactLastRecEnd
Definition: xlog.c:365
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3056
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
PGPROC * MyProc
Definition: proc.c:68
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2576
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3561
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:16120
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:620
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:2955
void AtAbort_Portals(void)
Definition: portalmem.c:774
TBlockState blockState
Definition: xact.c:192
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:121
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:4742
void XLogResetInsertion(void)
Definition: xloginsert.c:196
void ResetLogicalStreamingState(void)
Definition: logical.c:1763
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:2361
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6235
#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:3995
static void AtAbort_Memory(void)
Definition: xact.c:1753
void AtAbort_Notify(void)
Definition: async.c:1756
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2722
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:5443
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:372
void AtEOXact_MultiXact(void)
Definition: multixact.c:1683
void AtEOXact_RelationMap(bool isCommit, bool isParallelWorker)
Definition: relmapper.c:476
void AbortBufferIO(void)
Definition: bufmgr.c:4493
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:119
#define elog(elevel,...)
Definition: elog.h:232
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:664
void LWLockReleaseAll(void)
Definition: lwlock.c:1903
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:1223
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4063
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:218
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:330
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:153
void XactLockTableInsert(TransactionId xid)
Definition: lmgr.c:599
#define XLogStandbyInfoActive()
Definition: xlog.h:215
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:123

◆ 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:1088
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:687

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

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

Referenced by exec_simple_query().

4040 {
4042 
4043  /*
4044  * If we are in STARTED state (that is, no transaction block is open),
4045  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
4046  * block.
4047  *
4048  * For caller convenience, we consider all other transaction states as
4049  * legal here; otherwise the caller would need its own state check, which
4050  * seems rather pointless.
4051  */
4052  if (s->blockState == TBLOCK_STARTED)
4054 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

4408 {
4410 
4411  /*
4412  * Workers synchronize transaction state at the beginning of each parallel
4413  * operation, so we can't account for new subtransactions after that
4414  * point. We might be able to make an exception for the type of
4415  * subtransaction established by this function, which is typically used in
4416  * contexts where we're going to release or roll back the subtransaction
4417  * before proceeding further, so that no enduring change to the
4418  * transaction state occurs. For now, however, we prohibit this case along
4419  * with all the others.
4420  */
4421  if (IsInParallelMode())
4422  ereport(ERROR,
4423  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4424  errmsg("cannot start subtransactions during a parallel operation")));
4425 
4426  switch (s->blockState)
4427  {
4428  case TBLOCK_STARTED:
4429  case TBLOCK_INPROGRESS:
4431  case TBLOCK_END:
4432  case TBLOCK_PREPARE:
4433  case TBLOCK_SUBINPROGRESS:
4434  /* Normal subtransaction start */
4435  PushTransaction();
4436  s = CurrentTransactionState; /* changed by push */
4437 
4438  /*
4439  * Savepoint names, like the TransactionState block itself, live
4440  * in TopTransactionContext.
4441  */
4442  if (name)
4444  break;
4445 
4446  /* These cases are invalid. */
4447  case TBLOCK_DEFAULT:
4448  case TBLOCK_BEGIN:
4450  case TBLOCK_SUBBEGIN:
4451  case TBLOCK_SUBRELEASE:
4452  case TBLOCK_SUBCOMMIT:
4453  case TBLOCK_ABORT:
4454  case TBLOCK_SUBABORT:
4455  case TBLOCK_ABORT_END:
4456  case TBLOCK_SUBABORT_END:
4457  case TBLOCK_ABORT_PENDING:
4459  case TBLOCK_SUBRESTART:
4461  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4463  break;
4464  }
4465 
4468 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
MemoryContext TopTransactionContext
Definition: mcxt.c:53
void CommitTransactionCommand(void)
Definition: xact.c:2939
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:5390
#define ereport(elevel,...)
Definition: elog.h:157
static void PushTransaction(void)
Definition: xact.c:5111
void StartTransactionCommand(void)
Definition: xact.c:2838
const char * name
Definition: encode.c:515
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 3637 of file xact.c.

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

Referenced by standard_ProcessUtility().

3638 {
3640 
3641  switch (s->blockState)
3642  {
3643  /*
3644  * We are not inside a transaction block, so allow one to begin.
3645  */
3646  case TBLOCK_STARTED:
3647  s->blockState = TBLOCK_BEGIN;
3648  break;
3649 
3650  /*
3651  * BEGIN converts an implicit transaction block to a regular one.
3652  * (Note that we allow this even if we've already done some
3653  * commands, which is a bit odd but matches historical practice.)
3654  */
3656  s->blockState = TBLOCK_BEGIN;
3657  break;
3658 
3659  /*
3660  * Already a transaction block in progress.
3661  */
3662  case TBLOCK_INPROGRESS:
3664  case TBLOCK_SUBINPROGRESS:
3665  case TBLOCK_ABORT:
3666  case TBLOCK_SUBABORT:
3667  ereport(WARNING,
3668  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3669  errmsg("there is already a transaction in progress")));
3670  break;
3671 
3672  /* These cases are invalid. */
3673  case TBLOCK_DEFAULT:
3674  case TBLOCK_BEGIN:
3675  case TBLOCK_SUBBEGIN:
3676  case TBLOCK_END:
3677  case TBLOCK_SUBRELEASE:
3678  case TBLOCK_SUBCOMMIT:
3679  case TBLOCK_ABORT_END:
3680  case TBLOCK_SUBABORT_END:
3681  case TBLOCK_ABORT_PENDING:
3683  case TBLOCK_SUBRESTART:
3685  case TBLOCK_PREPARE:
3686  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3688  break;
3689  }
3690 }
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:5390
#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 5390 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().

5391 {
5392  switch (blockState)
5393  {
5394  case TBLOCK_DEFAULT:
5395  return "DEFAULT";
5396  case TBLOCK_STARTED:
5397  return "STARTED";
5398  case TBLOCK_BEGIN:
5399  return "BEGIN";
5400  case TBLOCK_INPROGRESS:
5401  return "INPROGRESS";
5403  return "IMPLICIT_INPROGRESS";
5405  return "PARALLEL_INPROGRESS";
5406  case TBLOCK_END:
5407  return "END";
5408  case TBLOCK_ABORT:
5409  return "ABORT";
5410  case TBLOCK_ABORT_END:
5411  return "ABORT_END";
5412  case TBLOCK_ABORT_PENDING:
5413  return "ABORT_PENDING";
5414  case TBLOCK_PREPARE:
5415  return "PREPARE";
5416  case TBLOCK_SUBBEGIN:
5417  return "SUBBEGIN";
5418  case TBLOCK_SUBINPROGRESS:
5419  return "SUBINPROGRESS";
5420  case TBLOCK_SUBRELEASE:
5421  return "SUBRELEASE";
5422  case TBLOCK_SUBCOMMIT:
5423  return "SUBCOMMIT";
5424  case TBLOCK_SUBABORT:
5425  return "SUBABORT";
5426  case TBLOCK_SUBABORT_END:
5427  return "SUBABORT_END";
5429  return "SUBABORT_PENDING";
5430  case TBLOCK_SUBRESTART:
5431  return "SUBRESTART";
5433  return "SUBABORT_RESTART";
5434  }
5435  return "UNRECOGNIZED";
5436 }

◆ CallSubXactCallbacks()

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

Definition at line 3616 of file xact.c.

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

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

3619 {
3620  SubXactCallbackItem *item;
3621 
3622  for (item = SubXact_callbacks; item; item = item->next)
3623  item->callback(event, mySubid, parentSubid, item->arg);
3624 }
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 3561 of file xact.c.

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

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

3562 {
3563  XactCallbackItem *item;
3564 
3565  for (item = Xact_callbacks; item; item = item->next)
3566  item->callback(event, item->arg);
3567 }
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 3454 of file xact.c.

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

Referenced by RequireTransactionBlock(), and WarnNoTransactionBlock().

3455 {
3456  /*
3457  * xact block already started?
3458  */
3459  if (IsTransactionBlock())
3460  return;
3461 
3462  /*
3463  * subtransaction?
3464  */
3465  if (IsSubTransaction())
3466  return;
3467 
3468  /*
3469  * inside a function call?
3470  */
3471  if (!isTopLevel)
3472  return;
3473 
3474  ereport(throwError ? ERROR : WARNING,
3475  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3476  /* translator: %s represents an SQL statement name */
3477  errmsg("%s can only be used in transaction blocks",
3478  stmtType)));
3479 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool IsTransactionBlock(void)
Definition: xact.c:4683
#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:4756
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ CleanupSubTransaction()

static void CleanupSubTransaction ( void  )
static

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

5079 {
5081 
5082  ShowTransactionState("CleanupSubTransaction");
5083 
5084  if (s->state != TRANS_ABORT)
5085  elog(WARNING, "CleanupSubTransaction while in %s state",
5087 
5089 
5092  if (s->curTransactionOwner)
5094  s->curTransactionOwner = NULL;
5095 
5097 
5098  s->state = TRANS_DEFAULT;
5099 
5100  PopTransaction();
5101 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void PopTransaction(void)
Definition: xact.c:5171
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:5340
SubTransactionId subTransactionId
Definition: xact.c:188
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1083
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1879
static const char * TransStateAsString(TransState state)
Definition: xact.c:5443
#define elog(elevel,...)
Definition: elog.h:232

◆ CleanupTransaction()

static void CleanupTransaction ( void  )
static

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

2790 {
2792 
2793  /*
2794  * State should still be TRANS_ABORT from AbortTransaction().
2795  */
2796  if (s->state != TRANS_ABORT)
2797  elog(FATAL, "CleanupTransaction: unexpected state %s",
2799 
2800  /*
2801  * do abort cleanup processing
2802  */
2803  AtCleanup_Portals(); /* now safe to release portal memory */
2804  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2805 
2806  CurrentResourceOwner = NULL; /* and resource owner */
2809  s->curTransactionOwner = NULL;
2812 
2813  AtCleanup_Memory(); /* and transaction memory */
2814 
2817  s->nestingLevel = 0;
2818  s->gucNestLevel = 0;
2819  s->childXids = NULL;
2820  s->nChildXids = 0;
2821  s->maxChildXids = 0;
2822  s->parallelModeLevel = 0;
2823 
2826 
2827  /*
2828  * done with abort processing, set current transaction state back to
2829  * default
2830  */
2831  s->state = TRANS_DEFAULT;
2832 }
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:851
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:5443
#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(), apply_handle_delete(), apply_handle_insert(), apply_handle_truncate(), apply_handle_update(), 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(), CreateTrigger(), DefineCollation(), DefineDomain(), DefineQueryRewrite(), DefineRelation(), DefineVirtualRelation(), deleteOneObject(), DelRoleMems(), DetachPartitionFinalize(), DropClonedTriggersFromPartition(), DropRole(), EventTriggerDDLCommandEnd(), EventTriggerDDLCommandStart(), EventTriggerInvoke(), EventTriggerSQLDrop(), EventTriggerTableRewrite(), exec_eval_simple_expr(), exec_execute_message(), exec_parse_message(), exec_simple_query(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), execute_sql_string(), ExplainOnePlan(), fmgr_sql(), hashadjustmembers(), ImportForeignSchema(), index_build(), index_concurrently_swap(), 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(), 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 4816 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().

4817 {
4819 
4820  ShowTransactionState("CommitSubTransaction");
4821 
4822  if (s->state != TRANS_INPROGRESS)
4823  elog(WARNING, "CommitSubTransaction while in %s state",
4825 
4826  /* Pre-commit processing goes here */
4827 
4829  s->parent->subTransactionId);
4830 
4831  /* If in parallel mode, clean up workers and exit parallel mode. */
4832  if (IsInParallelMode())
4833  {
4835  s->parallelModeLevel = 0;
4836  }
4837 
4838  /* Do the actual "commit", such as it is */
4839  s->state = TRANS_COMMIT;
4840 
4841  /* Must CCI to ensure commands of subtransaction are seen as done */
4843 
4844  /*
4845  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4846  * perform that step, if required, as part of the atomic update of the
4847  * whole transaction tree at top level commit or abort.
4848  */
4849 
4850  /* Post-commit cleanup */
4853  AfterTriggerEndSubXact(true);
4858  s->parent->subTransactionId);
4860 
4862  s->parent->subTransactionId);
4863 
4866  true, false);
4868  s->parent->subTransactionId);
4869  AtEOSubXact_Inval(true);
4870  AtSubCommit_smgr();
4871 
4872  /*
4873  * The only lock we actually release here is the subtransaction XID lock.
4874  */
4878 
4879  /*
4880  * Other locks should get transferred to their parent resource owner.
4881  */
4884  true, false);
4887  true, false);
4888 
4889  AtEOXact_GUC(true, s->gucNestLevel);
4892  s->parent->subTransactionId);
4894  s->parent->subTransactionId);
4896  s->parent->subTransactionId);
4898  AtEOSubXact_PgStat(true, s->nestingLevel);
4900 
4901  /*
4902  * We need to restore the upper transaction's read-only state, in case the
4903  * upper is read-write while the child is read-only; GUC will incorrectly
4904  * think it should leave the child state in place.
4905  */
4907 
4911  s->curTransactionOwner = NULL;
4912 
4914 
4915  s->state = TRANS_DEFAULT;
4916 
4917  PopTransaction();
4918 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3201
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void AtSubCommit_Memory(void)
Definition: xact.c:1522
static void PopTransaction(void)
Definition: xact.c:5171
#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:878
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:394
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:1776
static void ShowTransactionState(const char *str)
Definition: xact.c:5340
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6235
SubTransactionId subTransactionId
Definition: xact.c:188
FullTransactionId fullTransactionId
Definition: xact.c:187
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2448
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:16152
void CommandCounterIncrement(void)
Definition: xact.c:1021
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2922
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3616
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:5443
bool prevXactReadOnly
Definition: xact.c:202
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4838
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1011
#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:4131
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1204
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:936

◆ 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  AtCommit_Notify();
2273  AtEOXact_GUC(true, 1);
2274  AtEOXact_SPI(true);
2275  AtEOXact_Enum();
2277  AtEOXact_Namespace(true, is_parallel_worker);
2278  AtEOXact_SMgr();
2279  AtEOXact_Files(true);
2281  AtEOXact_HashTables(true);
2282  AtEOXact_PgStat(true, is_parallel_worker);
2283  AtEOXact_Snapshot(true, false);
2284  AtEOXact_ApplyLauncher(true);
2286 
2287  CurrentResourceOwner = NULL;
2289  s->curTransactionOwner = NULL;
2292 
2293  AtCommit_Memory();
2294 
2297  s->nestingLevel = 0;
2298  s->gucNestLevel = 0;
2299  s->childXids = NULL;
2300  s->nChildXids = 0;
2301  s->maxChildXids = 0;
2302 
2305 
2306  /*
2307  * done with commit processing, set current transaction state back to
2308  * default
2309  */
2310  s->state = TRANS_DEFAULT;
2311 
2313 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:767
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:596
static TransactionState CurrentTransactionState
Definition: xact.c:250
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:952
uint32 TransactionId
Definition: c.h:587
static void AtCommit_Memory(void)
Definition: xact.c:1495
XLogRecPtr XactLastRecEnd
Definition: xlog.c:365
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3056
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
PGPROC * MyProc
Definition: proc.c:68
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2576
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:3561
int nParallelCurrentXids
Definition: xact.c:122
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:16120
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:620
static TransactionId RecordTransactionCommit(void)
Definition: xact.c:1226
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2955
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:121
void pgstat_report_xact_timestamp(TimestampTz tstamp)
void PreCommit_Notify(void)
Definition: async.c:888
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:4742
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5340
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2361
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6235
#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:1494
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4686
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:5443
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:372
void AtEOXact_MultiXact(void)
Definition: multixact.c:1683
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:672
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:16020
#define InvalidFullTransactionId
Definition: transam.h:56
void AtCommit_Notify(void)
Definition: async.c:990
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:119
#define elog(elevel,...)
Definition: elog.h:232
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:664
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:1223
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4063
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1006
LocalTransactionId lxid
Definition: proc.h:143

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2939 of file xact.c.

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

Referenced by _SPI_commit(), apply_handle_commit_internal(), apply_handle_stream_abort(), 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(), ProcessCompletedNotifies(), ProcessIncomingNotify(), ReindexMultipleInternal(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), vacuum(), and vacuum_rel().

2940 {
2942 
2943  if (s->chain)
2945 
2946  switch (s->blockState)
2947  {
2948  /*
2949  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2950  * StartTransactionCommand didn't set the STARTED state
2951  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2952  * by EndParallelWorkerTransaction(), not this function.
2953  */
2954  case TBLOCK_DEFAULT:
2956  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2958  break;
2959 
2960  /*
2961  * If we aren't in a transaction block, just do our usual
2962  * transaction commit, and return to the idle state.
2963  */
2964  case TBLOCK_STARTED:
2967  break;
2968 
2969  /*
2970  * We are completing a "BEGIN TRANSACTION" command, so we change
2971  * to the "transaction block in progress" state and return. (We
2972  * assume the BEGIN did nothing to the database, so we need no
2973  * CommandCounterIncrement.)
2974  */
2975  case TBLOCK_BEGIN:
2977  break;
2978 
2979  /*
2980  * This is the case when we have finished executing a command
2981  * someplace within a transaction block. We increment the command
2982  * counter and return.
2983  */
2984  case TBLOCK_INPROGRESS:
2986  case TBLOCK_SUBINPROGRESS:
2988  break;
2989 
2990  /*
2991  * We are completing a "COMMIT" command. Do it and return to the
2992  * idle state.
2993  */
2994  case TBLOCK_END:
2997  if (s->chain)
2998  {
2999  StartTransaction();
3001  s->chain = false;
3003  }
3004  break;
3005 
3006  /*
3007  * Here we are in the middle of a transaction block but one of the
3008  * commands caused an abort so we do nothing but remain in the
3009  * abort state. Eventually we will get a ROLLBACK command.
3010  */
3011  case TBLOCK_ABORT:
3012  case TBLOCK_SUBABORT:
3013  break;
3014 
3015  /*
3016  * Here we were in an aborted transaction block and we just got
3017  * the ROLLBACK command from the user, so clean up the
3018  * already-aborted transaction and return to the idle state.
3019  */
3020  case TBLOCK_ABORT_END:
3023  if (s->chain)
3024  {
3025  StartTransaction();
3027  s->chain = false;
3029  }
3030  break;
3031 
3032  /*
3033  * Here we were in a perfectly good transaction block but the user
3034  * told us to ROLLBACK anyway. We have to abort the transaction
3035  * and then clean up.
3036  */
3037  case TBLOCK_ABORT_PENDING:
3038  AbortTransaction();
3041  if (s->chain)
3042  {
3043  StartTransaction();
3045  s->chain = false;
3047  }
3048  break;
3049 
3050  /*
3051  * We are completing a "PREPARE TRANSACTION" command. Do it and
3052  * return to the idle state.
3053  */
3054  case TBLOCK_PREPARE:
3057  break;
3058 
3059  /*
3060  * We were just issued a SAVEPOINT inside a transaction block.
3061  * Start a subtransaction. (DefineSavepoint already did
3062  * PushTransaction, so as to have someplace to put the SUBBEGIN
3063  * state.)
3064  */
3065  case TBLOCK_SUBBEGIN:
3068  break;
3069 
3070  /*
3071  * We were issued a RELEASE command, so we end the current
3072  * subtransaction and return to the parent transaction. The parent
3073  * might be ended too, so repeat till we find an INPROGRESS
3074  * transaction or subtransaction.
3075  */
3076  case TBLOCK_SUBRELEASE:
3077  do
3078  {
3080  s = CurrentTransactionState; /* changed by pop */
3081  } while (s->blockState == TBLOCK_SUBRELEASE);
3082 
3085  break;
3086 
3087  /*
3088  * We were issued a COMMIT, so we end the current subtransaction
3089  * hierarchy and perform final commit. We do this by rolling up
3090  * any subtransactions into their parent, which leads to O(N^2)
3091  * operations with respect to resource owners - this isn't that
3092  * bad until we approach a thousands of savepoints but is
3093  * necessary for correctness should after triggers create new
3094  * resource owners.
3095  */
3096  case TBLOCK_SUBCOMMIT:
3097  do
3098  {
3100  s = CurrentTransactionState; /* changed by pop */
3101  } while (s->blockState == TBLOCK_SUBCOMMIT);
3102  /* If we had a COMMIT command, finish off the main xact too */
3103  if (s->blockState == TBLOCK_END)
3104  {
3105  Assert(s->parent == NULL);
3108  if (s->chain)
3109  {
3110  StartTransaction();
3112  s->chain = false;
3114  }
3115  }
3116  else if (s->blockState == TBLOCK_PREPARE)
3117  {
3118  Assert(s->parent == NULL);
3121  }
3122  else
3123  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3125  break;
3126 
3127  /*
3128  * The current already-failed subtransaction is ending due to a
3129  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3130  * examine the parent (which could be in any of several states).
3131  */
3132  case TBLOCK_SUBABORT_END:
3135  break;
3136 
3137  /*
3138  * As above, but it's not dead yet, so abort first.
3139  */
3144  break;
3145 
3146  /*
3147  * The current subtransaction is the target of a ROLLBACK TO
3148  * command. Abort and pop it, then start a new subtransaction
3149  * with the same name.
3150  */
3151  case TBLOCK_SUBRESTART:
3152  {
3153  char *name;
3154  int savepointLevel;
3155 
3156  /* save name and keep Cleanup from freeing it */
3157  name = s->name;
3158  s->name = NULL;
3159  savepointLevel = s->savepointLevel;
3160 
3163 
3164  DefineSavepoint(NULL);
3165  s = CurrentTransactionState; /* changed by push */
3166  s->name = name;
3167  s->savepointLevel = savepointLevel;
3168 
3169  /* This is the same as TBLOCK_SUBBEGIN case */
3173  }
3174  break;
3175 
3176  /*
3177  * Same as above, but the subtransaction had already failed, so we
3178  * don't need AbortSubTransaction.
3179  */
3181  {
3182  char *name;
3183  int savepointLevel;
3184 
3185  /* save name and keep Cleanup from freeing it */
3186  name = s->name;
3187  s->name = NULL;
3188  savepointLevel = s->savepointLevel;
3189 
3191 
3192  DefineSavepoint(NULL);
3193  s = CurrentTransactionState; /* changed by push */
3194  s->name = name;
3195  s->savepointLevel = savepointLevel;
3196 
3197  /* This is the same as TBLOCK_SUBBEGIN case */
3201  }
3202  break;
3203  }
3204 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define AssertState(condition)
Definition: c.h:807
void CommitTransactionCommand(void)
Definition: xact.c:2939
void DefineSavepoint(const char *name)
Definition: xact.c:4086
TBlockState blockState
Definition: xact.c:192
static void PrepareTransaction(void)
Definition: xact.c:2322
#define ERROR
Definition: elog.h:46
static void CommitSubTransaction(void)
Definition: xact.c:4816
#define FATAL
Definition: elog.h:49
static void CleanupTransaction(void)
Definition: xact.c:2789
struct TransactionStateData * parent
Definition: xact.c:208
void SaveTransactionCharacteristics(void)
Definition: xact.c:2919
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5390
static void AbortSubTransaction(void)
Definition: xact.c:4924
void CommandCounterIncrement(void)
Definition: xact.c:1021
static void AbortTransaction(void)
Definition: xact.c:2601
static void StartTransaction(void)
Definition: xact.c:1914
#define Assert(condition)
Definition: c.h:804
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2927
static void CleanupSubTransaction(void)
Definition: xact.c:5078
const char * name
Definition: encode.c:515
#define elog(elevel,...)
Definition: elog.h:232
static void CommitTransaction(void)
Definition: xact.c:2073
static void StartSubTransaction(void)
Definition: xact.c:4779

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

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

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

Referenced by exec_simple_query().

4065 {
4067 
4068  /*
4069  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4070  * allowing CommitTransactionCommand to commit whatever happened during
4071  * the implicit transaction block as though it were a single statement.
4072  *
4073  * For caller convenience, we consider all other transaction states as
4074  * legal here; otherwise the caller would need its own state check, which
4075  * seems rather pointless.
4076  */
4079 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5328 of file xact.c.

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

Referenced by ParallelWorkerMain().

5329 {
5333 }
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 3757 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().

3758 {
3760  bool result = false;
3761 
3762  switch (s->blockState)
3763  {
3764  /*
3765  * We are in a transaction block, so tell CommitTransactionCommand
3766  * to COMMIT.
3767  */
3768  case TBLOCK_INPROGRESS:
3769  s->blockState = TBLOCK_END;
3770  result = true;
3771  break;
3772 
3773  /*
3774  * We are in an implicit transaction block. If AND CHAIN was
3775  * specified, error. Otherwise commit, but issue a warning
3776  * because there was no explicit BEGIN before this.
3777  */
3779  if (chain)
3780  ereport(ERROR,
3781  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3782  /* translator: %s represents an SQL statement name */
3783  errmsg("%s can only be used in transaction blocks",
3784  "COMMIT AND CHAIN")));
3785  else
3786  ereport(WARNING,
3787  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3788  errmsg("there is no transaction in progress")));
3789  s->blockState = TBLOCK_END;
3790  result = true;
3791  break;
3792 
3793  /*
3794  * We are in a failed transaction block. Tell
3795  * CommitTransactionCommand it's time to exit the block.
3796  */
3797  case TBLOCK_ABORT:
3799  break;
3800 
3801  /*
3802  * We are in a live subtransaction block. Set up to subcommit all
3803  * open subtransactions and then commit the main transaction.
3804  */
3805  case TBLOCK_SUBINPROGRESS:
3806  while (s->parent != NULL)
3807  {
3808  if (s->blockState == TBLOCK_SUBINPROGRESS)
3810  else
3811  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3813  s = s->parent;
3814  }
3815  if (s->blockState == TBLOCK_INPROGRESS)
3816  s->blockState = TBLOCK_END;
3817  else
3818  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3820  result = true;
3821  break;
3822 
3823  /*
3824  * Here we are inside an aborted subtransaction. Treat the COMMIT
3825  * as ROLLBACK: set up to abort everything and exit the main
3826  * transaction.
3827  */
3828  case TBLOCK_SUBABORT:
3829  while (s->parent != NULL)
3830  {
3831  if (s->blockState == TBLOCK_SUBINPROGRESS)
3833  else if (s->blockState == TBLOCK_SUBABORT)
3835  else
3836  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3838  s = s->parent;
3839  }
3840  if (s->blockState == TBLOCK_INPROGRESS)
3842  else if (s->blockState == TBLOCK_ABORT)
3844  else
3845  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3847  break;
3848 
3849  /*
3850  * The user issued COMMIT when not inside a transaction. For
3851  * COMMIT without CHAIN, issue a WARNING, staying in
3852  * TBLOCK_STARTED state. The upcoming call to
3853  * CommitTransactionCommand() will then close the transaction and
3854  * put us back into the default state. For COMMIT AND CHAIN,
3855  * error.
3856  */
3857  case TBLOCK_STARTED:
3858  if (chain)
3859  ereport(ERROR,
3860  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3861  /* translator: %s represents an SQL statement name */
3862  errmsg("%s can only be used in transaction blocks",
3863  "COMMIT AND CHAIN")));
3864  else
3865  ereport(WARNING,
3866  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3867  errmsg("there is no transaction in progress")));
3868  result = true;
3869  break;
3870 
3871  /*
3872  * The user issued a COMMIT that somehow ran inside a parallel
3873  * worker. We can't cope with that.
3874  */
3876  ereport(FATAL,
3877  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3878  errmsg("cannot commit during a parallel operation")));
3879  break;
3880 
3881  /* These cases are invalid. */
3882  case TBLOCK_DEFAULT:
3883  case TBLOCK_BEGIN:
3884  case TBLOCK_SUBBEGIN:
3885  case TBLOCK_END:
3886  case TBLOCK_SUBRELEASE:
3887  case TBLOCK_SUBCOMMIT:
3888  case TBLOCK_ABORT_END:
3889  case TBLOCK_SUBABORT_END:
3890  case TBLOCK_ABORT_PENDING:
3892  case TBLOCK_SUBRESTART:
3894  case TBLOCK_PREPARE:
3895  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3897  break;
3898  }
3899 
3901  s->blockState == TBLOCK_END ||
3902  s->blockState == TBLOCK_ABORT_END ||
3904 
3905  s->chain = chain;
3906 
3907  return result;
3908 }
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:5390
#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 5205 of file xact.c.

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

Referenced by InitializeParallelDSM().

5206 {
5207  TransactionState s;
5208  Size nxids = 0;
5210 
5211  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5212  {
5214  nxids = add_size(nxids, 1);
5215  nxids = add_size(nxids, s->nChildXids);
5216  }
5217 
5218  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5219 }
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:979
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_estate_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(), and pg_timezone_names().

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:308
#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 3492 of file xact.c.

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

Referenced by vacuum().

3493 {
3494  /*
3495  * Return true on same conditions that would make
3496  * PreventInTransactionBlock error out
3497  */
3498  if (IsTransactionBlock())
3499  return true;
3500 
3501  if (IsSubTransaction())
3502  return true;
3503 
3504  if (!isTopLevel)
3505  return true;
3506 
3509  return true;
3510 
3511  return false;
3512 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192
bool IsTransactionBlock(void)
Definition: xact.c:4683
bool IsSubTransaction(void)
Definition: xact.c:4756

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 6087 of file xact.c.

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

Referenced by MarkSubTransactionAssigned(), and XLogRecordAssemble().

6088 {
6089  /* wal_level has to be logical */
6090  if (!XLogLogicalInfoActive())
6091  return false;
6092 
6093  /* we need to be in a transaction state */
6094  if (!IsTransactionState())
6095  return false;
6096 
6097  /* it has to be a subtransaction */
6098  if (!IsSubTransaction())
6099  return false;
6100 
6101  /* the subtransaction has to have a XID assigned */
6103  return false;
6104 
6105  /* and it should not be already 'assigned' */
6107 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define XLogLogicalInfoActive()
Definition: xlog.h:218
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:455
bool IsTransactionState(void)
Definition: xact.c:371
bool IsSubTransaction(void)
Definition: xact.c:4756
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4683 of file xact.c.

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

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

4684 {
4686 
4688  return false;
4689 
4690  return true;
4691 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
Definition: