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

◆ 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 and
76  * 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 3224 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().

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4594 {
4596 
4597  /* Ensure we're not running in a doomed memory context */
4598  AtAbort_Memory();
4599 
4600  /*
4601  * Get out of any transaction or nested transaction
4602  */
4603  do
4604  {
4605  switch (s->blockState)
4606  {
4607  case TBLOCK_DEFAULT:
4608  if (s->state == TRANS_DEFAULT)
4609  {
4610  /* Not in a transaction, do nothing */
4611  }
4612  else
4613  {
4614  /*
4615  * We can get here after an error during transaction start
4616  * (state will be TRANS_START). Need to clean up the
4617  * incompletely started transaction. First, adjust the
4618  * low-level state to suppress warning message from
4619  * AbortTransaction.
4620  */
4621  if (s->state == TRANS_START)
4622  s->state = TRANS_INPROGRESS;
4623  AbortTransaction();
4625  }
4626  break;
4627  case TBLOCK_STARTED:
4628  case TBLOCK_BEGIN:
4629  case TBLOCK_INPROGRESS:
4632  case TBLOCK_END:
4633  case TBLOCK_ABORT_PENDING:
4634  case TBLOCK_PREPARE:
4635  /* In a transaction, so clean up */
4636  AbortTransaction();
4639  break;
4640  case TBLOCK_ABORT:
4641  case TBLOCK_ABORT_END:
4642 
4643  /*
4644  * AbortTransaction is already done, still need Cleanup.
4645  * However, if we failed partway through running ROLLBACK,
4646  * there will be an active portal running that command, which
4647  * we need to shut down before doing CleanupTransaction.
4648  */
4649  AtAbort_Portals();
4652  break;
4653 
4654  /*
4655  * In a subtransaction, so clean it up and abort parent too
4656  */
4657  case TBLOCK_SUBBEGIN:
4658  case TBLOCK_SUBINPROGRESS:
4659  case TBLOCK_SUBRELEASE:
4660  case TBLOCK_SUBCOMMIT:
4662  case TBLOCK_SUBRESTART:
4665  s = CurrentTransactionState; /* changed by pop */
4666  break;
4667 
4668  case TBLOCK_SUBABORT:
4669  case TBLOCK_SUBABORT_END:
4671  /* As above, but AbortSubTransaction already done */
4672  if (s->curTransactionOwner)
4673  {
4674  /* As in TBLOCK_ABORT, might have a live portal to zap */
4679  }
4681  s = CurrentTransactionState; /* changed by pop */
4682  break;
4683  }
4684  } while (s->blockState != TBLOCK_DEFAULT);
4685 
4686  /* Should be out of all subxacts now */
4687  Assert(s->parent == NULL);
4688 
4689  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4690  AtCleanup_Memory();
4691 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
static void AtCleanup_Memory(void)
Definition: xact.c:1844
void AtAbort_Portals(void)
Definition: portalmem.c:781
TBlockState blockState
Definition: xact.c:193
TransState state
Definition: xact.c:192
ResourceOwner curTransactionOwner
Definition: xact.c:197
static void CleanupTransaction(void)
Definition: xact.c:2803
struct TransactionStateData * parent
Definition: xact.c:209
SubTransactionId subTransactionId
Definition: xact.c:189
static void AbortSubTransaction(void)
Definition: xact.c:4939
static void AtAbort_Memory(void)
Definition: xact.c:1754
static void AbortTransaction(void)
Definition: xact.c:2612
#define Assert(condition)
Definition: c.h:804
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:979
static void CleanupSubTransaction(void)
Definition: xact.c:5098

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4053 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

Definition at line 3651 of file xact.c.

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

Referenced by apply_handle_prepare_internal(), and standard_ProcessUtility().

3652 {
3654 
3655  switch (s->blockState)
3656  {
3657  /*
3658  * We are not inside a transaction block, so allow one to begin.
3659  */
3660  case TBLOCK_STARTED:
3661  s->blockState = TBLOCK_BEGIN;
3662  break;
3663 
3664  /*
3665  * BEGIN converts an implicit transaction block to a regular one.
3666  * (Note that we allow this even if we've already done some
3667  * commands, which is a bit odd but matches historical practice.)
3668  */
3670  s->blockState = TBLOCK_BEGIN;
3671  break;
3672 
3673  /*
3674  * Already a transaction block in progress.
3675  */
3676  case TBLOCK_INPROGRESS:
3678  case TBLOCK_SUBINPROGRESS:
3679  case TBLOCK_ABORT:
3680  case TBLOCK_SUBABORT:
3681  ereport(WARNING,
3682  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3683  errmsg("there is already a transaction in progress")));
3684  break;
3685 
3686  /* These cases are invalid. */
3687  case TBLOCK_DEFAULT:
3688  case TBLOCK_BEGIN:
3689  case TBLOCK_SUBBEGIN:
3690  case TBLOCK_END:
3691  case TBLOCK_SUBRELEASE:
3692  case TBLOCK_SUBCOMMIT:
3693  case TBLOCK_ABORT_END:
3694  case TBLOCK_SUBABORT_END:
3695  case TBLOCK_ABORT_PENDING:
3697  case TBLOCK_SUBRESTART:
3699  case TBLOCK_PREPARE:
3700  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3702  break;
3703  }
3704 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:193
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5410
#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 1022 of file xact.c.

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

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

1023 {
1024  /*
1025  * If the current value of the command counter hasn't been "used" to mark
1026  * tuples, we need not increment it, since there's no need to distinguish
1027  * a read-only command from others. This helps postpone command counter
1028  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
1029  */
1031  {
1032  /*
1033  * Workers synchronize transaction state at the beginning of each
1034  * parallel operation, so we can't account for new commands after that
1035  * point.
1036  */
1038  elog(ERROR, "cannot start commands during a parallel operation");
1039 
1040  currentCommandId += 1;
1042  {
1043  currentCommandId -= 1;
1044  ereport(ERROR,
1045  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1046  errmsg("cannot have more than 2^32-2 commands in a transaction")));
1047  }
1048  currentCommandIdUsed = false;
1049 
1050  /* Propagate new command ID into static snapshots */
1052 
1053  /*
1054  * Make any catalog changes done by the just-completed command visible
1055  * in the local syscache. We obviously don't need to do this after a
1056  * read-only command. (But see hacks in inval.c to make real sure we
1057  * don't think a command that queued inval messages was read-only.)
1058  */
1059  AtCCI_LocalCache();
1060  }
1061 }
int errcode(int sqlerrcode)
Definition: elog.c:698
bool IsInParallelMode(void)
Definition: xact.c:1013
#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:1477
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:258
static bool currentCommandIdUsed
Definition: xact.c:259

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2953 of file xact.c.

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

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

