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

Go to the source code of this file.

Data Structures

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

Macros

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

Typedefs

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

Enumerations

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

Functions

bool IsTransactionState (void)
 
bool IsAbortedTransactionBlockState (void)
 
TransactionId GetTopTransactionId (void)
 
TransactionId GetTopTransactionIdIfAny (void)
 
TransactionId GetCurrentTransactionId (void)
 
TransactionId GetCurrentTransactionIdIfAny (void)
 
TransactionId GetStableLatestTransactionId (void)
 
SubTransactionId GetCurrentSubTransactionId (void)
 
FullTransactionId GetTopFullTransactionId (void)
 
FullTransactionId GetTopFullTransactionIdIfAny (void)
 
FullTransactionId GetCurrentFullTransactionId (void)
 
FullTransactionId GetCurrentFullTransactionIdIfAny (void)
 
void MarkCurrentTransactionIdLoggedIfAny (void)
 
bool SubTransactionIsActive (SubTransactionId subxid)
 
CommandId GetCurrentCommandId (bool used)
 
void SetParallelStartTimestamps (TimestampTz xact_ts, TimestampTz stmt_ts)
 
TimestampTz GetCurrentTransactionStartTimestamp (void)
 
TimestampTz GetCurrentStatementStartTimestamp (void)
 
TimestampTz GetCurrentTransactionStopTimestamp (void)
 
void SetCurrentStatementStartTimestamp (void)
 
int GetCurrentTransactionNestLevel (void)
 
bool TransactionIdIsCurrentTransactionId (TransactionId xid)
 
void CommandCounterIncrement (void)
 
void ForceSyncCommit (void)
 
void StartTransactionCommand (void)
 
void SaveTransactionCharacteristics (void)
 
void RestoreTransactionCharacteristics (void)
 
void CommitTransactionCommand (void)
 
void AbortCurrentTransaction (void)
 
void BeginTransactionBlock (void)
 
bool EndTransactionBlock (bool chain)
 
bool PrepareTransactionBlock (const char *gid)
 
void UserAbortTransactionBlock (bool chain)
 
void BeginImplicitTransactionBlock (void)
 
void EndImplicitTransactionBlock (void)
 
void ReleaseSavepoint (const char *name)
 
void DefineSavepoint (const char *name)
 
void RollbackToSavepoint (const char *name)
 
void BeginInternalSubTransaction (const char *name)
 
void ReleaseCurrentSubTransaction (void)
 
void RollbackAndReleaseCurrentSubTransaction (void)
 
bool IsSubTransaction (void)
 
Size EstimateTransactionStateSpace (void)
 
void SerializeTransactionState (Size maxsize, char *start_address)
 
void StartParallelWorkerTransaction (char *tstatespace)
 
void EndParallelWorkerTransaction (void)
 
bool IsTransactionBlock (void)
 
bool IsTransactionOrTransactionBlock (void)
 
char TransactionBlockStatusCode (void)
 
void AbortOutOfAnyTransaction (void)
 
void PreventInTransactionBlock (bool isTopLevel, const char *stmtType)
 
void RequireTransactionBlock (bool isTopLevel, const char *stmtType)
 
void WarnNoTransactionBlock (bool isTopLevel, const char *stmtType)
 
bool IsInTransactionBlock (bool isTopLevel)
 
void RegisterXactCallback (XactCallback callback, void *arg)
 
void UnregisterXactCallback (XactCallback callback, void *arg)
 
void RegisterSubXactCallback (SubXactCallback callback, void *arg)
 
void UnregisterSubXactCallback (SubXactCallback callback, void *arg)
 
bool IsSubTransactionAssignmentPending (void)
 
void MarkSubTransactionAssigned (void)
 
int xactGetCommittedChildren (TransactionId **ptr)
 
XLogRecPtr XactLogCommitRecord (TimestampTz commit_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int nmsgs, SharedInvalidationMessage *msgs, bool relcacheInval, int xactflags, TransactionId twophase_xid, const char *twophase_gid)
 
XLogRecPtr XactLogAbortRecord (TimestampTz abort_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int xactflags, TransactionId twophase_xid, const char *twophase_gid)
 
void xact_redo (XLogReaderState *record)
 
void xact_desc (StringInfo buf, XLogReaderState *record)
 
const char * xact_identify (uint8 info)
 
