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, bool forceSync, 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 3162 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().

3163 {
3165 
3166  switch (s->blockState)
3167  {
3168  case TBLOCK_DEFAULT:
3169  if (s->state == TRANS_DEFAULT)
3170  {
3171  /* we are idle, so nothing to do */
3172  }
3173  else
3174  {
3175  /*
3176  * We can get here after an error during transaction start
3177  * (state will be TRANS_START). Need to clean up the
3178  * incompletely started transaction. First, adjust the
3179  * low-level state to suppress warning message from
3180  * AbortTransaction.
3181  */
3182  if (s->state == TRANS_START)
3183  s->state = TRANS_INPROGRESS;
3184  AbortTransaction();
3186  }
3187  break;
3188 
3189  /*
3190  * If we aren't in a transaction block, we just do the basic abort
3191  * & cleanup transaction. For this purpose, we treat an implicit
3192  * transaction block as if it were a simple statement.
3193  */
3194  case TBLOCK_STARTED:
3196  AbortTransaction();
3199  break;
3200 
3201  /*
3202  * If we are in TBLOCK_BEGIN it means something screwed up right
3203  * after reading "BEGIN TRANSACTION". We assume that the user
3204  * will interpret the error as meaning the BEGIN failed to get him
3205  * into a transaction block, so we should abort and return to idle
3206  * state.
3207  */
3208  case TBLOCK_BEGIN:
3209  AbortTransaction();
3212  break;
3213 
3214  /*
3215  * We are somewhere in a transaction block and we've gotten a
3216  * failure, so we abort the transaction and set up the persistent
3217  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3218  */
3219  case TBLOCK_INPROGRESS:
3221  AbortTransaction();
3222  s->blockState = TBLOCK_ABORT;
3223  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3224  break;
3225 
3226  /*
3227  * Here, we failed while trying to COMMIT. Clean up the
3228  * transaction and return to idle state (we do not want to stay in
3229  * the transaction).
3230  */
3231  case TBLOCK_END:
3232  AbortTransaction();
3235  break;
3236 
3237  /*
3238  * Here, we are already in an aborted transaction state and are
3239  * waiting for a ROLLBACK, but for some reason we failed again! So
3240  * we just remain in the abort state.
3241  */
3242  case TBLOCK_ABORT:
3243  case TBLOCK_SUBABORT:
3244  break;
3245 
3246  /*
3247  * We are in a failed transaction and we got the ROLLBACK command.
3248  * We have already aborted, we just need to cleanup and go to idle
3249  * state.
3250  */
3251  case TBLOCK_ABORT_END:
3254  break;
3255 
3256  /*
3257  * We are in a live transaction and we got a ROLLBACK command.
3258  * Abort, cleanup, go to idle state.
3259  */
3260  case TBLOCK_ABORT_PENDING:
3261  AbortTransaction();
3264  break;
3265 
3266  /*
3267  * Here, we failed while trying to PREPARE. Clean up the
3268  * transaction and return to idle state (we do not want to stay in
3269  * the transaction).
3270  */
3271  case TBLOCK_PREPARE:
3272  AbortTransaction();
3275  break;
3276 
3277  /*
3278  * We got an error inside a subtransaction. Abort just the
3279  * subtransaction, and go to the persistent SUBABORT state until
3280  * we get ROLLBACK.
3281  */
3282  case TBLOCK_SUBINPROGRESS:
3285  break;
3286 
3287  /*
3288  * If we failed while trying to create a subtransaction, clean up
3289  * the broken subtransaction and abort the parent. The same
3290  * applies if we get a failure while ending a subtransaction.
3291  */
3292  case TBLOCK_SUBBEGIN:
3293  case TBLOCK_SUBRELEASE:
3294  case TBLOCK_SUBCOMMIT:
3296  case TBLOCK_SUBRESTART:
3300  break;
3301 
3302  /*
3303  * Same as above, except the Abort() was already done.
3304  */
3305  case TBLOCK_SUBABORT_END:
3309  break;
3310  }
3311 }
void AbortCurrentTransaction(void)
Definition: xact.c:3162
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
TransState state
Definition: xact.c:177
static void CleanupTransaction(void)
Definition: xact.c:2748
static void AbortSubTransaction(void)
Definition: xact.c:4877
static void AbortTransaction(void)
Definition: xact.c:2567
static void CleanupSubTransaction(void)
Definition: xact.c:5026

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4532 {
4534 
4535  /* Ensure we're not running in a doomed memory context */
4536  AtAbort_Memory();
4537 
4538  /*
4539  * Get out of any transaction or nested transaction
4540  */
4541  do
4542  {
4543  switch (s->blockState)
4544  {
4545  case TBLOCK_DEFAULT:
4546  if (s->state == TRANS_DEFAULT)
4547  {
4548  /* Not in a transaction, do nothing */
4549  }
4550  else
4551  {
4552  /*
4553  * We can get here after an error during transaction start
4554  * (state will be TRANS_START). Need to clean up the
4555  * incompletely started transaction. First, adjust the
4556  * low-level state to suppress warning message from
4557  * AbortTransaction.
4558  */
4559  if (s->state == TRANS_START)
4560  s->state = TRANS_INPROGRESS;
4561  AbortTransaction();
4563  }
4564  break;
4565  case TBLOCK_STARTED:
4566  case TBLOCK_BEGIN:
4567  case TBLOCK_INPROGRESS:
4570  case TBLOCK_END:
4571  case TBLOCK_ABORT_PENDING:
4572  case TBLOCK_PREPARE:
4573  /* In a transaction, so clean up */
4574  AbortTransaction();
4577  break;
4578  case TBLOCK_ABORT:
4579  case TBLOCK_ABORT_END:
4580 
4581  /*
4582  * AbortTransaction is already done, still need Cleanup.
4583  * However, if we failed partway through running ROLLBACK,
4584  * there will be an active portal running that command, which
4585  * we need to shut down before doing CleanupTransaction.
4586  */
4587  AtAbort_Portals();
4590  break;
4591 
4592  /*
4593  * In a subtransaction, so clean it up and abort parent too
4594  */
4595  case TBLOCK_SUBBEGIN:
4596  case TBLOCK_SUBINPROGRESS:
4597  case TBLOCK_SUBRELEASE:
4598  case TBLOCK_SUBCOMMIT:
4600  case TBLOCK_SUBRESTART:
4603  s = CurrentTransactionState; /* changed by pop */
4604  break;
4605 
4606  case TBLOCK_SUBABORT:
4607  case TBLOCK_SUBABORT_END:
4609  /* As above, but AbortSubTransaction already done */
4610  if (s->curTransactionOwner)
4611  {
4612  /* As in TBLOCK_ABORT, might have a live portal to zap */
4617  }
4619  s = CurrentTransactionState; /* changed by pop */
4620  break;
4621  }
4622  } while (s->blockState != TBLOCK_DEFAULT);
4623 
4624  /* Should be out of all subxacts now */
4625  Assert(s->parent == NULL);
4626 
4627  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4628  AtCleanup_Memory();
4629 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
static void AtCleanup_Memory(void)
Definition: xact.c:1815
void AtAbort_Portals(void)
Definition: portalmem.c:772
TBlockState blockState
Definition: xact.c:178
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
static void CleanupTransaction(void)
Definition: xact.c:2748
struct TransactionStateData * parent
Definition: xact.c:193
SubTransactionId subTransactionId
Definition: xact.c:174
static void AbortSubTransaction(void)
Definition: xact.c:4877
static void AtAbort_Memory(void)
Definition: xact.c:1725
static void AbortTransaction(void)
Definition: xact.c:2567
#define Assert(condition)
Definition: c.h:739
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:968
static void CleanupSubTransaction(void)
Definition: xact.c:5026

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 3991 of file xact.c.

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

Referenced by exec_simple_query().

3992 {
3994 
3995  /*
3996  * If we are in STARTED state (that is, no transaction block is open),
3997  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3998  * block.
3999  *
4000  * For caller convenience, we consider all other transaction states as
4001  * legal here; otherwise the caller would need its own state check, which
4002  * seems rather pointless.
4003  */
4004  if (s->blockState == TBLOCK_STARTED)
4006 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

4360 {
4362 
4363  /*
4364  * Workers synchronize transaction state at the beginning of each parallel
4365  * operation, so we can't account for new subtransactions after that
4366  * point. We might be able to make an exception for the type of
4367  * subtransaction established by this function, which is typically used in
4368  * contexts where we're going to release or roll back the subtransaction
4369  * before proceeding further, so that no enduring change to the
4370  * transaction state occurs. For now, however, we prohibit this case along
4371  * with all the others.
4372  */
4373  if (IsInParallelMode())
4374  ereport(ERROR,
4375  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4376  errmsg("cannot start subtransactions during a parallel operation")));
4377 
4378  switch (s->blockState)
4379  {
4380  case TBLOCK_STARTED:
4381  case TBLOCK_INPROGRESS:
4383  case TBLOCK_END:
4384  case TBLOCK_PREPARE:
4385  case TBLOCK_SUBINPROGRESS:
4386  /* Normal subtransaction start */
4387  PushTransaction();
4388  s = CurrentTransactionState; /* changed by push */
4389 
4390  /*
4391  * Savepoint names, like the TransactionState block itself, live
4392  * in TopTransactionContext.
4393  */
4394  if (name)
4396  break;
4397 
4398  /* These cases are invalid. */
4399  case TBLOCK_DEFAULT:
4400  case TBLOCK_BEGIN:
4402  case TBLOCK_SUBBEGIN:
4403  case TBLOCK_SUBRELEASE:
4404  case TBLOCK_SUBCOMMIT:
4405  case TBLOCK_ABORT:
4406  case TBLOCK_SUBABORT:
4407  case TBLOCK_ABORT_END:
4408  case TBLOCK_SUBABORT_END:
4409  case TBLOCK_ABORT_PENDING:
4411  case TBLOCK_SUBRESTART:
4413  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4415  break;
4416  }
4417 
4420 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
MemoryContext TopTransactionContext
Definition: mcxt.c:49
void CommitTransactionCommand(void)
Definition: xact.c:2898
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5059
void StartTransactionCommand(void)
Definition: xact.c:2797
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:822
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:228

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

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

3590 {
3592 
3593  switch (s->blockState)
3594  {
3595  /*
3596  * We are not inside a transaction block, so allow one to begin.
3597  */
3598  case TBLOCK_STARTED:
3599  s->blockState = TBLOCK_BEGIN;
3600  break;
3601 
3602  /*
3603  * BEGIN converts an implicit transaction block to a regular one.
3604  * (Note that we allow this even if we've already done some
3605  * commands, which is a bit odd but matches historical practice.)
3606  */
3608  s->blockState = TBLOCK_BEGIN;
3609  break;
3610 
3611  /*
3612  * Already a transaction block in progress.
3613  */
3614  case TBLOCK_INPROGRESS:
3616  case TBLOCK_SUBINPROGRESS:
3617  case TBLOCK_ABORT:
3618  case TBLOCK_SUBABORT:
3619  ereport(WARNING,
3620  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3621  errmsg("there is already a transaction in progress")));
3622  break;
3623 
3624  /* These cases are invalid. */
3625  case TBLOCK_DEFAULT:
3626  case TBLOCK_BEGIN:
3627  case TBLOCK_SUBBEGIN:
3628  case TBLOCK_END:
3629  case TBLOCK_SUBRELEASE:
3630  case TBLOCK_SUBCOMMIT:
3631  case TBLOCK_ABORT_END:
3632  case TBLOCK_SUBABORT_END:
3633  case TBLOCK_ABORT_PENDING:
3635  case TBLOCK_SUBRESTART:
3637  case TBLOCK_PREPARE:
3638  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3640  break;
3641  }
3642 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1005 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(), ATExecDropIdentity(), ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), AttachPartitionEnsureIndexes(), CloneFkReferencing(), CommitSubTransaction(), CommitTransactionCommand(), copy_table_data(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyActionTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), CreateTrigger(), DefineCollation(), DefineDomain(), DefineQueryRewrite(), DefineRange(), DefineRelation(), DefineType(), DefineVirtualRelation(), deleteOneObject(), DelRoleMems(), 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(), findTypeInputFunction(), findTypeOutputFunction(), 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().

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

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

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

2899 {
2901 
2902  if (s->chain)
2904 
2905  switch (s->blockState)
2906  {
2907  /*
2908  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2909  * StartTransactionCommand didn't set the STARTED state
2910  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2911  * by EndParallelWorkerTransaction(), not this function.
2912  */
2913  case TBLOCK_DEFAULT:
2915  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2917  break;
2918 
2919  /*
2920  * If we aren't in a transaction block, just do our usual
2921  * transaction commit, and return to the idle state.
2922  */
2923  case TBLOCK_STARTED:
2926  break;
2927 
2928  /*
2929  * We are completing a "BEGIN TRANSACTION" command, so we change
2930  * to the "transaction block in progress" state and return. (We
2931  * assume the BEGIN did nothing to the database, so we need no
2932  * CommandCounterIncrement.)
2933  */
2934  case TBLOCK_BEGIN:
2936  break;
2937 
2938  /*
2939  * This is the case when we have finished executing a command
2940  * someplace within a transaction block. We increment the command
2941  * counter and return.
2942  */
2943  case TBLOCK_INPROGRESS:
2945  case TBLOCK_SUBINPROGRESS:
2947  break;
2948 
2949  /*
2950  * We are completing a "COMMIT" command. Do it and return to the
2951  * idle state.
2952  */
2953  case TBLOCK_END:
2956  if (s->chain)
2957  {
2958  StartTransaction();
2960  s->chain = false;
2962  }
2963  break;
2964 
2965  /*
2966  * Here we are in the middle of a transaction block but one of the
2967  * commands caused an abort so we do nothing but remain in the
2968  * abort state. Eventually we will get a ROLLBACK command.
2969  */
2970  case TBLOCK_ABORT:
2971  case TBLOCK_SUBABORT:
2972  break;
2973 
2974  /*
2975  * Here we were in an aborted transaction block and we just got
2976  * the ROLLBACK command from the user, so clean up the
2977  * already-aborted transaction and return to the idle state.
2978  */
2979  case TBLOCK_ABORT_END:
2982  if (s->chain)
2983  {
2984  StartTransaction();
2986  s->chain = false;
2988  }
2989  break;
2990 
2991  /*
2992  * Here we were in a perfectly good transaction block but the user
2993  * told us to ROLLBACK anyway. We have to abort the transaction
2994  * and then clean up.
2995  */
2996  case TBLOCK_ABORT_PENDING:
2997  AbortTransaction();
3000  if (s->chain)
3001  {
3002  StartTransaction();
3004  s->chain = false;
3006  }
3007  break;
3008 
3009  /*
3010  * We are completing a "PREPARE TRANSACTION" command. Do it and
3011  * return to the idle state.
3012  */
3013  case TBLOCK_PREPARE:
3016  break;
3017 
3018  /*
3019  * We were just issued a SAVEPOINT inside a transaction block.
3020  * Start a subtransaction. (DefineSavepoint already did
3021  * PushTransaction, so as to have someplace to put the SUBBEGIN
3022  * state.)
3023  */
3024  case TBLOCK_SUBBEGIN:
3027  break;
3028 
3029  /*
3030  * We were issued a RELEASE command, so we end the current
3031  * subtransaction and return to the parent transaction. The parent
3032  * might be ended too, so repeat till we find an INPROGRESS
3033  * transaction or subtransaction.
3034  */
3035  case TBLOCK_SUBRELEASE:
3036  do
3037  {
3039  s = CurrentTransactionState; /* changed by pop */
3040  } while (s->blockState == TBLOCK_SUBRELEASE);
3041 
3044  break;
3045 
3046  /*
3047  * We were issued a COMMIT, so we end the current subtransaction
3048  * hierarchy and perform final commit. We do this by rolling up
3049  * any subtransactions into their parent, which leads to O(N^2)
3050  * operations with respect to resource owners - this isn't that
3051  * bad until we approach a thousands of savepoints but is
3052  * necessary for correctness should after triggers create new
3053  * resource owners.
3054  */
3055  case TBLOCK_SUBCOMMIT:
3056  do
3057  {
3059  s = CurrentTransactionState; /* changed by pop */
3060  } while (s->blockState == TBLOCK_SUBCOMMIT);
3061  /* If we had a COMMIT command, finish off the main xact too */
3062  if (s->blockState == TBLOCK_END)
3063  {
3064  Assert(s->parent == NULL);
3067  }
3068  else if (s->blockState == TBLOCK_PREPARE)
3069  {
3070  Assert(s->parent == NULL);
3073  }
3074  else
3075  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3077  break;
3078 
3079  /*
3080  * The current already-failed subtransaction is ending due to a
3081  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3082  * examine the parent (which could be in any of several states).
3083  */
3084  case TBLOCK_SUBABORT_END:
3087  break;
3088 
3089  /*
3090  * As above, but it's not dead yet, so abort first.
3091  */
3096  break;
3097 
3098  /*
3099  * The current subtransaction is the target of a ROLLBACK TO
3100  * command. Abort and pop it, then start a new subtransaction
3101  * with the same name.
3102  */
3103  case TBLOCK_SUBRESTART:
3104  {
3105  char *name;
3106  int savepointLevel;
3107 
3108  /* save name and keep Cleanup from freeing it */
3109  name = s->name;
3110  s->name = NULL;
3111  savepointLevel = s->savepointLevel;
3112 
3115 
3116  DefineSavepoint(NULL);
3117  s = CurrentTransactionState; /* changed by push */
3118  s->name = name;
3119  s->savepointLevel = savepointLevel;
3120 
3121  /* This is the same as TBLOCK_SUBBEGIN case */
3125  }
3126  break;
3127 
3128  /*
3129  * Same as above, but the subtransaction had already failed, so we
3130  * don't need AbortSubTransaction.
3131  */
3133  {
3134  char *name;
3135  int savepointLevel;
3136 
3137  /* save name and keep Cleanup from freeing it */
3138  name = s->name;
3139  s->name = NULL;
3140  savepointLevel = s->savepointLevel;
3141 
3143 
3144  DefineSavepoint(NULL);
3145  s = CurrentTransactionState; /* changed by push */
3146  s->name = name;
3147  s->savepointLevel = savepointLevel;
3148 
3149  /* This is the same as TBLOCK_SUBBEGIN case */
3153  }
3154  break;
3155  }
3156 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define AssertState(condition)
Definition: c.h:742
void CommitTransactionCommand(void)
Definition: xact.c:2898
void DefineSavepoint(const char *name)
Definition: xact.c:4038
TBlockState blockState
Definition: xact.c:178
static void PrepareTransaction(void)
Definition: xact.c:2286
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4768
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2748
struct TransactionStateData * parent
Definition: xact.c:193
void SaveTransactionCharacteristics(void)
Definition: xact.c:2878
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
static void AbortSubTransaction(void)
Definition: xact.c:4877
void CommandCounterIncrement(void)
Definition: xact.c:1005
static void AbortTransaction(void)
Definition: xact.c:2567
static void StartTransaction(void)
Definition: xact.c:1886
#define Assert(condition)
Definition: c.h:739
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2886
static void CleanupSubTransaction(void)
Definition: xact.c:5026
const char * name
Definition: encode.c:521
#define elog(elevel,...)
Definition: elog.h:228
static void CommitTransaction(void)
Definition: xact.c:2045
static void StartSubTransaction(void)
Definition: xact.c:4731

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

4039 {
4041 
4042  /*
4043  * Workers synchronize transaction state at the beginning of each parallel
4044  * operation, so we can't account for new subtransactions after that
4045  * point. (Note that this check will certainly error out if s->blockState
4046  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4047  * below.)
4048  */
4049  if (IsInParallelMode())
4050  ereport(ERROR,
4051  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4052  errmsg("cannot define savepoints during a parallel operation")));
4053 
4054  switch (s->blockState)
4055  {
4056  case TBLOCK_INPROGRESS:
4057  case TBLOCK_SUBINPROGRESS:
4058  /* Normal subtransaction start */
4059  PushTransaction();
4060  s = CurrentTransactionState; /* changed by push */
4061 
4062  /*
4063  * Savepoint names, like the TransactionState block itself, live
4064  * in TopTransactionContext.
4065  */
4066  if (name)
4068  break;
4069 
4070  /*
4071  * We disallow savepoint commands in implicit transaction blocks.
4072  * There would be no great difficulty in allowing them so far as
4073  * this module is concerned, but a savepoint seems inconsistent
4074  * with exec_simple_query's behavior of abandoning the whole query
4075  * string upon error. Also, the point of an implicit transaction
4076  * block (as opposed to a regular one) is to automatically close
4077  * after an error, so it's hard to see how a savepoint would fit
4078  * into that.
4079  *
4080  * The error messages for this are phrased as if there were no
4081  * active transaction block at all, which is historical but
4082  * perhaps could be improved.
4083  */
4085  ereport(ERROR,
4086  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4087  /* translator: %s represents an SQL statement name */
4088  errmsg("%s can only be used in transaction blocks",
4089  "SAVEPOINT")));
4090  break;
4091 
4092  /* These cases are invalid. */
4093  case TBLOCK_DEFAULT:
4094  case TBLOCK_STARTED:
4095  case TBLOCK_BEGIN:
4097  case TBLOCK_SUBBEGIN:
4098  case TBLOCK_END:
4099  case TBLOCK_SUBRELEASE:
4100  case TBLOCK_SUBCOMMIT:
4101  case TBLOCK_ABORT:
4102  case TBLOCK_SUBABORT:
4103  case TBLOCK_ABORT_END:
4104  case TBLOCK_SUBABORT_END:
4105  case TBLOCK_ABORT_PENDING:
4107  case TBLOCK_SUBRESTART:
4109  case TBLOCK_PREPARE:
4110  elog(FATAL, "DefineSavepoint: unexpected state %s",
4112  break;
4113  }
4114 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
MemoryContext TopTransactionContext
Definition: mcxt.c:49
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5059
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:822
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
#define elog(elevel,...)
Definition: elog.h:228

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4016 of file xact.c.

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

Referenced by exec_simple_query().

4017 {
4019 
4020  /*
4021  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
4022  * allowing CommitTransactionCommand to commit whatever happened during
4023  * the implicit transaction block as though it were a single statement.
4024  *
4025  * For caller convenience, we consider all other transaction states as
4026  * legal here; otherwise the caller would need its own state check, which
4027  * seems rather pointless.
4028  */
4031 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5275 of file xact.c.

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

Referenced by ParallelWorkerMain().

5276 {
5280 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
#define Assert(condition)
Definition: c.h:739
static void CommitTransaction(void)
Definition: xact.c:2045

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

3710 {
3712  bool result = false;
3713 
3714  switch (s->blockState)
3715  {
3716  /*
3717  * We are in a transaction block, so tell CommitTransactionCommand
3718  * to COMMIT.
3719  */
3720  case TBLOCK_INPROGRESS:
3721  s->blockState = TBLOCK_END;
3722  result = true;
3723  break;
3724 
3725  /*
3726  * We are in an implicit transaction block. If AND CHAIN was
3727  * specified, error. Otherwise commit, but issue a warning
3728  * because there was no explicit BEGIN before this.
3729  */
3731  if (chain)
3732  ereport(ERROR,
3733  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3734  /* translator: %s represents an SQL statement name */
3735  errmsg("%s can only be used in transaction blocks",
3736  "COMMIT AND CHAIN")));
3737  else
3738  ereport(WARNING,
3739  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3740  errmsg("there is no transaction in progress")));
3741  s->blockState = TBLOCK_END;
3742  result = true;
3743  break;
3744 
3745  /*
3746  * We are in a failed transaction block. Tell
3747  * CommitTransactionCommand it's time to exit the block.
3748  */
3749  case TBLOCK_ABORT:
3751  break;
3752 
3753  /*
3754  * We are in a live subtransaction block. Set up to subcommit all
3755  * open subtransactions and then commit the main transaction.
3756  */
3757  case TBLOCK_SUBINPROGRESS:
3758  while (s->parent != NULL)
3759  {
3760  if (s->blockState == TBLOCK_SUBINPROGRESS)
3762  else
3763  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3765  s = s->parent;
3766  }
3767  if (s->blockState == TBLOCK_INPROGRESS)
3768  s->blockState = TBLOCK_END;
3769  else
3770  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3772  result = true;
3773  break;
3774 
3775  /*
3776  * Here we are inside an aborted subtransaction. Treat the COMMIT
3777  * as ROLLBACK: set up to abort everything and exit the main
3778  * transaction.
3779  */
3780  case TBLOCK_SUBABORT:
3781  while (s->parent != NULL)
3782  {
3783  if (s->blockState == TBLOCK_SUBINPROGRESS)
3785  else if (s->blockState == TBLOCK_SUBABORT)
3787  else
3788  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3790  s = s->parent;
3791  }
3792  if (s->blockState == TBLOCK_INPROGRESS)
3794  else if (s->blockState == TBLOCK_ABORT)
3796  else
3797  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3799  break;
3800 
3801  /*
3802  * The user issued COMMIT when not inside a transaction. For
3803  * COMMIT without CHAIN, issue a WARNING, staying in
3804  * TBLOCK_STARTED state. The upcoming call to
3805  * CommitTransactionCommand() will then close the transaction and
3806  * put us back into the default state. For COMMIT AND CHAIN,
3807  * error.
3808  */
3809  case TBLOCK_STARTED:
3810  if (chain)
3811  ereport(ERROR,
3812  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3813  /* translator: %s represents an SQL statement name */
3814  errmsg("%s can only be used in transaction blocks",
3815  "COMMIT AND CHAIN")));
3816  else
3817  ereport(WARNING,
3818  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3819  errmsg("there is no transaction in progress")));
3820  result = true;
3821  break;
3822 
3823  /*
3824  * The user issued a COMMIT that somehow ran inside a parallel
3825  * worker. We can't cope with that.
3826  */
3828  ereport(FATAL,
3829  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3830  errmsg("cannot commit during a parallel operation")));
3831  break;
3832 
3833  /* These cases are invalid. */
3834  case TBLOCK_DEFAULT:
3835  case TBLOCK_BEGIN:
3836  case TBLOCK_SUBBEGIN:
3837  case TBLOCK_END:
3838  case TBLOCK_SUBRELEASE:
3839  case TBLOCK_SUBCOMMIT:
3840  case TBLOCK_ABORT_END:
3841  case TBLOCK_SUBABORT_END:
3842  case TBLOCK_ABORT_PENDING:
3844  case TBLOCK_SUBRESTART:
3846  case TBLOCK_PREPARE:
3847  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3849  break;
3850  }
3851 
3853  s->blockState == TBLOCK_END ||
3854  s->blockState == TBLOCK_ABORT_END ||
3856 
3857  s->chain = chain;
3858 
3859  return result;
3860 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:193
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:739
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 963 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

