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 297 of file xact.h.

Referenced by ParseAbortRecord(), and XactLogAbortRecord().

◆ MinSizeOfXactAssignment

#define MinSizeOfXactAssignment   offsetof(xl_xact_assignment, xsub)

Definition at line 202 of file xact.h.

Referenced by AssignTransactionId().

◆ MinSizeOfXactCommit

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

Definition at line 282 of file xact.h.

Referenced by ParseCommitRecord().

◆ MinSizeOfXactInvals

#define MinSizeOfXactInvals   offsetof(xl_xact_invals, msgs)

Definition at line 256 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 79 of file xact.h.

◆ XACT_COMPLETION_APPLY_FEEDBACK

#define XACT_COMPLETION_APPLY_FEEDBACK   (1U << 29)

Definition at line 183 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_FORCE_SYNC_COMMIT

#define XACT_COMPLETION_FORCE_SYNC_COMMIT   (1U << 31)

Definition at line 185 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_UPDATE_RELCACHE_FILE

#define XACT_COMPLETION_UPDATE_RELCACHE_FILE   (1U << 30)

Definition at line 184 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 172 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 169 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 188 of file xact.h.

Referenced by xact_redo_commit().

◆ XactCompletionForceSyncCommit

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

Definition at line 192 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 190 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 152 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 153 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 134 of file xact.h.

◆ XactCallback

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

Definition at line 124 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 348 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 126 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 flush 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 112 of file xact.h.

Function Documentation

◆ AbortCurrentTransaction()

void AbortCurrentTransaction ( void  )

Definition at line 3211 of file xact.c.

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

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

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

Definition at line 4580 of file xact.c.

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

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

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

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4040 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

Definition at line 4408 of file xact.c.

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

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

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

Definition at line 3638 of file xact.c.

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

Referenced by standard_ProcessUtility().

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

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1021 of file xact.c.

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

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

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

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2947 of file xact.c.

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

Referenced by _SPI_commit(), apply_handle_commit(), apply_handle_stream_abort(), apply_handle_stream_commit(), 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().

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

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

Definition at line 4087 of file xact.c.

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

Referenced by CommitTransactionCommand(), and standard_ProcessUtility().

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4065 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5331 of file xact.c.

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

Referenced by ParallelWorkerMain().

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

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

Definition at line 3758 of file xact.c.

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

Referenced by PrepareTransactionBlock(), and standard_ProcessUtility().

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

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 979 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

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

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5208 of file xact.c.

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

Referenced by InitializeParallelDSM().

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

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 992 of file xact.c.

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

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

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

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1071 of file xact.c.

References forceSyncCommit.

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

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

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 761 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

Referenced by ATRewriteTable(), CatalogTuplesMultiInsertWithInfo(), CopyFrom(), create_estate_for_relation(), GetSnapshotData(), 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:745
static CommandId currentCommandId
Definition: xact.c:257
static bool currentCommandIdUsed
Definition: xact.c:258

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 496 of file xact.c.

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

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

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 514 of file xact.c.

References TransactionStateData::fullTransactionId.

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

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 807 of file xact.c.

References stmtStartTimestamp.

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

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

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 455 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

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

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

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

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 467 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

Referenced by pg_current_xact_id().

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

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 483 of file xact.c.

References XactTopFullTransactionId.

Referenced by pg_current_xact_id_if_assigned().

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

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

Definition at line 410 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XidFromFullTransactionId.

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

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

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3493 of file xact.c.

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

Referenced by vacuum(), and vacuum_set_xid_limits().

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

◆ IsSubTransaction()

◆ IsSubTransactionAssignmentPending()

bool IsSubTransactionAssignmentPending ( void  )

Definition at line 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:4757
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4684 of file xact.c.

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

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

4685 {
4687 
4689  return false;
4690 
4691  return true;
4692 }
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(), 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:745

