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

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

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

◆ AbortSubTransaction()

static void AbortSubTransaction ( void  )
static

Definition at line 4927 of file xact.c.

References AbortBufferIO(), AfterTriggerEndSubXact(), AtEOSubXact_ApplyLauncher(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubAbort_childXids(), AtSubAbort_Memory(), AtSubAbort_Notify(), AtSubAbort_Portals(), AtSubAbort_ResourceOwner(), AtSubAbort_smgr(), AtSubAbort_Snapshot(), CallSubXactCallbacks(), ConditionVariableCancelSleep(), CurrentTransactionState, TransactionStateData::curTransactionOwner, elog, TransactionStateData::fullTransactionId, FullTransactionIdIsValid, TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, IsInParallelMode(), LockErrorCleanup(), LWLockReleaseAll(), TransactionStateData::nestingLevel, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PG_SETMASK, pgstat_progress_end_command(), pgstat_report_wait_end(), TransactionStateData::prevSecContext, TransactionStateData::prevUser, TransactionStateData::prevXactReadOnly, RecordTransactionAbort(), reschedule_timeouts(), 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().

4928 {
4930 
4931  /* Prevent cancel/die interrupt while cleaning up */
4932  HOLD_INTERRUPTS();
4933 
4934  /* Make sure we have a valid memory context and resource owner */
4937 
4938  /*
4939  * Release any LW locks we might be holding as quickly as possible.
4940  * (Regular locks, however, must be held till we finish aborting.)
4941  * Releasing LW locks is critical since we might try to grab them again
4942  * while cleaning up!
4943  *
4944  * FIXME This may be incorrect --- Are there some locks we should keep?
4945  * Buffer locks, for example? I don't think so but I'm not sure.
4946  */
4947  LWLockReleaseAll();
4948 
4951  AbortBufferIO();
4952  UnlockBuffers();
4953 
4954  /* Reset WAL record construction state */
4956 
4957  /* Cancel condition variable sleep */
4959 
4960  /*
4961  * Also clean up any open wait for lock, since the lock manager will choke
4962  * if we try to wait for another lock before doing this.
4963  */
4964  LockErrorCleanup();
4965 
4966  /*
4967  * If any timeout events are still active, make sure the timeout interrupt
4968  * is scheduled. This covers possible loss of a timeout interrupt due to
4969  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4970  * We delay this till after LockErrorCleanup so that we don't uselessly
4971  * reschedule lock or deadlock check timeouts.
4972  */
4974 
4975  /*
4976  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4977  * handler. We do this fairly early in the sequence so that the timeout
4978  * infrastructure will be functional if needed while aborting.
4979  */
4981 
4982  /*
4983  * check the current transaction state
4984  */
4985  ShowTransactionState("AbortSubTransaction");
4986 
4987  if (s->state != TRANS_INPROGRESS)
4988  elog(WARNING, "AbortSubTransaction while in %s state",
4990 
4991  s->state = TRANS_ABORT;
4992 
4993  /*
4994  * Reset user ID which might have been changed transiently. (See notes in
4995  * AbortTransaction.)
4996  */
4998 
4999  /* Forget about any active REINDEX. */
5001 
5002  /* Reset logical streaming state. */
5004 
5005  /* Exit from parallel mode, if necessary. */
5006  if (IsInParallelMode())
5007  {
5009  s->parallelModeLevel = 0;
5010  }
5011 
5012  /*
5013  * We can skip all this stuff if the subxact failed before creating a
5014  * ResourceOwner...
5015  */
5016  if (s->curTransactionOwner)
5017  {
5018  AfterTriggerEndSubXact(false);
5024  s->parent->subTransactionId);
5026 
5027  /* Advertise the fact that we aborted in pg_xact. */
5028  (void) RecordTransactionAbort(true);
5029 
5030  /* Post-abort cleanup */
5033 
5035  s->parent->subTransactionId);
5036 
5039  false, false);
5041  s->parent->subTransactionId);
5042  AtEOSubXact_Inval(false);
5045  false, false);
5048  false, false);
5049  AtSubAbort_smgr();
5050 
5051  AtEOXact_GUC(false, s->gucNestLevel);
5052  AtEOSubXact_SPI(false, s->subTransactionId);
5054  s->parent->subTransactionId);
5056  s->parent->subTransactionId);
5058  s->parent->subTransactionId);
5060  AtEOSubXact_PgStat(false, s->nestingLevel);
5063  }
5064 
5065  /*
5066  * Restore the upper transaction's read-only state, too. This should be
5067  * redundant with GUC's cleanup but we may as well do it for consistency
5068  * with the commit case.
5069  */
5071 
5073 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1798
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3227
void ResetReindexState(int nestLevel)
Definition: index.c:4093
static TransactionState CurrentTransactionState
Definition: xact.c:250
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:588
#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:392
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:504
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:119
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:1747
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5344
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:878
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6008
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
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1512
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2408
void LockErrorCleanup(void)
Definition: proc.c:707
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:3911
void AtSubAbort_Notify(void)
Definition: async.c:1846
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:15569
void pgstat_progress_end_command(void)
Definition: pgstat.c:3529
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2903
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3618
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:482
static const char * TransStateAsString(TransState state)
Definition: xact.c:5447
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:4429
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4816
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1011
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:117
#define elog(elevel,...)
Definition: elog.h:228
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4131
void LWLockReleaseAll(void)
Definition: lwlock.c:1907
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 2610 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().

