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

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

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

◆ AbortSubTransaction()

static void AbortSubTransaction ( void  )
static

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

4927 {
4929 
4930  /* Prevent cancel/die interrupt while cleaning up */
4931  HOLD_INTERRUPTS();
4932 
4933  /* Make sure we have a valid memory context and resource owner */
4936 
4937  /*
4938  * Release any LW locks we might be holding as quickly as possible.
4939  * (Regular locks, however, must be held till we finish aborting.)
4940  * Releasing LW locks is critical since we might try to grab them again
4941  * while cleaning up!
4942  *
4943  * FIXME This may be incorrect --- Are there some locks we should keep?
4944  * Buffer locks, for example? I don't think so but I'm not sure.
4945  */
4946  LWLockReleaseAll();
4947 
4950  AbortBufferIO();
4951  UnlockBuffers();
4952 
4953  /* Reset WAL record construction state */
4955 
4956  /* Cancel condition variable sleep */
4958 
4959  /*
4960  * Also clean up any open wait for lock, since the lock manager will choke
4961  * if we try to wait for another lock before doing this.
4962  */
4963  LockErrorCleanup();
4964 
4965  /*
4966  * If any timeout events are still active, make sure the timeout interrupt
4967  * is scheduled. This covers possible loss of a timeout interrupt due to
4968  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4969  * We delay this till after LockErrorCleanup so that we don't uselessly
4970  * reschedule lock or deadlock check timeouts.
4971  */
4973 
4974  /*
4975  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4976  * handler. We do this fairly early in the sequence so that the timeout
4977  * infrastructure will be functional if needed while aborting.
4978  */
4980 
4981  /*
4982  * check the current transaction state
4983  */
4984  ShowTransactionState("AbortSubTransaction");
4985 
4986  if (s->state != TRANS_INPROGRESS)
4987  elog(WARNING, "AbortSubTransaction while in %s state",
4989 
4990  s->state = TRANS_ABORT;
4991 
4992  /*
4993  * Reset user ID which might have been changed transiently. (See notes in
4994  * AbortTransaction.)
4995  */
4997 
4998  /* Forget about any active REINDEX. */
5000 
5001  /* Reset logical streaming state. */
5003 
5004  /* Exit from parallel mode, if necessary. */
5005  if (IsInParallelMode())
5006  {
5008  s->parallelModeLevel = 0;
5009  }
5010 
5011  /*
5012  * We can skip all this stuff if the subxact failed before creating a
5013  * ResourceOwner...
5014  */
5015  if (s->curTransactionOwner)
5016  {
5017  AfterTriggerEndSubXact(false);
5023  s->parent->subTransactionId);
5025 
5026  /* Advertise the fact that we aborted in pg_xact. */
5027  (void) RecordTransactionAbort(true);
5028 
5029  /* Post-abort cleanup */
5032 
5034  s->parent->subTransactionId);
5035 
5038  false, false);
5040  s->parent->subTransactionId);
5041  AtEOSubXact_Inval(false);
5044  false, false);
5047  false, false);
5048  AtSubAbort_smgr();
5049 
5050  AtEOXact_GUC(false, s->gucNestLevel);
5051  AtEOSubXact_SPI(false, s->subTransactionId);
5053  s->parent->subTransactionId);
5055  s->parent->subTransactionId);
5057  s->parent->subTransactionId);
5059  AtEOSubXact_PgStat(false, s->nestingLevel);
5062  }
5063 
5064  /*
5065  * Restore the upper transaction's read-only state, too. This should be
5066  * redundant with GUC's cleanup but we may as well do it for consistency
5067  * with the commit case.
5068  */
5070 
5072 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1798
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3207
void ResetReindexState(int nestLevel)
Definition: index.c:3881
static TransactionState CurrentTransactionState
Definition: xact.c:250
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:562
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1883
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:386
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:415
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:1055
TransState state
Definition: xact.c: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:1450
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5343
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:874
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5956
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:1381
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2230
void LockErrorCleanup(void)
Definition: proc.c:711
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:632
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3729
void AtSubAbort_Notify(void)
Definition: async.c:1833
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:15377
void pgstat_progress_end_command(void)
Definition: pgstat.c:3282
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2871
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3617
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:478
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:4207
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4748
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1011
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:214
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4132
void LWLockReleaseAll(void)
Definition: lwlock.c:1911
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1203
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1641