◆ 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:171
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:520
TransactionId xid
Definition: xact.h:260
#define MinSizeOfXactAbort
Definition: xact.h:297
#define XLOG_XACT_HAS_INFO
Definition: xact.h:160
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:167
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:249
TimestampTz xact_time
Definition: xact.h:286
Oid tsId
Definition: xact.h:234
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:247
#define MinSizeOfXactSubxacts
Definition: xact.h:242
struct RelFileNode RelFileNode
struct xl_xact_twophase xl_xact_twophase
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:166
XLogRecPtr origin_lsn
Definition: xact.h:265
TransactionId * subxacts
Definition: xact.h:359
uint32 xinfo
Definition: xact.h:228
XLogRecPtr origin_lsn
Definition: xact.h:367
RelFileNode * xnodes
Definition: xact.h:362
int nsubxacts
Definition: xact.h:239
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:170
Oid dbId
Definition: xact.h:233
TransactionId twophase_xid
Definition: xact.h:364
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz xact_time
Definition: xact.h:352
TimestampTz origin_timestamp
Definition: xact.h:266
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:168
TimestampTz origin_timestamp
Definition: xact.h:368
#define XACT_XINFO_HAS_GID
Definition: xact.h:173
char twophase_gid[GIDSIZE]
Definition: xact.h:365
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:240

◆ 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:334
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:171
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:520
TimestampTz origin_timestamp
Definition: xact.h:345
TransactionId xid
Definition: xact.h:260
TransactionId twophase_xid
Definition: xact.h:339
TransactionId * subxacts
Definition: xact.h:331
#define XLOG_XACT_HAS_INFO
Definition: xact.h:160
SharedInvalidationMessage * msgs
Definition: xact.h:337
XLogRecPtr origin_lsn
Definition: xact.h:344
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:167
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:249
Oid tsId
Definition: xact.h:234
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:247
#define MinSizeOfXactSubxacts
Definition: xact.h:242
#define MinSizeOfXactCommit
Definition: xact.h:282
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:254
struct RelFileNode RelFileNode
TimestampTz xact_time
Definition: xact.h:271
struct xl_xact_twophase xl_xact_twophase
#define MinSizeOfXactInvals
Definition: xact.h:256
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:166
XLogRecPtr origin_lsn
Definition: xact.h:265
uint32 xinfo
Definition: xact.h:228
int nsubxacts
Definition: xact.h:239
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:170
Oid dbId
Definition: xact.h:233
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz origin_timestamp
Definition: xact.h:266
#define XACT_XINFO_HAS_INVALS
Definition: xact.h:169
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:168
int nmsgs
Definition: xact.h:253
#define XACT_XINFO_HAS_GID
Definition: xact.h:173
char twophase_gid[GIDSIZE]
Definition: xact.h:340
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:240
TimestampTz xact_time
Definition: xact.h:324

◆ 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 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:334
int32 ninvalmsgs
Definition: xact.h:310
int32 nsubxacts
Definition: xact.h:307
uint32 TransactionId
Definition: c.h:520
int32 ncommitrels
Definition: xact.h:308
TimestampTz origin_timestamp
Definition: xact.h:345
TransactionId twophase_xid
Definition: xact.h:339
TransactionId * subxacts
Definition: xact.h:331
SharedInvalidationMessage * msgs
Definition: xact.h:337
XLogRecPtr origin_lsn
Definition: xact.h:344
uint16 gidlen
Definition: xact.h:312
Oid database
Definition: xact.h:304
int32 nabortrels
Definition: xact.h:309
XLogRecPtr origin_lsn
Definition: xact.h:313
RelFileNode * abortnodes
Definition: xact.h:342
TimestampTz origin_timestamp
Definition: xact.h:314
#define MAXALIGN(LEN)
Definition: c.h:698
TransactionId xid
Definition: xact.h:303
TimestampTz prepared_at
Definition: xact.h:305
char twophase_gid[GIDSIZE]
Definition: xact.h:340
TimestampTz xact_time
Definition: xact.h:324

◆ PrepareTransactionBlock()

bool PrepareTransactionBlock ( const char *  gid)

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

3707 {
3708  TransactionState s;
3709  bool result;
3710 
3711  /* Set up to commit the current transaction */
3712  result = EndTransactionBlock(false);
3713 
3714  /* If successful, change outer tblock state to PREPARE */
3715  if (result)
3716  {
3718 
3719  while (s->parent != NULL)
3720  s = s->parent;
3721 
3722  if (s->blockState == TBLOCK_END)
3723  {
3724  /* Save GID where PrepareTransaction can find it again */
3726 
3728  }
3729  else
3730  {
3731  /*
3732  * ignore case where we are not in a transaction;
3733  * EndTransactionBlock already issued a warning.
3734  */
3737  /* Don't send back a PREPARE result tag... */
3738  result = false;
3739  }
3740  }
3741 
3742  return result;
3743 }
static TransactionState CurrentTransactionState
Definition: xact.c:250
bool EndTransactionBlock(bool chain)
Definition: xact.c:3758
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:745
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 3380 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(), ProcessUtilitySlow(), ReindexPartitions(), standard_ProcessUtility(), and vacuum().

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