964 {
966 
967  Assert(s->parallelModeLevel >= 0);
968 
969  ++s->parallelModeLevel;
970 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:739

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5152 of file xact.c.

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

Referenced by InitializeParallelDSM().

5153 {
5154  TransactionState s;
5155  Size nxids = 0;
5157 
5158  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5159  {
5161  nxids = add_size(nxids, 1);
5162  nxids = add_size(nxids, s->nChildXids);
5163  }
5164 
5165  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5166 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
uint32 TransactionId
Definition: c.h:514
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
struct TransactionStateData * parent
Definition: xact.c:193
FullTransactionId fullTransactionId
Definition: xact.c:173
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define SerializedTransactionStateHeaderSize
Definition: xact.c:214
size_t Size
Definition: c.h:467

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 976 of file xact.c.

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

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

977 {
979 
980  Assert(s->parallelModeLevel > 0);
982 
983  --s->parallelModeLevel;
984 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
bool ParallelContextActive(void)
Definition: parallel.c:944
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:739

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1053 of file xact.c.

References forceSyncCommit.

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

1054 {
1055  forceSyncCommit = true;
1056 }
static bool forceSyncCommit
Definition: xact.c:264

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

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

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

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 480 of file xact.c.

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

481 {
483 
486  return s->fullTransactionId;
487 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
FullTransactionId fullTransactionId
Definition: xact.c:173
static void AssignTransactionId(TransactionState s)
Definition: xact.c:553

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 498 of file xact.c.

References TransactionStateData::fullTransactionId.

499 {
501 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
FullTransactionId fullTransactionId
Definition: xact.c:173

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 791 of file xact.c.

References stmtStartTimestamp.

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

792 {
793  return stmtStartTimestamp;
794 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:252

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 439 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

440 {
442 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define XidFromFullTransactionId(x)
Definition: transam.h:48
FullTransactionId fullTransactionId
Definition: xact.c:173

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 803 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

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

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 525 of file xact.c.

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

Referenced by xid_age().

526 {
528  static TransactionId stablexid = InvalidTransactionId;
529 
530  if (lxid != MyProc->lxid)
531  {
532  lxid = MyProc->lxid;
533  stablexid = GetTopTransactionIdIfAny();
534  if (!TransactionIdIsValid(stablexid))
535  stablexid = ReadNewTransactionId();
536  }
537 
538  Assert(TransactionIdIsValid(stablexid));
539 
540  return stablexid;
541 }
uint32 TransactionId
Definition: c.h:514
PGPROC * MyProc
Definition: proc.c:67
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:516
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:409
#define Assert(condition)
Definition: c.h:739
static TransactionId ReadNewTransactionId(void)
Definition: transam.h:244
#define InvalidLocalTransactionId
Definition: lock.h:68
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:106

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 451 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

452 {
456 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
static TransactionStateData TopTransactionStateData
Definition: xact.c:222
static void AssignTransactionId(TransactionState s)
Definition: xact.c:553

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 467 of file xact.c.

References XactTopFullTransactionId.

468 {
470 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

Definition at line 394 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XidFromFullTransactionId.

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

395 {
399 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static TransactionStateData TopTransactionStateData
Definition: xact.c:222
static void AssignTransactionId(TransactionState s)
Definition: xact.c:553

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3444 of file xact.c.

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

Referenced by vacuum().

3445 {
3446  /*
3447  * Return true on same conditions that would make
3448  * PreventInTransactionBlock error out
3449  */
3450  if (IsTransactionBlock())
3451  return true;
3452 
3453  if (IsSubTransaction())
3454  return true;
3455 
3456  if (!isTopLevel)
3457  return true;
3458 
3461  return true;
3462 
3463  return false;
3464 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
bool IsTransactionBlock(void)
Definition: xact.c:4635
bool IsSubTransaction(void)
Definition: xact.c:4708

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

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

4636 {
4638 
4640  return false;
4641 
4642  return true;
4643 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 355 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(), GetMessageEncoding(), 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(), SearchCatCacheInternal(), SetMultiXactIdLimit(), SetTransactionIdLimit(), SnapBuildClearExportedSnapshot(), and SocketBackend().

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

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 509 of file xact.c.

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

Referenced by XLogInsertRecord().

510 {
513 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
FullTransactionId fullTransactionId
Definition: xact.c:173

◆ 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:514
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:514
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:514
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:692
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 3657 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().

3658 {
3659  TransactionState s;
3660  bool result;
3661 
3662  /* Set up to commit the current transaction */
3663  result = EndTransactionBlock(false);
3664 
3665  /* If successful, change outer tblock state to PREPARE */
3666  if (result)
3667  {
3669 
3670  while (s->parent != NULL)
3671  s = s->parent;
3672 
3673  if (s->blockState == TBLOCK_END)
3674  {
3675  /* Save GID where PrepareTransaction can find it again */
3677 
3679  }
3680  else
3681  {
3682  /*
3683  * ignore case where we are not in a transaction;
3684  * EndTransactionBlock already issued a warning.
3685  */
3688  /* Don't send back a PREPARE result tag... */
3689  result = false;
3690  }
3691  }
3692 
3693  return result;
3694 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
bool EndTransactionBlock(bool chain)
Definition: xact.c:3709
MemoryContext TopTransactionContext
Definition: mcxt.c:49
TBlockState blockState
Definition: xact.c:178
struct TransactionStateData * parent
Definition: xact.c:193
#define Assert(condition)
Definition: c.h:739
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1173
static char * prepareGID
Definition: xact.c:259

◆ PreventInTransactionBlock()

void PreventInTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

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

3332 {
3333  /*
3334  * xact block already started?
3335  */
3336  if (IsTransactionBlock())
3337  ereport(ERROR,
3338  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3339  /* translator: %s represents an SQL statement name */
3340  errmsg("%s cannot run inside a transaction block",
3341  stmtType)));
3342 
3343  /*
3344  * subtransaction?
3345  */
3346  if (IsSubTransaction())
3347  ereport(ERROR,
3348  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3349  /* translator: %s represents an SQL statement name */
3350  errmsg("%s cannot run inside a subtransaction",
3351  stmtType)));
3352 
3353  /*
3354  * inside a function call?
3355  */
3356  if (!isTopLevel)
3357  ereport(ERROR,
3358  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3359  /* translator: %s represents an SQL statement name */
3360  errmsg("%s cannot be executed from a function", stmtType)));
3361 
3362  /* If we got past IsTransactionBlock test, should be in default state */
3365  elog(FATAL, "cannot prevent transaction chain");
3366  /* all okay */
3367 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
bool IsTransactionBlock(void)
Definition: xact.c:4635
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
bool IsSubTransaction(void)
Definition: xact.c:4708
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ RegisterSubXactCallback()

void RegisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

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

3535 {
3536  SubXactCallbackItem *item;
3537 
3538  item = (SubXactCallbackItem *)
3540  item->callback = callback;
3541  item->arg = arg;
3542  item->next = SubXact_callbacks;
3543  SubXact_callbacks = item;
3544 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:298
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:293
SubXactCallback callback
Definition: xact.c:294
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 3479 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().

3480 {
3481  XactCallbackItem *item;
3482 
3483  item = (XactCallbackItem *)
3485  item->callback = callback;
3486  item->arg = arg;
3487  item->next = Xact_callbacks;
3488  Xact_callbacks = item;
3489 }
struct XactCallbackItem * next
Definition: xact.c:281
void * arg
Definition: xact.c:283
XactCallback callback
Definition: xact.c:282
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:286
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
void * arg

◆ ReleaseCurrentSubTransaction()

void ReleaseCurrentSubTransaction ( void  )

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

4431 {
4433 
4434  /*
4435  * Workers synchronize transaction state at the beginning of each parallel
4436  * operation, so we can't account for commit of subtransactions after that
4437  * point. This should not happen anyway. Code calling this would
4438  * typically have called BeginInternalSubTransaction() first, failing
4439  * there.
4440  */
4441  if (IsInParallelMode())
4442  ereport(ERROR,
4443  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4444  errmsg("cannot commit subtransactions during a parallel operation")));
4445 
4446  if (s->blockState != TBLOCK_SUBINPROGRESS)
4447  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4449  Assert(s->state == TRANS_INPROGRESS);
4452  s = CurrentTransactionState; /* changed by pop */
4453  Assert(s->state == TRANS_INPROGRESS);
4454 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
TransState state
Definition: xact.c:177
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4768
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define Assert(condition)
Definition: c.h:739
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ ReleaseSavepoint()

void ReleaseSavepoint ( const char *  name)

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

4124 {
4126  TransactionState target,
4127  xact;
4128 
4129  /*
4130  * Workers synchronize transaction state at the beginning of each parallel
4131  * operation, so we can't account for transaction state change after that
4132  * point. (Note that this check will certainly error out if s->blockState
4133  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4134  * below.)
4135  */
4136  if (IsInParallelMode())
4137  ereport(ERROR,
4138  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4139  errmsg("cannot release savepoints during a parallel operation")));
4140 
4141  switch (s->blockState)
4142  {
4143  /*
4144  * We can't release a savepoint if there is no savepoint defined.
4145  */
4146  case TBLOCK_INPROGRESS:
4147  ereport(ERROR,
4148  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4149  errmsg("savepoint \"%s\" does not exist", name)));
4150  break;
4151 
4153  /* See comment about implicit transactions in DefineSavepoint */
4154  ereport(ERROR,
4155  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4156  /* translator: %s represents an SQL statement name */
4157  errmsg("%s can only be used in transaction blocks",
4158  "RELEASE SAVEPOINT")));
4159  break;
4160 
4161  /*
4162  * We are in a non-aborted subtransaction. This is the only valid
4163  * case.
4164  */
4165  case TBLOCK_SUBINPROGRESS:
4166  break;
4167 
4168  /* These cases are invalid. */
4169  case TBLOCK_DEFAULT:
4170  case TBLOCK_STARTED:
4171  case TBLOCK_BEGIN:
4173  case TBLOCK_SUBBEGIN:
4174  case TBLOCK_END:
4175  case TBLOCK_SUBRELEASE:
4176  case TBLOCK_SUBCOMMIT:
4177  case TBLOCK_ABORT:
4178  case TBLOCK_SUBABORT:
4179  case TBLOCK_ABORT_END:
4180  case TBLOCK_SUBABORT_END:
4181  case TBLOCK_ABORT_PENDING:
4183  case TBLOCK_SUBRESTART:
4185  case TBLOCK_PREPARE:
4186  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
4188  break;
4189  }
4190 
4191  for (target = s; PointerIsValid(target); target = target->parent)
4192  {
4193  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4194  break;
4195  }
4196 
4197  if (!PointerIsValid(target))
4198  ereport(ERROR,
4199  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4200  errmsg("savepoint \"%s\" does not exist", name)));
4201 
4202  /* disallow crossing savepoint level boundaries */
4203  if (target->savepointLevel != s->savepointLevel)
4204  ereport(ERROR,
4205  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4206  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4207 
4208  /*
4209  * Mark "commit pending" all subtransactions up to the target
4210  * subtransaction. The actual commits will happen when control gets to
4211  * CommitTransactionCommand.
4212  */
4213  xact = CurrentTransactionState;
4214  for (;;)
4215  {
4217  xact->blockState = TBLOCK_SUBRELEASE;
4218  if (xact == target)
4219  break;
4220  xact = xact->parent;
4221  Assert(PointerIsValid(xact));
4222  }
4223 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:193
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define Assert(condition)
Definition: c.h:739
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define PointerIsValid(pointer)
Definition: c.h:633

◆ RequireTransactionBlock()

void RequireTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3397 of file xact.c.

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

3398 {
3399  CheckTransactionBlock(isTopLevel, true, stmtType);
3400 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3406

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2886 of file xact.c.

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

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

2887 {
2891 }
bool XactDeferrable
Definition: xact.c:80
static bool save_XactDeferrable
Definition: xact.c:2875
bool XactReadOnly
Definition: xact.c:77
static bool save_XactReadOnly
Definition: xact.c:2874
int XactIsoLevel
Definition: xact.c:74
static int save_XactIsoLevel
Definition: xact.c:2873

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

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

4465 {
4467 
4468  /*
4469  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4470  * during parallel operations. That's because we may be in the master,
4471  * recovering from an error thrown while we were in parallel mode. We
4472  * won't reach here in a worker, because BeginInternalSubTransaction()
4473  * will have failed.
4474  */
4475 
4476  switch (s->blockState)
4477  {
4478  /* Must be in a subtransaction */
4479  case TBLOCK_SUBINPROGRESS:
4480  case TBLOCK_SUBABORT:
4481  break;
4482 
4483  /* These cases are invalid. */
4484  case TBLOCK_DEFAULT:
4485  case TBLOCK_STARTED:
4486  case TBLOCK_BEGIN:
4489  case TBLOCK_SUBBEGIN:
4490  case TBLOCK_INPROGRESS:
4491  case TBLOCK_END:
4492  case TBLOCK_SUBRELEASE:
4493  case TBLOCK_SUBCOMMIT:
4494  case TBLOCK_ABORT:
4495  case TBLOCK_ABORT_END:
4496  case TBLOCK_SUBABORT_END:
4497  case TBLOCK_ABORT_PENDING:
4499  case TBLOCK_SUBRESTART:
4501  case TBLOCK_PREPARE:
4502  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4504  break;
4505  }
4506 
4507  /*
4508  * Abort the current subtransaction, if needed.
4509  */
4510  if (s->blockState == TBLOCK_SUBINPROGRESS)
4512 
4513  /* And clean it up, too */
4515 
4516  s = CurrentTransactionState; /* changed by pop */
4518  s->blockState == TBLOCK_INPROGRESS ||
4520  s->blockState == TBLOCK_STARTED);
4521 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define AssertState(condition)
Definition: c.h:742
TBlockState blockState
Definition: xact.c:178
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
static void AbortSubTransaction(void)
Definition: xact.c:4877
static void CleanupSubTransaction(void)
Definition: xact.c:5026
#define elog(elevel,...)
Definition: elog.h:228

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

4233 {
4235  TransactionState target,
4236  xact;
4237 
4238  /*
4239  * Workers synchronize transaction state at the beginning of each parallel
4240  * operation, so we can't account for transaction state change after that
4241  * point. (Note that this check will certainly error out if s->blockState
4242  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4243  * below.)
4244  */
4245  if (IsInParallelMode())
4246  ereport(ERROR,
4247  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4248  errmsg("cannot rollback to savepoints during a parallel operation")));
4249 
4250  switch (s->blockState)
4251  {
4252  /*
4253  * We can't rollback to a savepoint if there is no savepoint
4254  * defined.
4255  */
4256  case TBLOCK_INPROGRESS:
4257  case TBLOCK_ABORT:
4258  ereport(ERROR,
4259  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4260  errmsg("savepoint \"%s\" does not exist", name)));
4261  break;
4262 
4264  /* See comment about implicit transactions in DefineSavepoint */
4265  ereport(ERROR,
4266  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4267  /* translator: %s represents an SQL statement name */
4268  errmsg("%s can only be used in transaction blocks",
4269  "ROLLBACK TO SAVEPOINT")));
4270  break;
4271 
4272  /*
4273  * There is at least one savepoint, so proceed.
4274  */
4275  case TBLOCK_SUBINPROGRESS:
4276  case TBLOCK_SUBABORT:
4277  break;
4278 
4279  /* These cases are invalid. */
4280  case TBLOCK_DEFAULT:
4281  case TBLOCK_STARTED:
4282  case TBLOCK_BEGIN:
4284  case TBLOCK_SUBBEGIN:
4285  case TBLOCK_END:
4286  case TBLOCK_SUBRELEASE:
4287  case TBLOCK_SUBCOMMIT:
4288  case TBLOCK_ABORT_END:
4289  case TBLOCK_SUBABORT_END:
4290  case TBLOCK_ABORT_PENDING:
4292  case TBLOCK_SUBRESTART:
4294  case TBLOCK_PREPARE:
4295  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4297  break;
4298  }
4299 
4300  for (target = s; PointerIsValid(target); target = target->parent)
4301  {
4302  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4303  break;
4304  }
4305 
4306  if (!PointerIsValid(target))
4307  ereport(ERROR,
4308  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4309  errmsg("savepoint \"%s\" does not exist", name)));
4310 
4311  /* disallow crossing savepoint level boundaries */
4312  if (target->savepointLevel != s->savepointLevel)
4313  ereport(ERROR,
4314  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4315  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4316 
4317  /*
4318  * Mark "abort pending" all subtransactions up to the target
4319  * subtransaction. The actual aborts will happen when control gets to
4320  * CommitTransactionCommand.
4321  */
4322  xact = CurrentTransactionState;
4323  for (;;)
4324  {
4325  if (xact == target)
4326  break;
4327  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4329  else if (xact->blockState == TBLOCK_SUBABORT)
4331  else
4332  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4334  xact = xact->parent;
4335  Assert(PointerIsValid(xact));
4336  }
4337 
4338  /* And mark the target as "restart pending" */
4339  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4340  xact->blockState = TBLOCK_SUBRESTART;
4341  else if (xact->blockState == TBLOCK_SUBABORT)
4343  else
4344  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4346 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:193
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define Assert(condition)
Definition: c.h:739
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
#define PointerIsValid(pointer)
Definition: c.h:633

◆ SaveTransactionCharacteristics()

void SaveTransactionCharacteristics ( void  )

Definition at line 2878 of file xact.c.

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

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

2879 {
2883 }
bool XactDeferrable
Definition: xact.c:80
static bool save_XactDeferrable
Definition: xact.c:2875
bool XactReadOnly
Definition: xact.c:77
static bool save_XactReadOnly
Definition: xact.c:2874
int XactIsoLevel
Definition: xact.c:74
static int save_XactIsoLevel
Definition: xact.c:2873

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

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

5181 {
5182  TransactionState s;
5183  Size nxids = 0;
5184  Size i = 0;
5185  TransactionId *workspace;
5187 
5188  result = (SerializedTransactionState *) start_address;
5189 
5190  result->xactIsoLevel = XactIsoLevel;
5191  result->xactDeferrable = XactDeferrable;
5193  result->currentFullTransactionId =
5196 
5197  /*
5198  * If we're running in a parallel worker and launching a parallel worker
5199  * of our own, we can just pass along the information that was passed to
5200  * us.
5201  */
5202  if (nParallelCurrentXids > 0)
5203  {
5205  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5207  return;
5208  }
5209 
5210  /*
5211  * OK, we need to generate a sorted list of XIDs that our workers should
5212  * view as current. First, figure out how many there are.
5213  */
5214  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5215  {
5217  nxids = add_size(nxids, 1);
5218  nxids = add_size(nxids, s->nChildXids);
5219  }
5221  <= maxsize);
5222 
5223  /* Copy them to our scratch space. */
5224  workspace = palloc(nxids * sizeof(TransactionId));
5225  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5226  {
5228  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5229  memcpy(&workspace[i], s->childXids,
5230  s->nChildXids * sizeof(TransactionId));
5231  i += s->nChildXids;
5232  }
5233  Assert(i == nxids);
5234 
5235  /* Sort them. */
5236  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5237 
5238  /* Copy data into output area. */
5239  result->nParallelCurrentXids = nxids;
5240  memcpy(&result->parallelCurrentXids[0], workspace,
5241  nxids * sizeof(TransactionId));
5242 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
bool XactDeferrable
Definition: xact.c:80
static TransactionState CurrentTransactionState
Definition: xact.c:234
uint32 TransactionId
Definition: c.h:514
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:210
int nParallelCurrentXids
Definition: xact.c:108
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransactionId * childXids
Definition: xact.c:183
CommandId currentCommandId
Definition: xact.c:208
struct TransactionStateData * parent
Definition: xact.c:193
FullTransactionId fullTransactionId
Definition: xact.c:173
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
TransactionId * ParallelCurrentXids
Definition: xact.c:109
#define Assert(condition)
Definition: c.h:739
#define SerializedTransactionStateHeaderSize
Definition: xact.c:214
size_t Size
Definition: c.h:467
int XactIsoLevel
Definition: xact.c:74
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define qsort(a, b, c, d)
Definition: port.h:488
static CommandId currentCommandId
Definition: xact.c:241
FullTransactionId topFullTransactionId
Definition: xact.c:206
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:138
FullTransactionId currentFullTransactionId
Definition: xact.c:207

◆ SetCurrentStatementStartTimestamp()

void SetCurrentStatementStartTimestamp ( void  )

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

818 {
819  if (!IsParallelWorker())
821  else
823 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
static TimestampTz stmtStartTimestamp
Definition: xact.c:252
#define IsParallelWorker()
Definition: parallel.h:60
#define Assert(condition)
Definition: c.h:739

◆ SetParallelStartTimestamps()

void SetParallelStartTimestamps ( TimestampTz  xact_ts,
TimestampTz  stmt_ts 
)

Definition at line 771 of file xact.c.

References Assert, IsParallelWorker, stmtStartTimestamp, and xactStartTimestamp.

Referenced by ParallelWorkerMain().

772 {
774  xactStartTimestamp = xact_ts;
775  stmtStartTimestamp = stmt_ts;
776 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:252
#define IsParallelWorker()
Definition: parallel.h:60
#define Assert(condition)
Definition: c.h:739
static TimestampTz xactStartTimestamp
Definition: xact.c:251

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

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

5251 {
5253 
5255  StartTransaction();
5256 
5257  tstate = (SerializedTransactionState *) tstatespace;
5258  XactIsoLevel = tstate->xactIsoLevel;
5259  XactDeferrable = tstate->xactDeferrable;
5262  tstate->currentFullTransactionId;
5266 
5268 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
bool XactDeferrable
Definition: xact.c:80
static TransactionState CurrentTransactionState
Definition: xact.c:234
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:210
int nParallelCurrentXids
Definition: xact.c:108
TBlockState blockState
Definition: xact.c:178
CommandId currentCommandId
Definition: xact.c:208
FullTransactionId fullTransactionId
Definition: xact.c:173
static void StartTransaction(void)
Definition: xact.c:1886
TransactionId * ParallelCurrentXids
Definition: xact.c:109
#define Assert(condition)
Definition: c.h:739
int XactIsoLevel
Definition: xact.c:74
static CommandId currentCommandId
Definition: xact.c:241
FullTransactionId topFullTransactionId
Definition: xact.c:206
FullTransactionId currentFullTransactionId
Definition: xact.c:207

◆ StartTransactionCommand()

void StartTransactionCommand ( void  )

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

2798 {
2800 
2801  switch (s->blockState)
2802  {
2803  /*
2804  * if we aren't in a transaction block, we just do our usual start
2805  * transaction.
2806  */
2807  case TBLOCK_DEFAULT:
2808  StartTransaction();
2810  break;
2811 
2812  /*
2813  * We are somewhere in a transaction block or subtransaction and
2814  * about to start a new command. For now we do nothing, but
2815  * someday we may do command-local resource initialization. (Note
2816  * that any needed CommandCounterIncrement was done by the
2817  * previous CommitTransactionCommand.)
2818  */
2819  case TBLOCK_INPROGRESS:
2821  case TBLOCK_SUBINPROGRESS:
2822  break;
2823 
2824  /*
2825  * Here we are in a failed transaction block (one of the commands
2826  * caused an abort) so we do nothing but remain in the abort
2827  * state. Eventually we will get a ROLLBACK command which will
2828  * get us out of this state. (It is up to other code to ensure
2829  * that no commands other than ROLLBACK will be processed in these
2830  * states.)
2831  */
2832  case TBLOCK_ABORT:
2833  case TBLOCK_SUBABORT:
2834  break;
2835 
2836  /* These cases are invalid. */
2837  case TBLOCK_STARTED:
2838  case TBLOCK_BEGIN:
2840  case TBLOCK_SUBBEGIN:
2841  case TBLOCK_END:
2842  case TBLOCK_SUBRELEASE:
2843  case TBLOCK_SUBCOMMIT:
2844  case TBLOCK_ABORT_END:
2845  case TBLOCK_SUBABORT_END:
2846  case TBLOCK_ABORT_PENDING:
2848  case TBLOCK_SUBRESTART:
2850  case TBLOCK_PREPARE:
2851  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2853  break;
2854  }
2855 
2856  /*
2857  * We must switch to CurTransactionContext before returning. This is
2858  * already done if we called StartTransaction, otherwise not.
2859  */
2860  Assert(CurTransactionContext != NULL);
2862 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:50
TBlockState blockState
Definition: xact.c:178
#define ERROR
Definition: elog.h:43
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
static void StartTransaction(void)
Definition: xact.c:1886
#define Assert(condition)
Definition: c.h:739
#define elog(elevel,...)
Definition: elog.h:228

◆ SubTransactionIsActive()

bool SubTransactionIsActive ( SubTransactionId  subxid)

Definition at line 721 of file xact.c.

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

Referenced by fmgr_sql().

722 {
724 
725  for (s = CurrentTransactionState; s != NULL; s = s->parent)
726  {
727  if (s->state == TRANS_ABORT)
728  continue;
729  if (s->subTransactionId == subxid)
730  return true;
731  }
732  return false;
733 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TransState state
Definition: xact.c:177
struct TransactionStateData * parent
Definition: xact.c:193
SubTransactionId subTransactionId
Definition: xact.c:174

◆ TransactionBlockStatusCode()

char TransactionBlockStatusCode ( void  )

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

4668 {
4670 
4671  switch (s->blockState)
4672  {
4673  case TBLOCK_DEFAULT:
4674  case TBLOCK_STARTED:
4675  return 'I'; /* idle --- not in transaction */
4676  case TBLOCK_BEGIN:
4677  case TBLOCK_SUBBEGIN:
4678  case TBLOCK_INPROGRESS:
4681  case TBLOCK_SUBINPROGRESS:
4682  case TBLOCK_END:
4683  case TBLOCK_SUBRELEASE:
4684  case TBLOCK_SUBCOMMIT:
4685  case TBLOCK_PREPARE:
4686  return 'T'; /* in transaction */
4687  case TBLOCK_ABORT:
4688  case TBLOCK_SUBABORT:
4689  case TBLOCK_ABORT_END:
4690  case TBLOCK_SUBABORT_END:
4691  case TBLOCK_ABORT_PENDING:
4693  case TBLOCK_SUBRESTART:
4695  return 'E'; /* in failed transaction */
4696  }
4697 
4698  /* should never get here */
4699  elog(FATAL, "invalid transaction block state: %s",
4701  return 0; /* keep compiler quiet */
4702 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define elog(elevel,...)
Definition: elog.h:228

◆ TransactionIdIsCurrentTransactionId()

bool TransactionIdIsCurrentTransactionId ( TransactionId  xid)

Definition at line 853 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(), PredicateLockTuple(), RI_FKey_fk_upd_check_required(), SnapBuildWaitSnapshot(), test_lockmode_for_conflict(), TransactionIdIsInProgress(), and txid_status().

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

◆ UnregisterSubXactCallback()

void UnregisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3547 of file xact.c.

References SubXactCallbackItem::next, and pfree().

3548 {
3549  SubXactCallbackItem *item;
3550  SubXactCallbackItem *prev;
3551 
3552  prev = NULL;
3553  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3554  {
3555  if (item->callback == callback && item->arg == arg)
3556  {
3557  if (prev)
3558  prev->next = item->next;
3559  else
3560  SubXact_callbacks = item->next;
3561  pfree(item);
3562  break;
3563  }
3564  }
3565 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:298
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:293
void * arg

◆ UnregisterXactCallback()

void UnregisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3492 of file xact.c.

References XactCallbackItem::next, and pfree().

3493 {
3494  XactCallbackItem *item;
3495  XactCallbackItem *prev;
3496 
3497  prev = NULL;
3498  for (item = Xact_callbacks; item; prev = item, item = item->next)
3499  {
3500  if (item->callback == callback && item->arg == arg)
3501  {
3502  if (prev)
3503  prev->next = item->next;
3504  else
3505  Xact_callbacks = item->next;
3506  pfree(item);
3507  break;
3508  }
3509  }
3510 }
struct XactCallbackItem * next
Definition: xact.c:281
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:286
void * arg

◆ UserAbortTransactionBlock()

void UserAbortTransactionBlock ( bool  chain)

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

3870 {
3872 
3873  switch (s->blockState)
3874  {
3875  /*
3876  * We are inside a transaction block and we got a ROLLBACK command
3877  * from the user, so tell CommitTransactionCommand to abort and
3878  * exit the transaction block.
3879  */
3880  case TBLOCK_INPROGRESS:
3882  break;
3883 
3884  /*
3885  * We are inside a failed transaction block and we got a ROLLBACK
3886  * command from the user. Abort processing is already done, so
3887  * CommitTransactionCommand just has to cleanup and go back to
3888  * idle state.
3889  */
3890  case TBLOCK_ABORT:
3892  break;
3893 
3894  /*
3895  * We are inside a subtransaction. Mark everything up to top
3896  * level as exitable.
3897  */
3898  case TBLOCK_SUBINPROGRESS:
3899  case TBLOCK_SUBABORT:
3900  while (s->parent != NULL)
3901  {
3902  if (s->blockState == TBLOCK_SUBINPROGRESS)
3904  else if (s->blockState == TBLOCK_SUBABORT)
3906  else
3907  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3909  s = s->parent;
3910  }
3911  if (s->blockState == TBLOCK_INPROGRESS)
3913  else if (s->blockState == TBLOCK_ABORT)
3915  else
3916  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3918  break;
3919 
3920  /*
3921  * The user issued ABORT when not inside a transaction. For
3922  * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
3923  * The upcoming call to CommitTransactionCommand() will then put
3924  * us back into the default state. For ROLLBACK AND CHAIN, error.
3925  *
3926  * We do the same thing with ABORT inside an implicit transaction,
3927  * although in this case we might be rolling back actual database
3928  * state changes. (It's debatable whether we should issue a
3929  * WARNING in this case, but we have done so historically.)
3930  */
3931  case TBLOCK_STARTED:
3933  if (chain)
3934  ereport(ERROR,
3935  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3936  /* translator: %s represents an SQL statement name */
3937  errmsg("%s can only be used in transaction blocks",
3938  "ROLLBACK AND CHAIN")));
3939  else
3940  ereport(WARNING,
3941  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3942  errmsg("there is no transaction in progress")));
3944  break;
3945 
3946  /*
3947  * The user issued an ABORT that somehow ran inside a parallel
3948  * worker. We can't cope with that.
3949  */
3951  ereport(FATAL,
3952  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3953  errmsg("cannot abort during a parallel operation")));
3954  break;
3955 
3956  /* These cases are invalid. */
3957  case TBLOCK_DEFAULT:
3958  case TBLOCK_BEGIN:
3959  case TBLOCK_SUBBEGIN:
3960  case TBLOCK_END:
3961  case TBLOCK_SUBRELEASE:
3962  case TBLOCK_SUBCOMMIT:
3963  case TBLOCK_ABORT_END:
3964  case TBLOCK_SUBABORT_END:
3965  case TBLOCK_ABORT_PENDING:
3967  case TBLOCK_SUBRESTART:
3969  case TBLOCK_PREPARE:
3970  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3972  break;
3973  }
3974 
3977 
3978  s->chain = chain;
3979 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:608
TBlockState blockState
Definition: xact.c:178
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:193
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5338
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define Assert(condition)
Definition: c.h:739
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228

◆ WarnNoTransactionBlock()

void WarnNoTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3391 of file xact.c.

References CheckTransactionBlock().

Referenced by ExecSetVariableStmt(), and standard_ProcessUtility().

3392 {
3393  CheckTransactionBlock(isTopLevel, false, stmtType);
3394 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3406

◆ xact_desc()

void xact_desc ( StringInfo  buf,
XLogReaderState record 
)