◆ AbortTransaction()

static void AbortTransaction ( void  )
static

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

2610 {
2612  TransactionId latestXid;
2613  bool is_parallel_worker;
2614 
2615  /* Prevent cancel/die interrupt while cleaning up */
2616  HOLD_INTERRUPTS();
2617 
2618  /* Make sure we have a valid memory context and resource owner */
2619  AtAbort_Memory();
2621 
2622  /*
2623  * Release any LW locks we might be holding as quickly as possible.
2624  * (Regular locks, however, must be held till we finish aborting.)
2625  * Releasing LW locks is critical since we might try to grab them again
2626  * while cleaning up!
2627  */
2628  LWLockReleaseAll();
2629 
2630  /* Clear wait information and command progress indicator */
2633 
2634  /* Clean up buffer I/O and buffer context locks, too */
2635  AbortBufferIO();
2636  UnlockBuffers();
2637 
2638  /* Reset WAL record construction state */
2640 
2641  /* Cancel condition variable sleep */
2643 
2644  /*
2645  * Also clean up any open wait for lock, since the lock manager will choke
2646  * if we try to wait for another lock before doing this.
2647  */
2648  LockErrorCleanup();
2649 
2650  /*
2651  * If any timeout events are still active, make sure the timeout interrupt
2652  * is scheduled. This covers possible loss of a timeout interrupt due to
2653  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2654  * We delay this till after LockErrorCleanup so that we don't uselessly
2655  * reschedule lock or deadlock check timeouts.
2656  */
2658 
2659  /*
2660  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2661  * handler. We do this fairly early in the sequence so that the timeout
2662  * infrastructure will be functional if needed while aborting.
2663  */
2665 
2666  /*
2667  * check the current transaction state
2668  */
2669  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2670  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2671  elog(WARNING, "AbortTransaction while in %s state",
2673  Assert(s->parent == NULL);
2674 
2675  /*
2676  * set the current transaction state information appropriately during the
2677  * abort processing
2678  */
2679  s->state = TRANS_ABORT;
2680 
2681  /*
2682  * Reset user ID which might have been changed transiently. We need this
2683  * to clean up in case control escaped out of a SECURITY DEFINER function
2684  * or other local change of CurrentUserId; therefore, the prior value of
2685  * SecurityRestrictionContext also needs to be restored.
2686  *
2687  * (Note: it is not necessary to restore session authorization or role
2688  * settings here because those can only be changed via GUC, and GUC will
2689  * take care of rolling them back if need be.)
2690  */
2692 
2693  /* Forget about any active REINDEX. */
2695 
2696  /* Reset logical streaming state. */
2698 
2699  /* If in parallel mode, clean up workers and exit parallel mode. */
2700  if (IsInParallelMode())
2701  {
2702  AtEOXact_Parallel(false);
2703  s->parallelModeLevel = 0;
2704  }
2705 
2706  /*
2707  * do abort processing
2708  */
2709  AfterTriggerEndXact(false); /* 'false' means it's abort */
2710  AtAbort_Portals();
2711  smgrDoPendingSyncs(false, is_parallel_worker);
2712  AtEOXact_LargeObject(false);
2713  AtAbort_Notify();
2714  AtEOXact_RelationMap(false, is_parallel_worker);
2715  AtAbort_Twophase();
2716 
2717  /*
2718  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2719  * far as assigning an XID to advertise). But if we're inside a parallel
2720  * worker, skip this; the user backend must be the one to write the abort
2721  * record.
2722  */
2723  if (!is_parallel_worker)
2724  latestXid = RecordTransactionAbort(false);
2725  else
2726  {
2727  latestXid = InvalidTransactionId;
2728 
2729  /*
2730  * Since the parallel leader won't get our value of XactLastRecEnd in
2731  * this case, we nudge WAL-writer ourselves in this case. See related
2732  * comments in RecordTransactionAbort for why this matters.
2733  */
2735  }
2736 
2737  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2738 
2739  /*
2740  * Let others know about no transaction in progress by me. Note that this
2741  * must be done _before_ releasing locks we hold and _after_
2742  * RecordTransactionAbort.
2743  */
2744  ProcArrayEndTransaction(MyProc, latestXid);
2745 
2746  /*
2747  * Post-abort cleanup. See notes in CommitTransaction() concerning
2748  * ordering. We can skip all of it if the transaction failed before
2749  * creating a resource owner.
2750  */
2751  if (TopTransactionResourceOwner != NULL)
2752  {
2753  if (is_parallel_worker)
2755  else
2757 
2760  false, true);
2761  AtEOXact_Buffers(false);
2762  AtEOXact_RelationCache(false);
2763  AtEOXact_Inval(false);
2767  false, true);
2770  false, true);
2771  smgrDoPendingDeletes(false);
2772 
2773  AtEOXact_GUC(false, 1);
2774  AtEOXact_SPI(false);
2775  AtEOXact_Enum();
2777  AtEOXact_Namespace(false, is_parallel_worker);
2778  AtEOXact_SMgr();
2779  AtEOXact_Files(false);
2781  AtEOXact_HashTables(false);
2782  AtEOXact_PgStat(false, is_parallel_worker);
2783  AtEOXact_ApplyLauncher(false);
2785  }
2786 
2787  /*
2788  * State remains TRANS_ABORT until CleanupTransaction().
2789  */
2791 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:832
void ResetReindexState(int nestLevel)
Definition: index.c:3881
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:595
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:520
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:562
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3334
XLogRecPtr XactLastRecEnd
Definition: xlog.c:360
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3062
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2478
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3562
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:15345
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:397
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:415
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2904
void AtAbort_Portals(void)
Definition: portalmem.c:774
TBlockState blockState
Definition: xact.c:192
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
TransState state
Definition: xact.c:191
void AtEOXact_SMgr(void)
Definition: smgr.c:661
bool IsInParallelMode(void)
Definition: xact.c:1012
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4652
void XLogResetInsertion(void)
Definition: xloginsert.c:196
void ResetLogicalStreamingState(void)
Definition: logical.c:1450
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:2143
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5956
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1381
void LockErrorCleanup(void)
Definition: proc.c:711
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3729
static void AtAbort_Memory(void)
Definition: xact.c:1753
void pgstat_progress_end_command(void)
Definition: pgstat.c:3282
void AtAbort_Notify(void)
Definition: async.c:1743
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2682
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:745
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5447
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:364
void AtEOXact_MultiXact(void)
Definition: multixact.c:1664
void AtEOXact_RelationMap(bool isCommit, bool isParallelWorker)
Definition: relmapper.c:476
void AbortBufferIO(void)
Definition: bufmgr.c:4207
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:214
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:664
void LWLockReleaseAll(void)
Definition: lwlock.c:1911
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1857
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1222
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4064
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1641
LocalTransactionId lxid
Definition: proc.h:112