2954 {
2956 
2957  if (s->chain)
2959 
2960  switch (s->blockState)
2961  {
2962  /*
2963  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2964  * StartTransactionCommand didn't set the STARTED state
2965  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2966  * by EndParallelWorkerTransaction(), not this function.
2967  */
2968  case TBLOCK_DEFAULT:
2970  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2972  break;
2973 
2974  /*
2975  * If we aren't in a transaction block, just do our usual
2976  * transaction commit, and return to the idle state.
2977  */
2978  case TBLOCK_STARTED:
2981  break;
2982 
2983  /*
2984  * We are completing a "BEGIN TRANSACTION" command, so we change
2985  * to the "transaction block in progress" state and return. (We
2986  * assume the BEGIN did nothing to the database, so we need no
2987  * CommandCounterIncrement.)
2988  */
2989  case TBLOCK_BEGIN:
2991  break;
2992 
2993  /*
2994  * This is the case when we have finished executing a command
2995  * someplace within a transaction block. We increment the command
2996  * counter and return.
2997  */
2998  case TBLOCK_INPROGRESS:
3000  case TBLOCK_SUBINPROGRESS:
3002  break;
3003 
3004  /*
3005  * We are completing a "COMMIT" command. Do it and return to the
3006  * idle state.
3007  */
3008  case TBLOCK_END:
3011  if (s->chain)
3012  {
3013  StartTransaction();
3015  s->chain = false;
3017  }
3018  break;
3019 
3020  /*
3021  * Here we are in the middle of a transaction block but one of the
3022  * commands caused an abort so we do nothing but remain in the
3023  * abort state. Eventually we will get a ROLLBACK command.
3024  */
3025  case TBLOCK_ABORT:
3026  case TBLOCK_SUBABORT:
3027  break;
3028 
3029  /*
3030  * Here we were in an aborted transaction block and we just got
3031  * the ROLLBACK command from the user, so clean up the
3032  * already-aborted transaction and return to the idle state.
3033  */
3034  case TBLOCK_ABORT_END:
3037  if (s->chain)
3038  {
3039  StartTransaction();
3041  s->chain = false;
3043  }
3044  break;
3045 
3046  /*
3047  * Here we were in a perfectly good transaction block but the user
3048  * told us to ROLLBACK anyway. We have to abort the transaction
3049  * and then clean up.
3050  */
3051  case TBLOCK_ABORT_PENDING:
3052  AbortTransaction();
3055  if (s->chain)
3056  {
3057  StartTransaction();
3059  s->chain = false;
3061  }
3062  break;
3063 
3064  /*
3065  * We are completing a "PREPARE TRANSACTION" command. Do it and
3066  * return to the idle state.
3067  */
3068  case TBLOCK_PREPARE:
3071  break;
3072 
3073  /*
3074  * We were just issued a SAVEPOINT inside a transaction block.
3075  * Start a subtransaction. (DefineSavepoint already did
3076  * PushTransaction, so as to have someplace to put the SUBBEGIN
3077  * state.)
3078  */
3079  case TBLOCK_SUBBEGIN:
3082  break;
3083 
3084  /*
3085  * We were issued a RELEASE command, so we end the current
3086  * subtransaction and return to the parent transaction. The parent
3087  * might be ended too, so repeat till we find an INPROGRESS
3088  * transaction or subtransaction.
3089  */
3090  case TBLOCK_SUBRELEASE:
3091  do
3092  {
3094  s = CurrentTransactionState; /* changed by pop */
3095  } while (s->blockState == TBLOCK_SUBRELEASE);
3096 
3099  break;
3100 
3101  /*
3102  * We were issued a COMMIT, so we end the current subtransaction
3103  * hierarchy and perform final commit. We do this by rolling up
3104  * any subtransactions into their parent, which leads to O(N^2)
3105  * operations with respect to resource owners - this isn't that
3106  * bad until we approach a thousands of savepoints but is
3107  * necessary for correctness should after triggers create new
3108  * resource owners.
3109  */
3110  case TBLOCK_SUBCOMMIT:
3111  do
3112  {
3114  s = CurrentTransactionState; /* changed by pop */
3115  } while (s->blockState == TBLOCK_SUBCOMMIT);
3116  /* If we had a COMMIT command, finish off the main xact too */
3117  if (s->blockState == TBLOCK_END)
3118  {
3119  Assert(s->parent == NULL);
3122  if (s->chain)
3123  {
3124  StartTransaction();
3126  s->chain = false;
3128  }
3129  }
3130  else if (s->blockState == TBLOCK_PREPARE)
3131  {
3132  Assert(s->parent == NULL);
3135  }
3136  else
3137  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3139  break;
3140 
3141  /*
3142  * The current already-failed subtransaction is ending due to a
3143  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3144  * examine the parent (which could be in any of several states).
3145  */
3146  case TBLOCK_SUBABORT_END:
3149  break;
3150 
3151  /*
3152  * As above, but it's not dead yet, so abort first.
3153  */
3158  break;
3159 
3160  /*
3161  * The current subtransaction is the target of a ROLLBACK TO
3162  * command. Abort and pop it, then start a new subtransaction
3163  * with the same name.
3164  */
3165  case TBLOCK_SUBRESTART:
3166  {
3167  char *name;
3168  int savepointLevel;
3169 
3170  /* save name and keep Cleanup from freeing it */
3171  name = s->name;
3172  s->name = NULL;
3173  savepointLevel = s->savepointLevel;
3174 
3177 
3178  DefineSavepoint(NULL);
3179  s = CurrentTransactionState; /* changed by push */
3180  s->name = name;
3181  s->savepointLevel = savepointLevel;
3182 
3183  /* This is the same as TBLOCK_SUBBEGIN case */
3187  }
3188  break;
3189 
3190  /*
3191  * Same as above, but the subtransaction had already failed, so we
3192  * don't need AbortSubTransaction.
3193  */
3195  {
3196  char *name;
3197  int savepointLevel;
3198 
3199  /* save name and keep Cleanup from freeing it */
3200  name = s->name;
3201  s->name = NULL;
3202  savepointLevel = s->savepointLevel;
3203 
3205 
3206  DefineSavepoint(NULL);
3207  s = CurrentTransactionState; /* changed by push */
3208  s->name = name;
3209  s->savepointLevel = savepointLevel;
3210 
3211  /* This is the same as TBLOCK_SUBBEGIN case */
3215  }
3216  break;
3217  }
3218 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
#define AssertState(condition)
Definition: c.h:807
void CommitTransactionCommand(void)
Definition: xact.c:2953
void DefineSavepoint(const char *name)
Definition: xact.c:4100
TBlockState blockState
Definition: xact.c:193
static void PrepareTransaction(void)
Definition: xact.c:2333
#define ERROR
Definition: elog.h:46
static void CommitSubTransaction(void)
Definition: xact.c:4830
#define FATAL
Definition: elog.h:49
static void CleanupTransaction(void)
Definition: xact.c:2803
struct TransactionStateData * parent
Definition: xact.c:209
void SaveTransactionCharacteristics(void)
Definition: xact.c:2933
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5410
static void AbortSubTransaction(void)
Definition: xact.c:4939
void CommandCounterIncrement(void)
Definition: xact.c:1022
static void AbortTransaction(void)
Definition: xact.c:2612
static void StartTransaction(void)
Definition: xact.c:1915
#define Assert(condition)
Definition: c.h:804
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2941
static void CleanupSubTransaction(void)
Definition: xact.c:5098
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:232
static void CommitTransaction(void)
Definition: xact.c:2074
static void StartSubTransaction(void)
Definition: xact.c:4793

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4078 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5348 of file xact.c.

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

Referenced by ParallelWorkerMain().

5349 {
5353 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
TBlockState blockState
Definition: xact.c:193
#define Assert(condition)
Definition: c.h:804
static void CommitTransaction(void)
Definition: xact.c:2074

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

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

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

981 {
983 
984  Assert(s->parallelModeLevel >= 0);
985 
986  ++s->parallelModeLevel;
987 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
int parallelModeLevel
Definition: xact.c:206
#define Assert(condition)
Definition: c.h:804

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5225 of file xact.c.

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

Referenced by InitializeParallelDSM().

5226 {
5227  TransactionState s;
5228  Size nxids = 0;
5230 
5231  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5232  {
5234  nxids = add_size(nxids, 1);
5235  nxids = add_size(nxids, s->nChildXids);
5236  }
5237 
5238  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5239 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
uint32 TransactionId
Definition: c.h:587
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
struct TransactionStateData * parent
Definition: xact.c:209
FullTransactionId fullTransactionId
Definition: xact.c:188
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:230
size_t Size
Definition: c.h:540

◆ ExitParallelMode()

void ExitParallelMode ( void  )

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

994 {
996 
997  Assert(s->parallelModeLevel > 0);
999 
1000  --s->parallelModeLevel;
1001 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
bool ParallelContextActive(void)
Definition: parallel.c:990
int parallelModeLevel
Definition: xact.c:206
#define Assert(condition)
Definition: c.h:804

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1072 of file xact.c.

References forceSyncCommit.

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

1073 {
1074  forceSyncCommit = true;
1075 }
static bool forceSyncCommit
Definition: xact.c:281

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 762 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

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

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

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 497 of file xact.c.

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

498 {
500 
503  return s->fullTransactionId;
504 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
FullTransactionId fullTransactionId
Definition: xact.c:188
static void AssignTransactionId(TransactionState s)
Definition: xact.c:570

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 515 of file xact.c.

References TransactionStateData::fullTransactionId.

516 {
518 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
FullTransactionId fullTransactionId
Definition: xact.c:188

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 808 of file xact.c.

References stmtStartTimestamp.

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

809 {
810  return stmtStartTimestamp;
811 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:269

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 456 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

457 {
459 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
#define XidFromFullTransactionId(x)
Definition: transam.h:48
FullTransactionId fullTransactionId
Definition: xact.c:188

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

Definition at line 799 of file xact.c.

References xactStartTimestamp.

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

800 {
801  return xactStartTimestamp;
802 }
static TimestampTz xactStartTimestamp
Definition: xact.c:268

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 820 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

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

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 542 of file xact.c.

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

Referenced by xid_age().

543 {
545  static TransactionId stablexid = InvalidTransactionId;
546 
547  if (lxid != MyProc->lxid)
548  {
549  lxid = MyProc->lxid;
550  stablexid = GetTopTransactionIdIfAny();
551  if (!TransactionIdIsValid(stablexid))
552  stablexid = ReadNextTransactionId();
553  }
554 
555  Assert(TransactionIdIsValid(stablexid));
556 
557  return stablexid;
558 }
uint32 TransactionId
Definition: c.h:587
PGPROC * MyProc
Definition: proc.c:68
static TransactionId ReadNextTransactionId(void)
Definition: transam.h:316
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:589
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:426
#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 468 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

Referenced by pg_current_xact_id().

469 {
473 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:122
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
static TransactionStateData TopTransactionStateData
Definition: xact.c:238
static void AssignTransactionId(TransactionState s)
Definition: xact.c:570

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 484 of file xact.c.

References XactTopFullTransactionId.

Referenced by pg_current_xact_id_if_assigned().

485 {
487 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:122

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

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

412 {
416 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:122
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static TransactionStateData TopTransactionStateData
Definition: xact.c:238
static void AssignTransactionId(TransactionState s)
Definition: xact.c:570

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3506 of file xact.c.

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

Referenced by vacuum().

3507 {
3508  /*
3509  * Return true on same conditions that would make
3510  * PreventInTransactionBlock error out
3511  */
3512  if (IsTransactionBlock())
3513  return true;
3514 
3515  if (IsSubTransaction())
3516  return true;
3517 
3518  if (!isTopLevel)
3519  return true;
3520 
3523  return true;
3524 
3525  return false;
3526 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
TBlockState blockState
Definition: xact.c:193
bool IsTransactionBlock(void)
Definition: xact.c:4697
bool IsSubTransaction(void)
Definition: xact.c:4770

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 6116 of file xact.c.

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

Referenced by MarkSubTransactionAssigned(), and XLogRecordAssemble().

6117 {
6118  /* wal_level has to be logical */
6119  if (!XLogLogicalInfoActive())
6120  return false;
6121 
6122  /* we need to be in a transaction state */
6123  if (!IsTransactionState())
6124  return false;
6125 
6126  /* it has to be a subtransaction */
6127  if (!IsSubTransaction())
6128  return false;
6129 
6130  /* the subtransaction has to have a XID assigned */
6132  return false;
6133 
6134  /* and it should not be already 'assigned' */
6136 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
#define XLogLogicalInfoActive()
Definition: xlog.h:183
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:456
bool IsTransactionState(void)
Definition: xact.c:372
bool IsSubTransaction(void)
Definition: xact.c:4770
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4697 of file xact.c.

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

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

4698 {
4700 
4702  return false;
4703 
4704  return true;
4705 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
TBlockState blockState
Definition: xact.c:193

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 372 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

Referenced by apply_handle_commit_internal(), apply_handle_origin(), apply_handle_stream_stop(), begin_replication_step(), 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(), FetchTableStates(), 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().

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

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 526 of file xact.c.

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

Referenced by XLogInsertRecord().

527 {
530 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
FullTransactionId fullTransactionId
Definition: xact.c:188

◆ MarkSubTransactionAssigned()

void MarkSubTransactionAssigned ( void  )

Definition at line 6144 of file xact.c.

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

Referenced by XLogResetInsertion().

6145 {
6147 
6149 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
bool IsSubTransactionAssignmentPending(void)
Definition: xact.c:6116
#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 3719 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 apply_handle_prepare_internal(), and standard_ProcessUtility().

3720 {
3721  TransactionState s;
3722  bool result;
3723 
3724  /* Set up to commit the current transaction */
3725  result = EndTransactionBlock(false);
3726 
3727  /* If successful, change outer tblock state to PREPARE */
3728  if (result)
3729  {
3731 
3732  while (s->parent != NULL)
3733  s = s->parent;
3734 
3735  if (s->blockState == TBLOCK_END)
3736  {
3737  /* Save GID where PrepareTransaction can find it again */
3739 
3741  }
3742  else
3743  {
3744  /*
3745  * ignore case where we are not in a transaction;
3746  * EndTransactionBlock already issued a warning.
3747  */
3750  /* Don't send back a PREPARE result tag... */
3751  result = false;
3752  }
3753  }
3754 
3755  return result;
3756 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
bool EndTransactionBlock(bool chain)
Definition: xact.c:3771
MemoryContext TopTransactionContext
Definition: mcxt.c:53
TBlockState blockState
Definition: xact.c:193
struct TransactionStateData * parent
Definition: xact.c:209
#define Assert(condition)
Definition: c.h:804
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1286
static char * prepareGID
Definition: xact.c:276

◆ PreventInTransactionBlock()

void PreventInTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

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

3394 {
3395  /*
3396  * xact block already started?
3397  */
3398  if (IsTransactionBlock())
3399  ereport(ERROR,
3400  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3401  /* translator: %s represents an SQL statement name */
3402  errmsg("%s cannot run inside a transaction block",
3403  stmtType)));
3404 
3405  /*
3406  * subtransaction?
3407  */
3408  if (IsSubTransaction())
3409  ereport(ERROR,
3410  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3411  /* translator: %s represents an SQL statement name */
3412  errmsg("%s cannot run inside a subtransaction",
3413  stmtType)));
3414 
3415  /*
3416  * inside a function call?
3417  */
3418  if (!isTopLevel)
3419  ereport(ERROR,
3420  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3421  /* translator: %s represents an SQL statement name */
3422  errmsg("%s cannot be executed from a function", stmtType)));
3423 
3424  /* If we got past IsTransactionBlock test, should be in default state */
3427  elog(FATAL, "cannot prevent transaction chain");
3428  /* all okay */
3429 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
int errcode(int sqlerrcode)
Definition: elog.c:698
TBlockState blockState
Definition: xact.c:193
bool IsTransactionBlock(void)
Definition: xact.c:4697
#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:4770
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 3596 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().

3597 {
3598  SubXactCallbackItem *item;
3599 
3600  item = (SubXactCallbackItem *)
3602  item->callback = callback;
3603  item->arg = arg;
3604  item->next = SubXact_callbacks;
3605  SubXact_callbacks = item;
3606 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:315
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:310
SubXactCallback callback
Definition: xact.c:311
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 3541 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().

3542 {
3543  XactCallbackItem *item;
3544 
3545  item = (XactCallbackItem *)
3547  item->callback = callback;
3548  item->arg = arg;
3549  item->next = Xact_callbacks;
3550  Xact_callbacks = item;
3551 }
struct XactCallbackItem * next
Definition: xact.c:298
void * arg
Definition: xact.c:300
XactCallback callback
Definition: xact.c:299
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:303
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
void * arg

◆ ReleaseCurrentSubTransaction()

void ReleaseCurrentSubTransaction ( void  )

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

4493 {
4495 
4496  /*
4497  * Workers synchronize transaction state at the beginning of each parallel
4498  * operation, so we can't account for commit of subtransactions after that
4499  * point. This should not happen anyway. Code calling this would
4500  * typically have called BeginInternalSubTransaction() first, failing
4501  * there.
4502  */
4503  if (IsInParallelMode())
4504  ereport(ERROR,
4505  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4506  errmsg("cannot commit subtransactions during a parallel operation")));
4507 
4508  if (s->blockState != TBLOCK_SUBINPROGRESS)
4509  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4511  Assert(s->state == TRANS_INPROGRESS);
4514  s = CurrentTransactionState; /* changed by pop */
4515  Assert(s->state == TRANS_INPROGRESS);
4516 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
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:193
TransState state
Definition: xact.c:192
bool IsInParallelMode(void)
Definition: xact.c:1013
#define ERROR
Definition: elog.h:46
static void CommitSubTransaction(void)
Definition: xact.c:4830
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5410
#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 4185 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().

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

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

3460 {
3461  CheckTransactionBlock(isTopLevel, true, stmtType);
3462 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3468

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2941 of file xact.c.

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

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

2942 {
2946 }
bool XactDeferrable
Definition: xact.c:82
static bool save_XactDeferrable
Definition: xact.c:2930
bool XactReadOnly
Definition: xact.c:79
static bool save_XactReadOnly
Definition: xact.c:2929
int XactIsoLevel
Definition: xact.c:76
static int save_XactIsoLevel
Definition: xact.c:2928

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

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

4527 {
4529 
4530  /*
4531  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4532  * during parallel operations. That's because we may be in the leader,
4533  * recovering from an error thrown while we were in parallel mode. We
4534  * won't reach here in a worker, because BeginInternalSubTransaction()
4535  * will have failed.
4536  */
4537 
4538  switch (s->blockState)
4539  {
4540  /* Must be in a subtransaction */
4541  case TBLOCK_SUBINPROGRESS:
4542  case TBLOCK_SUBABORT:
4543  break;
4544 
4545  /* These cases are invalid. */
4546  case TBLOCK_DEFAULT:
4547  case TBLOCK_STARTED:
4548  case TBLOCK_BEGIN:
4551  case TBLOCK_SUBBEGIN:
4552  case TBLOCK_INPROGRESS:
4553  case TBLOCK_END:
4554  case TBLOCK_SUBRELEASE:
4555  case TBLOCK_SUBCOMMIT:
4556  case TBLOCK_ABORT:
4557  case TBLOCK_ABORT_END:
4558  case TBLOCK_SUBABORT_END:
4559  case TBLOCK_ABORT_PENDING:
4561  case TBLOCK_SUBRESTART:
4563  case TBLOCK_PREPARE:
4564  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4566  break;
4567  }
4568 
4569  /*
4570  * Abort the current subtransaction, if needed.
4571  */
4572  if (s->blockState == TBLOCK_SUBINPROGRESS)
4574 
4575  /* And clean it up, too */
4577 
4578  s = CurrentTransactionState; /* changed by pop */
4580  s->blockState == TBLOCK_INPROGRESS ||
4582  s->blockState == TBLOCK_STARTED);
4583 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
#define AssertState(condition)
Definition: c.h:807
TBlockState blockState
Definition: xact.c:193
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5410
static void AbortSubTransaction(void)
Definition: xact.c:4939
static void CleanupSubTransaction(void)
Definition: xact.c:5098
#define elog(elevel,...)
Definition: elog.h:232

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

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

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

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

2934 {
2938 }
bool XactDeferrable
Definition: xact.c:82
static bool save_XactDeferrable
Definition: xact.c:2930
bool XactReadOnly
Definition: xact.c:79
static bool save_XactReadOnly
Definition: xact.c:2929
int XactIsoLevel
Definition: xact.c:76
static int save_XactIsoLevel
Definition: xact.c:2928

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

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

5254 {
5255  TransactionState s;
5256  Size nxids = 0;
5257  Size i = 0;
5258  TransactionId *workspace;
5260 
5261  result = (SerializedTransactionState *) start_address;
5262 
5263  result->xactIsoLevel = XactIsoLevel;
5264  result->xactDeferrable = XactDeferrable;
5266  result->currentFullTransactionId =
5269 
5270  /*
5271  * If we're running in a parallel worker and launching a parallel worker
5272  * of our own, we can just pass along the information that was passed to
5273  * us.
5274  */
5275  if (nParallelCurrentXids > 0)
5276  {
5278  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5280  return;
5281  }
5282 
5283  /*
5284  * OK, we need to generate a sorted list of XIDs that our workers should
5285  * view as current. First, figure out how many there are.
5286  */
5287  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5288  {
5290  nxids = add_size(nxids, 1);
5291  nxids = add_size(nxids, s->nChildXids);
5292  }
5294  <= maxsize);
5295 
5296  /* Copy them to our scratch space. */
5297  workspace = palloc(nxids * sizeof(TransactionId));
5298  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5299  {
5301  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5302  memcpy(&workspace[i], s->childXids,
5303  s->nChildXids * sizeof(TransactionId));
5304  i += s->nChildXids;
5305  }
5306  Assert(i == nxids);
5307 
5308  /* Sort them. */
5309  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5310 
5311  /* Copy data into output area. */
5312  result->nParallelCurrentXids = nxids;
5313  memcpy(&result->parallelCurrentXids[0], workspace,
5314  nxids * sizeof(TransactionId));
5315 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:122
bool XactDeferrable
Definition: xact.c:82
static TransactionState CurrentTransactionState
Definition: xact.c:251
uint32 TransactionId
Definition: c.h:587
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:226
int nParallelCurrentXids
Definition: xact.c:123
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:198
CommandId currentCommandId
Definition: xact.c:224
struct TransactionStateData * parent
Definition: xact.c:209
FullTransactionId fullTransactionId
Definition: xact.c:188
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
TransactionId * ParallelCurrentXids
Definition: xact.c:124
#define Assert(condition)
Definition: c.h:804
#define SerializedTransactionStateHeaderSize
Definition: xact.c:230
size_t Size
Definition: c.h:540
int XactIsoLevel
Definition: xact.c:76
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define qsort(a, b, c, d)
Definition: port.h:505
static CommandId currentCommandId
Definition: xact.c:258
FullTransactionId topFullTransactionId
Definition: xact.c:222
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:136
FullTransactionId currentFullTransactionId
Definition: xact.c:223

◆ SetCurrentStatementStartTimestamp()

void SetCurrentStatementStartTimestamp ( void  )

Definition at line 834 of file xact.c.

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

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

835 {
836  if (!IsParallelWorker())
838  else
840 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
static TimestampTz stmtStartTimestamp
Definition: xact.c:269
#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 788 of file xact.c.

References Assert, IsParallelWorker, stmtStartTimestamp, and xactStartTimestamp.

Referenced by ParallelWorkerMain().

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

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

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

5324 {
5326 
5328  StartTransaction();
5329 
5330  tstate = (SerializedTransactionState *) tstatespace;
5331  XactIsoLevel = tstate->xactIsoLevel;
5332  XactDeferrable = tstate->xactDeferrable;
5335  tstate->currentFullTransactionId;
5339 
5341 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:122
bool XactDeferrable
Definition: xact.c:82
static TransactionState CurrentTransactionState
Definition: xact.c:251
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:226
int nParallelCurrentXids
Definition: xact.c:123
TBlockState blockState
Definition: xact.c:193
CommandId currentCommandId
Definition: xact.c:224
FullTransactionId fullTransactionId
Definition: xact.c:188
static void StartTransaction(void)
Definition: xact.c:1915
TransactionId * ParallelCurrentXids
Definition: xact.c:124
#define Assert(condition)
Definition: c.h:804
int XactIsoLevel
Definition: xact.c:76
static CommandId currentCommandId
Definition: xact.c:258
FullTransactionId topFullTransactionId
Definition: xact.c:222
FullTransactionId currentFullTransactionId
Definition: xact.c:223

◆ StartTransactionCommand()

void StartTransactionCommand ( void  )

Definition at line 2852 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(), begin_replication_step(), BeginInternalSubTransaction(), BootstrapModeMain(), cluster(), DefineIndex(), do_autovacuum(), exec_replication_command(), FetchTableStates(), 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(), ProcessIncomingNotify(), ReindexMultipleInternal(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), ReorderBufferProcessTXN(), SnapBuildExportSnapshot(), SPI_start_transaction(), start_xact_command(), vacuum(), and vacuum_rel().

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

◆ SubTransactionIsActive()

bool SubTransactionIsActive ( SubTransactionId  subxid)

Definition at line 738 of file xact.c.

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

Referenced by fmgr_sql().

739 {
741 
742  for (s = CurrentTransactionState; s != NULL; s = s->parent)
743  {
744  if (s->state == TRANS_ABORT)
745  continue;
746  if (s->subTransactionId == subxid)
747  return true;
748  }
749  return false;
750 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
TransState state
Definition: xact.c:192
struct TransactionStateData * parent
Definition: xact.c:209
SubTransactionId subTransactionId
Definition: xact.c:189

◆ TransactionBlockStatusCode()

char TransactionBlockStatusCode ( void  )

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

4730 {
4732 
4733  switch (s->blockState)
4734  {
4735  case TBLOCK_DEFAULT:
4736  case TBLOCK_STARTED:
4737  return 'I'; /* idle --- not in transaction */
4738  case TBLOCK_BEGIN:
4739  case TBLOCK_SUBBEGIN:
4740  case TBLOCK_INPROGRESS:
4743  case TBLOCK_SUBINPROGRESS:
4744  case TBLOCK_END:
4745  case TBLOCK_SUBRELEASE:
4746  case TBLOCK_SUBCOMMIT:
4747  case TBLOCK_PREPARE:
4748  return 'T'; /* in transaction */
4749  case TBLOCK_ABORT:
4750  case TBLOCK_SUBABORT:
4751  case TBLOCK_ABORT_END:
4752  case TBLOCK_SUBABORT_END:
4753  case TBLOCK_ABORT_PENDING:
4755  case TBLOCK_SUBRESTART:
4757  return 'E'; /* in failed transaction */
4758  }
4759 
4760  /* should never get here */
4761  elog(FATAL, "invalid transaction block state: %s",
4763  return 0; /* keep compiler quiet */
4764 }
static TransactionState CurrentTransactionState
Definition: xact.c:251
TBlockState blockState
Definition: xact.c:193
#define FATAL
Definition: elog.h:49
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5410
#define elog(elevel,...)
Definition: elog.h:232

◆ TransactionIdIsCurrentTransactionId()

bool TransactionIdIsCurrentTransactionId ( TransactionId  xid)

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

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

◆ UnregisterSubXactCallback()

void UnregisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3609 of file xact.c.

References SubXactCallbackItem::next, and pfree().

3610 {
3611  SubXactCallbackItem *item;
3612  SubXactCallbackItem *prev;
3613 
3614  prev = NULL;
3615  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3616  {
3617  if (item->callback == callback && item->arg == arg)
3618  {
3619  if (prev)
3620  prev->next = item->next;
3621  else
3622  SubXact_callbacks = item->next;
3623  pfree(item);
3624  break;
3625  }
3626  }
3627 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:315
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:310
void * arg

◆ UnregisterXactCallback()

void UnregisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3554 of file xact.c.

References XactCallbackItem::next, and pfree().

3555 {
3556  XactCallbackItem *item;
3557  XactCallbackItem *prev;
3558 
3559  prev = NULL;
3560  for (item = Xact_callbacks; item; prev = item, item = item->next)
3561  {
3562  if (item->callback == callback && item->arg == arg)
3563  {
3564  if (prev)
3565  prev->next = item->next;
3566  else
3567  Xact_callbacks = item->next;
3568  pfree(item);
3569  break;
3570  }
3571  }
3572 }
struct XactCallbackItem * next
Definition: xact.c:298
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:303
void * arg

◆ UserAbortTransactionBlock()

void UserAbortTransactionBlock ( bool  chain)

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

3932 {
3934 
3935  switch (s->blockState)
3936  {
3937  /*
3938  * We are inside a transaction block and we got a ROLLBACK command
3939  * from the user, so tell CommitTransactionCommand to abort and
3940  * exit the transaction block.
3941  */
3942  case TBLOCK_INPROGRESS:
3944  break;
3945 
3946  /*
3947  * We are inside a failed transaction block and we got a ROLLBACK
3948  * command from the user. Abort processing is already done, so
3949  * CommitTransactionCommand just has to cleanup and go back to
3950  * idle state.
3951  */
3952  case TBLOCK_ABORT:
3954  break;
3955 
3956  /*
3957  * We are inside a subtransaction. Mark everything up to top
3958  * level as exitable.
3959  */
3960  case TBLOCK_SUBINPROGRESS:
3961  case TBLOCK_SUBABORT:
3962  while (s->parent != NULL)
3963  {
3964  if (s->blockState == TBLOCK_SUBINPROGRESS)
3966  else if (s->blockState == TBLOCK_SUBABORT)
3968  else
3969  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3971  s = s->parent;
3972  }
3973  if (s->blockState == TBLOCK_INPROGRESS)
3975  else if (s->blockState == TBLOCK_ABORT)
3977  else
3978  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3980  break;
3981 
3982  /*
3983  * The user issued ABORT when not inside a transaction. For
3984  * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
3985  * The upcoming call to CommitTransactionCommand() will then put