2611 {
2613  TransactionId latestXid;
2614  bool is_parallel_worker;
2615 
2616  /* Prevent cancel/die interrupt while cleaning up */
2617  HOLD_INTERRUPTS();
2618 
2619  /* Make sure we have a valid memory context and resource owner */
2620  AtAbort_Memory();
2622 
2623  /*
2624  * Release any LW locks we might be holding as quickly as possible.
2625  * (Regular locks, however, must be held till we finish aborting.)
2626  * Releasing LW locks is critical since we might try to grab them again
2627  * while cleaning up!
2628  */
2629  LWLockReleaseAll();
2630 
2631  /* Clear wait information and command progress indicator */
2634 
2635  /* Clean up buffer I/O and buffer context locks, too */
2636  AbortBufferIO();
2637  UnlockBuffers();
2638 
2639  /* Reset WAL record construction state */
2641 
2642  /* Cancel condition variable sleep */
2644 
2645  /*
2646  * Also clean up any open wait for lock, since the lock manager will choke
2647  * if we try to wait for another lock before doing this.
2648  */
2649  LockErrorCleanup();
2650 
2651  /*
2652  * If any timeout events are still active, make sure the timeout interrupt
2653  * is scheduled. This covers possible loss of a timeout interrupt due to
2654  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2655  * We delay this till after LockErrorCleanup so that we don't uselessly
2656  * reschedule lock or deadlock check timeouts.
2657  */
2659 
2660  /*
2661  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2662  * handler. We do this fairly early in the sequence so that the timeout
2663  * infrastructure will be functional if needed while aborting.
2664  */
2666 
2667  /*
2668  * check the current transaction state
2669  */
2670  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2671  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2672  elog(WARNING, "AbortTransaction while in %s state",
2674  Assert(s->parent == NULL);
2675 
2676  /*
2677  * set the current transaction state information appropriately during the
2678  * abort processing
2679  */
2680  s->state = TRANS_ABORT;
2681 
2682  /*
2683  * Reset user ID which might have been changed transiently. We need this
2684  * to clean up in case control escaped out of a SECURITY DEFINER function
2685  * or other local change of CurrentUserId; therefore, the prior value of
2686  * SecurityRestrictionContext also needs to be restored.
2687  *
2688  * (Note: it is not necessary to restore session authorization or role
2689  * settings here because those can only be changed via GUC, and GUC will
2690  * take care of rolling them back if need be.)
2691  */
2693 
2694  /* Forget about any active REINDEX. */
2696 
2697  /* Reset logical streaming state. */
2699 
2700  /* If in parallel mode, clean up workers and exit parallel mode. */
2701  if (IsInParallelMode())
2702  {
2703  AtEOXact_Parallel(false);
2704  s->parallelModeLevel = 0;
2705  }
2706 
2707  /*
2708  * do abort processing
2709  */
2710  AfterTriggerEndXact(false); /* 'false' means it's abort */
2711  AtAbort_Portals();
2712  smgrDoPendingSyncs(false, is_parallel_worker);
2713  AtEOXact_LargeObject(false);
2714  AtAbort_Notify();
2715  AtEOXact_RelationMap(false, is_parallel_worker);
2716  AtAbort_Twophase();
2717 
2718  /*
2719  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2720  * far as assigning an XID to advertise). But if we're inside a parallel
2721  * worker, skip this; the user backend must be the one to write the abort
2722  * record.
2723  */
2724  if (!is_parallel_worker)
2725  latestXid = RecordTransactionAbort(false);
2726  else
2727  {
2728  latestXid = InvalidTransactionId;
2729 
2730  /*
2731  * Since the parallel leader won't get our value of XactLastRecEnd in
2732  * this case, we nudge WAL-writer ourselves in this case. See related
2733  * comments in RecordTransactionAbort for why this matters.
2734  */
2736  }
2737 
2738  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2739 
2740  /*
2741  * Let others know about no transaction in progress by me. Note that this
2742  * must be done _before_ releasing locks we hold and _after_
2743  * RecordTransactionAbort.
2744  */
2745  ProcArrayEndTransaction(MyProc, latestXid);
2746 
2747  /*
2748  * Post-abort cleanup. See notes in CommitTransaction() concerning
2749  * ordering. We can skip all of it if the transaction failed before
2750  * creating a resource owner.
2751  */
2752  if (TopTransactionResourceOwner != NULL)
2753  {
2754  if (is_parallel_worker)
2756  else
2758 
2761  false, true);
2762  AtEOXact_Buffers(false);
2763  AtEOXact_RelationCache(false);
2764  AtEOXact_Inval(false);
2768  false, true);
2771  false, true);
2772  smgrDoPendingDeletes(false);
2773 
2774  AtEOXact_GUC(false, 1);
2775  AtEOXact_SPI(false);
2776  AtEOXact_Enum();
2778  AtEOXact_Namespace(false, is_parallel_worker);
2779  AtEOXact_SMgr();
2780  AtEOXact_Files(false);
2782  AtEOXact_HashTables(false);
2783  AtEOXact_PgStat(false, is_parallel_worker);
2784  AtEOXact_ApplyLauncher(false);
2786  }
2787 
2788  /*
2789  * State remains TRANS_ABORT until CleanupTransaction().
2790  */
2792 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:836
void ResetReindexState(int nestLevel)
Definition: index.c:4093
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
uint32 TransactionId
Definition: c.h:575
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:588
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3581
XLogRecPtr XactLastRecEnd
Definition: xlog.c:362
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3082
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:146
PGPROC * MyProc
Definition: proc.c:68
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2492
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3563
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:15537
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:620
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:504
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2936
void AtAbort_Portals(void)
Definition: portalmem.c:774
TBlockState blockState
Definition: xact.c:192
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:119
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:4720
void XLogResetInsertion(void)
Definition: xloginsert.c:196
void ResetLogicalStreamingState(void)
Definition: logical.c:1747
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:2321
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6008
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1512
void LockErrorCleanup(void)
Definition: proc.c:707
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3911
static void AtAbort_Memory(void)
Definition: xact.c:1753
void pgstat_progress_end_command(void)
Definition: pgstat.c:3529
void AtAbort_Notify(void)
Definition: async.c:1756
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2698
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:792
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:482
static const char * TransStateAsString(TransState state)
Definition: xact.c:5447
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:370
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:4429
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:117
#define elog(elevel,...)
Definition: elog.h:228
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:664
void LWLockReleaseAll(void)
Definition: lwlock.c:1907
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:575
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
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:1057
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:45
#define XLogLogicalInfoActive()
Definition: xlog.h:208
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:598
#define XLogStandbyInfoActive()
Definition: xlog.h:205
static int nUnreportedXids
Definition: xact.c:247
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
#define Assert(condition)
Definition: c.h:792
static TransactionStateData TopTransactionStateData
Definition: xact.c:237
void RegisterPredicateLockingXid(TransactionId xid)
Definition: predicate.c:1941
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
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:44

