PostgreSQL Source Code  git master
xact.h File Reference
#include "access/transam.h"
#include "access/xlogreader.h"
#include "datatype/timestamp.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "storage/relfilenode.h"
#include "storage/sinval.h"
Include dependency graph for xact.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  xl_xact_assignment
 
struct  xl_xact_xinfo
 
struct  xl_xact_dbinfo
 
struct  xl_xact_subxacts
 
struct  xl_xact_relfilenodes
 
struct  xl_xact_invals
 
struct  xl_xact_twophase
 
struct  xl_xact_origin
 
struct  xl_xact_commit
 
struct  xl_xact_abort
 
struct  xl_xact_prepare
 
struct  xl_xact_parsed_commit
 
struct  xl_xact_parsed_abort
 

Macros

#define GIDSIZE   200
 
#define XACT_READ_UNCOMMITTED   0
 
#define XACT_READ_COMMITTED   1
 
#define XACT_REPEATABLE_READ   2
 
#define XACT_SERIALIZABLE   3
 
#define IsolationUsesXactSnapshot()   (XactIsoLevel >= XACT_REPEATABLE_READ)
 
#define IsolationIsSerializable()   (XactIsoLevel == XACT_SERIALIZABLE)
 
#define SYNCHRONOUS_COMMIT_ON   SYNCHRONOUS_COMMIT_REMOTE_FLUSH
 
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE   (1U << 0)
 
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK   (1U << 1)
 
#define XLOG_XACT_COMMIT   0x00
 
#define XLOG_XACT_PREPARE   0x10
 
#define XLOG_XACT_ABORT   0x20
 
#define XLOG_XACT_COMMIT_PREPARED   0x30
 
#define XLOG_XACT_ABORT_PREPARED   0x40
 
#define XLOG_XACT_ASSIGNMENT   0x50
 
#define XLOG_XACT_INVALIDATIONS   0x60
 
#define XLOG_XACT_OPMASK   0x70
 
#define XLOG_XACT_HAS_INFO   0x80
 
#define XACT_XINFO_HAS_DBINFO   (1U << 0)
 
#define XACT_XINFO_HAS_SUBXACTS   (1U << 1)
 
#define XACT_XINFO_HAS_RELFILENODES   (1U << 2)
 
#define XACT_XINFO_HAS_INVALS   (1U << 3)
 
#define XACT_XINFO_HAS_TWOPHASE   (1U << 4)
 
#define XACT_XINFO_HAS_ORIGIN   (1U << 5)
 
#define XACT_XINFO_HAS_AE_LOCKS   (1U << 6)
 
#define XACT_XINFO_HAS_GID   (1U << 7)
 
#define XACT_COMPLETION_APPLY_FEEDBACK   (1U << 29)
 
#define XACT_COMPLETION_UPDATE_RELCACHE_FILE   (1U << 30)
 
#define XACT_COMPLETION_FORCE_SYNC_COMMIT   (1U << 31)
 
#define XactCompletionApplyFeedback(xinfo)   ((xinfo & XACT_COMPLETION_APPLY_FEEDBACK) != 0)
 
#define XactCompletionRelcacheInitFileInval(xinfo)   ((xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE) != 0)
 
#define XactCompletionForceSyncCommit(xinfo)   ((xinfo & XACT_COMPLETION_FORCE_SYNC_COMMIT) != 0)
 
#define MinSizeOfXactAssignment   offsetof(xl_xact_assignment, xsub)
 
#define MinSizeOfXactSubxacts   offsetof(xl_xact_subxacts, subxacts)
 
#define MinSizeOfXactRelfilenodes   offsetof(xl_xact_relfilenodes, xnodes)
 
#define MinSizeOfXactInvals   offsetof(xl_xact_invals, msgs)
 
#define MinSizeOfXactCommit   (offsetof(xl_xact_commit, xact_time) + sizeof(TimestampTz))
 
#define MinSizeOfXactAbort   sizeof(xl_xact_abort)
 

Typedefs

typedef void(* XactCallback) (XactEvent event, void *arg)
 
