PostgreSQL Source Code  git master
xact.h File Reference
#include "access/transam.h"
#include "access/xlogreader.h"
#include "lib/stringinfo.h"
#include "nodes/pg_list.h"
#include "storage/relfilenode.h"
#include "storage/sinval.h"
#include "utils/datetime.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_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)
 
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
 
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 293 of file xact.h.

Referenced by ParseAbortRecord(), and XactLogAbortRecord().

◆ MinSizeOfXactAssignment

#define MinSizeOfXactAssignment   offsetof(xl_xact_assignment, xsub)

Definition at line 198 of file xact.h.

Referenced by AssignTransactionId().

◆ MinSizeOfXactCommit

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

Definition at line 278 of file xact.h.

Referenced by ParseCommitRecord().

◆ MinSizeOfXactInvals

#define MinSizeOfXactInvals   offsetof(xl_xact_invals, msgs)

Definition at line 252 of file xact.h.

Referenced by 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 179 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_FORCE_SYNC_COMMIT

#define XACT_COMPLETION_FORCE_SYNC_COMMIT   (1U << 31)

Definition at line 181 of file xact.h.

Referenced by XactLogCommitRecord().

◆ XACT_COMPLETION_UPDATE_RELCACHE_FILE

#define XACT_COMPLETION_UPDATE_RELCACHE_FILE   (1U << 30)

Definition at line 180 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 168 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 165 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 184 of file xact.h.

Referenced by xact_redo_commit().

◆ XactCompletionForceSyncCommit

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

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

Referenced by xact_desc_commit(), and xact_redo_commit().

◆ XLOG_XACT_ABORT

#define XLOG_XACT_ABORT   0x20

◆ XLOG_XACT_ABORT_PREPARED

#define XLOG_XACT_ABORT_PREPARED   0x40

◆ XLOG_XACT_ASSIGNMENT

#define XLOG_XACT_ASSIGNMENT   0x50

Definition at line 148 of file xact.h.

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

◆ XLOG_XACT_COMMIT

◆ XLOG_XACT_COMMIT_PREPARED

#define XLOG_XACT_COMMIT_PREPARED   0x30

◆ XLOG_XACT_HAS_INFO

#define XLOG_XACT_HAS_INFO   0x80

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

◆ XactCallback

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

Definition at line 120 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 344 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 122 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 108 of file xact.h.

Function Documentation

◆ AbortCurrentTransaction()

void AbortCurrentTransaction ( void  )

Definition at line 3183 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(), ReorderBufferCommit(), ReorderBufferImmediateInvalidation(), and SnapBuildClearExportedSnapshot().