◆ 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:520
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
TransactionId xtop
Definition: xact.h:197
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:1056
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:43
#define XLogLogicalInfoActive()
Definition: xlog.h:208
struct TransactionStateData * parent
Definition: xact.c:208
#define MinSizeOfXactAssignment
Definition: xact.h:202
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:152
void XactLockTableInsert(TransactionId xid)
Definition: lmgr.c:583
#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:745
static TransactionStateData TopTransactionStateData
Definition: xact.c:237
void RegisterPredicateLockingXid(TransactionId xid)
Definition: predicate.c:1872
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:214
FullTransactionId GetNewTransactionId(bool isSubXact)
Definition: varsup.c:49
static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: xact.c: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:144
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142

◆ 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:211
MemoryContext TopTransactionContext
Definition: mcxt.c:49
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
static MemoryContext TransactionAbortContext
Definition: xact.c: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:745

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

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

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

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

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

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

◆ 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:142
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:211
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:745

◆ 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:520
#define Min(x, y)
Definition: c.h:927
int errcode(int sqlerrcode)
Definition: elog.c:610
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:197
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
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:144
#define Assert(condition)
Definition: c.h:745
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
int errmsg(const char *fmt,...)
Definition: elog.c:824
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796

◆ AtSubCommit_Memory()

static void AtSubCommit_Memory ( void  )
static

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

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

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

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4040 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

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

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