typedef void(* SubXactCallback) (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
 
typedef struct xl_xact_assignment xl_xact_assignment
 
typedef struct xl_xact_xinfo xl_xact_xinfo
 
typedef struct xl_xact_dbinfo xl_xact_dbinfo
 
typedef struct xl_xact_subxacts xl_xact_subxacts
 
typedef struct xl_xact_relfilenodes xl_xact_relfilenodes
 
typedef struct xl_xact_invals xl_xact_invals
 
typedef struct xl_xact_twophase xl_xact_twophase
 
typedef struct xl_xact_origin xl_xact_origin
 
typedef struct xl_xact_commit xl_xact_commit
 
typedef struct xl_xact_abort xl_xact_abort
 
typedef struct xl_xact_prepare xl_xact_prepare
 
typedef struct xl_xact_parsed_commit xl_xact_parsed_commit
 
typedef xl_xact_parsed_commit xl_xact_parsed_prepare
 
typedef struct xl_xact_parsed_abort xl_xact_parsed_abort
 

Enumerations

enum  SyncCommitLevel {
  SYNCHRONOUS_COMMIT_OFF, SYNCHRONOUS_COMMIT_LOCAL_FLUSH, SYNCHRONOUS_COMMIT_REMOTE_WRITE, SYNCHRONOUS_COMMIT_REMOTE_FLUSH,
  SYNCHRONOUS_COMMIT_REMOTE_APPLY
}
 
enum  XactEvent {
  XACT_EVENT_COMMIT, XACT_EVENT_PARALLEL_COMMIT, XACT_EVENT_ABORT, XACT_EVENT_PARALLEL_ABORT,
  XACT_EVENT_PREPARE, XACT_EVENT_PRE_COMMIT, XACT_EVENT_PARALLEL_PRE_COMMIT, XACT_EVENT_PRE_PREPARE
}
 
enum  SubXactEvent { SUBXACT_EVENT_START_SUB, SUBXACT_EVENT_COMMIT_SUB, SUBXACT_EVENT_ABORT_SUB, SUBXACT_EVENT_PRE_COMMIT_SUB }
 

Functions

bool IsTransactionState (void)
 
bool IsAbortedTransactionBlockState (void)
 
TransactionId GetTopTransactionId (void)
 
TransactionId GetTopTransactionIdIfAny (void)
 
TransactionId GetCurrentTransactionId (void)
 
TransactionId GetCurrentTransactionIdIfAny (void)
 
TransactionId GetStableLatestTransactionId (void)
 
SubTransactionId GetCurrentSubTransactionId (void)
 
FullTransactionId GetTopFullTransactionId (void)
 
FullTransactionId GetTopFullTransactionIdIfAny (void)
 
FullTransactionId GetCurrentFullTransactionId (void)
 
FullTransactionId GetCurrentFullTransactionIdIfAny (void)
 
void MarkCurrentTransactionIdLoggedIfAny (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)
 
int GetCurrentTransactionNestLevel (void)
 
bool TransactionIdIsCurrentTransactionId (TransactionId xid)
 
void CommandCounterIncrement (void)
 
void ForceSyncCommit (void)
 
void StartTransactionCommand (void)
 
void SaveTransactionCharacteristics (void)
 
void RestoreTransactionCharacteristics (void)
 
void CommitTransactionCommand (void)
 
void AbortCurrentTransaction (void)
 
void BeginTransactionBlock (void)
 
bool EndTransactionBlock (bool chain)
 
bool PrepareTransactionBlock (const char *gid)
 
void UserAbortTransactionBlock (bool chain)
 
void BeginImplicitTransactionBlock (void)
 
void EndImplicitTransactionBlock (void)
 
void ReleaseSavepoint (const char *name)
 
void DefineSavepoint (const char *name)
 
void RollbackToSavepoint (const char *name)
 
void BeginInternalSubTransaction (const char *name)
 
void ReleaseCurrentSubTransaction (void)
 
void RollbackAndReleaseCurrentSubTransaction (void)
 
bool IsSubTransaction (void)
 
Size EstimateTransactionStateSpace (void)
 
void SerializeTransactionState (Size maxsize, char *start_address)
 
void StartParallelWorkerTransaction (char *tstatespace)
 
void EndParallelWorkerTransaction (void)
 
bool IsTransactionBlock (void)
 
bool IsTransactionOrTransactionBlock (void)
 
char TransactionBlockStatusCode (void)
 
void AbortOutOfAnyTransaction (void)
 
void PreventInTransactionBlock (bool isTopLevel, const char *stmtType)
 
void RequireTransactionBlock (bool isTopLevel, const char *stmtType)
 
void WarnNoTransactionBlock (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)
 
bool IsSubTransactionAssignmentPending (void)
 
void MarkSubTransactionAssigned (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)
 
void xact_redo (XLogReaderState *record)
 
void xact_desc (StringInfo buf, XLogReaderState *record)
 
const char * xact_identify (uint8 info)
 
void ParseCommitRecord (uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
 
void ParseAbortRecord (uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
 
void ParsePrepareRecord (uint8 info, xl_xact_prepare *xlrec, xl_xact_parsed_prepare *parsed)
 
void EnterParallelMode (void)
 
void ExitParallelMode (void)
 
bool IsInParallelMode (void)
 

Variables

int DefaultXactIsoLevel
 
PGDLLIMPORT int XactIsoLevel
 
bool DefaultXactReadOnly
 
bool XactReadOnly
 
bool xact_is_sampled
 
bool DefaultXactDeferrable
 
bool XactDeferrable
 
int synchronous_commit
 
PGDLLIMPORT TransactionId CheckXidAlive
 
PGDLLIMPORT bool bsysscan
 
int MyXactFlags
 

Macro Definition Documentation

◆ GIDSIZE

#define GIDSIZE   200

Definition at line 31 of file xact.h.

Referenced by MarkAsPreparing().

◆ IsolationIsSerializable

◆ IsolationUsesXactSnapshot

◆ MinSizeOfXactAbort

#define MinSizeOfXactAbort   sizeof(xl_xact_abort)

Definition at line 298 of file xact.h.

Referenced by ParseAbortRecord(), and XactLogAbortRecord().

◆ MinSizeOfXactAssignment

#define MinSizeOfXactAssignment   offsetof(xl_xact_assignment, xsub)

Definition at line 203 of file xact.h.

Referenced by AssignTransactionId().

◆ MinSizeOfXactCommit

#define MinSizeOfXactCommit   (offsetof(xl_xact_commit, xact_time) + sizeof(TimestampTz))

Definition at line 283 of file xact.h.

Referenced by ParseCommitRecord().

◆ MinSizeOfXactInvals

#define MinSizeOfXactInvals   offsetof(xl_xact_invals, msgs)

Definition at line 257 of file xact.h.

Referenced by LogLogicalInvalidations(), ParseCommitRecord(), and XactLogCommitRecord().

◆ MinSizeOfXactRelfilenodes

#define MinSizeOfXactRelfilenodes   offsetof(xl_xact_relfilenodes, xnodes)

◆ MinSizeOfXactSubxacts

#define MinSizeOfXactSubxacts   offsetof(xl_xact_subxacts, subxacts)

◆ SYNCHRONOUS_COMMIT_ON

#define SYNCHRONOUS_COMMIT_ON   SYNCHRONOUS_COMMIT_REMOTE_FLUSH

Definition at line 80 of file xact.h.

◆ XACT_COMPLETION_APPLY_FEEDBACK

#define XACT_COMPLETION_APPLY_FEEDBACK   (1U << 29)

Definition at line 184 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_FORCE_SYNC_COMMIT

#define XACT_COMPLETION_FORCE_SYNC_COMMIT   (1U << 31)

Definition at line 186 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_UPDATE_RELCACHE_FILE

#define XACT_COMPLETION_UPDATE_RELCACHE_FILE   (1U << 30)

Definition at line 185 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_FLAGS_ACCESSEDTEMPNAMESPACE

◆ XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK

#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK   (1U << 1)

◆ XACT_READ_COMMITTED

#define XACT_READ_COMMITTED   1

Definition at line 37 of file xact.h.

Referenced by InitPostgres().

◆ XACT_READ_UNCOMMITTED

#define XACT_READ_UNCOMMITTED   0

Definition at line 36 of file xact.h.

◆ XACT_REPEATABLE_READ

#define XACT_REPEATABLE_READ   2

Definition at line 38 of file xact.h.

Referenced by CreateReplicationSlot(), SnapBuildExportSnapshot(), and SnapBuildInitialSnapshot().

◆ XACT_SERIALIZABLE

#define XACT_SERIALIZABLE   3

Definition at line 39 of file xact.h.

Referenced by check_XactIsoLevel(), and ImportSnapshot().

◆ XACT_XINFO_HAS_AE_LOCKS

#define XACT_XINFO_HAS_AE_LOCKS   (1U << 6)

Definition at line 173 of file xact.h.

Referenced by xact_redo_abort(), xact_redo_commit(), XactLogAbortRecord(), and XactLogCommitRecord().

◆ XACT_XINFO_HAS_DBINFO

#define XACT_XINFO_HAS_DBINFO   (1U << 0)

◆ XACT_XINFO_HAS_GID

#define XACT_XINFO_HAS_GID   (1U << 7)

◆ XACT_XINFO_HAS_INVALS

#define XACT_XINFO_HAS_INVALS   (1U << 3)

Definition at line 170 of file xact.h.

Referenced by ParseCommitRecord(), and XactLogCommitRecord().

◆ XACT_XINFO_HAS_ORIGIN

#define XACT_XINFO_HAS_ORIGIN   (1U << 5)

◆ XACT_XINFO_HAS_RELFILENODES

#define XACT_XINFO_HAS_RELFILENODES   (1U << 2)

◆ XACT_XINFO_HAS_SUBXACTS

#define XACT_XINFO_HAS_SUBXACTS   (1U << 1)

◆ XACT_XINFO_HAS_TWOPHASE

#define XACT_XINFO_HAS_TWOPHASE   (1U << 4)

◆ XactCompletionApplyFeedback

#define XactCompletionApplyFeedback (   xinfo)    ((xinfo & XACT_COMPLETION_APPLY_FEEDBACK) != 0)

Definition at line 189 of file xact.h.

Referenced by xact_redo_commit().

◆ XactCompletionForceSyncCommit

#define XactCompletionForceSyncCommit (   xinfo)    ((xinfo & XACT_COMPLETION_FORCE_SYNC_COMMIT) != 0)

Definition at line 193 of file xact.h.

Referenced by xact_desc_commit(), and xact_redo_commit().

◆ XactCompletionRelcacheInitFileInval

#define XactCompletionRelcacheInitFileInval (   xinfo)    ((xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE) != 0)

Definition at line 191 of file xact.h.

Referenced by xact_desc_commit(), and xact_redo_commit().

◆ XLOG_XACT_ABORT

◆ XLOG_XACT_ABORT_PREPARED

#define XLOG_XACT_ABORT_PREPARED   0x40

◆ XLOG_XACT_ASSIGNMENT

#define XLOG_XACT_ASSIGNMENT   0x50

Definition at line 153 of file xact.h.

Referenced by AssignTransactionId(), DecodeXactOp(), xact_desc(), xact_identify(), and xact_redo().

◆ XLOG_XACT_COMMIT

◆ XLOG_XACT_COMMIT_PREPARED

◆ XLOG_XACT_HAS_INFO

#define XLOG_XACT_HAS_INFO   0x80

◆ XLOG_XACT_INVALIDATIONS

#define XLOG_XACT_INVALIDATIONS   0x60

Definition at line 154 of file xact.h.

Referenced by DecodeXactOp(), LogLogicalInvalidations(), xact_desc(), xact_identify(), and xact_redo().

◆ XLOG_XACT_OPMASK

◆ XLOG_XACT_PREPARE

#define XLOG_XACT_PREPARE   0x10

Typedef Documentation

◆ SubXactCallback

typedef void(* SubXactCallback) (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)

Definition at line 135 of file xact.h.

◆ XactCallback

typedef void(* XactCallback) (XactEvent event, void *arg)

Definition at line 125 of file xact.h.

◆ xl_xact_abort

typedef struct xl_xact_abort xl_xact_abort

◆ xl_xact_assignment

◆ xl_xact_commit

◆ xl_xact_dbinfo

◆ xl_xact_invals

◆ xl_xact_origin

◆ xl_xact_parsed_abort

◆ xl_xact_parsed_commit

◆ xl_xact_parsed_prepare

Definition at line 349 of file xact.h.

◆ xl_xact_prepare

◆ xl_xact_relfilenodes

◆ xl_xact_subxacts

◆ xl_xact_twophase

◆ xl_xact_xinfo

typedef struct xl_xact_xinfo xl_xact_xinfo

Enumeration Type Documentation

◆ SubXactEvent

Enumerator
SUBXACT_EVENT_START_SUB 
SUBXACT_EVENT_COMMIT_SUB 
SUBXACT_EVENT_ABORT_SUB 
SUBXACT_EVENT_PRE_COMMIT_SUB 

Definition at line 127 of file xact.h.

◆ SyncCommitLevel

Enumerator
SYNCHRONOUS_COMMIT_OFF 
SYNCHRONOUS_COMMIT_LOCAL_FLUSH 
SYNCHRONOUS_COMMIT_REMOTE_WRITE 
SYNCHRONOUS_COMMIT_REMOTE_FLUSH 
SYNCHRONOUS_COMMIT_REMOTE_APPLY 

Definition at line 68 of file xact.h.

69 {
70  SYNCHRONOUS_COMMIT_OFF, /* asynchronous commit */
71  SYNCHRONOUS_COMMIT_LOCAL_FLUSH, /* wait for local flush only */
72  SYNCHRONOUS_COMMIT_REMOTE_WRITE, /* wait for local flush and remote
73  * write */
74  SYNCHRONOUS_COMMIT_REMOTE_FLUSH, /* wait for local and remote flush */
75  SYNCHRONOUS_COMMIT_REMOTE_APPLY /* wait for local and remote flush
76  and remote apply */
SyncCommitLevel
Definition: xact.h:68

◆ XactEvent

enum XactEvent
Enumerator
XACT_EVENT_COMMIT 
XACT_EVENT_PARALLEL_COMMIT 
XACT_EVENT_ABORT 
XACT_EVENT_PARALLEL_ABORT 
XACT_EVENT_PREPARE 
XACT_EVENT_PRE_COMMIT 
XACT_EVENT_PARALLEL_PRE_COMMIT 
XACT_EVENT_PRE_PREPARE 

Definition at line 113 of file xact.h.

Function Documentation

◆ AbortCurrentTransaction()

void AbortCurrentTransaction ( void  )

Definition at line 3210 of file xact.c.

References AbortCurrentTransaction(), AbortSubTransaction(), AbortTransaction(), TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, TransactionStateData::state, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, TRANS_DEFAULT, TRANS_INPROGRESS, and TRANS_START.

Referenced by _SPI_rollback(), AbortCurrentTransaction(), AutoVacLauncherMain(), PostgresMain(), ReorderBufferImmediateInvalidation(), ReorderBufferProcessTXN(), and SnapBuildClearExportedSnapshot().

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

Definition at line 4579 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, AtAbort_Memory(), AtAbort_Portals(), AtCleanup_Memory(), AtSubAbort_Portals(), TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, TransactionStateData::curTransactionOwner, TransactionStateData::parent, TransactionStateData::state, TransactionStateData::subTransactionId, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, TRANS_DEFAULT, TRANS_INPROGRESS, and TRANS_START.

Referenced by do_autovacuum(), perform_work_item(), RemoveTempRelationsCallback(), and ShutdownPostgres().

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

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4039 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

Definition at line 4407 of file xact.c.

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

Referenced by exec_stmt_block(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_fetchrow(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), pltcl_returnnext(), pltcl_subtrans_begin(), pltcl_subtransaction(), PLy_spi_subtransaction_begin(), PLy_subtransaction_enter(), ReorderBufferImmediateInvalidation(), and ReorderBufferProcessTXN().

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

Definition at line 3637 of file xact.c.

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

Referenced by standard_ProcessUtility().

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

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1021 of file xact.c.

References AtCCI_LocalCache(), currentCommandId, currentCommandIdUsed, elog, ereport, errcode(), errmsg(), ERROR, InvalidCommandId, IsInParallelMode(), IsParallelWorker, and SnapshotSetCommandId().

Referenced by _SPI_execute_plan(), acquire_inherited_sample_rows(), addFkRecurseReferenced(), addFkRecurseReferencing(), AddRoleMems(), AlterPublicationOptions(), AlterRole(), apply_handle_delete(), apply_handle_insert(), apply_handle_truncate(), apply_handle_update(), ATAddCheckConstraint(), ATExecAddColumn(), ATExecAlterColumnType(), ATExecCmd(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropExpression(), ATExecDropIdentity(), ATExecSetCompression(), ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), ATParseTransformCmd(), ATRewriteTables(), AttachPartitionEnsureIndexes(), btadjustmembers(), CloneFkReferencing(), CommitSubTransaction(), CommitTransactionCommand(), copy_table_data(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyActionTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), CreateTrigger(), DefineCollation(), DefineDomain(), DefineQueryRewrite(), DefineRelation(), DefineVirtualRelation(), deleteOneObject(), DelRoleMems(), DetachPartitionFinalize(), DropClonedTriggersFromPartition(), DropRole(), EventTriggerDDLCommandEnd(), EventTriggerDDLCommandStart(), EventTriggerInvoke(), EventTriggerSQLDrop(), EventTriggerTableRewrite(), exec_eval_simple_expr(), exec_execute_message(), exec_parse_message(), exec_simple_query(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), execute_sql_string(), ExplainOnePlan(), fmgr_sql(), hashadjustmembers(), ImportForeignSchema(), index_build(), index_concurrently_swap(), index_create(), IndexSetParentIndex(), InitTempTableNamespace(), inv_create(), inv_drop(), inv_truncate(), inv_write(), LogicalRepSyncTableStart(), make_new_heap(), makeConfigurationDependencies(), moveArrayTypeName(), OperatorShellMake(), OperatorUpd(), pg_import_system_collations(), PortalRunMulti(), ProcedureCreate(), ProcessUtilitySlow(), recordExtensionInitPrivWorker(), reindex_index(), reindex_relation(), ReindexRelationConcurrently(), RelationSetNewRelfilenode(), RenumberEnumType(), replorigin_create(), replorigin_drop_guts(), ri_PerformCheck(), SetDefaultACL(), SetMatViewPopulatedState(), shdepReassignOwned(), SPI_cursor_open_internal(), standard_ProcessUtility(), StoreConstraints(), StorePartitionBound(), tryAttachPartitionForeignKey(), vacuum(), and validatePartitionedIndex().

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

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2939 of file xact.c.

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

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

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

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

Definition at line 4086 of file xact.c.

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

Referenced by CommitTransactionCommand(), and standard_ProcessUtility().

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4064 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5328 of file xact.c.

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

Referenced by ParallelWorkerMain().

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

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

Definition at line 3757 of file xact.c.

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

Referenced by PrepareTransactionBlock(), and standard_ProcessUtility().

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

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 979 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

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

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5205 of file xact.c.

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

Referenced by InitializeParallelDSM().

5206 {
5207  TransactionState s;
5208  Size nxids = 0;
5210 
5211  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5212  {
5214  nxids = add_size(nxids, 1);
5215  nxids = add_size(nxids, s->nChildXids);
5216  }
5217 
5218  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5219 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
uint32 TransactionId
Definition: c.h:587
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
struct TransactionStateData * parent
Definition: xact.c:208
FullTransactionId fullTransactionId
Definition: xact.c:187
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
#define SerializedTransactionStateHeaderSize
Definition: xact.c:229
size_t Size
Definition: c.h:540

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 992 of file xact.c.

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

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

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

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1071 of file xact.c.

References forceSyncCommit.

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

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

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 761 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

Referenced by ATRewriteTable(), CatalogTuplesMultiInsertWithInfo(), CopyFrom(), create_estate_for_relation(), GetSnapshotData(), GetSnapshotDataReuse(), intorel_startup(), pgrowlocks(), RegisterRelcacheInvalidation(), RelationFindReplTupleByIndex(), RelationFindReplTupleSeq(), simple_heap_delete(), simple_heap_insert(), simple_heap_update(), simple_table_tuple_delete(), simple_table_tuple_insert(), simple_table_tuple_update(), standard_ExecutorStart(), toast_save_datum(), transientrel_startup(), and UpdateActiveSnapshotCommandId().

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

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 496 of file xact.c.

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

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

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 514 of file xact.c.

References TransactionStateData::fullTransactionId.

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

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 807 of file xact.c.

References stmtStartTimestamp.

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

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

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 455 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

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

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

Definition at line 798 of file xact.c.

References xactStartTimestamp.

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

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

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 819 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

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

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 541 of file xact.c.

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

Referenced by xid_age().

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

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 467 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

Referenced by pg_current_xact_id().

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

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 483 of file xact.c.

References XactTopFullTransactionId.

Referenced by pg_current_xact_id_if_assigned().

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

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

Definition at line 410 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XidFromFullTransactionId.

Referenced by AlterSequence(), AssignTransactionId(), do_setval(), fill_seq_with_data(), log_heap_new_cid(), and nextval_internal().

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

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3492 of file xact.c.

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

Referenced by vacuum().

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

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 6087 of file xact.c.

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

Referenced by MarkSubTransactionAssigned(), and XLogRecordAssemble().

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

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4683 of file xact.c.

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

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

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

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 371 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

Referenced by apply_handle_commit_internal(), apply_handle_origin(), apply_handle_stream_stop(), check_client_encoding(), check_default_table_access_method(), check_default_tablespace(), check_role(), check_session_authorization(), check_temp_tablespaces(), check_transaction_read_only(), check_TSCurrentConfig(), check_XactIsoLevel(), CreateInitDecodingContext(), ensure_transaction(), InitializeClientEncoding(), IsSubTransactionAssignmentPending(), LogLogicalMessage(), maybe_reread_subscription(), pg_attribute_noreturn(), pg_do_encoding_conversion(), PrepareClientEncoding(), PrepareTempTablespaces(), process_syncing_tables_for_apply(), process_syncing_tables_for_sync(), RelationClearRelation(), RelationIdGetRelation(), RelationInitPhysicalAddr(), RelationReloadNailed(), replorigin_create(), replorigin_drop_by_name(), report_untranslatable_char(), SearchCatCacheInternal(), SetMultiXactIdLimit(), SetTransactionIdLimit(), SnapBuildClearExportedSnapshot(), and SocketBackend().

372 {
374 
375  /*
376  * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
377  * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
378  * TRANS_PREPARE since it might be too soon or too late within those
379  * transition states to do anything interesting. Hence, the only "valid"
380  * state is TRANS_INPROGRESS.
381  */
382  return (s->state == TRANS_INPROGRESS);
383 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TransState state
Definition: xact.c:191

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 525 of file xact.c.

References TransactionStateData::didLogXid, TransactionStateData::fullTransactionId, and FullTransactionIdIsValid.

Referenced by XLogInsertRecord().

526 {
529 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
FullTransactionId fullTransactionId
Definition: xact.c:187

◆ MarkSubTransactionAssigned()

void MarkSubTransactionAssigned ( void  )

Definition at line 6115 of file xact.c.

References Assert, TransactionStateData::assigned, and IsSubTransactionAssignmentPending().

Referenced by XLogResetInsertion().

6116 {
6118 
6120 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
bool IsSubTransactionAssignmentPending(void)
Definition: xact.c:6087
#define Assert(condition)
Definition: c.h:804

◆ ParseAbortRecord()

void ParseAbortRecord ( uint8  info,
xl_xact_abort xlrec,
xl_xact_parsed_abort parsed 
)

Definition at line 129 of file xactdesc.c.

References xl_xact_dbinfo::dbId, xl_xact_parsed_abort::dbId, MinSizeOfXactAbort, MinSizeOfXactRelfilenodes, MinSizeOfXactSubxacts, xl_xact_relfilenodes::nrels, xl_xact_parsed_abort::nrels, xl_xact_subxacts::nsubxacts, xl_xact_parsed_abort::nsubxacts, xl_xact_origin::origin_lsn, xl_xact_parsed_abort::origin_lsn, xl_xact_origin::origin_timestamp, xl_xact_parsed_abort::origin_timestamp, strlcpy(), xl_xact_subxacts::subxacts, xl_xact_parsed_abort::subxacts, xl_xact_dbinfo::tsId, xl_xact_parsed_abort::tsId, xl_xact_parsed_abort::twophase_gid, xl_xact_parsed_abort::twophase_xid, xl_xact_abort::xact_time, xl_xact_parsed_abort::xact_time, XACT_XINFO_HAS_DBINFO, XACT_XINFO_HAS_GID, XACT_XINFO_HAS_ORIGIN, XACT_XINFO_HAS_RELFILENODES, XACT_XINFO_HAS_SUBXACTS, XACT_XINFO_HAS_TWOPHASE, xl_xact_twophase::xid, xl_xact_xinfo::xinfo, xl_xact_parsed_abort::xinfo, XLOG_XACT_HAS_INFO, xl_xact_relfilenodes::xnodes, and xl_xact_parsed_abort::xnodes.

Referenced by DecodeXactOp(), recoveryStopsAfter(), recoveryStopsBefore(), xact_desc_abort(), and xact_redo().

130 {
131  char *data = ((char *) xlrec) + MinSizeOfXactAbort;
132 
133  memset(parsed, 0, sizeof(*parsed));
134 
135  parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
136  * present */
137 
138  parsed->xact_time = xlrec->xact_time;
139 
140  if (info & XLOG_XACT_HAS_INFO)
141  {
142  xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
143 
144  parsed->xinfo = xl_xinfo->xinfo;
145 
146  data += sizeof(xl_xact_xinfo);
147  }
148 
149  if (parsed->xinfo & XACT_XINFO_HAS_DBINFO)
150  {
151  xl_xact_dbinfo *xl_dbinfo = (xl_xact_dbinfo *) data;
152 
153  parsed->dbId = xl_dbinfo->dbId;
154  parsed->tsId = xl_dbinfo->tsId;
155 
156  data += sizeof(xl_xact_dbinfo);
157  }
158 
159  if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
160  {
161  xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
162 
163  parsed->nsubxacts = xl_subxacts->nsubxacts;
164  parsed->subxacts = xl_subxacts->subxacts;
165 
166  data += MinSizeOfXactSubxacts;
167  data += parsed->nsubxacts * sizeof(TransactionId);
168  }
169 
170  if (parsed->xinfo & XACT_XINFO_HAS_RELFILENODES)
171  {
172  xl_xact_relfilenodes *xl_relfilenodes = (xl_xact_relfilenodes *) data;
173 
174  parsed->nrels = xl_relfilenodes->nrels;
175  parsed->xnodes = xl_relfilenodes->xnodes;
176 
178  data += xl_relfilenodes->nrels * sizeof(RelFileNode);
179  }
180 
181  if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
182  {
183  xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
184 
185  parsed->twophase_xid = xl_twophase->xid;
186 
187  data += sizeof(xl_xact_twophase);
188 
189  if (parsed->xinfo & XACT_XINFO_HAS_GID)
190  {
191  strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid));
192  data += strlen(data) + 1;
193  }
194  }
195 
196  /* Note: no alignment is guaranteed after this point */
197 
198  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
199  {
200  xl_xact_origin xl_origin;
201 
202  /* no alignment is guaranteed, so copy onto stack */
203  memcpy(&xl_origin, data, sizeof(xl_origin));
204 
205  parsed->origin_lsn = xl_origin.origin_lsn;
206  parsed->origin_timestamp = xl_origin.origin_timestamp;
207 
208  data += sizeof(xl_xact_origin);
209  }
210 }
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:172
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:587
TransactionId xid
Definition: xact.h:261
#define MinSizeOfXactAbort
Definition: xact.h:298
#define XLOG_XACT_HAS_INFO
Definition: xact.h:161
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:168
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:250
TimestampTz xact_time
Definition: xact.h:287
Oid tsId
Definition: xact.h:235
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:248
#define MinSizeOfXactSubxacts
Definition: xact.h:243
struct RelFileNode RelFileNode
struct xl_xact_twophase xl_xact_twophase
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:167
XLogRecPtr origin_lsn
Definition: xact.h:266
TransactionId * subxacts
Definition: xact.h:360
uint32 xinfo
Definition: xact.h:229
XLogRecPtr origin_lsn
Definition: xact.h:368
RelFileNode * xnodes
Definition: xact.h:363
int nsubxacts
Definition: xact.h:240
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:171
Oid dbId
Definition: xact.h:234
TransactionId twophase_xid
Definition: xact.h:365
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz xact_time
Definition: xact.h:353
TimestampTz origin_timestamp
Definition: xact.h:267
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:169
TimestampTz origin_timestamp
Definition: xact.h:369
#define XACT_XINFO_HAS_GID
Definition: xact.h:174
char twophase_gid[GIDSIZE]
Definition: xact.h:366
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:241

◆ ParseCommitRecord()

void ParseCommitRecord ( uint8  info,
xl_xact_commit xlrec,
xl_xact_parsed_commit parsed 
)

Definition at line 34 of file xactdesc.c.

References xl_xact_dbinfo::dbId, xl_xact_parsed_commit::dbId, MinSizeOfXactCommit, MinSizeOfXactInvals, MinSizeOfXactRelfilenodes, MinSizeOfXactSubxacts, xl_xact_invals::msgs, xl_xact_parsed_commit::msgs, xl_xact_invals::nmsgs, xl_xact_parsed_commit::nmsgs, xl_xact_relfilenodes::nrels, xl_xact_parsed_commit::nrels, xl_xact_subxacts::nsubxacts, xl_xact_parsed_commit::nsubxacts, xl_xact_origin::origin_lsn, xl_xact_parsed_commit::origin_lsn, xl_xact_origin::origin_timestamp, xl_xact_parsed_commit::origin_timestamp, strlcpy(), xl_xact_subxacts::subxacts, xl_xact_parsed_commit::subxacts, xl_xact_dbinfo::tsId, xl_xact_parsed_commit::tsId, xl_xact_parsed_commit::twophase_gid, xl_xact_parsed_commit::twophase_xid, xl_xact_commit::xact_time, xl_xact_parsed_commit::xact_time, XACT_XINFO_HAS_DBINFO, XACT_XINFO_HAS_GID, XACT_XINFO_HAS_INVALS, XACT_XINFO_HAS_ORIGIN, XACT_XINFO_HAS_RELFILENODES, XACT_XINFO_HAS_SUBXACTS, XACT_XINFO_HAS_TWOPHASE, xl_xact_twophase::xid, xl_xact_xinfo::xinfo, xl_xact_parsed_commit::xinfo, XLOG_XACT_HAS_INFO, xl_xact_relfilenodes::xnodes, and xl_xact_parsed_commit::xnodes.

Referenced by DecodeXactOp(), recoveryStopsAfter(), recoveryStopsBefore(), xact_desc_commit(), and xact_redo().

35 {
36  char *data = ((char *) xlrec) + MinSizeOfXactCommit;
37 
38  memset(parsed, 0, sizeof(*parsed));
39 
40  parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
41  * present */
42 
43  parsed->xact_time = xlrec->xact_time;
44 
45  if (info & XLOG_XACT_HAS_INFO)
46  {
47  xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
48 
49  parsed->xinfo = xl_xinfo->xinfo;
50 
51  data += sizeof(xl_xact_xinfo);
52  }
53 
54  if (parsed->xinfo & XACT_XINFO_HAS_DBINFO)
55  {
56  xl_xact_dbinfo *xl_dbinfo = (xl_xact_dbinfo *) data;
57 
58  parsed->dbId = xl_dbinfo->dbId;
59  parsed->tsId = xl_dbinfo->tsId;
60 
61  data += sizeof(xl_xact_dbinfo);
62  }
63 
64  if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
65  {
66  xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
67 
68  parsed->nsubxacts = xl_subxacts->nsubxacts;
69  parsed->subxacts = xl_subxacts->subxacts;
70 
71  data += MinSizeOfXactSubxacts;
72  data += parsed->nsubxacts * sizeof(TransactionId);
73  }
74 
75  if (parsed->xinfo & XACT_XINFO_HAS_RELFILENODES)
76  {
77  xl_xact_relfilenodes *xl_relfilenodes = (xl_xact_relfilenodes *) data;
78 
79  parsed->nrels = xl_relfilenodes->nrels;
80  parsed->xnodes = xl_relfilenodes->xnodes;
81 
83  data += xl_relfilenodes->nrels * sizeof(RelFileNode);
84  }
85 
86  if (parsed->xinfo & XACT_XINFO_HAS_INVALS)
87  {
88  xl_xact_invals *xl_invals = (xl_xact_invals *) data;
89 
90  parsed->nmsgs = xl_invals->nmsgs;
91  parsed->msgs = xl_invals->msgs;
92 
93  data += MinSizeOfXactInvals;
94  data += xl_invals->nmsgs * sizeof(SharedInvalidationMessage);
95  }
96 
97  if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
98  {
99  xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
100 
101  parsed->twophase_xid = xl_twophase->xid;
102 
103  data += sizeof(xl_xact_twophase);
104 
105  if (parsed->xinfo & XACT_XINFO_HAS_GID)
106  {
107  strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid));
108  data += strlen(data) + 1;
109  }
110  }
111 
112  /* Note: no alignment is guaranteed after this point */
113 
114  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
115  {
116  xl_xact_origin xl_origin;
117 
118  /* no alignment is guaranteed, so copy onto stack */
119  memcpy(&xl_origin, data, sizeof(xl_origin));
120 
121  parsed->origin_lsn = xl_origin.origin_lsn;
122  parsed->origin_timestamp = xl_origin.origin_timestamp;
123 
124  data += sizeof(xl_xact_origin);
125  }
126 }
RelFileNode * xnodes
Definition: xact.h:335
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:172
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:587
TimestampTz origin_timestamp
Definition: xact.h:346
TransactionId xid
Definition: xact.h:261
TransactionId twophase_xid
Definition: xact.h:340
TransactionId * subxacts
Definition: xact.h:332
#define XLOG_XACT_HAS_INFO
Definition: xact.h:161
SharedInvalidationMessage * msgs
Definition: xact.h:338
XLogRecPtr origin_lsn
Definition: xact.h:345
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:168
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:250
Oid tsId
Definition: xact.h:235
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:248
#define MinSizeOfXactSubxacts
Definition: xact.h:243
#define MinSizeOfXactCommit
Definition: xact.h:283
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:255
struct RelFileNode RelFileNode
TimestampTz xact_time
Definition: xact.h:272
struct xl_xact_twophase xl_xact_twophase
#define MinSizeOfXactInvals
Definition: xact.h:257
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:167
XLogRecPtr origin_lsn
Definition: xact.h:266
uint32 xinfo
Definition: xact.h:229
int nsubxacts
Definition: xact.h:240
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:171
Oid dbId
Definition: xact.h:234
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz origin_timestamp
Definition: xact.h:267
#define XACT_XINFO_HAS_INVALS
Definition: xact.h:170
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:169
int nmsgs
Definition: xact.h:254
#define XACT_XINFO_HAS_GID
Definition: xact.h:174
char twophase_gid[GIDSIZE]
Definition: xact.h:341
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:241
TimestampTz xact_time
Definition: xact.h:325

◆ ParsePrepareRecord()

void ParsePrepareRecord ( uint8  info,
xl_xact_prepare xlrec,
xl_xact_parsed_prepare parsed 
)

Definition at line 216 of file xactdesc.c.

References xl_xact_parsed_commit::abortnodes, xl_xact_prepare::database, xl_xact_parsed_commit::dbId, xl_xact_prepare::gidlen, MAXALIGN, xl_xact_parsed_commit::msgs, xl_xact_prepare::nabortrels, xl_xact_parsed_commit::nabortrels, xl_xact_prepare::ncommitrels, xl_xact_prepare::ninvalmsgs, xl_xact_parsed_commit::nmsgs, xl_xact_parsed_commit::nrels, xl_xact_prepare::nsubxacts, xl_xact_parsed_commit::nsubxacts, xl_xact_prepare::origin_lsn, xl_xact_parsed_commit::origin_lsn, xl_xact_prepare::origin_timestamp, xl_xact_parsed_commit::origin_timestamp, xl_xact_prepare::prepared_at, xl_xact_parsed_commit::subxacts, xl_xact_parsed_commit::twophase_gid, xl_xact_parsed_commit::twophase_xid, xl_xact_parsed_commit::xact_time, xl_xact_prepare::xid, and xl_xact_parsed_commit::xnodes.

Referenced by DecodeXactOp(), and xact_desc_prepare().

217 {
218  char *bufptr;
219 
220  bufptr = ((char *) xlrec) + MAXALIGN(sizeof(xl_xact_prepare));
221 
222  memset(parsed, 0, sizeof(*parsed));
223 
224  parsed->xact_time = xlrec->prepared_at;
225  parsed->origin_lsn = xlrec->origin_lsn;
226  parsed->origin_timestamp = xlrec->origin_timestamp;
227  parsed->twophase_xid = xlrec->xid;
228  parsed->dbId = xlrec->database;
229  parsed->nsubxacts = xlrec->nsubxacts;
230  parsed->nrels = xlrec->ncommitrels;
231  parsed->nabortrels = xlrec->nabortrels;
232  parsed->nmsgs = xlrec->ninvalmsgs;
233 
234  strncpy(parsed->twophase_gid, bufptr, xlrec->gidlen);
235  bufptr += MAXALIGN(xlrec->gidlen);
236 
237  parsed->subxacts = (TransactionId *) bufptr;
238  bufptr += MAXALIGN(xlrec->nsubxacts * sizeof(TransactionId));
239 
240  parsed->xnodes = (RelFileNode *) bufptr;
241  bufptr += MAXALIGN(xlrec->ncommitrels * sizeof(RelFileNode));
242 
243  parsed->abortnodes = (RelFileNode *) bufptr;
244  bufptr += MAXALIGN(xlrec->nabortrels * sizeof(RelFileNode));
245 
246  parsed->msgs = (SharedInvalidationMessage *) bufptr;
247  bufptr += MAXALIGN(xlrec->ninvalmsgs * sizeof(SharedInvalidationMessage));
248 }
RelFileNode * xnodes
Definition: xact.h:335
int32 ninvalmsgs
Definition: xact.h:311
int32 nsubxacts
Definition: xact.h:308
uint32 TransactionId
Definition: c.h:587
int32 ncommitrels
Definition: xact.h:309
TimestampTz origin_timestamp
Definition: xact.h:346
TransactionId twophase_xid
Definition: xact.h:340
TransactionId * subxacts
Definition: xact.h:332
SharedInvalidationMessage * msgs
Definition: xact.h:338
XLogRecPtr origin_lsn
Definition: xact.h:345
uint16 gidlen
Definition: xact.h:313
Oid database
Definition: xact.h:305
int32 nabortrels
Definition: xact.h:310
XLogRecPtr origin_lsn
Definition: xact.h:314
RelFileNode * abortnodes
Definition: xact.h:343
TimestampTz origin_timestamp
Definition: xact.h:315
#define MAXALIGN(LEN)
Definition: c.h:757
TransactionId xid
Definition: xact.h:304
TimestampTz prepared_at
Definition: xact.h:306
char twophase_gid[GIDSIZE]
Definition: xact.h:341
TimestampTz xact_time
Definition: xact.h:325

◆ PrepareTransactionBlock()

bool PrepareTransactionBlock ( const char *  gid)

Definition at line 3705 of file xact.c.

References Assert, TransactionStateData::blockState, CurrentTransactionState, EndTransactionBlock(), MemoryContextStrdup(), TransactionStateData::parent, prepareGID, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, and TopTransactionContext.

Referenced by standard_ProcessUtility().

3706 {
3707  TransactionState s;
3708  bool result;
3709 
3710  /* Set up to commit the current transaction */
3711  result = EndTransactionBlock(false);
3712 
3713  /* If successful, change outer tblock state to PREPARE */
3714  if (result)
3715  {
3717 
3718  while (s->parent != NULL)
3719  s = s->parent;
3720 
3721  if (s->blockState == TBLOCK_END)
3722  {
3723  /* Save GID where PrepareTransaction can find it again */
3725 
3727  }
3728  else
3729  {
3730  /*
3731  * ignore case where we are not in a transaction;
3732  * EndTransactionBlock already issued a warning.
3733  */
3736  /* Don't send back a PREPARE result tag... */
3737  result = false;
3738  }
3739  }
3740 
3741  return result;
3742 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
bool EndTransactionBlock(bool chain)
Definition: xact.c:3757
MemoryContext TopTransactionContext
Definition: mcxt.c:53
TBlockState blockState
Definition: xact.c:192
struct TransactionStateData * parent
Definition: xact.c:208
#define Assert(condition)
Definition: c.h:804
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1286
static char * prepareGID
Definition: xact.c:275

◆ PreventInTransactionBlock()

void PreventInTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3379 of file xact.c.

References TransactionStateData::blockState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsSubTransaction(), IsTransactionBlock(), TBLOCK_DEFAULT, and TBLOCK_STARTED.

Referenced by AlterDatabase(), AlterSubscription(), cluster(), CreateSubscription(), DiscardAll(), DropSubscription(), exec_replication_command(), ExecDropStmt(), ExecReindex(), ProcessUtilitySlow(), ReindexPartitions(), standard_ProcessUtility(), and vacuum().

3380 {
3381  /*
3382  * xact block already started?
3383  */
3384  if (IsTransactionBlock())
3385  ereport(ERROR,
3386  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3387  /* translator: %s represents an SQL statement name */
3388  errmsg("%s cannot run inside a transaction block",
3389  stmtType)));
3390 
3391  /*
3392  * subtransaction?
3393  */
3394  if (IsSubTransaction())
3395  ereport(ERROR,
3396  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3397  /* translator: %s represents an SQL statement name */
3398  errmsg("%s cannot run inside a subtransaction",
3399  stmtType)));
3400 
3401  /*
3402  * inside a function call?
3403  */
3404  if (!isTopLevel)
3405  ereport(ERROR,
3406  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3407  /* translator: %s represents an SQL statement name */
3408  errmsg("%s cannot be executed from a function", stmtType)));
3409 
3410  /* If we got past IsTransactionBlock test, should be in default state */
3413  elog(FATAL, "cannot prevent transaction chain");
3414  /* all okay */
3415 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
bool IsTransactionBlock(void)
Definition: xact.c:4683
#define ERROR
Definition: elog.h:46
#define FATAL
Definition: elog.h:49
#define ereport(elevel,...)
Definition: elog.h:157
bool IsSubTransaction(void)
Definition: xact.c:4756
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ RegisterSubXactCallback()

void RegisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3582 of file xact.c.

References arg, SubXactCallbackItem::arg, callback(), SubXactCallbackItem::callback, MemoryContextAlloc(), SubXactCallbackItem::next, SubXact_callbacks, and TopMemoryContext.

Referenced by _PG_init(), GetConnection(), and sepgsql_init_client_label().

3583 {
3584  SubXactCallbackItem *item;
3585 
3586  item = (SubXactCallbackItem *)
3588  item->callback = callback;
3589  item->arg = arg;
3590  item->next = SubXact_callbacks;
3591  SubXact_callbacks = item;
3592 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:314
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:309
SubXactCallback callback
Definition: xact.c:310
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
void * arg

◆ RegisterXactCallback()

void RegisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3527 of file xact.c.

References arg, XactCallbackItem::arg, callback(), XactCallbackItem::callback, MemoryContextAlloc(), XactCallbackItem::next, TopMemoryContext, and Xact_callbacks.

Referenced by _PG_init(), GetConnection(), and sepgsql_init_client_label().

3528 {
3529  XactCallbackItem *item;
3530 
3531  item = (XactCallbackItem *)
3533  item->callback = callback;
3534  item->arg = arg;
3535  item->next = Xact_callbacks;
3536  Xact_callbacks = item;
3537 }
struct XactCallbackItem * next
Definition: xact.c:297
void * arg
Definition: xact.c:299
XactCallback callback
Definition: xact.c:298
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
MemoryContext TopMemoryContext
Definition: mcxt.c:48
static XactCallbackItem * Xact_callbacks
Definition: xact.c:302
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
void * arg

◆ ReleaseCurrentSubTransaction()

void ReleaseCurrentSubTransaction ( void  )

Definition at line 4478 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), CommitSubTransaction(), CurrentTransactionState, CurTransactionContext, elog, ereport, errcode(), errmsg(), ERROR, IsInParallelMode(), MemoryContextSwitchTo(), TransactionStateData::state, TBLOCK_SUBINPROGRESS, and TRANS_INPROGRESS.

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_subtrans_commit(), pltcl_subtransaction(), PLy_spi_subtransaction_commit(), and PLy_subtransaction_exit().

4479 {
4481 
4482  /*
4483  * Workers synchronize transaction state at the beginning of each parallel
4484  * operation, so we can't account for commit of subtransactions after that
4485  * point. This should not happen anyway. Code calling this would
4486  * typically have called BeginInternalSubTransaction() first, failing
4487  * there.
4488  */
4489  if (IsInParallelMode())
4490  ereport(ERROR,
4491  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4492  errmsg("cannot commit subtransactions during a parallel operation")));
4493 
4494  if (s->blockState != TBLOCK_SUBINPROGRESS)
4495  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4497  Assert(s->state == TRANS_INPROGRESS);
4500  s = CurrentTransactionState; /* changed by pop */
4501  Assert(s->state == TRANS_INPROGRESS);
4502 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
TransState state
Definition: xact.c:191
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:46
static void CommitSubTransaction(void)
Definition: xact.c:4816
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5390
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ ReleaseSavepoint()

void ReleaseSavepoint ( const char *  name)

Definition at line 4171 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), TransactionStateData::name, TransactionStateData::parent, PointerIsValid, TransactionStateData::savepointLevel, 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 standard_ProcessUtility().

4172 {
4174  TransactionState target,
4175  xact;
4176 
4177  /*
4178  * Workers synchronize transaction state at the beginning of each parallel
4179  * operation, so we can't account for transaction state change after that
4180  * point. (Note that this check will certainly error out if s->blockState
4181  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4182  * below.)
4183  */
4184  if (IsInParallelMode())
4185  ereport(ERROR,
4186  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4187  errmsg("cannot release savepoints during a parallel operation")));
4188 
4189  switch (s->blockState)
4190  {
4191  /*
4192  * We can't release a savepoint if there is no savepoint defined.
4193  */
4194  case TBLOCK_INPROGRESS:
4195  ereport(ERROR,
4196  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4197  errmsg("savepoint \"%s\" does not exist", name)));
4198  break;
4199 
4201  /* See comment about implicit transactions in DefineSavepoint */
4202  ereport(ERROR,
4203  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4204  /* translator: %s represents an SQL statement name */
4205  errmsg("%s can only be used in transaction blocks",
4206  "RELEASE SAVEPOINT")));
4207  break;
4208 
4209  /*
4210  * We are in a non-aborted subtransaction. This is the only valid
4211  * case.
4212  */
4213  case TBLOCK_SUBINPROGRESS:
4214  break;
4215 
4216  /* These cases are invalid. */
4217  case TBLOCK_DEFAULT:
4218  case TBLOCK_STARTED:
4219  case TBLOCK_BEGIN:
4221  case TBLOCK_SUBBEGIN:
4222  case TBLOCK_END:
4223  case TBLOCK_SUBRELEASE:
4224  case TBLOCK_SUBCOMMIT:
4225  case TBLOCK_ABORT:
4226  case TBLOCK_SUBABORT:
4227  case TBLOCK_ABORT_END:
4228  case TBLOCK_SUBABORT_END:
4229  case TBLOCK_ABORT_PENDING:
4231  case TBLOCK_SUBRESTART:
4233  case TBLOCK_PREPARE:
4234  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
4236  break;
4237  }
4238 
4239  for (target = s; PointerIsValid(target); target = target->parent)
4240  {
4241  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4242  break;
4243  }
4244 
4245  if (!PointerIsValid(target))
4246  ereport(ERROR,
4247  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4248  errmsg("savepoint \"%s\" does not exist", name)));
4249 
4250  /* disallow crossing savepoint level boundaries */
4251  if (target->savepointLevel != s->savepointLevel)
4252  ereport(ERROR,
4253  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4254  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4255 
4256  /*
4257  * Mark "commit pending" all subtransactions up to the target
4258  * subtransaction. The actual commits will happen when control gets to
4259  * CommitTransactionCommand.
4260  */
4261  xact = CurrentTransactionState;
4262  for (;;)
4263  {
4265  xact->blockState = TBLOCK_SUBRELEASE;
4266  if (xact == target)
4267  break;
4268  xact = xact->parent;
4269  Assert(PointerIsValid(xact));
4270  }
4271 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:46
#define FATAL
Definition: elog.h:49
struct TransactionStateData * parent
Definition: xact.c:208
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5390
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
const char * name
Definition: encode.c:515
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define PointerIsValid(pointer)
Definition: c.h:698

◆ RequireTransactionBlock()

void RequireTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3445 of file xact.c.

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

3446 {
3447  CheckTransactionBlock(isTopLevel, true, stmtType);
3448 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3454

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2927 of file xact.c.

References save_XactDeferrable, save_XactIsoLevel, save_XactReadOnly, XactDeferrable, XactIsoLevel, and XactReadOnly.

Referenced by _SPI_commit(), _SPI_rollback(), and CommitTransactionCommand().

2928 {
2932 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2916
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2915
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2914

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

Definition at line 4512 of file xact.c.

References AbortSubTransaction(), AssertState, TransactionStateData::blockState, BlockStateAsString(), CleanupSubTransaction(), CurrentTransactionState, elog, 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, and TBLOCK_SUBRESTART.

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_subtrans_abort(), pltcl_subtransaction(), PLy_abort_open_subtransactions(), PLy_spi_subtransaction_abort(), PLy_subtransaction_exit(), ReorderBufferImmediateInvalidation(), and ReorderBufferProcessTXN().

4513 {
4515 
4516  /*
4517  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4518  * during parallel operations. That's because we may be in the leader,
4519  * recovering from an error thrown while we were in parallel mode. We
4520  * won't reach here in a worker, because BeginInternalSubTransaction()
4521  * will have failed.
4522  */
4523 
4524  switch (s->blockState)
4525  {
4526  /* Must be in a subtransaction */
4527  case TBLOCK_SUBINPROGRESS:
4528  case TBLOCK_SUBABORT:
4529  break;
4530 
4531  /* These cases are invalid. */
4532  case TBLOCK_DEFAULT:
4533  case TBLOCK_STARTED:
4534  case TBLOCK_BEGIN:
4537  case TBLOCK_SUBBEGIN:
4538  case TBLOCK_INPROGRESS:
4539  case TBLOCK_END:
4540  case TBLOCK_SUBRELEASE:
4541  case TBLOCK_SUBCOMMIT:
4542  case TBLOCK_ABORT:
4543  case TBLOCK_ABORT_END:
4544  case TBLOCK_SUBABORT_END:
4545  case TBLOCK_ABORT_PENDING:
4547  case TBLOCK_SUBRESTART:
4549  case TBLOCK_PREPARE:
4550  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4552  break;
4553  }
4554 
4555  /*
4556  * Abort the current subtransaction, if needed.
4557  */
4558  if (s->blockState == TBLOCK_SUBINPROGRESS)
4560 
4561  /* And clean it up, too */
4563 
4564  s = CurrentTransactionState; /* changed by pop */
4566  s->blockState == TBLOCK_INPROGRESS ||
4568  s->blockState == TBLOCK_STARTED);
4569 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define AssertState(condition)
Definition: c.h:807
TBlockState blockState
Definition: xact.c:192
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5390
static void AbortSubTransaction(void)
Definition: xact.c:4924
static void CleanupSubTransaction(void)
Definition: xact.c:5078
#define elog(elevel,...)
Definition: elog.h:232

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

Definition at line 4280 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), TransactionStateData::name, TransactionStateData::parent, PointerIsValid, TransactionStateData::savepointLevel, 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 standard_ProcessUtility().

4281 {
4283  TransactionState target,
4284  xact;
4285 
4286  /*
4287  * Workers synchronize transaction state at the beginning of each parallel
4288  * operation, so we can't account for transaction state change after that
4289  * point. (Note that this check will certainly error out if s->blockState
4290  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4291  * below.)
4292  */
4293  if (IsInParallelMode())
4294  ereport(ERROR,
4295  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4296  errmsg("cannot rollback to savepoints during a parallel operation")));
4297 
4298  switch (s->blockState)
4299  {
4300  /*
4301  * We can't rollback to a savepoint if there is no savepoint
4302  * defined.
4303  */
4304  case TBLOCK_INPROGRESS:
4305  case TBLOCK_ABORT:
4306  ereport(ERROR,
4307  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4308  errmsg("savepoint \"%s\" does not exist", name)));
4309  break;
4310 
4312  /* See comment about implicit transactions in DefineSavepoint */
4313  ereport(ERROR,
4314  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4315  /* translator: %s represents an SQL statement name */
4316  errmsg("%s can only be used in transaction blocks",
4317  "ROLLBACK TO SAVEPOINT")));
4318  break;
4319 
4320  /*
4321  * There is at least one savepoint, so proceed.
4322  */
4323  case TBLOCK_SUBINPROGRESS:
4324  case TBLOCK_SUBABORT:
4325  break;
4326 
4327  /* These cases are invalid. */
4328  case TBLOCK_DEFAULT:
4329  case TBLOCK_STARTED:
4330  case TBLOCK_BEGIN:
4332  case TBLOCK_SUBBEGIN:
4333  case TBLOCK_END:
4334  case TBLOCK_SUBRELEASE:
4335  case TBLOCK_SUBCOMMIT:
4336  case TBLOCK_ABORT_END:
4337  case TBLOCK_SUBABORT_END:
4338  case TBLOCK_ABORT_PENDING:
4340  case TBLOCK_SUBRESTART:
4342  case TBLOCK_PREPARE:
4343  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4345  break;
4346  }
4347 
4348  for (target = s; PointerIsValid(target); target = target->parent)
4349  {
4350  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4351  break;
4352  }
4353 
4354  if (!PointerIsValid(target))
4355  ereport(ERROR,
4356  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4357  errmsg("savepoint \"%s\" does not exist", name)));
4358 
4359  /* disallow crossing savepoint level boundaries */
4360  if (target->savepointLevel != s->savepointLevel)
4361  ereport(ERROR,
4362  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4363  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4364 
4365  /*
4366  * Mark "abort pending" all subtransactions up to the target
4367  * subtransaction. The actual aborts will happen when control gets to
4368  * CommitTransactionCommand.
4369  */
4370  xact = CurrentTransactionState;
4371  for (;;)
4372  {
4373  if (xact == target)
4374  break;
4375  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4377  else if (xact->blockState == TBLOCK_SUBABORT)
4379  else
4380  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4382  xact = xact->parent;
4383  Assert(PointerIsValid(xact));
4384  }
4385 
4386  /* And mark the target as "restart pending" */
4387  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4388  xact->blockState = TBLOCK_SUBRESTART;
4389  else if (xact->blockState == TBLOCK_SUBABORT)
4391  else
4392  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4394 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:192
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:46
#define FATAL
Definition: elog.h:49
struct TransactionStateData * parent
Definition: xact.c:208
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5390
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
const char * name
Definition: encode.c:515
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define PointerIsValid(pointer)
Definition: c.h:698

◆ SaveTransactionCharacteristics()

void SaveTransactionCharacteristics ( void  )

Definition at line 2919 of file xact.c.

References save_XactDeferrable, save_XactIsoLevel, save_XactReadOnly, XactDeferrable, XactIsoLevel, and XactReadOnly.

Referenced by _SPI_commit(), _SPI_rollback(), and CommitTransactionCommand().

2920 {
2924 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2916
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2915
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2914

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

Definition at line 5233 of file xact.c.

References add_size(), Assert, TransactionStateData::childXids, SerializedTransactionState::currentCommandId, currentCommandId, SerializedTransactionState::currentFullTransactionId, TransactionStateData::fullTransactionId, FullTransactionIdIsValid, i, TransactionStateData::nChildXids, nParallelCurrentXids, SerializedTransactionState::nParallelCurrentXids, palloc(), ParallelCurrentXids, SerializedTransactionState::parallelCurrentXids, TransactionStateData::parent, qsort, SerializedTransactionStateHeaderSize, SerializedTransactionState::topFullTransactionId, XactDeferrable, SerializedTransactionState::xactDeferrable, XactIsoLevel, SerializedTransactionState::xactIsoLevel, XactTopFullTransactionId, xidComparator(), and XidFromFullTransactionId.

Referenced by InitializeParallelDSM().

5234 {
5235  TransactionState s;
5236  Size nxids = 0;
5237  Size i = 0;
5238  TransactionId *workspace;
5240 
5241  result = (SerializedTransactionState *) start_address;
5242 
5243  result->xactIsoLevel = XactIsoLevel;
5244  result->xactDeferrable = XactDeferrable;
5246  result->currentFullTransactionId =
5249 
5250  /*
5251  * If we're running in a parallel worker and launching a parallel worker
5252  * of our own, we can just pass along the information that was passed to
5253  * us.
5254  */
5255  if (nParallelCurrentXids > 0)
5256  {
5258  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5260  return;
5261  }
5262 
5263  /*
5264  * OK, we need to generate a sorted list of XIDs that our workers should
5265  * view as current. First, figure out how many there are.
5266  */
5267  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5268  {
5270  nxids = add_size(nxids, 1);
5271  nxids = add_size(nxids, s->nChildXids);
5272  }
5274  <= maxsize);
5275 
5276  /* Copy them to our scratch space. */
5277  workspace = palloc(nxids * sizeof(TransactionId));
5278  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5279  {
5281  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5282  memcpy(&workspace[i], s->childXids,
5283  s->nChildXids * sizeof(TransactionId));
5284  i += s->nChildXids;
5285  }
5286  Assert(i == nxids);
5287 
5288  /* Sort them. */
5289  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5290 
5291  /* Copy data into output area. */
5292  result->nParallelCurrentXids = nxids;
5293  memcpy(&result->parallelCurrentXids[0], workspace,
5294  nxids * sizeof(TransactionId));
5295 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
bool XactDeferrable
Definition: xact.c:81
static TransactionState CurrentTransactionState
Definition: xact.c:250
uint32 TransactionId
Definition: c.h:587
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:225
int nParallelCurrentXids
Definition: xact.c:122
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:197
CommandId currentCommandId
Definition: xact.c:223
struct TransactionStateData * parent
Definition: xact.c:208
FullTransactionId fullTransactionId
Definition: xact.c:187
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
TransactionId * ParallelCurrentXids
Definition: xact.c:123
#define Assert(condition)
Definition: c.h:804
#define SerializedTransactionStateHeaderSize
Definition: xact.c:229
size_t Size
Definition: c.h:540
int XactIsoLevel
Definition: xact.c:75
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define qsort(a, b, c, d)
Definition: port.h:504
static CommandId currentCommandId
Definition: xact.c:257
FullTransactionId topFullTransactionId
Definition: xact.c:221
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:136
FullTransactionId currentFullTransactionId
Definition: xact.c:222

◆ SetCurrentStatementStartTimestamp()

void SetCurrentStatementStartTimestamp ( void  )

Definition at line 833 of file xact.c.

References Assert, GetCurrentTimestamp(), IsParallelWorker, and stmtStartTimestamp.

Referenced by autovac_report_activity(), autovac_report_workitem(), ensure_transaction(), initialize_worker_spi(), InitPostgres(), and PostgresMain().

834 {
835  if (!IsParallelWorker())
837  else
839 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
static TimestampTz stmtStartTimestamp
Definition: xact.c:268
#define IsParallelWorker()
Definition: parallel.h:61
#define Assert(condition)
Definition: c.h:804

◆ SetParallelStartTimestamps()

void SetParallelStartTimestamps ( TimestampTz  xact_ts,
TimestampTz  stmt_ts 
)

Definition at line 787 of file xact.c.

References Assert, IsParallelWorker, stmtStartTimestamp, and xactStartTimestamp.

Referenced by ParallelWorkerMain().

788 {
790  xactStartTimestamp = xact_ts;
791  stmtStartTimestamp = stmt_ts;
792 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:268
#define IsParallelWorker()
Definition: parallel.h:61
#define Assert(condition)
Definition: c.h:804
static TimestampTz xactStartTimestamp
Definition: xact.c:267

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

Definition at line 5303 of file xact.c.

References Assert, TransactionStateData::blockState, SerializedTransactionState::currentCommandId, currentCommandId, SerializedTransactionState::currentFullTransactionId, TransactionStateData::fullTransactionId, nParallelCurrentXids, SerializedTransactionState::nParallelCurrentXids, ParallelCurrentXids, SerializedTransactionState::parallelCurrentXids, StartTransaction(), TBLOCK_DEFAULT, TBLOCK_PARALLEL_INPROGRESS, SerializedTransactionState::topFullTransactionId, XactDeferrable, SerializedTransactionState::xactDeferrable, XactIsoLevel, and SerializedTransactionState::xactIsoLevel.

Referenced by ParallelWorkerMain().

5304 {
5306 
5308  StartTransaction();
5309 
5310  tstate = (SerializedTransactionState *) tstatespace;
5311  XactIsoLevel = tstate->xactIsoLevel;
5312  XactDeferrable = tstate->xactDeferrable;
5315  tstate->currentFullTransactionId;
5319 
5321 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
bool XactDeferrable
Definition: xact.c:81
static TransactionState CurrentTransactionState
Definition: xact.c:250
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:225
int nParallelCurrentXids
Definition: xact.c:122
TBlockState blockState
Definition: xact.c:192
CommandId currentCommandId
Definition: xact.c:223
FullTransactionId fullTransactionId
Definition: xact.c:187
static void StartTransaction(void)
Definition: xact.c:1914
TransactionId * ParallelCurrentXids
Definition: xact.c:123
#define Assert(condition)
Definition: c.h:804
int XactIsoLevel
Definition: xact.c:75
static CommandId currentCommandId
Definition: xact.c:257
FullTransactionId topFullTransactionId
Definition: xact.c:221
FullTransactionId currentFullTransactionId
Definition: xact.c:222

◆ StartTransactionCommand()

void StartTransactionCommand ( void  )

Definition at line 2838 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, CurTransactionContext, elog, ERROR, MemoryContextSwitchTo(), 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(), _SPI_rollback(), ApplyWorkerMain(), ATExecDetachPartition(), autoprewarm_database_main(), BeginInternalSubTransaction(), BootstrapModeMain(), cluster(), DefineIndex(), do_autovacuum(), ensure_transaction(), exec_replication_command(), get_database_list(), get_subscription_list(), IdentifySystem(), index_drop(), initialize_worker_spi(), InitPostgres(), LogicalRepSyncTableStart(), maybe_reread_subscription(), movedb(), ParallelWorkerMain(), perform_work_item(), pg_attribute_noreturn(), process_syncing_tables_for_apply(), process_syncing_tables_for_sync(), ProcessCatchupInterrupt(), ProcessCompletedNotifies(), ProcessIncomingNotify(), ReindexMultipleInternal(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), ReorderBufferProcessTXN(), SnapBuildExportSnapshot(), SPI_start_transaction(), start_xact_command(), vacuum(), and vacuum_rel().

2839 {
2841 
2842  switch (s->blockState)
2843  {
2844  /*
2845  * if we aren't in a transaction block, we just do our usual start
2846  * transaction.
2847  */
2848  case TBLOCK_DEFAULT:
2849  StartTransaction();
2851  break;
2852 
2853  /*
2854  * We are somewhere in a transaction block or subtransaction and
2855  * about to start a new command. For now we do nothing, but
2856  * someday we may do command-local resource initialization. (Note
2857  * that any needed CommandCounterIncrement was done by the
2858  * previous CommitTransactionCommand.)
2859  */
2860  case TBLOCK_INPROGRESS:
2862  case TBLOCK_SUBINPROGRESS:
2863  break;
2864 
2865  /*
2866  * Here we are in a failed transaction block (one of the commands
2867  * caused an abort) so we do nothing but remain in the abort
2868  * state. Eventually we will get a ROLLBACK command which will
2869  * get us out of this state. (It is up to other code to ensure
2870  * that no commands other than ROLLBACK will be processed in these
2871  * states.)
2872  */
2873  case TBLOCK_ABORT:
2874  case TBLOCK_SUBABORT:
2875  break;
2876 
2877  /* These cases are invalid. */
2878  case TBLOCK_STARTED:
2879  case TBLOCK_BEGIN:
2881  case TBLOCK_SUBBEGIN:
2882  case TBLOCK_END:
2883  case TBLOCK_SUBRELEASE:
2884  case TBLOCK_SUBCOMMIT:
2885  case TBLOCK_ABORT_END:
2886  case TBLOCK_SUBABORT_END:
2887  case TBLOCK_ABORT_PENDING:
2889  case TBLOCK_SUBRESTART:
2891  case TBLOCK_PREPARE:
2892  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2894  break;
2895  }
2896 
2897  /*
2898  * We must switch to CurTransactionContext before returning. This is
2899  * already done if we called StartTransaction, otherwise not.
2900  */
2901  Assert(CurTransactionContext != NULL);
2903 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:54
TBlockState blockState
Definition: xact.c:192
#define ERROR
Definition: elog.h:46
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5390
static void StartTransaction(void)
Definition: xact.c:1914
#define Assert(condition)
Definition: c.h:804
#define elog(elevel,...)
Definition: elog.h:232

◆ SubTransactionIsActive()

bool SubTransactionIsActive ( SubTransactionId  subxid)

Definition at line 737 of file xact.c.

References TransactionStateData::parent, TransactionStateData::state, TransactionStateData::subTransactionId, and TRANS_ABORT.

Referenced by fmgr_sql().

738 {
740 
741  for (s = CurrentTransactionState; s != NULL; s = s->parent)
742  {
743  if (s->state == TRANS_ABORT)
744  continue;
745  if (s->subTransactionId == subxid)
746  return true;
747  }
748  return false;
749 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TransState state
Definition: xact.c:191
struct TransactionStateData * parent
Definition: xact.c:208
SubTransactionId subTransactionId
Definition: xact.c:188

◆ TransactionBlockStatusCode()

char TransactionBlockStatusCode ( void  )

Definition at line 4715 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, 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, and TBLOCK_SUBRESTART.

Referenced by ReadyForQuery().

4716 {
4718 
4719  switch (s->blockState)
4720  {
4721  case TBLOCK_DEFAULT:
4722  case TBLOCK_STARTED:
4723  return 'I'; /* idle --- not in transaction */
4724  case TBLOCK_BEGIN:
4725  case TBLOCK_SUBBEGIN:
4726  case TBLOCK_INPROGRESS:
4729  case TBLOCK_SUBINPROGRESS:
4730  case TBLOCK_END:
4731  case TBLOCK_SUBRELEASE:
4732  case TBLOCK_SUBCOMMIT:
4733  case TBLOCK_PREPARE:
4734  return 'T'; /* in transaction */
4735  case TBLOCK_ABORT:
4736  case TBLOCK_SUBABORT:
4737  case TBLOCK_ABORT_END:
4738  case TBLOCK_SUBABORT_END:
4739  case TBLOCK_ABORT_PENDING:
4741  case TBLOCK_SUBRESTART:
4743  return 'E'; /* in failed transaction */
4744  }
4745 
4746  /* should never get here */
4747  elog(FATAL, "invalid transaction block state: %s",
4749  return 0; /* keep compiler quiet */
4750 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
TBlockState blockState
Definition: xact.c:192
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5390
#define elog(elevel,...)
Definition: elog.h:232

◆ TransactionIdIsCurrentTransactionId()

bool TransactionIdIsCurrentTransactionId ( TransactionId  xid)

Definition at line 869 of file xact.c.

References TransactionStateData::childXids, TransactionStateData::fullTransactionId, FullTransactionIdIsValid, GetTopTransactionIdIfAny(), TransactionStateData::nChildXids, nParallelCurrentXids, ParallelCurrentXids, TransactionStateData::parent, TransactionStateData::state, TRANS_ABORT, TransactionIdEquals, TransactionIdIsNormal, TransactionIdPrecedes(), and XidFromFullTransactionId.

Referenced by compute_new_xmax_infomask(), Do_MultiXactIdWait(), DoesMultiXactIdConflict(), ExecCheckTupleVisible(), ExecOnConflictUpdate(), FreezeMultiXactId(), get_xid_status(), heap_delete(), heap_lock_tuple(), heap_update(), heapam_index_build_range_scan(), heapam_relation_copy_for_cluster(), heapam_scan_analyze_next_tuple(), heapam_tuple_lock(), HeapTupleHeaderAdjustCmax(), HeapTupleHeaderGetCmax(), HeapTupleHeaderGetCmin(), HeapTupleHeaderIsOnlyLocked(), HeapTupleSatisfiesDirty(), HeapTupleSatisfiesMVCC(), HeapTupleSatisfiesSelf(), HeapTupleSatisfiesToast(), HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVacuumHorizon(), MultiXactIdIsRunning(), pg_xact_status(), PredicateLockTID(), RI_FKey_fk_upd_check_required(), SnapBuildWaitSnapshot(), test_lockmode_for_conflict(), and TransactionIdIsInProgress().

870 {
872 
873  /*
874  * We always say that BootstrapTransactionId is "not my transaction ID"
875  * even when it is (ie, during bootstrap). Along with the fact that
876  * transam.c always treats BootstrapTransactionId as already committed,
877  * this causes the heapam_visibility.c routines to see all tuples as
878  * committed, which is what we need during bootstrap. (Bootstrap mode
879  * only inserts tuples, it never updates or deletes them, so all tuples
880  * can be presumed good immediately.)
881  *
882  * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
883  * not my transaction ID, so we can just return "false" immediately for
884  * any non-normal XID.
885  */
886  if (!TransactionIdIsNormal(xid))
887  return false;
888 
890  return true;
891 
892  /*
893  * In parallel workers, the XIDs we must consider as current are stored in
894  * ParallelCurrentXids rather than the transaction-state stack. Note that
895  * the XIDs in this array are sorted numerically rather than according to
896  * transactionIdPrecedes order.
897  */
898  if (nParallelCurrentXids > 0)
899  {
900  int low,
901  high;
902 
903  low = 0;
904  high = nParallelCurrentXids - 1;
905  while (low <= high)
906  {
907  int middle;
908  TransactionId probe;
909 
910  middle = low + (high - low) / 2;
911  probe = ParallelCurrentXids[middle];
912  if (probe == xid)
913  return true;
914  else if (probe < xid)
915  low = middle + 1;
916  else
917  high = middle - 1;
918  }
919  return false;
920  }
921 
922  /*
923  * We will return true for the Xid of the current subtransaction, any of
924  * its subcommitted children, any of its parents, or any of their
925  * previously subcommitted children. However, a transaction being aborted
926  * is no longer "current", even though it may still have an entry on the
927  * state stack.
928  */
929  for (s = CurrentTransactionState; s != NULL; s = s->parent)
930  {
931  int low,
932  high;
933 
934  if (s->state == TRANS_ABORT)
935  continue;
937  continue; /* it can't have any child XIDs either */
939  return true;
940  /* As the childXids array is ordered, we can use binary search */
941  low = 0;
942  high = s->nChildXids - 1;
943  while (low <= high)
944  {
945  int middle;
946  TransactionId probe;
947 
948  middle = low + (high - low) / 2;
949  probe = s->childXids[middle];
950  if (TransactionIdEquals(probe, xid))
951  return true;
952  else if (TransactionIdPrecedes(probe, xid))
953  low = middle + 1;
954  else
955  high = middle - 1;
956  }
957  }
958 
959  return false;
960 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:587
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
int nParallelCurrentXids
Definition: xact.c:122
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:197
TransState state
Definition: xact.c:191
struct TransactionStateData * parent
Definition: xact.c:208
FullTransactionId fullTransactionId
Definition: xact.c:187
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:425
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
TransactionId * ParallelCurrentXids
Definition: xact.c:123
#define TransactionIdIsNormal(xid)
Definition: transam.h:42

◆ UnregisterSubXactCallback()

void UnregisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3595 of file xact.c.

References SubXactCallbackItem::next, and pfree().

3596 {
3597  SubXactCallbackItem *item;
3598  SubXactCallbackItem *prev;
3599 
3600  prev = NULL;
3601  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3602  {
3603  if (item->callback == callback && item->arg == arg)
3604  {
3605  if (prev)
3606  prev->next = item->next;
3607  else
3608  SubXact_callbacks = item->next;
3609  pfree(item);
3610  break;
3611  }
3612  }
3613 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:314
void pfree(void *pointer)
Definition: mcxt.c:1169
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:309
void * arg

◆ UnregisterXactCallback()

void UnregisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3540 of file xact.c.

References XactCallbackItem::next, and pfree().

3541 {
3542  XactCallbackItem *item;
3543  XactCallbackItem *prev;
3544 
3545  prev = NULL;
3546  for (item = Xact_callbacks; item; prev = item, item = item->next)
3547  {
3548  if (item->callback == callback && item->arg == arg)
3549  {
3550  if (prev)
3551  prev->next = item->next;
3552  else
3553  Xact_callbacks = item->next;
3554  pfree(item);
3555  break;
3556  }
3557  }
3558 }
struct XactCallbackItem * next
Definition: xact.c:297
void pfree(void *pointer)
Definition: mcxt.c:1169
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
static XactCallbackItem * Xact_callbacks
Definition: xact.c:302
void * arg

◆ UserAbortTransactionBlock()

void UserAbortTransactionBlock ( bool  chain)

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

3918 {
3920 
3921  switch (s->blockState)
3922  {
3923  /*
3924  * We are inside a transaction block and we got a ROLLBACK command
3925  * from the user, so tell CommitTransactionCommand to abort and
3926  * exit the transaction block.
3927  */
3928  case TBLOCK_INPROGRESS:
3930  break;
3931 
3932  /*
3933  * We are inside a failed transaction block and we got a ROLLBACK
3934  * command from the user. Abort processing is already done, so
3935  * CommitTransactionCommand just has to cleanup and go back to
3936  * idle state.
3937  */
3938  case TBLOCK_ABORT:
3940  break;
3941 
3942  /*
3943  * We are inside a subtransaction. Mark everything up to top
3944  * level as exitable.
3945  */
3946  case TBLOCK_SUBINPROGRESS:
3947  case TBLOCK_SUBABORT:
3948  while (s->parent != NULL)
3949  {
3950  if (s->blockState == TBLOCK_SUBINPROGRESS)
3952  else if (s->blockState == TBLOCK_SUBABORT)
3954  else
3955  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3957  s = s->parent;
3958  }
3959  if (s->blockState == TBLOCK_INPROGRESS)
3961  else if (s->blockState == TBLOCK_ABORT)
3963  else
3964  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3966  break;
3967 
3968  /*
3969  * The user issued ABORT when not inside a transaction. For
3970  * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
3971  * The upcoming call to CommitTransactionCommand() will then put
3972  * us back into the default state. For ROLLBACK AND CHAIN, error.
3973  *
3974  * We do the same thing with ABORT inside an implicit transaction,
3975  * although in this case we might be rolling back actual database
3976  * state changes. (It's debatable whether we should issue a
3977  * WARNING in this case, but we have done so historically.)
3978  */
3979  case TBLOCK_STARTED:
3981  if (chain)
3982  ereport(ERROR,
3983  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3984  /* translator: %s represents an SQL statement name */
3985  errmsg("%s can only be used in transaction blocks",
3986  "ROLLBACK AND CHAIN")));
3987