◆ RegisterSubXactCallback()

void RegisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

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

3584 {
3585  SubXactCallbackItem *item;
3586 
3587  item = (SubXactCallbackItem *)
3589  item->callback = callback;
3590  item->arg = arg;
3591  item->next = SubXact_callbacks;
3592  SubXact_callbacks = item;
3593 }
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 3528 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().

3529 {
3530  XactCallbackItem *item;
3531 
3532  item = (XactCallbackItem *)
3534  item->callback = callback;
3535  item->arg = arg;
3536  item->next = Xact_callbacks;
3537  Xact_callbacks = item;
3538 }
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 4479 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().

4480 {
4482 
4483  /*
4484  * Workers synchronize transaction state at the beginning of each parallel
4485  * operation, so we can't account for commit of subtransactions after that
4486  * point. This should not happen anyway. Code calling this would
4487  * typically have called BeginInternalSubTransaction() first, failing
4488  * there.
4489  */
4490  if (IsInParallelMode())
4491  ereport(ERROR,
4492  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4493  errmsg("cannot commit subtransactions during a parallel operation")));
4494 
4495  if (s->blockState != TBLOCK_SUBINPROGRESS)
4496  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4498  Assert(s->state == TRANS_INPROGRESS);
4501  s = CurrentTransactionState; /* changed by pop */
4502  Assert(s->state == TRANS_INPROGRESS);
4503 }
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:610
TBlockState blockState
Definition: xact.c:192
TransState state
Definition: xact.c:191
bool IsInParallelMode(void)
Definition: xact.c:1012
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4817
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5394
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ ReleaseSavepoint()

void ReleaseSavepoint ( const char *  name)

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

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

◆ RequireTransactionBlock()

void RequireTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3446 of file xact.c.

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

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

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2935 of file xact.c.

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

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