void ParseCommitRecord (uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
 
void ParseAbortRecord (uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
 
void ParsePrepareRecord (uint8 info, xl_xact_prepare *xlrec, xl_xact_parsed_prepare *parsed)
 
void EnterParallelMode (void)
 
void ExitParallelMode (void)
 
bool IsInParallelMode (void)
 

Variables

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

Macro Definition Documentation

◆ GIDSIZE

#define GIDSIZE   200

Definition at line 31 of file xact.h.

Referenced by MarkAsPreparing().

◆ IsolationIsSerializable

◆ IsolationUsesXactSnapshot

◆ MinSizeOfXactAbort

#define MinSizeOfXactAbort   sizeof(xl_xact_abort)

Definition at line 298 of file xact.h.

Referenced by ParseAbortRecord(), and XactLogAbortRecord().

◆ MinSizeOfXactAssignment

#define MinSizeOfXactAssignment   offsetof(xl_xact_assignment, xsub)

Definition at line 203 of file xact.h.

Referenced by AssignTransactionId().

◆ MinSizeOfXactCommit

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

Definition at line 283 of file xact.h.

Referenced by ParseCommitRecord().

◆ MinSizeOfXactInvals

#define MinSizeOfXactInvals   offsetof(xl_xact_invals, msgs)

Definition at line 257 of file xact.h.

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

◆ MinSizeOfXactRelfilenodes

#define MinSizeOfXactRelfilenodes   offsetof(xl_xact_relfilenodes, xnodes)

◆ MinSizeOfXactSubxacts

#define MinSizeOfXactSubxacts   offsetof(xl_xact_subxacts, subxacts)

◆ SYNCHRONOUS_COMMIT_ON

#define SYNCHRONOUS_COMMIT_ON   SYNCHRONOUS_COMMIT_REMOTE_FLUSH

Definition at line 80 of file xact.h.

◆ XACT_COMPLETION_APPLY_FEEDBACK

#define XACT_COMPLETION_APPLY_FEEDBACK   (1U << 29)

Definition at line 184 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_FORCE_SYNC_COMMIT

#define XACT_COMPLETION_FORCE_SYNC_COMMIT   (1U << 31)

Definition at line 186 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_UPDATE_RELCACHE_FILE

#define XACT_COMPLETION_UPDATE_RELCACHE_FILE   (1U << 30)

Definition at line 185 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_FLAGS_ACCESSEDTEMPNAMESPACE

◆ XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK

#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK   (1U << 1)

◆ XACT_READ_COMMITTED

#define XACT_READ_COMMITTED   1

Definition at line 37 of file xact.h.

Referenced by InitPostgres().

◆ XACT_READ_UNCOMMITTED

#define XACT_READ_UNCOMMITTED   0

Definition at line 36 of file xact.h.

◆ XACT_REPEATABLE_READ

#define XACT_REPEATABLE_READ   2

Definition at line 38 of file xact.h.

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

◆ XACT_SERIALIZABLE

#define XACT_SERIALIZABLE   3

Definition at line 39 of file xact.h.

Referenced by check_XactIsoLevel(), and ImportSnapshot().

◆ XACT_XINFO_HAS_AE_LOCKS

#define XACT_XINFO_HAS_AE_LOCKS   (1U << 6)

Definition at line 173 of file xact.h.

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

◆ XACT_XINFO_HAS_DBINFO

#define XACT_XINFO_HAS_DBINFO   (1U << 0)

◆ XACT_XINFO_HAS_GID

#define XACT_XINFO_HAS_GID   (1U << 7)

◆ XACT_XINFO_HAS_INVALS

#define XACT_XINFO_HAS_INVALS   (1U << 3)

Definition at line 170 of file xact.h.

Referenced by ParseCommitRecord(), and XactLogCommitRecord().

◆ XACT_XINFO_HAS_ORIGIN

#define XACT_XINFO_HAS_ORIGIN   (1U << 5)

◆ XACT_XINFO_HAS_RELFILENODES

#define XACT_XINFO_HAS_RELFILENODES   (1U << 2)

◆ XACT_XINFO_HAS_SUBXACTS

#define XACT_XINFO_HAS_SUBXACTS   (1U << 1)

◆ XACT_XINFO_HAS_TWOPHASE

#define XACT_XINFO_HAS_TWOPHASE   (1U << 4)

◆ XactCompletionApplyFeedback

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

Definition at line 189 of file xact.h.

Referenced by xact_redo_commit().

◆ XactCompletionForceSyncCommit

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

Definition at line 193 of file xact.h.

Referenced by xact_desc_commit(), and xact_redo_commit().

◆ XactCompletionRelcacheInitFileInval

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

Definition at line 191 of file xact.h.

Referenced by xact_desc_commit(), and xact_redo_commit().

◆ XLOG_XACT_ABORT

◆ XLOG_XACT_ABORT_PREPARED

#define XLOG_XACT_ABORT_PREPARED   0x40

◆ XLOG_XACT_ASSIGNMENT

#define XLOG_XACT_ASSIGNMENT   0x50

Definition at line 153 of file xact.h.

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

◆ XLOG_XACT_COMMIT

◆ XLOG_XACT_COMMIT_PREPARED

◆ XLOG_XACT_HAS_INFO

#define XLOG_XACT_HAS_INFO   0x80

◆ XLOG_XACT_INVALIDATIONS

#define XLOG_XACT_INVALIDATIONS   0x60

Definition at line 154 of file xact.h.

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

◆ XLOG_XACT_OPMASK

◆ XLOG_XACT_PREPARE

#define XLOG_XACT_PREPARE   0x10

Typedef Documentation

◆ SubXactCallback

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

Definition at line 135 of file xact.h.

◆ XactCallback

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

Definition at line 125 of file xact.h.

◆ xl_xact_abort

typedef struct xl_xact_abort xl_xact_abort

◆ xl_xact_assignment

◆ xl_xact_commit

◆ xl_xact_dbinfo

◆ xl_xact_invals

◆ xl_xact_origin

◆ xl_xact_parsed_abort

◆ xl_xact_parsed_commit

◆ xl_xact_parsed_prepare

Definition at line 349 of file xact.h.

◆ xl_xact_prepare

◆ xl_xact_relfilenodes

◆ xl_xact_subxacts

◆ xl_xact_twophase

◆ xl_xact_xinfo

typedef struct xl_xact_xinfo xl_xact_xinfo

Enumeration Type Documentation

◆ SubXactEvent

Enumerator
SUBXACT_EVENT_START_SUB 
SUBXACT_EVENT_COMMIT_SUB 
SUBXACT_EVENT_ABORT_SUB 
SUBXACT_EVENT_PRE_COMMIT_SUB 

Definition at line 127 of file xact.h.

◆ SyncCommitLevel

Enumerator
SYNCHRONOUS_COMMIT_OFF 
SYNCHRONOUS_COMMIT_LOCAL_FLUSH 
SYNCHRONOUS_COMMIT_REMOTE_WRITE 
SYNCHRONOUS_COMMIT_REMOTE_FLUSH 
SYNCHRONOUS_COMMIT_REMOTE_APPLY 

Definition at line 68 of file xact.h.

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

◆ XactEvent

enum XactEvent
Enumerator
XACT_EVENT_COMMIT 
XACT_EVENT_PARALLEL_COMMIT 
XACT_EVENT_ABORT 
XACT_EVENT_PARALLEL_ABORT 
XACT_EVENT_PREPARE 
XACT_EVENT_PRE_COMMIT 
XACT_EVENT_PARALLEL_PRE_COMMIT 
XACT_EVENT_PRE_PREPARE 

Definition at line 113 of file xact.h.

Function Documentation

◆ AbortCurrentTransaction()

void AbortCurrentTransaction ( void  )

Definition at line 3212 of file xact.c.

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

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

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

Definition at line 4581 of file xact.c.

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

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

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

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4041 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

Definition at line 4409 of file xact.c.

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

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

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

Definition at line 3639 of file xact.c.

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

Referenced by standard_ProcessUtility().

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

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1021 of file xact.c.

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

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

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

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2948 of file xact.c.

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

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

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

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

Definition at line 4088 of file xact.c.

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

Referenced by CommitTransactionCommand(), and standard_ProcessUtility().

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4066 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5332 of file xact.c.

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

Referenced by ParallelWorkerMain().

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

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

Definition at line 3759 of file xact.c.

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

Referenced by PrepareTransactionBlock(), and standard_ProcessUtility().

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

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 979 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

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

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5209 of file xact.c.

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

Referenced by InitializeParallelDSM().

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

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 992 of file xact.c.

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

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

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

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1071 of file xact.c.

References forceSyncCommit.

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

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

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 761 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

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

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

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 496 of file xact.c.

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

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

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 514 of file xact.c.

References TransactionStateData::fullTransactionId.

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

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 807 of file xact.c.

References stmtStartTimestamp.

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

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

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 455 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

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

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

Definition at line 798 of file xact.c.

References xactStartTimestamp.

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

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

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 819 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

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

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 541 of file xact.c.

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

Referenced by xid_age().

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

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 467 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

Referenced by pg_current_xact_id().

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

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 483 of file xact.c.

References XactTopFullTransactionId.

Referenced by pg_current_xact_id_if_assigned().

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

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

Definition at line 410 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XidFromFullTransactionId.

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

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

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3494 of file xact.c.

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

Referenced by vacuum().

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

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 6079 of file xact.c.

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

Referenced by MarkSubTransactionAssigned(), and XLogRecordAssemble().

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

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4685 of file xact.c.

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

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

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

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 371 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

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

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

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 525 of file xact.c.

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

Referenced by XLogInsertRecord().

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

◆ MarkSubTransactionAssigned()

void MarkSubTransactionAssigned ( void  )

Definition at line 6107 of file xact.c.

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

Referenced by XLogResetInsertion().

6108 {
6110 
6112 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
bool IsSubTransactionAssignmentPending(void)
Definition: xact.c:6079
#define Assert(condition)
Definition: c.h:792

◆ 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:575
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:575
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:575
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:745
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 3707 of file xact.c.

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

Referenced by standard_ProcessUtility().

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

◆ PreventInTransactionBlock()

void PreventInTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3381 of file xact.c.

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

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

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

◆ RegisterSubXactCallback()

void RegisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

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

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

◆ RegisterXactCallback()

void RegisterXactCallback ( XactCallback  callback,
void *  arg 
)

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

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

◆ ReleaseCurrentSubTransaction()

void ReleaseCurrentSubTransaction ( void  )

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

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

◆ ReleaseSavepoint()

void ReleaseSavepoint ( const char *  name)

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

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

◆ RequireTransactionBlock()

void RequireTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3447 of file xact.c.

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

3448 {
3449  CheckTransactionBlock(isTopLevel, true, stmtType);
3450 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3456

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2936 of file xact.c.

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

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

2937 {
2941 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2925
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2924
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2923

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

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

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

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

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

◆ SaveTransactionCharacteristics()

void SaveTransactionCharacteristics ( void  )

Definition at line 2928 of file xact.c.

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

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

2929 {
2933 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2925
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2924
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2923

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

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

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

◆ SetCurrentStatementStartTimestamp()

void SetCurrentStatementStartTimestamp ( void  )

Definition at line 833 of file xact.c.

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

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

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

◆ SetParallelStartTimestamps()

void SetParallelStartTimestamps ( TimestampTz  xact_ts,
TimestampTz  stmt_ts 
)

Definition at line 787 of file xact.c.

References Assert, IsParallelWorker, stmtStartTimestamp, and xactStartTimestamp.

Referenced by ParallelWorkerMain().

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

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

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

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

◆ StartTransactionCommand()

void StartTransactionCommand ( void  )

Definition at line 2847 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(), autoprewarm_database_main(), BeginInternalSubTransaction(), BootstrapModeMain(), cluster(), DefineIndex(), do_autovacuum(), ensure_transaction(), exec_replication_command(), get_database_list(), get_subscription_list(), IdentifySystem(), index_drop(), initialize_worker_spi(), InitPostgres(), LogicalRepSyncTableStart(), maybe_reread_subscription(), movedb(), ParallelWorkerMain(), perform_work_item(), pg_attribute_noreturn(), process_syncing_tables_for_apply(), ProcessCatchupInterrupt(), ProcessCompletedNotifies(), ProcessIncomingNotify(), ReindexMultipleInternal(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), ReorderBufferProcessTXN(), SnapBuildExportSnapshot(), SPI_start_transaction(), start_xact_command(), vacuum(), and vacuum_rel().

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

◆ SubTransactionIsActive()

bool SubTransactionIsActive ( SubTransactionId  subxid)

Definition at line 737 of file xact.c.

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

Referenced by fmgr_sql().

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

◆ TransactionBlockStatusCode()

char TransactionBlockStatusCode ( void  )

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

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

◆ TransactionIdIsCurrentTransactionId()

bool TransactionIdIsCurrentTransactionId ( TransactionId  xid)

Definition at line 869 of file xact.c.

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

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

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

◆ UnregisterSubXactCallback()

void UnregisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3597 of file xact.c.

References SubXactCallbackItem::next, and pfree().

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

◆ UnregisterXactCallback()

void UnregisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3542 of file xact.c.

References XactCallbackItem::next, and pfree().

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

◆ UserAbortTransactionBlock()

void UserAbortTransactionBlock ( bool  chain)

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

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