◆ 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:146
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144

◆ 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:212
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
static MemoryContext TransactionAbortContext
Definition: xact.c:290
struct TransactionStateData * parent
Definition: xact.c:208
MemoryContext curTransactionContext
Definition: xact.c:195
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:792

◆ 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:212
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
MemoryContext curTransactionContext
Definition: xact.c:195
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define Assert(condition)
Definition: c.h:792

◆ 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:170
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
static MemoryContext TransactionAbortContext
Definition: xact.c:290
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext curTransactionContext
Definition: xact.c:195
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define Assert(condition)
Definition: c.h:792

◆ 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:146
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:145
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
ResourceOwner curTransactionOwner
Definition: xact.c:196
#define Assert(condition)
Definition: c.h:792
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:425

◆ 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:1057

◆ 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:792

◆ 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:144
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:212
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
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:792

◆ 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:49
uint32 TransactionId
Definition: c.h:575
#define Min(x, y)
Definition: c.h:974
int errcode(int sqlerrcode)
Definition: elog.c:704
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:197
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:45
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:155
#define Assert(condition)
Definition: c.h:792
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
int errmsg(const char *fmt,...)
Definition: elog.c:915
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797

◆ 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:212
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
bool MemoryContextIsEmpty(MemoryContext context)
Definition: mcxt.c:452
struct TransactionStateData * parent
Definition: xact.c:208
MemoryContext curTransactionContext
Definition: xact.c:195
#define Assert(condition)
Definition: c.h:792