◆ BlockStateAsString()

static const char * BlockStateAsString ( TBlockState  blockState)
static

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

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

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

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

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

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

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

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

Referenced by RequireTransactionBlock(), and WarnNoTransactionBlock().

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

◆ CleanupSubTransaction()

static void CleanupSubTransaction ( void  )
static

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

5082 {
5084 
5085  ShowTransactionState("CleanupSubTransaction");
5086 
5087  if (s->state != TRANS_ABORT)
5088  elog(WARNING, "CleanupSubTransaction while in %s state",
5090 
5092 
5095  if (s->curTransactionOwner)
5097  s->curTransactionOwner = NULL;
5098 
5100 
5101  s->state = TRANS_DEFAULT;
5102 
5103  PopTransaction();
5104 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void PopTransaction(void)
Definition: xact.c:5174
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5343
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:214

◆ CleanupTransaction()

static void CleanupTransaction ( void  )
static

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

2798 {
2800 
2801  /*
2802  * State should still be TRANS_ABORT from AbortTransaction().
2803  */
2804  if (s->state != TRANS_ABORT)
2805  elog(FATAL, "CleanupTransaction: unexpected state %s",
2807 
2808  /*
2809  * do abort cleanup processing
2810  */
2811  AtCleanup_Portals(); /* now safe to release portal memory */
2812  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2813 
2814  CurrentResourceOwner = NULL; /* and resource owner */
2817  s->curTransactionOwner = NULL;
2820 
2821  AtCleanup_Memory(); /* and transaction memory */
2822 
2825  s->nestingLevel = 0;
2826  s->gucNestLevel = 0;
2827  s->childXids = NULL;
2828  s->nChildXids = 0;
2829  s->maxChildXids = 0;
2830  s->parallelModeLevel = 0;
2831 
2834 
2835  /*
2836  * done with abort processing, set current transaction state back to
2837  * default
2838  */
2839  s->state = TRANS_DEFAULT;
2840 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
static TransactionState CurrentTransactionState
Definition: xact.c:250
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
static void AtCleanup_Memory(void)
Definition: xact.c: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:52
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:526
#define InvalidFullTransactionId
Definition: transam.h:56
#define elog(elevel,...)
Definition: elog.h:214
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 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_create(), IndexSetParentIndex(), InitTempTableNamespace(), inv_create(), inv_drop(), inv_truncate(), inv_write(), LogicalRepSyncTableStart(), make_new_heap(), makeConfigurationDependencies(), moveArrayTypeName(), OperatorShellMake(), OperatorUpd(), pg_import_system_collations(), PortalRunMulti(), ProcedureCreate(), ProcessUtilitySlow(), recordExtensionInitPrivWorker(), reindex_relation(), ReindexRelationConcurrently(), RelationSetNewRelfilenode(), RenumberEnumType(), replorigin_create(), replorigin_drop(), ri_PerformCheck(), SetMatViewPopulatedState(), shdepReassignOwned(), SPI_cursor_open_internal(), standard_ProcessUtility(), StoreConstraints(), StorePartitionBound(), tryAttachPartitionForeignKey(), vacuum(), and validatePartitionedIndex().

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:610
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:43
#define IsParallelWorker()
Definition: parallel.h:61
#define InvalidCommandId
Definition: c.h:537
#define ereport(elevel,...)
Definition: elog.h:144
static void AtCCI_LocalCache(void)
Definition: xact.c:1476
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:546
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
static CommandId currentCommandId
Definition: xact.c:257
static bool currentCommandIdUsed
Definition: xact.c:258

◆ CommitSubTransaction()

static void CommitSubTransaction ( void  )
static

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

4818 {
4820 
4821  ShowTransactionState("CommitSubTransaction");
4822 
4823  if (s->state != TRANS_INPROGRESS)
4824  elog(WARNING, "CommitSubTransaction while in %s state",
4826 
4827  /* Pre-commit processing goes here */
4828 
4830  s->parent->subTransactionId);
4831 
4832  /* If in parallel mode, clean up workers and exit parallel mode. */
4833  if (IsInParallelMode())
4834  {
4836  s->parallelModeLevel = 0;
4837  }
4838 
4839  /* Do the actual "commit", such as it is */
4840  s->state = TRANS_COMMIT;
4841 
4842  /* Must CCI to ensure commands of subtransaction are seen as done */
4844 
4845  /*
4846  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4847  * perform that step, if required, as part of the atomic update of the
4848  * whole transaction tree at top level commit or abort.
4849  */
4850 
4851  /* Post-commit cleanup */
4854  AfterTriggerEndSubXact(true);
4859  s->parent->subTransactionId);
4861 
4863  s->parent->subTransactionId);
4864 
4867  true, false);
4869  s->parent->subTransactionId);
4870  AtEOSubXact_Inval(true);
4871  AtSubCommit_smgr();
4872 
4873  /*
4874  * The only lock we actually release here is the subtransaction XID lock.
4875  */
4879 
4880  /*
4881  * Other locks should get transferred to their parent resource owner.
4882  */
4885  true, false);
4888  true, false);
4889 
4890  AtEOXact_GUC(true, s->gucNestLevel);
4893  s->parent->subTransactionId);
4895  s->parent->subTransactionId);
4897  s->parent->subTransactionId);
4899  AtEOSubXact_PgStat(true, s->nestingLevel);
4902 
4903  /*
4904  * We need to restore the upper transaction's read-only state, in case the
4905  * upper is read-write while the child is read-only; GUC will incorrectly
4906  * think it should leave the child state in place.
4907  */
4909 
4913  s->curTransactionOwner = NULL;
4914 
4916 
4917  s->state = TRANS_DEFAULT;
4918 
4919  PopTransaction();
4920 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3207
static TransactionState CurrentTransactionState
Definition: xact.c:250
static void AtSubCommit_Memory(void)
Definition: xact.c:1522
static void PopTransaction(void)
Definition: xact.c:5174
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1883
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
void AtSubCommit_smgr(void)
Definition: storage.c:878
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:386
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:1034
#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:1763
static void ShowTransactionState(const char *str)
Definition: xact.c:5343
void AtEOSubXact_ApplyLauncher(bool isCommit, int nestDepth)
Definition: launcher.c:874
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5956
SubTransactionId subTransactionId
Definition: xact.c:188
FullTransactionId fullTransactionId
Definition: xact.c:187
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2230
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:15377
void CommandCounterIncrement(void)
Definition: xact.c:1021
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2871
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3617
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:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5447
bool prevXactReadOnly
Definition: xact.c:202
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4748
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:1011
#define elog(elevel,...)
Definition: elog.h:214
void XactLockTableDelete(TransactionId xid)
Definition: lmgr.c:600
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:4132
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:1203
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:936