2936 {
2940 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2924
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2923
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2922

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

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

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

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

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

◆ SaveTransactionCharacteristics()

void SaveTransactionCharacteristics ( void  )

Definition at line 2927 of file xact.c.

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

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

2928 {
2932 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2924
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2923
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2922

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

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

5237 {
5238  TransactionState s;
5239  Size nxids = 0;
5240  Size i = 0;
5241  TransactionId *workspace;
5243 
5244  result = (SerializedTransactionState *) start_address;
5245 
5246  result->xactIsoLevel = XactIsoLevel;
5247  result->xactDeferrable = XactDeferrable;
5249  result->currentFullTransactionId =
5252 
5253  /*
5254  * If we're running in a parallel worker and launching a parallel worker
5255  * of our own, we can just pass along the information that was passed to
5256  * us.
5257  */
5258  if (nParallelCurrentXids > 0)
5259  {
5261  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5263  return;
5264  }
5265 
5266  /*
5267  * OK, we need to generate a sorted list of XIDs that our workers should
5268  * view as current. First, figure out how many there are.
5269  */
5270  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5271  {
5273  nxids = add_size(nxids, 1);
5274  nxids = add_size(nxids, s->nChildXids);
5275  }
5277  <= maxsize);
5278 
5279  /* Copy them to our scratch space. */
5280  workspace = palloc(nxids * sizeof(TransactionId));
5281  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5282  {
5284  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5285  memcpy(&workspace[i], s->childXids,
5286  s->nChildXids * sizeof(TransactionId));
5287  i += s->nChildXids;
5288  }
5289  Assert(i == nxids);
5290 
5291  /* Sort them. */
5292  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5293 
5294  /* Copy data into output area. */
5295  result->nParallelCurrentXids = nxids;
5296  memcpy(&result->parallelCurrentXids[0], workspace,
5297  nxids * sizeof(TransactionId));
5298 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:121
bool XactDeferrable
Definition: xact.c:81
static TransactionState CurrentTransactionState
Definition: xact.c:250
uint32 TransactionId
Definition: c.h:520
#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:498
TransactionId * ParallelCurrentXids
Definition: xact.c:123
#define Assert(condition)
Definition: c.h:745
#define SerializedTransactionStateHeaderSize
Definition: xact.c:229
size_t Size
Definition: c.h:473
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:475
static CommandId currentCommandId
Definition: xact.c:257
FullTransactionId topFullTransactionId
Definition: xact.c:221
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:139
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:745

◆ 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:745
static TimestampTz xactStartTimestamp
Definition: xact.c:267

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

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

5307 {
5309 
5311  StartTransaction();
5312 
5313  tstate = (SerializedTransactionState *) tstatespace;
5314  XactIsoLevel = tstate->xactIsoLevel;
5315  XactDeferrable = tstate->xactDeferrable;
5318  tstate->currentFullTransactionId;
5322 
5324 }
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:745
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 2846 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().

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

◆ 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 4716 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().

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

◆ 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(), 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:520
#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 3596 of file xact.c.

References SubXactCallbackItem::next, and pfree().

3597 {
3598  SubXactCallbackItem *item;
3599  SubXactCallbackItem *prev;
3600 
3601  prev = NULL;
3602  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3603  {
3604  if (item->callback == callback && item->arg == arg)
3605  {
3606  if (prev)
3607  prev->next = item->next;
3608  else
3609  SubXact_callbacks = item->next;
3610  pfree(item);
3611  break;
3612  }
3613  }
3614 }
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 3541 of file xact.c.

References XactCallbackItem::next, and pfree().

3542 {
3543  XactCallbackItem *item;
3544  XactCallbackItem *prev;
3545 
3546  prev = NULL;
3547  for (item = Xact_callbacks; item; prev = item, item = item->next)
3548  {
3549  if (item->callback == callback && item->arg == arg)
3550  {
3551  if (prev)
3552  prev->next = item->next;
3553  else
3554  Xact_callbacks = item->next;
3555  pfree(item);
3556  break;
3557  }
3558  }
3559 }
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 3918 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().

3919 {
3921 
3922  switch (s->blockState)
3923  {
3924  /*
3925  * We are inside a transaction block and we got a ROLLBACK command
3926  * from the user, so tell CommitTransactionCommand to abort and
3927  * exit the transaction block.
3928  */
3929  case TBLOCK_INPROGRESS:
3931  break;
3932 
3933  /*
3934  * We are inside a failed transaction block and we got a ROLLBACK
3935  * command from the user. Abort processing is already done, so
3936  * CommitTransactionCommand just has to cleanup and go back to
3937  * idle state.
3938  */
3939  case TBLOCK_ABORT:
3941  break;
3942 
3943  /*
3944  * We are inside a subtransaction. Mark everything up to top
3945  * level as exitable.
3946  */
3947  case TBLOCK_SUBINPROGRESS:
3948  case TBLOCK_SUBABORT:
3949  while (s->parent != NULL)
3950  {
3951  if (s->blockState == TBLOCK_SUBINPROGRESS)
3953  else if (s->blockState == TBLOCK_SUBABORT)
3955  else
3956  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3958  s = s->parent;
3959  }
3960  if (s->blockState == TBLOCK_INPROGRESS)
3962  else if (s->blockState == TBLOCK_ABORT)
3964  else
3965  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3967  break;
3968 
3969  /*
3970  * The user issued ABORT when not inside a transaction. For
3971  * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
3972  * The upcoming call to CommitTransactionCommand() will then put
3973  * us back into the default state. For ROLLBACK AND CHAIN, error.
3974  *
3975  * We do the same thing with ABORT inside an implicit transaction,
3976  * although in this case we might be rolling back actual database
3977  * state changes. (It's debatable whether we should issue a
3978  * WARNING in this case, but we have done so historically.)
3979  */
3980  case TBLOCK_STARTED:
3982  if (chain)
3983  ereport(ERROR,
3984  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3985  /* translator: %s represents an SQL statement name */
3986  errmsg("%s can only be used in transaction blocks",
3987  "ROLLBACK AND CHAIN")));
3988  else
3989  ereport(WARNING,
3990  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3991  errmsg("there is no transaction in progress")));
3993  break;
3994 
3995  /*
3996  * The user issued an ABORT that somehow ran inside a parallel
3997  * worker. We can't cope with that.
3998  */
4000  ereport(FATAL,
4001  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4002  errmsg("cannot abort during a parallel operation")));
4003  break;
4004 
4005  /* These cases are invalid. */
4006  case TBLOCK_DEFAULT:
4007  case TBLOCK_BEGIN:
4008  case TBLOCK_SUBBEGIN:
4009  case TBLOCK_END:
4010  case TBLOCK_SUBRELEASE:
4011  case TBLOCK_SUBCOMMIT:
4012  case TBLOCK_ABORT_END:
4013  case TBLOCK_SUBABORT_END:
4014  case TBLOCK_ABORT_PENDING:
4016  case TBLOCK_SUBRESTART:
4017  case