◆ 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:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
MemoryContext curTransactionContext
Definition: xact.c:195
#define Assert(condition)
Definition: c.h:792

◆ 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:145
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
ResourceOwner curTransactionOwner
Definition: xact.c:196
struct TransactionStateData * parent
Definition: xact.c:208
#define Assert(condition)
Definition: c.h:792
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:425

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4041 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

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

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

◆ BlockStateAsString()

static const char * BlockStateAsString ( TBlockState  blockState)
static

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

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

◆ CallSubXactCallbacks()

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

Definition at line 3618 of file xact.c.

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

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

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

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

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

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

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

Referenced by RequireTransactionBlock(), and WarnNoTransactionBlock().

3457 {
3458  /*
3459  * xact block already started?
3460  */
3461  if (IsTransactionBlock())
3462  return;
3463 
3464  /*
3465  * subtransaction?
3466  */
3467  if (IsSubTransaction())
3468  return;
3469 
3470  /*
3471  * inside a function call?
3472  */
3473  if (!isTopLevel)
3474  return;
3475 
3476  ereport(throwError ? ERROR : WARNING,
3477  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3478  /* translator: %s represents an SQL statement name */
3479  errmsg("%s can only be used in transaction blocks",
3480  stmtType)));
3481 }
int errcode(int sqlerrcode)
Definition: elog.c:704
bool IsTransactionBlock(void)
Definition: xact.c:4685
#define ERROR
Definition: elog.h:45
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:155
bool IsSubTransaction(void)
Definition: xact.c:4758
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ CleanupSubTransaction()

static void CleanupSubTransaction ( void  )
static

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