◆ CommitTransaction()

static void CommitTransaction ( void  )
static

Definition at line 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. Since closing cursors could queue trigger actions,
2098  * triggers could open cursors, etc, we have to keep looping until there's
2099  * nothing left to do.
2100  */
2101  for (;;)
2102  {
2103  /*
2104  * Fire all currently pending deferred triggers.
2105  */
2107 
2108  /*
2109  * Close open portals (converting holdable ones into static portals).
2110  * If there weren't any, we are done ... otherwise loop back to check
2111  * if they queued deferred triggers. Lather, rinse, repeat.
2112  */
2113  if (!PreCommit_Portals(false))
2114  break;
2115  }
2116 
2119 
2120  /*
2121  * The remaining actions cannot call any user-defined code, so it's safe
2122  * to start shutting down within-transaction services. But note that most
2123  * of this stuff could still throw an error, which would switch us into
2124  * the transaction-abort path.
2125  */
2126 
2127  /* If we might have parallel workers, clean them up now. */
2128  if (IsInParallelMode())
2129  AtEOXact_Parallel(true);
2130 
2131  /* Shut down the deferred-trigger manager */
2132  AfterTriggerEndXact(true);
2133 
2134  /*
2135  * Let ON COMMIT management do its thing (must happen after closing
2136  * cursors, to avoid dangling-reference problems)
2137  */
2139 
2140  /*
2141  * Synchronize files that are created and not WAL-logged during this
2142  * transaction. This must happen before AtEOXact_RelationMap(), so that we
2143  * don't see committed-but-broken files after a crash.
2144  */
2145  smgrDoPendingSyncs(true, is_parallel_worker);
2146 
2147  /* close large objects before lower-level cleanup */
2148  AtEOXact_LargeObject(true);
2149 
2150  /*
2151  * Insert notifications sent by NOTIFY commands into the queue. This
2152  * should be late in the pre-commit sequence to minimize time spent
2153  * holding the notify-insertion lock. However, this could result in
2154  * creating a snapshot, so we must do it before serializable cleanup.
2155  */
2156  PreCommit_Notify();
2157 
2158  /*
2159  * Mark serializable transaction as complete for predicate locking
2160  * purposes. This should be done as late as we can put it and still allow
2161  * errors to be raised for failure patterns found at commit. This is not
2162  * appropriate in a parallel worker however, because we aren't committing
2163  * the leader's transaction and its serializable state will live on.
2164  */
2165  if (!is_parallel_worker)
2167 
2168  /* Prevent cancel/die interrupt while cleaning up */
2169  HOLD_INTERRUPTS();
2170 
2171  /* Commit updates to the relation map --- do this as late as possible */
2172  AtEOXact_RelationMap(true, is_parallel_worker);
2173 
2174  /*
2175  * set the current transaction state information appropriately during
2176  * commit processing
2177  */
2178  s->state = TRANS_COMMIT;
2179  s->parallelModeLevel = 0;
2180 
2181  if (!is_parallel_worker)
2182  {
2183  /*
2184  * We need to mark our XIDs as committed in pg_xact. This is where we
2185  * durably commit.
2186  */
2187  latestXid = RecordTransactionCommit();
2188  }
2189  else
2190  {
2191  /*
2192  * We must not mark our XID committed; the parallel leader is
2193  * responsible for that.
2194  */
2195  latestXid = InvalidTransactionId;
2196 
2197  /*
2198  * Make sure the leader will know about any WAL we wrote before it
2199  * commits.
2200  */
2202  }
2203 
2204  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2205 
2206  /*
2207  * Let others know about no transaction in progress by me. Note that this
2208  * must be done _before_ releasing locks we hold and _after_
2209  * RecordTransactionCommit.
2210  */
2211  ProcArrayEndTransaction(MyProc, latestXid);
2212 
2213  /*
2214  * This is all post-commit cleanup. Note that if an error is raised here,
2215  * it's too late to abort the transaction. This should be just
2216  * noncritical resource releasing.
2217  *
2218  * The ordering of operations is not entirely random. The idea is:
2219  * release resources visible to other backends (eg, files, buffer pins);
2220  * then release locks; then release backend-local resources. We want to
2221  * release locks at the point where any backend waiting for us will see
2222  * our transaction as being fully cleaned up.
2223  *
2224  * Resources that can be associated with individual queries are handled by
2225  * the ResourceOwner mechanism. The other calls here are for backend-wide
2226  * state.
2227  */
2228 
2229  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2230  : XACT_EVENT_COMMIT);
2231 
2234  true, true);
2235 
2236  /* Check we've released all buffer pins */
2237  AtEOXact_Buffers(true);
2238 
2239  /* Clean up the relation cache */
2240  AtEOXact_RelationCache(true);
2241 
2242  /*
2243  * Make catalog changes visible to all backends. This has to happen after
2244  * relcache references are dropped (see comments for
2245  * AtEOXact_RelationCache), but before locks are released (if anyone is
2246  * waiting for lock on a relation we've modified, we want them to know
2247  * about the catalog change before they start using the relation).
2248  */
2249  AtEOXact_Inval(true);
2250 
2252 
2255  true, true);
2258  true, true);
2259 
2260  /*
2261  * Likewise, dropping of files deleted during the transaction is best done
2262  * after releasing relcache and buffer pins. (This is not strictly
2263  * necessary during commit, since such pins should have been released
2264  * already, but this ordering is definitely critical during abort.) Since
2265  * this may take many seconds, also delay until after releasing locks.
2266  * Other backends will observe the attendant catalog changes and not
2267  * attempt to access affected files.
2268  */
2269  smgrDoPendingDeletes(true);
2270 
2271  AtCommit_Notify();
2272  AtEOXact_GUC(true, 1);
2273  AtEOXact_SPI(true);
2274  AtEOXact_Enum();
2276  AtEOXact_Namespace(true, is_parallel_worker);
2277  AtEOXact_SMgr();
2278  AtEOXact_Files(true);
2280  AtEOXact_HashTables(true);
2281  AtEOXact_PgStat(true, is_parallel_worker);
2282  AtEOXact_Snapshot(true, false);
2283  AtEOXact_ApplyLauncher(true);
2285 
2286  CurrentResourceOwner = NULL;
2288  s->curTransactionOwner = NULL;
2291 
2292  AtCommit_Memory();
2293 
2296  s->nestingLevel = 0;
2297  s->gucNestLevel = 0;
2298  s->childXids = NULL;
2299  s->nChildXids = 0;
2300  s->maxChildXids = 0;
2301 
2304 
2305  /*
2306  * done with commit processing, set current transaction state back to
2307  * default
2308  */
2309  s->state = TRANS_DEFAULT;
2310 
2312 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:832
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:595
static TransactionState CurrentTransactionState
Definition: xact.c:250
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:952
uint32 TransactionId
Definition: c.h:520
static void AtCommit_Memory(void)
Definition: xact.c:1495
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3334
XLogRecPtr XactLastRecEnd
Definition: xlog.c:360
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:3062
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:144
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2478
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:143
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3562
int nParallelCurrentXids
Definition: xact.c:122
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:15345
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:397
static TransactionId RecordTransactionCommit(void)
Definition: xact.c:1226
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2904
TBlockState blockState
Definition: xact.c:192
TransactionId * childXids
Definition: xact.c:197
void PreCommit_CheckForSerializationFailure(void)
Definition: predicate.c:4762
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
void PreCommit_Notify(void)
Definition: async.c:880
TransState state
Definition: xact.c:191
ResourceOwner curTransactionOwner
Definition: xact.c:196
void AtEOXact_SMgr(void)
Definition: smgr.c:661
bool IsInParallelMode(void)
Definition: xact.c:1012
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4652
struct TransactionStateData * parent
Definition: xact.c:208
static void ShowTransactionState(const char *str)
Definition: xact.c:5343
void AtEOXact_PgStat(bool isCommit, bool parallel)
Definition: pgstat.c:2143
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5956
#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:1493
void AtEOXact_ComboCid(void)
Definition: combocid.c:182
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4596
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:745
void AtEOXact_Enum(void)
Definition: pg_enum.c:609
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:478
static const char * TransStateAsString(TransState state)
Definition: xact.c:5447
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:364
void AtEOXact_MultiXact(void)
Definition: multixact.c:1664
void AtEOXact_RelationMap(bool isCommit, bool isParallelWorker)
Definition: relmapper.c:476
void EnterParallelMode(void)
Definition: xact.c:979
#define InvalidSubTransactionId
Definition: c.h:526
bool PreCommit_Portals(bool isPrepare)
Definition: portalmem.c:672
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:15245
#define InvalidFullTransactionId
Definition: transam.h:56
void AtCommit_Notify(void)
Definition: async.c:982
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
#define elog(elevel,...)
Definition: elog.h:214
void smgrDoPendingSyncs(bool isCommit, bool isParallelWorker)
Definition: storage.c:664
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1857
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:591
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:1222
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:4064
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091
LocalTransactionId lxid
Definition: proc.h:112

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2947 of file xact.c.

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