3184 {
3186 
3187  switch (s->blockState)
3188  {
3189  case TBLOCK_DEFAULT:
3190  if (s->state == TRANS_DEFAULT)
3191  {
3192  /* we are idle, so nothing to do */
3193  }
3194  else
3195  {
3196  /*
3197  * We can get here after an error during transaction start
3198  * (state will be TRANS_START). Need to clean up the
3199  * incompletely started transaction. First, adjust the
3200  * low-level state to suppress warning message from
3201  * AbortTransaction.
3202  */
3203  if (s->state == TRANS_START)
3204  s->state = TRANS_INPROGRESS;
3205  AbortTransaction();
3207  }
3208  break;
3209 
3210  /*
3211  * If we aren't in a transaction block, we just do the basic abort
3212  * & cleanup transaction. For this purpose, we treat an implicit
3213  * transaction block as if it were a simple statement.
3214  */
3215  case TBLOCK_STARTED:
3217  AbortTransaction();
3220  break;
3221 
3222  /*
3223  * If we are in TBLOCK_BEGIN it means something screwed up right
3224  * after reading "BEGIN TRANSACTION". We assume that the user
3225  * will interpret the error as meaning the BEGIN failed to get him
3226  * into a transaction block, so we should abort and return to idle
3227  * state.
3228  */
3229  case TBLOCK_BEGIN:
3230  AbortTransaction();
3233  break;
3234 
3235  /*
3236  * We are somewhere in a transaction block and we've gotten a
3237  * failure, so we abort the transaction and set up the persistent
3238  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3239  */
3240  case TBLOCK_INPROGRESS:
3242  AbortTransaction();
3243  s->blockState = TBLOCK_ABORT;
3244  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3245  break;
3246 
3247  /*
3248  * Here, we failed while trying to COMMIT. Clean up the
3249  * transaction and return to idle state (we do not want to stay in
3250  * the transaction).
3251  */
3252  case TBLOCK_END:
3253  AbortTransaction();
3256  break;
3257 
3258  /*
3259  * Here, we are already in an aborted transaction state and are
3260  * waiting for a ROLLBACK, but for some reason we failed again! So
3261  * we just remain in the abort state.
3262  */
3263  case TBLOCK_ABORT:
3264  case TBLOCK_SUBABORT:
3265  break;
3266 
3267  /*
3268  * We are in a failed transaction and we got the ROLLBACK command.
3269  * We have already aborted, we just need to cleanup and go to idle
3270  * state.
3271  */
3272  case TBLOCK_ABORT_END:
3275  break;
3276 
3277  /*
3278  * We are in a live transaction and we got a ROLLBACK command.
3279  * Abort, cleanup, go to idle state.
3280  */
3281  case TBLOCK_ABORT_PENDING:
3282  AbortTransaction();
3285  break;
3286 
3287  /*
3288  * Here, we failed while trying to PREPARE. Clean up the
3289  * transaction and return to idle state (we do not want to stay in
3290  * the transaction).
3291  */
3292  case TBLOCK_PREPARE:
3293  AbortTransaction();
3296  break;
3297 
3298  /*
3299  * We got an error inside a subtransaction. Abort just the
3300  * subtransaction, and go to the persistent SUBABORT state until
3301  * we get ROLLBACK.
3302  */
3303  case TBLOCK_SUBINPROGRESS:
3306  break;
3307 
3308  /*
3309  * If we failed while trying to create a subtransaction, clean up
3310  * the broken subtransaction and abort the parent. The same
3311  * applies if we get a failure while ending a subtransaction.
3312  */
3313  case TBLOCK_SUBBEGIN:
3314  case TBLOCK_SUBRELEASE:
3315  case TBLOCK_SUBCOMMIT:
3317  case TBLOCK_SUBRESTART:
3321  break;
3322 
3323  /*
3324  * Same as above, except the Abort() was already done.
3325  */
3326  case TBLOCK_SUBABORT_END:
3330  break;
3331  }
3332 }
void AbortCurrentTransaction(void)
Definition: xact.c:3183
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
TransState state
Definition: xact.c:178
static void CleanupTransaction(void)
Definition: xact.c:2769
static void AbortSubTransaction(void)
Definition: xact.c:4898
static void AbortTransaction(void)
Definition: xact.c:2584
static void CleanupSubTransaction(void)
Definition: xact.c:5050

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4553 {
4555 
4556  /* Ensure we're not running in a doomed memory context */
4557  AtAbort_Memory();
4558 
4559  /*
4560  * Get out of any transaction or nested transaction
4561  */
4562  do
4563  {
4564  switch (s->blockState)
4565  {
4566  case TBLOCK_DEFAULT:
4567  if (s->state == TRANS_DEFAULT)
4568  {
4569  /* Not in a transaction, do nothing */
4570  }
4571  else
4572  {
4573  /*
4574  * We can get here after an error during transaction start
4575  * (state will be TRANS_START). Need to clean up the
4576  * incompletely started transaction. First, adjust the
4577  * low-level state to suppress warning message from
4578  * AbortTransaction.
4579  */
4580  if (s->state == TRANS_START)
4581  s->state = TRANS_INPROGRESS;
4582  AbortTransaction();
4584  }
4585  break;
4586  case TBLOCK_STARTED:
4587  case TBLOCK_BEGIN:
4588  case TBLOCK_INPROGRESS:
4591  case TBLOCK_END:
4592  case TBLOCK_ABORT_PENDING:
4593  case TBLOCK_PREPARE:
4594  /* In a transaction, so clean up */
4595  AbortTransaction();
4598  break;
4599  case TBLOCK_ABORT:
4600  case TBLOCK_ABORT_END:
4601 
4602  /*
4603  * AbortTransaction is already done, still need Cleanup.
4604  * However, if we failed partway through running ROLLBACK,
4605  * there will be an active portal running that command, which
4606  * we need to shut down before doing CleanupTransaction.
4607  */
4608  AtAbort_Portals();
4611  break;
4612 
4613  /*
4614  * In a subtransaction, so clean it up and abort parent too
4615  */
4616  case TBLOCK_SUBBEGIN:
4617  case TBLOCK_SUBINPROGRESS:
4618  case TBLOCK_SUBRELEASE:
4619  case TBLOCK_SUBCOMMIT:
4621  case TBLOCK_SUBRESTART:
4624  s = CurrentTransactionState; /* changed by pop */
4625  break;
4626 
4627  case TBLOCK_SUBABORT:
4628  case TBLOCK_SUBABORT_END:
4630  /* As above, but AbortSubTransaction already done */
4631  if (s->curTransactionOwner)
4632  {
4633  /* As in TBLOCK_ABORT, might have a live portal to zap */
4638  }
4640  s = CurrentTransactionState; /* changed by pop */
4641  break;
4642  }
4643  } while (s->blockState != TBLOCK_DEFAULT);
4644 
4645  /* Should be out of all subxacts now */
4646  Assert(s->parent == NULL);
4647 
4648  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4649  AtCleanup_Memory();
4650 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
static void AtCleanup_Memory(void)
Definition: xact.c:1818
void AtAbort_Portals(void)
Definition: portalmem.c:774
TBlockState blockState
Definition: xact.c:179
TransState state
Definition: xact.c:178
ResourceOwner curTransactionOwner
Definition: xact.c:183
static void CleanupTransaction(void)
Definition: xact.c:2769
struct TransactionStateData * parent
Definition: xact.c:194
SubTransactionId subTransactionId
Definition: xact.c:175
static void AbortSubTransaction(void)
Definition: xact.c:4898
static void AtAbort_Memory(void)
Definition: xact.c:1728
static void AbortTransaction(void)
Definition: xact.c:2584
#define Assert(condition)
Definition: c.h:738
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:970
static void CleanupSubTransaction(void)
Definition: xact.c:5050

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4012 of file xact.c.

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

Referenced by exec_simple_query().

4013 {
4015 
4016  /*
4017  * If we are in STARTED state (that is, no transaction block is open),
4018  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
4019  * block.
4020  *
4021  * For caller convenience, we consider all other transaction states as
4022  * legal here; otherwise the caller would need its own state check, which
4023  * seems rather pointless.
4024  */
4025  if (s->blockState == TBLOCK_STARTED)
4027 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

Definition at line 4380 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(), ReorderBufferCommit(), and ReorderBufferImmediateInvalidation().

4381 {
4383 
4384  /*
4385  * Workers synchronize transaction state at the beginning of each parallel
4386  * operation, so we can't account for new subtransactions after that
4387  * point. We might be able to make an exception for the type of
4388  * subtransaction established by this function, which is typically used in
4389  * contexts where we're going to release or roll back the subtransaction
4390  * before proceeding further, so that no enduring change to the
4391  * transaction state occurs. For now, however, we prohibit this case along
4392  * with all the others.
4393  */
4394  if (IsInParallelMode())
4395  ereport(ERROR,
4396  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4397  errmsg("cannot start subtransactions during a parallel operation")));
4398 
4399  switch (s->blockState)
4400  {
4401  case TBLOCK_STARTED:
4402  case TBLOCK_INPROGRESS:
4404  case TBLOCK_END:
4405  case TBLOCK_PREPARE:
4406  case TBLOCK_SUBINPROGRESS:
4407  /* Normal subtransaction start */
4408  PushTransaction();
4409  s = CurrentTransactionState; /* changed by push */
4410 
4411  /*
4412  * Savepoint names, like the TransactionState block itself, live
4413  * in TopTransactionContext.
4414  */
4415  if (name)
4417  break;
4418 
4419  /* These cases are invalid. */
4420  case TBLOCK_DEFAULT:
4421  case TBLOCK_BEGIN:
4423  case TBLOCK_SUBBEGIN:
4424  case TBLOCK_SUBRELEASE:
4425  case TBLOCK_SUBCOMMIT:
4426  case TBLOCK_ABORT:
4427  case TBLOCK_SUBABORT:
4428  case TBLOCK_ABORT_END:
4429  case TBLOCK_SUBABORT_END:
4430  case TBLOCK_ABORT_PENDING:
4432  case TBLOCK_SUBRESTART:
4434  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4436  break;
4437  }
4438 
4441 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
MemoryContext TopTransactionContext
Definition: mcxt.c:49
void CommitTransactionCommand(void)
Definition: xact.c:2919
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define ereport(elevel,...)
Definition: elog.h:144
static void PushTransaction(void)
Definition: xact.c:5083
void StartTransactionCommand(void)
Definition: xact.c:2818
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:214

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

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

3611 {
3613 
3614  switch (s->blockState)
3615  {
3616  /*
3617  * We are not inside a transaction block, so allow one to begin.
3618  */
3619  case TBLOCK_STARTED:
3620  s->blockState = TBLOCK_BEGIN;
3621  break;
3622 
3623  /*
3624  * BEGIN converts an implicit transaction block to a regular one.
3625  * (Note that we allow this even if we've already done some
3626  * commands, which is a bit odd but matches historical practice.)
3627  */
3629  s->blockState = TBLOCK_BEGIN;
3630  break;
3631 
3632  /*
3633  * Already a transaction block in progress.
3634  */
3635  case TBLOCK_INPROGRESS:
3637  case TBLOCK_SUBINPROGRESS:
3638  case TBLOCK_ABORT:
3639  case TBLOCK_SUBABORT:
3640  ereport(WARNING,
3641  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3642  errmsg("there is already a transaction in progress")));
3643  break;
3644 
3645  /* These cases are invalid. */
3646  case TBLOCK_DEFAULT:
3647  case TBLOCK_BEGIN:
3648  case TBLOCK_SUBBEGIN:
3649  case TBLOCK_END:
3650  case TBLOCK_SUBRELEASE:
3651  case TBLOCK_SUBCOMMIT:
3652  case TBLOCK_ABORT_END:
3653  case TBLOCK_SUBABORT_END:
3654  case TBLOCK_ABORT_PENDING:
3656  case TBLOCK_SUBRESTART:
3658  case TBLOCK_PREPARE:
3659  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3661  break;
3662  }
3663 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#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 1006 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(), 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(), 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().

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

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

Definition at line 2919 of file xact.c.

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

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

2920 {
2922 
2923  if (s->chain)
2925 
2926  switch (s->blockState)
2927  {
2928  /*
2929  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2930  * StartTransactionCommand didn't set the STARTED state
2931  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2932  * by EndParallelWorkerTransaction(), not this function.
2933  */
2934  case TBLOCK_DEFAULT:
2936  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2938  break;
2939 
2940  /*
2941  * If we aren't in a transaction block, just do our usual
2942  * transaction commit, and return to the idle state.
2943  */
2944  case TBLOCK_STARTED:
2947  break;
2948 
2949  /*
2950  * We are completing a "BEGIN TRANSACTION" command, so we change
2951  * to the "transaction block in progress" state and return. (We
2952  * assume the BEGIN did nothing to the database, so we need no
2953  * CommandCounterIncrement.)
2954  */
2955  case TBLOCK_BEGIN:
2957  break;
2958 
2959  /*
2960  * This is the case when we have finished executing a command
2961  * someplace within a transaction block. We increment the command
2962  * counter and return.
2963  */
2964  case TBLOCK_INPROGRESS:
2966  case TBLOCK_SUBINPROGRESS:
2968  break;
2969 
2970  /*
2971  * We are completing a "COMMIT" command. Do it and return to the
2972  * idle state.
2973  */
2974  case TBLOCK_END:
2977  if (s->chain)
2978  {
2979  StartTransaction();
2981  s->chain = false;
2983  }
2984  break;
2985 
2986  /*
2987  * Here we are in the middle of a transaction block but one of the
2988  * commands caused an abort so we do nothing but remain in the
2989  * abort state. Eventually we will get a ROLLBACK command.
2990  */
2991  case TBLOCK_ABORT:
2992  case TBLOCK_SUBABORT:
2993  break;
2994 
2995  /*
2996  * Here we were in an aborted transaction block and we just got
2997  * the ROLLBACK command from the user, so clean up the
2998  * already-aborted transaction and return to the idle state.
2999  */
3000  case TBLOCK_ABORT_END:
3003  if (s->chain)
3004  {
3005  StartTransaction();
3007  s->chain = false;
3009  }
3010  break;
3011 
3012  /*
3013  * Here we were in a perfectly good transaction block but the user
3014  * told us to ROLLBACK anyway. We have to abort the transaction
3015  * and then clean up.
3016  */
3017  case TBLOCK_ABORT_PENDING:
3018  AbortTransaction();
3021  if (s->chain)
3022  {
3023  StartTransaction();
3025  s->chain = false;
3027  }
3028  break;
3029 
3030  /*
3031  * We are completing a "PREPARE TRANSACTION" command. Do it and
3032  * return to the idle state.
3033  */
3034  case TBLOCK_PREPARE:
3037  break;
3038 
3039  /*
3040  * We were just issued a SAVEPOINT inside a transaction block.
3041  * Start a subtransaction. (DefineSavepoint already did
3042  * PushTransaction, so as to have someplace to put the SUBBEGIN
3043  * state.)
3044  */
3045  case TBLOCK_SUBBEGIN:
3048  break;
3049 
3050  /*
3051  * We were issued a RELEASE command, so we end the current
3052  * subtransaction and return to the parent transaction. The parent
3053  * might be ended too, so repeat till we find an INPROGRESS
3054  * transaction or subtransaction.
3055  */
3056  case TBLOCK_SUBRELEASE:
3057  do
3058  {
3060  s = CurrentTransactionState; /* changed by pop */
3061  } while (s->blockState == TBLOCK_SUBRELEASE);
3062 
3065  break;
3066 
3067  /*
3068  * We were issued a COMMIT, so we end the current subtransaction
3069  * hierarchy and perform final commit. We do this by rolling up
3070  * any subtransactions into their parent, which leads to O(N^2)
3071  * operations with respect to resource owners - this isn't that
3072  * bad until we approach a thousands of savepoints but is
3073  * necessary for correctness should after triggers create new
3074  * resource owners.
3075  */
3076  case TBLOCK_SUBCOMMIT:
3077  do
3078  {
3080  s = CurrentTransactionState; /* changed by pop */
3081  } while (s->blockState == TBLOCK_SUBCOMMIT);
3082  /* If we had a COMMIT command, finish off the main xact too */
3083  if (s->blockState == TBLOCK_END)
3084  {
3085  Assert(s->parent == NULL);
3088  }
3089  else if (s->blockState == TBLOCK_PREPARE)
3090  {
3091  Assert(s->parent == NULL);
3094  }
3095  else
3096  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3098  break;
3099 
3100  /*
3101  * The current already-failed subtransaction is ending due to a
3102  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3103  * examine the parent (which could be in any of several states).
3104  */
3105  case TBLOCK_SUBABORT_END:
3108  break;
3109 
3110  /*
3111  * As above, but it's not dead yet, so abort first.
3112  */
3117  break;
3118 
3119  /*
3120  * The current subtransaction is the target of a ROLLBACK TO
3121  * command. Abort and pop it, then start a new subtransaction
3122  * with the same name.
3123  */
3124  case TBLOCK_SUBRESTART:
3125  {
3126  char *name;
3127  int savepointLevel;
3128 
3129  /* save name and keep Cleanup from freeing it */
3130  name = s->name;
3131  s->name = NULL;
3132  savepointLevel = s->savepointLevel;
3133 
3136 
3137  DefineSavepoint(NULL);
3138  s = CurrentTransactionState; /* changed by push */
3139  s->name = name;
3140  s->savepointLevel = savepointLevel;
3141 
3142  /* This is the same as TBLOCK_SUBBEGIN case */
3146  }
3147  break;
3148 
3149  /*
3150  * Same as above, but the subtransaction had already failed, so we
3151  * don't need AbortSubTransaction.
3152  */
3154  {
3155  char *name;
3156  int savepointLevel;
3157 
3158  /* save name and keep Cleanup from freeing it */
3159  name = s->name;
3160  s->name = NULL;
3161  savepointLevel = s->savepointLevel;
3162 
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 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define AssertState(condition)
Definition: c.h:741
void CommitTransactionCommand(void)
Definition: xact.c:2919
void DefineSavepoint(const char *name)
Definition: xact.c:4059
TBlockState blockState
Definition: xact.c:179
static void PrepareTransaction(void)
Definition: xact.c:2296
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4789
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2769
struct TransactionStateData * parent
Definition: xact.c:194
void SaveTransactionCharacteristics(void)
Definition: xact.c:2899
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
static void AbortSubTransaction(void)
Definition: xact.c:4898
void CommandCounterIncrement(void)
Definition: xact.c:1006
static void AbortTransaction(void)
Definition: xact.c:2584
static void StartTransaction(void)
Definition: xact.c:1889
#define Assert(condition)
Definition: c.h:738
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2907
static void CleanupSubTransaction(void)
Definition: xact.c:5050
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:214
static void CommitTransaction(void)
Definition: xact.c:2048
static void StartSubTransaction(void)
Definition: xact.c:4752

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

4060 {
4062 
4063  /*
4064  * Workers synchronize transaction state at the beginning of each parallel
4065  * operation, so we can't account for new subtransactions after that
4066  * point. (Note that this check will certainly error out if s->blockState
4067  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4068  * below.)
4069  */
4070  if (IsInParallelMode())
4071  ereport(ERROR,
4072  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4073  errmsg("cannot define savepoints during a parallel operation")));
4074 
4075  switch (s->blockState)
4076  {
4077  case TBLOCK_INPROGRESS:
4078  case TBLOCK_SUBINPROGRESS:
4079  /* Normal subtransaction start */
4080  PushTransaction();
4081  s = CurrentTransactionState; /* changed by push */
4082 
4083  /*
4084  * Savepoint names, like the TransactionState block itself, live
4085  * in TopTransactionContext.
4086  */
4087  if (name)
4089  break;
4090 
4091  /*
4092  * We disallow savepoint commands in implicit transaction blocks.
4093  * There would be no great difficulty in allowing them so far as
4094  * this module is concerned, but a savepoint seems inconsistent
4095  * with exec_simple_query's behavior of abandoning the whole query
4096  * string upon error. Also, the point of an implicit transaction
4097  * block (as opposed to a regular one) is to automatically close
4098  * after an error, so it's hard to see how a savepoint would fit
4099  * into that.
4100  *
4101  * The error messages for this are phrased as if there were no
4102  * active transaction block at all, which is historical but
4103  * perhaps could be improved.
4104  */
4106  ereport(ERROR,
4107  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4108  /* translator: %s represents an SQL statement name */
4109  errmsg("%s can only be used in transaction blocks",
4110  "SAVEPOINT")));
4111  break;
4112 
4113  /* These cases are invalid. */
4114  case TBLOCK_DEFAULT:
4115  case TBLOCK_STARTED:
4116  case TBLOCK_BEGIN:
4118  case TBLOCK_SUBBEGIN:
4119  case TBLOCK_END:
4120  case TBLOCK_SUBRELEASE:
4121  case TBLOCK_SUBCOMMIT:
4122  case TBLOCK_ABORT:
4123  case TBLOCK_SUBABORT:
4124  case TBLOCK_ABORT_END:
4125  case TBLOCK_SUBABORT_END:
4126  case TBLOCK_ABORT_PENDING:
4128  case TBLOCK_SUBRESTART:
4130  case TBLOCK_PREPARE:
4131  elog(FATAL, "DefineSavepoint: unexpected state %s",
4133  break;
4134  }
4135 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
MemoryContext TopTransactionContext
Definition: mcxt.c:49
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define ereport(elevel,...)
Definition: elog.h:144
static void PushTransaction(void)
Definition: xact.c:5083
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:824
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:214

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4037 of file xact.c.

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

Referenced by exec_simple_query().

4038 {
4040 
4041  /*
4042  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4043  * allowing CommitTransactionCommand to commit whatever happened during
4044  * the implicit transaction block as though it were a single statement.
4045  *
4046  * For caller convenience, we consider all other transaction states as
4047  * legal here; otherwise the caller would need its own state check, which
4048  * seems rather pointless.
4049  */
4052 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5299 of file xact.c.

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

Referenced by ParallelWorkerMain().

5300 {
5304 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
#define Assert(condition)
Definition: c.h:738
static void CommitTransaction(void)
Definition: xact.c:2048

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

3731 {
3733  bool result = false;
3734 
3735  switch (s->blockState)
3736  {
3737  /*
3738  * We are in a transaction block, so tell CommitTransactionCommand
3739  * to COMMIT.
3740  */
3741  case TBLOCK_INPROGRESS:
3742  s->blockState = TBLOCK_END;
3743  result = true;
3744  break;
3745 
3746  /*
3747  * We are in an implicit transaction block. If AND CHAIN was
3748  * specified, error. Otherwise commit, but issue a warning
3749  * because there was no explicit BEGIN before this.
3750  */
3752  if (chain)
3753  ereport(ERROR,
3754  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3755  /* translator: %s represents an SQL statement name */
3756  errmsg("%s can only be used in transaction blocks",
3757  "COMMIT AND CHAIN")));
3758  else
3759  ereport(WARNING,
3760  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3761  errmsg("there is no transaction in progress")));
3762  s->blockState = TBLOCK_END;
3763  result = true;
3764  break;
3765 
3766  /*
3767  * We are in a failed transaction block. Tell
3768  * CommitTransactionCommand it's time to exit the block.
3769  */
3770  case TBLOCK_ABORT:
3772  break;
3773 
3774  /*
3775  * We are in a live subtransaction block. Set up to subcommit all
3776  * open subtransactions and then commit the main transaction.
3777  */
3778  case TBLOCK_SUBINPROGRESS:
3779  while (s->parent != NULL)
3780  {
3781  if (s->blockState == TBLOCK_SUBINPROGRESS)
3783  else
3784  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3786  s = s->parent;
3787  }
3788  if (s->blockState == TBLOCK_INPROGRESS)
3789  s->blockState = TBLOCK_END;
3790  else
3791  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3793  result = true;
3794  break;
3795 
3796  /*
3797  * Here we are inside an aborted subtransaction. Treat the COMMIT
3798  * as ROLLBACK: set up to abort everything and exit the main
3799  * transaction.
3800  */
3801  case TBLOCK_SUBABORT:
3802  while (s->parent != NULL)
3803  {
3804  if (s->blockState == TBLOCK_SUBINPROGRESS)
3806  else if (s->blockState == TBLOCK_SUBABORT)
3808  else
3809  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3811  s = s->parent;
3812  }
3813  if (s->blockState == TBLOCK_INPROGRESS)
3815  else if (s->blockState == TBLOCK_ABORT)
3817  else
3818  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3820  break;
3821 
3822  /*
3823  * The user issued COMMIT when not inside a transaction. For
3824  * COMMIT without CHAIN, issue a WARNING, staying in
3825  * TBLOCK_STARTED state. The upcoming call to
3826  * CommitTransactionCommand() will then close the transaction and
3827  * put us back into the default state. For COMMIT AND CHAIN,
3828  * error.
3829  */
3830  case TBLOCK_STARTED:
3831  if (chain)
3832  ereport(ERROR,
3833  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3834  /* translator: %s represents an SQL statement name */
3835  errmsg("%s can only be used in transaction blocks",
3836  "COMMIT AND CHAIN")));
3837  else
3838  ereport(WARNING,
3839  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3840  errmsg("there is no transaction in progress")));
3841  result = true;
3842  break;
3843 
3844  /*
3845  * The user issued a COMMIT that somehow ran inside a parallel
3846  * worker. We can't cope with that.
3847  */
3849  ereport(FATAL,
3850  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3851  errmsg("cannot commit during a parallel operation")));
3852  break;
3853 
3854  /* These cases are invalid. */
3855  case TBLOCK_DEFAULT:
3856  case TBLOCK_BEGIN:
3857  case TBLOCK_SUBBEGIN:
3858  case TBLOCK_END:
3859  case TBLOCK_SUBRELEASE:
3860  case TBLOCK_SUBCOMMIT:
3861  case TBLOCK_ABORT_END:
3862  case TBLOCK_SUBABORT_END:
3863  case TBLOCK_ABORT_PENDING:
3865  case TBLOCK_SUBRESTART:
3867  case TBLOCK_PREPARE:
3868  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3870  break;
3871  }
3872 
3874  s->blockState == TBLOCK_END ||
3875  s->blockState == TBLOCK_ABORT_END ||
3877 
3878  s->chain = chain;
3879 
3880  return result;
3881 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 964 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

965 {
967 
968  Assert(s->parallelModeLevel >= 0);
969 
970  ++s->parallelModeLevel;
971 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:738

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5176 of file xact.c.

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

Referenced by InitializeParallelDSM().

5177 {
5178  TransactionState s;
5179  Size nxids = 0;
5181 
5182  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5183  {
5185  nxids = add_size(nxids, 1);
5186  nxids = add_size(nxids, s->nChildXids);
5187  }
5188 
5189  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5190 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
uint32 TransactionId
Definition: c.h:513
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
struct TransactionStateData * parent
Definition: xact.c:194
FullTransactionId fullTransactionId
Definition: xact.c:174
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:215
size_t Size
Definition: c.h:466

◆ ExitParallelMode()

void ExitParallelMode ( void  )

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

978 {
980 
981  Assert(s->parallelModeLevel > 0);
983 
984  --s->parallelModeLevel;
985 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
bool ParallelContextActive(void)
Definition: parallel.c:978
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:738

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1056 of file xact.c.

References forceSyncCommit.

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

1057 {
1058  forceSyncCommit = true;
1059 }
static bool forceSyncCommit
Definition: xact.c:265

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 746 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

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

747 {
748  /* this is global to a transaction, not subtransaction-local */
749  if (used)
750  {
751  /*
752  * Forbid setting currentCommandIdUsed in a parallel worker, because
753  * we have no provision for communicating this back to the leader. We
754  * could relax this restriction when currentCommandIdUsed was already
755  * true at the start of the parallel operation.
756  */
758  currentCommandIdUsed = true;
759  }
760  return currentCommandId;
761 }
#define IsParallelWorker()
Definition: parallel.h:61
#define Assert(condition)
Definition: c.h:738
static CommandId currentCommandId
Definition: xact.c:242
static bool currentCommandIdUsed
Definition: xact.c:243

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 481 of file xact.c.

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

482 {
484 
487  return s->fullTransactionId;
488 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
FullTransactionId fullTransactionId
Definition: xact.c:174
static void AssignTransactionId(TransactionState s)
Definition: xact.c:554

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 499 of file xact.c.

References TransactionStateData::fullTransactionId.

500 {
502 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
FullTransactionId fullTransactionId
Definition: xact.c:174

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 792 of file xact.c.

References stmtStartTimestamp.

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

793 {
794  return stmtStartTimestamp;
795 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:253

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 440 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

Referenced by RecordTransactionAbort(), ReorderBufferCommit(), and XLogRecordAssemble().

441 {
443 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define XidFromFullTransactionId(x)
Definition: transam.h:48
FullTransactionId fullTransactionId
Definition: xact.c:174

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 804 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

805 {
806  if (xactStopTimestamp != 0)
807  return xactStopTimestamp;
808  return GetCurrentTimestamp();
809 }
static TimestampTz xactStopTimestamp
Definition: xact.c:254
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 526 of file xact.c.

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

Referenced by xid_age().

527 {
529  static TransactionId stablexid = InvalidTransactionId;
530 
531  if (lxid != MyProc->lxid)
532  {
533  lxid = MyProc->lxid;
534  stablexid = GetTopTransactionIdIfAny();
535  if (!TransactionIdIsValid(stablexid))
536  stablexid = ReadNewTransactionId();
537  }
538 
539  Assert(TransactionIdIsValid(stablexid));
540 
541  return stablexid;
542 }
uint32 TransactionId
Definition: c.h:513
PGPROC * MyProc
Definition: proc.c:67
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:515
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:410
#define Assert(condition)
Definition: c.h:738
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:258
#define InvalidLocalTransactionId
Definition: lock.h:68
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:113

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 452 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

Referenced by pg_current_xact_id().

453 {
457 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
static TransactionStateData TopTransactionStateData
Definition: xact.c:223
static void AssignTransactionId(TransactionState s)
Definition: xact.c:554

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 468 of file xact.c.

References XactTopFullTransactionId.

Referenced by pg_current_xact_id_if_assigned().

469 {
471 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

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

396 {
400 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static TransactionStateData TopTransactionStateData
Definition: xact.c:223
static void AssignTransactionId(TransactionState s)
Definition: xact.c:554

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3465 of file xact.c.

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

Referenced by vacuum().

3466 {
3467  /*
3468  * Return true on same conditions that would make
3469  * PreventInTransactionBlock error out
3470  */
3471  if (IsTransactionBlock())
3472  return true;
3473 
3474  if (IsSubTransaction())
3475  return true;
3476 
3477  if (!isTopLevel)
3478  return true;
3479 
3482  return true;
3483 
3484  return false;
3485 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
bool IsTransactionBlock(void)
Definition: xact.c:4656
bool IsSubTransaction(void)
Definition: xact.c:4729

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

Definition at line 4656 of file xact.c.

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

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

4657 {
4659 
4661  return false;
4662 
4663  return true;
4664 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 356 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

Referenced by apply_handle_commit(), apply_handle_origin(), 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(), 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().

357 {
359 
360  /*
361  * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states. However, we
362  * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
363  * TRANS_PREPARE since it might be too soon or too late within those
364  * transition states to do anything interesting. Hence, the only "valid"
365  * state is TRANS_INPROGRESS.
366  */
367  return (s->state == TRANS_INPROGRESS);
368 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TransState state
Definition: xact.c:178

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 510 of file xact.c.

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

Referenced by XLogInsertRecord().

511 {
514 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
FullTransactionId fullTransactionId
Definition: xact.c:174

◆ 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:167
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:513
TransactionId xid
Definition: xact.h:256
#define MinSizeOfXactAbort
Definition: xact.h:293
#define XLOG_XACT_HAS_INFO
Definition: xact.h:156
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:163
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:245
TimestampTz xact_time
Definition: xact.h:282
Oid tsId
Definition: xact.h:230
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:243
#define MinSizeOfXactSubxacts
Definition: xact.h:238
struct RelFileNode RelFileNode
struct xl_xact_twophase xl_xact_twophase
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:162
XLogRecPtr origin_lsn
Definition: xact.h:261
TransactionId * subxacts
Definition: xact.h:355
uint32 xinfo
Definition: xact.h:224
XLogRecPtr origin_lsn
Definition: xact.h:363
RelFileNode * xnodes
Definition: xact.h:358
int nsubxacts
Definition: xact.h:235
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:166
Oid dbId
Definition: xact.h:229
TransactionId twophase_xid
Definition: xact.h:360
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz xact_time
Definition: xact.h:348
TimestampTz origin_timestamp
Definition: xact.h:262
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:164
TimestampTz origin_timestamp
Definition: xact.h:364
#define XACT_XINFO_HAS_GID
Definition: xact.h:169
char twophase_gid[GIDSIZE]
Definition: xact.h:361
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:236

◆ 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:330
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:167
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:513
TimestampTz origin_timestamp
Definition: xact.h:341
TransactionId xid
Definition: xact.h:256
TransactionId twophase_xid
Definition: xact.h:335
TransactionId * subxacts
Definition: xact.h:327
#define XLOG_XACT_HAS_INFO
Definition: xact.h:156
SharedInvalidationMessage * msgs
Definition: xact.h:333
XLogRecPtr origin_lsn
Definition: xact.h:340
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:163
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:245
Oid tsId
Definition: xact.h:230
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:243
#define MinSizeOfXactSubxacts
Definition: xact.h:238
#define MinSizeOfXactCommit
Definition: xact.h:278
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:250
struct RelFileNode RelFileNode
TimestampTz xact_time
Definition: xact.h:267
struct xl_xact_twophase xl_xact_twophase
#define MinSizeOfXactInvals
Definition: xact.h:252
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:162
XLogRecPtr origin_lsn
Definition: xact.h:261
uint32 xinfo
Definition: xact.h:224
int nsubxacts
Definition: xact.h:235
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:166
Oid dbId
Definition: xact.h:229
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz origin_timestamp
Definition: xact.h:262
#define XACT_XINFO_HAS_INVALS
Definition: xact.h:165
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:164
int nmsgs
Definition: xact.h:249
#define XACT_XINFO_HAS_GID
Definition: xact.h:169
char twophase_gid[GIDSIZE]
Definition: xact.h:336
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:236
TimestampTz xact_time
Definition: xact.h:320

◆ 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:330
int32 ninvalmsgs
Definition: xact.h:306
int32 nsubxacts
Definition: xact.h:303
uint32 TransactionId
Definition: c.h:513
int32 ncommitrels
Definition: xact.h:304
TimestampTz origin_timestamp
Definition: xact.h:341
TransactionId twophase_xid
Definition: xact.h:335
TransactionId * subxacts
Definition: xact.h:327
SharedInvalidationMessage * msgs
Definition: xact.h:333
XLogRecPtr origin_lsn
Definition: xact.h:340
uint16 gidlen
Definition: xact.h:308
Oid database
Definition: xact.h:300
int32 nabortrels
Definition: xact.h:305
XLogRecPtr origin_lsn
Definition: xact.h:309
RelFileNode * abortnodes
Definition: xact.h:338
TimestampTz origin_timestamp
Definition: xact.h:310
#define MAXALIGN(LEN)
Definition: c.h:691
TransactionId xid
Definition: xact.h:299
TimestampTz prepared_at
Definition: xact.h:301
char twophase_gid[GIDSIZE]
Definition: xact.h:336
TimestampTz xact_time
Definition: xact.h:320

◆ PrepareTransactionBlock()

bool PrepareTransactionBlock ( const char *  gid)

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

3679 {
3680  TransactionState s;
3681  bool result;
3682 
3683  /* Set up to commit the current transaction */
3684  result = EndTransactionBlock(false);
3685 
3686  /* If successful, change outer tblock state to PREPARE */
3687  if (result)
3688  {
3690 
3691  while (s->parent != NULL)
3692  s = s->parent;
3693 
3694  if (s->blockState == TBLOCK_END)
3695  {
3696  /* Save GID where PrepareTransaction can find it again */
3698 
3700  }
3701  else
3702  {
3703  /*
3704  * ignore case where we are not in a transaction;
3705  * EndTransactionBlock already issued a warning.
3706  */
3709  /* Don't send back a PREPARE result tag... */
3710  result = false;
3711  }
3712  }
3713 
3714  return result;
3715 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
bool EndTransactionBlock(bool chain)
Definition: xact.c:3730
MemoryContext TopTransactionContext
Definition: mcxt.c:49
TBlockState blockState
Definition: xact.c:179
struct TransactionStateData * parent
Definition: xact.c:194
#define Assert(condition)
Definition: c.h:738
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
static char * prepareGID
Definition: xact.c:260

◆ PreventInTransactionBlock()

void PreventInTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3352 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(), standard_ProcessUtility(), and vacuum().

3353 {
3354  /*
3355  * xact block already started?
3356  */
3357  if (IsTransactionBlock())
3358  ereport(ERROR,
3359  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3360  /* translator: %s represents an SQL statement name */
3361  errmsg("%s cannot run inside a transaction block",
3362  stmtType)));
3363 
3364  /*
3365  * subtransaction?
3366  */
3367  if (IsSubTransaction())
3368  ereport(ERROR,
3369  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3370  /* translator: %s represents an SQL statement name */
3371  errmsg("%s cannot run inside a subtransaction",
3372  stmtType)));
3373 
3374  /*
3375  * inside a function call?
3376  */
3377  if (!isTopLevel)
3378  ereport(ERROR,
3379  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3380  /* translator: %s represents an SQL statement name */
3381  errmsg("%s cannot be executed from a function", stmtType)));
3382 
3383  /* If we got past IsTransactionBlock test, should be in default state */
3386  elog(FATAL, "cannot prevent transaction chain");
3387  /* all okay */
3388 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
bool IsTransactionBlock(void)
Definition: xact.c:4656
#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:4729
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 3555 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().

3556 {
3557  SubXactCallbackItem *item;
3558 
3559  item = (SubXactCallbackItem *)
3561  item->callback = callback;
3562  item->arg = arg;
3563  item->next = SubXact_callbacks;
3564  SubXact_callbacks = item;
3565 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:299
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:294
SubXactCallback callback
Definition: xact.c:295
MemoryContext TopMemoryContext
Definition: mcxt.c:44
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
void * arg

◆ RegisterXactCallback()

void RegisterXactCallback ( XactCallback  callback,
void *  arg 
)

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

3501 {
3502  XactCallbackItem *item;
3503 
3504  item = (XactCallbackItem *)
3506  item->callback = callback;
3507  item->arg = arg;
3508  item->next = Xact_callbacks;
3509  Xact_callbacks = item;
3510 }
struct XactCallbackItem * next
Definition: xact.c:282
void * arg
Definition: xact.c:284
XactCallback callback
Definition: xact.c:283
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:287
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
void * arg

◆ ReleaseCurrentSubTransaction()

void ReleaseCurrentSubTransaction ( void  )

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

4452 {
4454 
4455  /*
4456  * Workers synchronize transaction state at the beginning of each parallel
4457  * operation, so we can't account for commit of subtransactions after that
4458  * point. This should not happen anyway. Code calling this would
4459  * typically have called BeginInternalSubTransaction() first, failing
4460  * there.
4461  */
4462  if (IsInParallelMode())
4463  ereport(ERROR,
4464  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4465  errmsg("cannot commit subtransactions during a parallel operation")));
4466 
4467  if (s->blockState != TBLOCK_SUBINPROGRESS)
4468  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4470  Assert(s->state == TRANS_INPROGRESS);
4473  s = CurrentTransactionState; /* changed by pop */
4474  Assert(s->state == TRANS_INPROGRESS);
4475 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
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:179
TransState state
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4789
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
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 4144 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().

4145 {
4147  TransactionState target,
4148  xact;
4149 
4150  /*
4151  * Workers synchronize transaction state at the beginning of each parallel
4152  * operation, so we can't account for transaction state change after that
4153  * point. (Note that this check will certainly error out if s->blockState
4154  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4155  * below.)
4156  */
4157  if (IsInParallelMode())
4158  ereport(ERROR,
4159  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4160  errmsg("cannot release savepoints during a parallel operation")));
4161 
4162  switch (s->blockState)
4163  {
4164  /*
4165  * We can't release a savepoint if there is no savepoint defined.
4166  */
4167  case TBLOCK_INPROGRESS:
4168  ereport(ERROR,
4169  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4170  errmsg("savepoint \"%s\" does not exist", name)));
4171  break;
4172 
4174  /* See comment about implicit transactions in DefineSavepoint */
4175  ereport(ERROR,
4176  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4177  /* translator: %s represents an SQL statement name */
4178  errmsg("%s can only be used in transaction blocks",
4179  "RELEASE SAVEPOINT")));
4180  break;
4181 
4182  /*
4183  * We are in a non-aborted subtransaction. This is the only valid
4184  * case.
4185  */
4186  case TBLOCK_SUBINPROGRESS:
4187  break;
4188 
4189  /* These cases are invalid. */
4190  case TBLOCK_DEFAULT:
4191  case TBLOCK_STARTED:
4192  case TBLOCK_BEGIN:
4194  case TBLOCK_SUBBEGIN:
4195  case TBLOCK_END:
4196  case TBLOCK_SUBRELEASE:
4197  case TBLOCK_SUBCOMMIT:
4198  case TBLOCK_ABORT:
4199  case TBLOCK_SUBABORT:
4200  case TBLOCK_ABORT_END:
4201  case TBLOCK_SUBABORT_END:
4202  case TBLOCK_ABORT_PENDING:
4204  case TBLOCK_SUBRESTART:
4206  case TBLOCK_PREPARE:
4207  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
4209  break;
4210  }
4211 
4212  for (target = s; PointerIsValid(target); target = target->parent)
4213  {
4214  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4215  break;
4216  }
4217 
4218  if (!PointerIsValid(target))
4219  ereport(ERROR,
4220  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4221  errmsg("savepoint \"%s\" does not exist", name)));
4222 
4223  /* disallow crossing savepoint level boundaries */
4224  if (target->savepointLevel != s->savepointLevel)
4225  ereport(ERROR,
4226  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4227  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4228 
4229  /*
4230  * Mark "commit pending" all subtransactions up to the target
4231  * subtransaction. The actual commits will happen when control gets to
4232  * CommitTransactionCommand.
4233  */
4234  xact = CurrentTransactionState;
4235  for (;;)
4236  {
4238  xact->blockState = TBLOCK_SUBRELEASE;
4239  if (xact == target)
4240  break;
4241  xact = xact->parent;
4242  Assert(PointerIsValid(xact));
4243  }
4244 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
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:632

◆ RequireTransactionBlock()

void RequireTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3418 of file xact.c.

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

3419 {
3420  CheckTransactionBlock(isTopLevel, true, stmtType);
3421 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3427

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2907 of file xact.c.

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

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

2908 {
2912 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2896
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2895
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2894

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

Definition at line 4485 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(), ReorderBufferCommit(), and ReorderBufferImmediateInvalidation().

4486 {
4488 
4489  /*
4490  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4491  * during parallel operations. That's because we may be in the leader,
4492  * recovering from an error thrown while we were in parallel mode. We
4493  * won't reach here in a worker, because BeginInternalSubTransaction()
4494  * will have failed.
4495  */
4496 
4497  switch (s->blockState)
4498  {
4499  /* Must be in a subtransaction */
4500  case TBLOCK_SUBINPROGRESS:
4501  case TBLOCK_SUBABORT:
4502  break;
4503 
4504  /* These cases are invalid. */
4505  case TBLOCK_DEFAULT:
4506  case TBLOCK_STARTED:
4507  case TBLOCK_BEGIN:
4510  case TBLOCK_SUBBEGIN:
4511  case TBLOCK_INPROGRESS:
4512  case TBLOCK_END:
4513  case TBLOCK_SUBRELEASE:
4514  case TBLOCK_SUBCOMMIT:
4515  case TBLOCK_ABORT:
4516  case TBLOCK_ABORT_END:
4517  case TBLOCK_SUBABORT_END:
4518  case TBLOCK_ABORT_PENDING:
4520  case TBLOCK_SUBRESTART:
4522  case TBLOCK_PREPARE:
4523  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4525  break;
4526  }
4527 
4528  /*
4529  * Abort the current subtransaction, if needed.
4530  */
4531  if (s->blockState == TBLOCK_SUBINPROGRESS)
4533 
4534  /* And clean it up, too */
4536 
4537  s = CurrentTransactionState; /* changed by pop */
4539  s->blockState == TBLOCK_INPROGRESS ||
4541  s->blockState == TBLOCK_STARTED);
4542 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define AssertState(condition)
Definition: c.h:741
TBlockState blockState
Definition: xact.c:179
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
static void AbortSubTransaction(void)
Definition: xact.c:4898
static void CleanupSubTransaction(void)
Definition: xact.c:5050
#define elog(elevel,...)
Definition: elog.h:214

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

4254 {
4256  TransactionState target,
4257  xact;
4258 
4259  /*
4260  * Workers synchronize transaction state at the beginning of each parallel
4261  * operation, so we can't account for transaction state change after that
4262  * point. (Note that this check will certainly error out if s->blockState
4263  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4264  * below.)
4265  */
4266  if (IsInParallelMode())
4267  ereport(ERROR,
4268  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4269  errmsg("cannot rollback to savepoints during a parallel operation")));
4270 
4271  switch (s->blockState)
4272  {
4273  /*
4274  * We can't rollback to a savepoint if there is no savepoint
4275  * defined.
4276  */
4277  case TBLOCK_INPROGRESS:
4278  case TBLOCK_ABORT:
4279  ereport(ERROR,
4280  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4281  errmsg("savepoint \"%s\" does not exist", name)));
4282  break;
4283 
4285  /* See comment about implicit transactions in DefineSavepoint */
4286  ereport(ERROR,
4287  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4288  /* translator: %s represents an SQL statement name */
4289  errmsg("%s can only be used in transaction blocks",
4290  "ROLLBACK TO SAVEPOINT")));
4291  break;
4292 
4293  /*
4294  * There is at least one savepoint, so proceed.
4295  */
4296  case TBLOCK_SUBINPROGRESS:
4297  case TBLOCK_SUBABORT:
4298  break;
4299 
4300  /* These cases are invalid. */
4301  case TBLOCK_DEFAULT:
4302  case TBLOCK_STARTED:
4303  case TBLOCK_BEGIN:
4305  case TBLOCK_SUBBEGIN:
4306  case TBLOCK_END:
4307  case TBLOCK_SUBRELEASE:
4308  case TBLOCK_SUBCOMMIT:
4309  case TBLOCK_ABORT_END:
4310  case TBLOCK_SUBABORT_END:
4311  case TBLOCK_ABORT_PENDING:
4313  case TBLOCK_SUBRESTART:
4315  case TBLOCK_PREPARE:
4316  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4318  break;
4319  }
4320 
4321  for (target = s; PointerIsValid(target); target = target->parent)
4322  {
4323  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4324  break;
4325  }
4326 
4327  if (!PointerIsValid(target))
4328  ereport(ERROR,
4329  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4330  errmsg("savepoint \"%s\" does not exist", name)));
4331 
4332  /* disallow crossing savepoint level boundaries */
4333  if (target->savepointLevel != s->savepointLevel)
4334  ereport(ERROR,
4335  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4336  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4337 
4338  /*
4339  * Mark "abort pending" all subtransactions up to the target
4340  * subtransaction. The actual aborts will happen when control gets to
4341  * CommitTransactionCommand.
4342  */
4343  xact = CurrentTransactionState;
4344  for (;;)
4345  {
4346  if (xact == target)
4347  break;
4348  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4350  else if (xact->blockState == TBLOCK_SUBABORT)
4352  else
4353  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4355  xact = xact->parent;
4356  Assert(PointerIsValid(xact));
4357  }
4358 
4359  /* And mark the target as "restart pending" */
4360  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4361  xact->blockState = TBLOCK_SUBRESTART;
4362  else if (xact->blockState == TBLOCK_SUBABORT)
4364  else
4365  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4367 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
bool IsInParallelMode(void)
Definition: xact.c:997
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
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:632

◆ SaveTransactionCharacteristics()

void SaveTransactionCharacteristics ( void  )

Definition at line 2899 of file xact.c.

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

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

2900 {
2904 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2896
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2895
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2894

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

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

5205 {
5206  TransactionState s;
5207  Size nxids = 0;
5208  Size i = 0;
5209  TransactionId *workspace;
5211 
5212  result = (SerializedTransactionState *) start_address;
5213 
5214  result->xactIsoLevel = XactIsoLevel;
5215  result->xactDeferrable = XactDeferrable;
5217  result->currentFullTransactionId =
5220 
5221  /*
5222  * If we're running in a parallel worker and launching a parallel worker
5223  * of our own, we can just pass along the information that was passed to
5224  * us.
5225  */
5226  if (nParallelCurrentXids > 0)
5227  {
5229  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5231  return;
5232  }
5233 
5234  /*
5235  * OK, we need to generate a sorted list of XIDs that our workers should
5236  * view as current. First, figure out how many there are.
5237  */
5238  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5239  {
5241  nxids = add_size(nxids, 1);
5242  nxids = add_size(nxids, s->nChildXids);
5243  }
5245  <= maxsize);
5246 
5247  /* Copy them to our scratch space. */
5248  workspace = palloc(nxids * sizeof(TransactionId));
5249  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5250  {
5252  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5253  memcpy(&workspace[i], s->childXids,
5254  s->nChildXids * sizeof(TransactionId));
5255  i += s->nChildXids;
5256  }
5257  Assert(i == nxids);
5258 
5259  /* Sort them. */
5260  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5261 
5262  /* Copy data into output area. */
5263  result->nParallelCurrentXids = nxids;
5264  memcpy(&result->parallelCurrentXids[0], workspace,
5265  nxids * sizeof(TransactionId));
5266 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
bool XactDeferrable
Definition: xact.c:81
static TransactionState CurrentTransactionState
Definition: xact.c:235
uint32 TransactionId
Definition: c.h:513
#define FullTransactionIdIsValid(x)
Definition: transam.h:55
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:211
int nParallelCurrentXids
Definition: xact.c:109
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:184
CommandId currentCommandId
Definition: xact.c:209
struct TransactionStateData * parent
Definition: xact.c:194
FullTransactionId fullTransactionId
Definition: xact.c:174
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
TransactionId * ParallelCurrentXids
Definition: xact.c:110
#define Assert(condition)
Definition: c.h:738
#define SerializedTransactionStateHeaderSize
Definition: xact.c:215
size_t Size
Definition: c.h:466
int XactIsoLevel
Definition: xact.c:75
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define qsort(a, b, c, d)
Definition: port.h:479
static CommandId currentCommandId
Definition: xact.c:242
FullTransactionId topFullTransactionId
Definition: xact.c:207
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:139
FullTransactionId currentFullTransactionId
Definition: xact.c:208

◆ SetCurrentStatementStartTimestamp()

void SetCurrentStatementStartTimestamp ( void  )

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

819 {
820  if (!IsParallelWorker())
822  else
824 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
static TimestampTz stmtStartTimestamp
Definition: xact.c:253
#define IsParallelWorker()
Definition: parallel.h:61
#define Assert(condition)
Definition: c.h:738

◆ SetParallelStartTimestamps()

void SetParallelStartTimestamps ( TimestampTz  xact_ts,
TimestampTz  stmt_ts 
)

Definition at line 772 of file xact.c.

References Assert, IsParallelWorker, stmtStartTimestamp, and xactStartTimestamp.

Referenced by ParallelWorkerMain().

773 {
775  xactStartTimestamp = xact_ts;
776  stmtStartTimestamp = stmt_ts;
777 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:253
#define IsParallelWorker()
Definition: parallel.h:61
#define Assert(condition)
Definition: c.h:738
static TimestampTz xactStartTimestamp
Definition: xact.c:252

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

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

5275 {
5277 
5279  StartTransaction();
5280 
5281  tstate = (SerializedTransactionState *) tstatespace;
5282  XactIsoLevel = tstate->xactIsoLevel;
5283  XactDeferrable = tstate->xactDeferrable;
5286  tstate->currentFullTransactionId;
5290 
5292 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
bool XactDeferrable
Definition: xact.c:81
static TransactionState CurrentTransactionState
Definition: xact.c:235
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:211
int nParallelCurrentXids
Definition: xact.c:109
TBlockState blockState
Definition: xact.c:179
CommandId currentCommandId
Definition: xact.c:209
FullTransactionId fullTransactionId
Definition: xact.c:174
static void StartTransaction(void)
Definition: xact.c:1889
TransactionId * ParallelCurrentXids
Definition: xact.c:110
#define Assert(condition)
Definition: c.h:738
int XactIsoLevel
Definition: xact.c:75
static CommandId currentCommandId
Definition: xact.c:242
FullTransactionId topFullTransactionId
Definition: xact.c:207
FullTransactionId currentFullTransactionId
Definition: xact.c:208

◆ StartTransactionCommand()

void StartTransactionCommand ( void  )

Definition at line 2818 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(), ReindexMultipleTables(), ReindexRelationConcurrently(), RemoveTempRelationsCallback(), ReorderBufferCommit(), SnapBuildExportSnapshot(), SPI_start_transaction(), start_xact_command(), vacuum(), and vacuum_rel().

2819 {
2821 
2822  switch (s->blockState)
2823  {
2824  /*
2825  * if we aren't in a transaction block, we just do our usual start
2826  * transaction.
2827  */
2828  case TBLOCK_DEFAULT:
2829  StartTransaction();
2831  break;
2832 
2833  /*
2834  * We are somewhere in a transaction block or subtransaction and
2835  * about to start a new command. For now we do nothing, but
2836  * someday we may do command-local resource initialization. (Note
2837  * that any needed CommandCounterIncrement was done by the
2838  * previous CommitTransactionCommand.)
2839  */
2840  case TBLOCK_INPROGRESS:
2842  case TBLOCK_SUBINPROGRESS:
2843  break;
2844 
2845  /*
2846  * Here we are in a failed transaction block (one of the commands
2847  * caused an abort) so we do nothing but remain in the abort
2848  * state. Eventually we will get a ROLLBACK command which will
2849  * get us out of this state. (It is up to other code to ensure
2850  * that no commands other than ROLLBACK will be processed in these
2851  * states.)
2852  */
2853  case TBLOCK_ABORT:
2854  case TBLOCK_SUBABORT:
2855  break;
2856 
2857  /* These cases are invalid. */
2858  case TBLOCK_STARTED:
2859  case TBLOCK_BEGIN:
2861  case TBLOCK_SUBBEGIN:
2862  case TBLOCK_END:
2863  case TBLOCK_SUBRELEASE:
2864  case TBLOCK_SUBCOMMIT:
2865  case TBLOCK_ABORT_END:
2866  case TBLOCK_SUBABORT_END:
2867  case TBLOCK_ABORT_PENDING:
2869  case TBLOCK_SUBRESTART:
2871  case TBLOCK_PREPARE:
2872  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2874  break;
2875  }
2876 
2877  /*
2878  * We must switch to CurTransactionContext before returning. This is
2879  * already done if we called StartTransaction, otherwise not.
2880  */
2881  Assert(CurTransactionContext != NULL);
2883 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
TBlockState blockState
Definition: xact.c:179
#define ERROR
Definition: elog.h:43
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
static void StartTransaction(void)
Definition: xact.c:1889
#define Assert(condition)
Definition: c.h:738
#define elog(elevel,...)
Definition: elog.h:214

◆ SubTransactionIsActive()

bool SubTransactionIsActive ( SubTransactionId  subxid)

Definition at line 722 of file xact.c.

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

Referenced by fmgr_sql().

723 {
725 
726  for (s = CurrentTransactionState; s != NULL; s = s->parent)
727  {
728  if (s->state == TRANS_ABORT)
729  continue;
730  if (s->subTransactionId == subxid)
731  return true;
732  }
733  return false;
734 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TransState state
Definition: xact.c:178
struct TransactionStateData * parent
Definition: xact.c:194
SubTransactionId subTransactionId
Definition: xact.c:175

◆ TransactionBlockStatusCode()

char TransactionBlockStatusCode ( void  )

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

4689 {
4691 
4692  switch (s->blockState)
4693  {
4694  case TBLOCK_DEFAULT:
4695  case TBLOCK_STARTED:
4696  return 'I'; /* idle --- not in transaction */
4697  case TBLOCK_BEGIN:
4698  case TBLOCK_SUBBEGIN:
4699  case TBLOCK_INPROGRESS:
4702  case TBLOCK_SUBINPROGRESS:
4703  case TBLOCK_END:
4704  case TBLOCK_SUBRELEASE:
4705  case TBLOCK_SUBCOMMIT:
4706  case TBLOCK_PREPARE:
4707  return 'T'; /* in transaction */
4708  case TBLOCK_ABORT:
4709  case TBLOCK_SUBABORT:
4710  case TBLOCK_ABORT_END:
4711  case TBLOCK_SUBABORT_END:
4712  case TBLOCK_ABORT_PENDING:
4714  case TBLOCK_SUBRESTART:
4716  return 'E'; /* in failed transaction */
4717  }
4718 
4719  /* should never get here */
4720  elog(FATAL, "invalid transaction block state: %s",
4722  return 0; /* keep compiler quiet */
4723 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define elog(elevel,...)
Definition: elog.h:214

◆ TransactionIdIsCurrentTransactionId()

bool TransactionIdIsCurrentTransactionId ( TransactionId  xid)

Definition at line 854 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(), HeapTupleSatisfiesVacuum(), MultiXactIdIsRunning(), pg_xact_status(), PredicateLockTID(), RI_FKey_fk_upd_check_required(), SnapBuildWaitSnapshot(), test_lockmode_for_conflict(), and TransactionIdIsInProgress().

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

◆ UnregisterSubXactCallback()

void UnregisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3568 of file xact.c.

References SubXactCallbackItem::next, and pfree().

3569 {
3570  SubXactCallbackItem *item;
3571  SubXactCallbackItem *prev;
3572 
3573  prev = NULL;
3574  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3575  {
3576  if (item->callback == callback && item->arg == arg)
3577  {
3578  if (prev)
3579  prev->next = item->next;
3580  else
3581  SubXact_callbacks = item->next;
3582  pfree(item);
3583  break;
3584  }
3585  }
3586 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:299
void pfree(void *pointer)
Definition: mcxt.c:1056
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:294
void * arg

◆ UnregisterXactCallback()

void UnregisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3513 of file xact.c.

References XactCallbackItem::next, and pfree().

3514 {
3515  XactCallbackItem *item;
3516  XactCallbackItem *prev;
3517 
3518  prev = NULL;
3519  for (item = Xact_callbacks; item; prev = item, item = item->next)
3520  {
3521  if (item->callback == callback && item->arg == arg)
3522  {
3523  if (prev)
3524  prev->next = item->next;
3525  else
3526  Xact_callbacks = item->next;
3527  pfree(item);
3528  break;
3529  }
3530  }
3531 }
struct XactCallbackItem * next
Definition: xact.c:282
void pfree(void *pointer)
Definition: mcxt.c:1056
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
static XactCallbackItem * Xact_callbacks
Definition: xact.c:287
void * arg

◆ UserAbortTransactionBlock()

void UserAbortTransactionBlock ( bool  chain)

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

3891 {
3893 
3894  switch (s->blockState)
3895  {
3896  /*
3897  * We are inside a transaction block and we got a ROLLBACK command
3898  * from the user, so tell CommitTransactionCommand to abort and
3899  * exit the transaction block.
3900  */
3901  case TBLOCK_INPROGRESS:
3903  break;
3904 
3905  /*
3906  * We are inside a failed transaction block and we got a ROLLBACK
3907  * command from the user. Abort processing is already done, so
3908  * CommitTransactionCommand just has to cleanup and go back to
3909  * idle state.
3910  */
3911  case TBLOCK_ABORT:
3913  break;
3914 
3915  /*
3916  * We are inside a subtransaction. Mark everything up to top
3917  * level as exitable.
3918  */
3919  case TBLOCK_SUBINPROGRESS:
3920  case TBLOCK_SUBABORT:
3921  while (s->parent != NULL)
3922  {
3923  if (s->blockState == TBLOCK_SUBINPROGRESS)
3925  else if (s->blockState == TBLOCK_SUBABORT)
3927  else
3928  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3930  s = s->parent;
3931  }
3932  if (s->blockState == TBLOCK_INPROGRESS)
3934  else if (s->blockState == TBLOCK_ABORT)
3936  else
3937  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3939  break;
3940 
3941  /*
3942  * The user issued ABORT when not inside a transaction. For
3943  * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
3944  * The upcoming call to CommitTransactionCommand() will then put
3945  * us back into the default state. For ROLLBACK AND CHAIN, error.
3946  *
3947  * We do the same thing with ABORT inside an implicit transaction,
3948  * although in this case we might be rolling back actual database
3949  * state changes. (It's debatable whether we should issue a
3950  * WARNING in this case, but we have done so historically.)
3951  */
3952  case TBLOCK_STARTED:
3954  if (chain)
3955  ereport(ERROR,
3956  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3957  /* translator: %s represents an SQL statement name */
3958  errmsg("%s can only be used in transaction blocks",
3959  "ROLLBACK AND CHAIN")));
3960  else
3961  ereport(WARNING,
3962  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3963  errmsg("there is no transaction in progress")));
3965  break;
3966 
3967  /*
3968  * The user issued an ABORT that somehow ran inside a parallel
3969  * worker. We can't cope with that.
3970  */
3972  ereport(FATAL,
3973  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3974  errmsg("cannot abort during a parallel operation")));
3975  break;
3976 
3977  /* These cases are invalid. */
3978  case TBLOCK_DEFAULT:
3979  case TBLOCK_BEGIN:
3980  case TBLOCK_SUBBEGIN:
3981  case TBLOCK_END:
3982  case TBLOCK_SUBRELEASE:
3983  case TBLOCK_SUBCOMMIT:
3984  case TBLOCK_ABORT_END:
3985  case TBLOCK_SUBABORT_END:
3986  case TBLOCK_ABORT_PENDING:
3988  case TBLOCK_SUBRESTART:
3990  case TBLOCK_PREPARE:
3991  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3993  break;
3994  }
3995 
3998 
3999  s->chain = chain;
4000 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:179
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5362
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ WarnNoTransactionBlock()

void WarnNoTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3412 of file xact.c.

References CheckTransactionBlock().

Referenced by ExecSetVariableStmt(), and standard_ProcessUtility().

3413 {
3414  CheckTransactionBlock(isTopLevel, false, stmtType);
3415 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3427

◆ xact_desc()