5083 {
5085 
5086  ShowTransactionState("CleanupSubTransaction");
5087 
5088  if (s->state != TRANS_ABORT)
5089  elog(WARNING, "CleanupSubTransaction while in %s state",
5091 
5093 
5096  if (s->curTransactionOwner)
5098  s->curTransactionOwner = NULL;
5099 
5101 
5102  s->state = TRANS_DEFAULT;
5103 
5104  PopTransaction();
5105 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void PopTransaction(void)
Definition: xact.c:5175
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:145
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:727
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:5344
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:5447
#define elog(elevel,...)
Definition: elog.h:228

◆ CleanupTransaction()

static void CleanupTransaction ( void  )
static

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

2799 {
2801 
2802  /*
2803  * State should still be TRANS_ABORT from AbortTransaction().
2804  */
2805  if (s->state != TRANS_ABORT)
2806  elog(FATAL, "CleanupTransaction: unexpected state %s",
2808 
2809  /*
2810  * do abort cleanup processing
2811  */
2812  AtCleanup_Portals(); /* now safe to release portal memory */
2813  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2814 
2815  CurrentResourceOwner = NULL; /* and resource owner */
2818  s->curTransactionOwner = NULL;
2821 
2822  AtCleanup_Memory(); /* and transaction memory */
2823 
2826  s->nestingLevel = 0;
2827  s->gucNestLevel = 0;
2828  s->childXids = NULL;
2829  s->nChildXids = 0;
2830  s->maxChildXids = 0;
2831  s->parallelModeLevel = 0;
2832 
2835 
2836  /*
2837  * done with abort processing, set current transaction state back to
2838  * default
2839  */
2840  s->state = TRANS_DEFAULT;
2841 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
static TransactionState CurrentTransactionState
Definition: xact.c:250
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:146
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:145
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:727
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:54
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:5447
#define InvalidSubTransactionId
Definition: c.h:581
#define InvalidFullTransactionId
Definition: transam.h:56
#define elog(elevel,...)
Definition: elog.h:228
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(), ATExecDetachPartition(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropExpression(), ATExecDropIdentity(), 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(), 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_relation(), ReindexRelationConcurrently(), RelationSetNewRelfilenode(), RenumberEnumType(), replorigin_create(), replorigin_drop(), 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:704
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:45
#define IsParallelWorker()
Definition: parallel.h:61
#define InvalidCommandId
Definition: c.h:592
#define ereport(elevel,...)
Definition: elog.h:155
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:915
#define elog(elevel,...)
Definition: elog.h:228
static CommandId currentCommandId
Definition: xact.c:257
static bool currentCommandIdUsed
Definition: xact.c:258

◆ CommitSubTransaction()

static void CommitSubTransaction ( void  )
static

Definition at line 4818 of file xact.c.

References AfterTriggerEndSubXact(), AtEOSubXact_ApplyLauncher(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubCommit_childXids(), AtSubCommit_Memory(), AtSubCommit_Notify(), AtSubCommit_Portals(), AtSubCommit_smgr(), AtSubCommit_Snapshot(), CallSubXactCallbacks(), CommandCounterIncrement(), CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, TransactionStateData::fullTransactionId, FullTransactionIdIsValid, TransactionStateData::gucNestLevel, IsInParallelMode(), TransactionStateData::nestingLevel, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PopTransaction(), TransactionStateData::prevXactReadOnly, RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), ResourceOwnerRelease(), ShowTransactionState(), TransactionStateData::state, TransactionStateData::subTransactionId, SUBXACT_EVENT_COMMIT_SUB, SUBXACT_EVENT_PRE_COMMIT_SUB, TRANS_COMMIT, TRANS_DEFAULT, TRANS_INPROGRESS, TransStateAsString(), WARNING, XactLockTableDelete(), XactReadOnly, and XidFromFullTransactionId.

Referenced by CommitTransactionCommand(), and ReleaseCurrentSubTransaction().

4819 {
4821 
4822  ShowTransactionState("CommitSubTransaction");
4823 
4824  if (s->state != TRANS_INPROGRESS)
4825  elog(WARNING, "CommitSubTransaction while in %s state",
4827 
4828  /* Pre-commit processing goes here */
4829 
4831  s->parent->subTransactionId);
4832 
4833  /* If in parallel mode, clean up workers and exit parallel mode. */
4834  if (IsInParallelMode())
4835  {
4837  s->parallelModeLevel = 0;
4838  }
4839 
4840  /* Do the actual "commit", such as it is */
4841  s->state = TRANS_COMMIT;
4842 
4843  /* Must CCI to ensure commands of subtransaction are seen as done */
4845 
4846  /*
4847  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4848  * perform that step, if required, as part of the atomic update of the
4849  * whole transaction tree at top level commit or abort.
4850  */
4851 
4852  /* Post-commit cleanup */
4855  AfterTriggerEndSubXact(true);
4860  s->parent->subTransactionId);
4862 
4864  s->parent->subTransactionId);
4865 
4868  true, false);
4870  s->parent->subTransactionId);
4871  AtEOSubXact_Inval(true);
4872  AtSubCommit_smgr();
4873 
4874  /*
4875  * The only lock we actually release here is the subtransaction XID lock.
4876  */
4880 
4881  /*
4882  * Other locks should get transferred to their parent resource owner.
4883  */
4886  true, false);
4889  true, false);
4890 
4891  AtEOXact_GUC(true, s->gucNestLevel);
4894  s->parent->subTransactionId);
4896  s->parent->subTransactionId);
4898  s->parent->subTransactionId);
4900  AtEOSubXact_PgStat(true, s->nestingLevel);
4903 
4904  /*
4905  * We need to restore the upper transaction's read-only state, in case the
4906  * upper is read-write while the child is read-only; GUC will incorrectly
4907  * think it should leave the child state in place.
4908  */
4910 
4914  s->curTransactionOwner = NULL;
4915 
4917 
4918  s->state = TRANS_DEFAULT;
4919 
4920  PopTransaction();
4921 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3227
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void AtSubCommit_Memory(void)
Definition: xact.c:1522
static void PopTransaction(void)
Definition: xact.c:5175
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:145
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1909
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:727
void AtSubCommit_smgr(void)
Definition: storage.c:878
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:392
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:5344
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:878
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6008
SubTransactionId subTransactionId
Definition: xact.c:188
FullTransactionId fullTransactionId
Definition: xact.c:187
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2408
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:15569
void CommandCounterIncrement(void)
Definition: xact.c:1021
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2903
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3618
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:482
static const char * TransStateAsString(TransState state)
Definition: xact.c:5447
bool prevXactReadOnly
Definition: xact.c:202
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4816
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1011
#define elog(elevel,...)
Definition: elog.h:228
void XactLockTableDelete(TransactionId xid)
Definition: lmgr.c:615
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:836
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:575
static void AtCommit_Memory(void)
Definition: xact.c:1495
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3581
XLogRecPtr XactLastRecEnd
Definition: xlog.c:362
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3082
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:146
PGPROC * MyProc
Definition: proc.c:68
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2492
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:145
ResourceOwner CurrentResourceOwner
Definition: resowner.c:144
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:727
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3563
int nParallelCurrentXids
Definition: xact.c:122
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:15537
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:2936
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:119
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:4720
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5344
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2321
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:6008
#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:4664
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:792
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:482
static const char * TransStateAsString(TransState state)
Definition: xact.c:5447
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:370
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:581
bool PreCommit_Portals(bool isPrepare)
Definition: portalmem.c:672
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:15437
#define InvalidFullTransactionId
Definition: transam.h:56
void AtCommit_Notify(void)
Definition: async.c:990
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:117
#define elog(elevel,...)
Definition: elog.h:228
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 2948 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(), 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().

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

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4066 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5332 of file xact.c.

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

Referenced by ParallelWorkerMain().

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

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

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

◆ 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:792

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5209 of file xact.c.

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

Referenced by InitializeParallelDSM().

5210 {
5211  TransactionState s;
5212  Size nxids = 0;
5214 
5215  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5216  {
5218  nxids = add_size(nxids, 1);
5219  nxids = add_size(nxids, s->nChildXids);
5220  }
5221 
5222  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5223 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
uint32 TransactionId
Definition: c.h:575
#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:528

◆ 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:792

◆ 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:792
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:1578

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 541 of file xact.c.

References Assert, GetTopTransactionIdIfAny(), InvalidLocalTransactionId, InvalidTransactionId, PGPROC::lxid, MyProc, ReadNewTransactionId(), 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 = ReadNewTransactionId();
552  }
553 
554  Assert(TransactionIdIsValid(stablexid));
555 
556  return stablexid;
557 }
uint32 TransactionId
Definition: c.h:575
PGPROC * MyProc
Definition: proc.c:68
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:577
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:425
#define Assert(condition)
Definition: c.h:792
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:308
#define InvalidLocalTransactionId
Definition: lock.h:68
#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 3494 of file xact.c.

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

Referenced by vacuum().

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

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 6079 of file xact.c.

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

Referenced by MarkSubTransactionAssigned(), and XLogRecordAssemble().

6080 {
6081  /* wal_level has to be logical */
6082  if (!XLogLogicalInfoActive())
6083  return false;
6084 
6085  /* we need to be in a transaction state */
6086  if (!IsTransactionState())
6087  return false;
6088 
6089  /* it has to be a subtransaction */
6090  if (!IsSubTransaction())
6091  return false;
6092 
6093  /* the subtransaction has to have a XID assigned */
6095  return false;
6096 
6097  /* and it should not be already 'assigned' */
6099 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define XLogLogicalInfoActive()
Definition: xlog.h:208
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:455
bool IsTransactionState(void)
Definition: xact.c:371
bool IsSubTransaction(void)
Definition: xact.c:4758
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4685 of file xact.c.

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

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

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