Referenced by _SPI_commit(), apply_handle_commit(), ApplyWorkerMain(), autoprewarm_database_main(), BeginInternalSubTransaction(), BootstrapModeMain(), cluster(), CommitTransactionCommand(), DefineIndex(), do_autovacuum(), exec_replication_command(), finish_xact_command(), get_database_list(), get_subscription_list(), IdentifySystem(), index_drop(), initialize_worker_spi(), InitPostgres(), LogicalRepSyncTableStart(), maybe_reread_subscription(), movedb(), ParallelWorkerMain(), pg_attribute_noreturn(), process_syncing_tables_for_apply(), ProcessCatchupInterrupt(), ProcessCompletedNotifies(), ProcessIncomingNotify(), ReindexMultipleTables(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), vacuum(), and vacuum_rel().

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

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4065 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5331 of file xact.c.

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

Referenced by ParallelWorkerMain().

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

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

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

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

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5208 of file xact.c.

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

Referenced by InitializeParallelDSM().

5209 {
5210  TransactionState s;
5211  Size nxids = 0;
5213 
5214  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5215  {
5217  nxids = add_size(nxids, 1);
5218  nxids = add_size(nxids, s->nChildXids);
5219  }
5220 
5221  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5222 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
uint32 TransactionId
Definition: c.h:520
#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:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
#define SerializedTransactionStateHeaderSize
Definition: xact.c:229
size_t Size
Definition: c.h:473

◆ 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:978
int parallelModeLevel
Definition: xact.c:205
#define Assert(condition)
Definition: c.h:745

◆ 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(), 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:745
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  )

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

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

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

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

Referenced by vacuum().

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

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 6077 of file xact.c.

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

Referenced by MarkSubTransactionAssigned(), and XLogRecordAssemble().

6078 {
6079  /* wal_level has to be logical */
6080  if (!XLogLogicalInfoActive())
6081  return false;
6082 
6083  /* we need to be in a transaction state */
6084  if (!IsTransactionState())
6085  return false;
6086 
6087  /* it has to be a subtransaction */
6088  if (!IsSubTransaction())
6089  return false;
6090 
6091  /* the subtransaction has to have a XID assigned */
6093  return false;
6094 
6095  /* and it should not be already 'assigned' */
6097 }
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:4757
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4684 of file xact.c.

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

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

4685 {
4687 
4689  return false;
4690 
4691  return true;
4692 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
Definition: