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 3177 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().

3178 {
3180 
3181  switch (s->blockState)
3182  {
3183  case TBLOCK_DEFAULT:
3184  if (s->state == TRANS_DEFAULT)
3185  {
3186  /* we are idle, so nothing to do */
3187  }
3188  else
3189  {
3190  /*
3191  * We can get here after an error during transaction start
3192  * (state will be TRANS_START). Need to clean up the
3193  * incompletely started transaction. First, adjust the
3194  * low-level state to suppress warning message from
3195  * AbortTransaction.
3196  */
3197  if (s->state == TRANS_START)
3198  s->state = TRANS_INPROGRESS;
3199  AbortTransaction();
3201  }
3202  break;
3203 
3204  /*
3205  * If we aren't in a transaction block, we just do the basic abort
3206  * & cleanup transaction. For this purpose, we treat an implicit
3207  * transaction block as if it were a simple statement.
3208  */
3209  case TBLOCK_STARTED:
3211  AbortTransaction();
3214  break;
3215 
3216  /*
3217  * If we are in TBLOCK_BEGIN it means something screwed up right
3218  * after reading "BEGIN TRANSACTION". We assume that the user
3219  * will interpret the error as meaning the BEGIN failed to get him
3220  * into a transaction block, so we should abort and return to idle
3221  * state.
3222  */
3223  case TBLOCK_BEGIN:
3224  AbortTransaction();
3227  break;
3228 
3229  /*
3230  * We are somewhere in a transaction block and we've gotten a
3231  * failure, so we abort the transaction and set up the persistent
3232  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3233  */
3234  case TBLOCK_INPROGRESS:
3236  AbortTransaction();
3237  s->blockState = TBLOCK_ABORT;
3238  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3239  break;
3240 
3241  /*
3242  * Here, we failed while trying to COMMIT. Clean up the
3243  * transaction and return to idle state (we do not want to stay in
3244  * the transaction).
3245  */
3246  case TBLOCK_END:
3247  AbortTransaction();
3250  break;
3251 
3252  /*
3253  * Here, we are already in an aborted transaction state and are
3254  * waiting for a ROLLBACK, but for some reason we failed again! So
3255  * we just remain in the abort state.
3256  */
3257  case TBLOCK_ABORT:
3258  case TBLOCK_SUBABORT:
3259  break;
3260 
3261  /*
3262  * We are in a failed transaction and we got the ROLLBACK command.
3263  * We have already aborted, we just need to cleanup and go to idle
3264  * state.
3265  */
3266  case TBLOCK_ABORT_END:
3269  break;
3270 
3271  /*
3272  * We are in a live transaction and we got a ROLLBACK command.
3273  * Abort, cleanup, go to idle state.
3274  */
3275  case TBLOCK_ABORT_PENDING:
3276  AbortTransaction();
3279  break;
3280 
3281  /*
3282  * Here, we failed while trying to PREPARE. Clean up the
3283  * transaction and return to idle state (we do not want to stay in
3284  * the transaction).
3285  */
3286  case TBLOCK_PREPARE:
3287  AbortTransaction();
3290  break;
3291 
3292  /*
3293  * We got an error inside a subtransaction. Abort just the
3294  * subtransaction, and go to the persistent SUBABORT state until
3295  * we get ROLLBACK.
3296  */
3297  case TBLOCK_SUBINPROGRESS:
3300  break;
3301 
3302  /*
3303  * If we failed while trying to create a subtransaction, clean up
3304  * the broken subtransaction and abort the parent. The same
3305  * applies if we get a failure while ending a subtransaction.
3306  */
3307  case TBLOCK_SUBBEGIN:
3308  case TBLOCK_SUBRELEASE:
3309  case TBLOCK_SUBCOMMIT:
3311  case TBLOCK_SUBRESTART:
3315  break;
3316 
3317  /*
3318  * Same as above, except the Abort() was already done.
3319  */
3320  case TBLOCK_SUBABORT_END:
3324  break;
3325  }
3326 }
void AbortCurrentTransaction(void)
Definition: xact.c:3177
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:2763
static void AbortSubTransaction(void)
Definition: xact.c:4892
static void AbortTransaction(void)
Definition: xact.c:2581
static void CleanupSubTransaction(void)
Definition: xact.c:5041

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4547 {
4549 
4550  /* Ensure we're not running in a doomed memory context */
4551  AtAbort_Memory();
4552 
4553  /*
4554  * Get out of any transaction or nested transaction
4555  */
4556  do
4557  {
4558  switch (s->blockState)
4559  {
4560  case TBLOCK_DEFAULT:
4561  if (s->state == TRANS_DEFAULT)
4562  {
4563  /* Not in a transaction, do nothing */
4564  }
4565  else
4566  {
4567  /*
4568  * We can get here after an error during transaction start
4569  * (state will be TRANS_START). Need to clean up the
4570  * incompletely started transaction. First, adjust the
4571  * low-level state to suppress warning message from
4572  * AbortTransaction.
4573  */
4574  if (s->state == TRANS_START)
4575  s->state = TRANS_INPROGRESS;
4576  AbortTransaction();
4578  }
4579  break;
4580  case TBLOCK_STARTED:
4581  case TBLOCK_BEGIN:
4582  case TBLOCK_INPROGRESS:
4585  case TBLOCK_END:
4586  case TBLOCK_ABORT_PENDING:
4587  case TBLOCK_PREPARE:
4588  /* In a transaction, so clean up */
4589  AbortTransaction();
4592  break;
4593  case TBLOCK_ABORT:
4594  case TBLOCK_ABORT_END:
4595 
4596  /*
4597  * AbortTransaction is already done, still need Cleanup.
4598  * However, if we failed partway through running ROLLBACK,
4599  * there will be an active portal running that command, which
4600  * we need to shut down before doing CleanupTransaction.
4601  */
4602  AtAbort_Portals();
4605  break;
4606 
4607  /*
4608  * In a subtransaction, so clean it up and abort parent too
4609  */
4610  case TBLOCK_SUBBEGIN:
4611  case TBLOCK_SUBINPROGRESS:
4612  case TBLOCK_SUBRELEASE:
4613  case TBLOCK_SUBCOMMIT:
4615  case TBLOCK_SUBRESTART:
4618  s = CurrentTransactionState; /* changed by pop */
4619  break;
4620 
4621  case TBLOCK_SUBABORT:
4622  case TBLOCK_SUBABORT_END:
4624  /* As above, but AbortSubTransaction already done */
4625  if (s->curTransactionOwner)
4626  {
4627  /* As in TBLOCK_ABORT, might have a live portal to zap */
4632  }
4634  s = CurrentTransactionState; /* changed by pop */
4635  break;
4636  }
4637  } while (s->blockState != TBLOCK_DEFAULT);
4638 
4639  /* Should be out of all subxacts now */
4640  Assert(s->parent == NULL);
4641 
4642  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4643  AtCleanup_Memory();
4644 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
static void AtCleanup_Memory(void)
Definition: xact.c:1815
void AtAbort_Portals(void)
Definition: portalmem.c:774
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:2763
struct TransactionStateData * parent
Definition: xact.c:193
SubTransactionId subTransactionId
Definition: xact.c:174
static void AbortSubTransaction(void)
Definition: xact.c:4892
static void AtAbort_Memory(void)
Definition: xact.c:1725
static void AbortTransaction(void)
Definition: xact.c:2581
#define Assert(condition)
Definition: c.h:738
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:970
static void CleanupSubTransaction(void)
Definition: xact.c:5041

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 4006 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

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

3605 {
3607 
3608  switch (s->blockState)
3609  {
3610  /*
3611  * We are not inside a transaction block, so allow one to begin.
3612  */
3613  case TBLOCK_STARTED:
3614  s->blockState = TBLOCK_BEGIN;
3615  break;
3616 
3617  /*
3618  * BEGIN converts an implicit transaction block to a regular one.
3619  * (Note that we allow this even if we've already done some
3620  * commands, which is a bit odd but matches historical practice.)
3621  */
3623  s->blockState = TBLOCK_BEGIN;
3624  break;
3625 
3626  /*
3627  * Already a transaction block in progress.
3628  */
3629  case TBLOCK_INPROGRESS:
3631  case TBLOCK_SUBINPROGRESS:
3632  case TBLOCK_ABORT:
3633  case TBLOCK_SUBABORT:
3634  ereport(WARNING,
3635  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3636  errmsg("there is already a transaction in progress")));
3637  break;
3638 
3639  /* These cases are invalid. */
3640  case TBLOCK_DEFAULT:
3641  case TBLOCK_BEGIN:
3642  case TBLOCK_SUBBEGIN:
3643  case TBLOCK_END:
3644  case TBLOCK_SUBRELEASE:
3645  case TBLOCK_SUBCOMMIT:
3646  case TBLOCK_ABORT_END:
3647  case TBLOCK_SUBABORT_END:
3648  case TBLOCK_ABORT_PENDING:
3650  case TBLOCK_SUBRESTART:
3652  case TBLOCK_PREPARE:
3653  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3655  break;
3656  }
3657 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:178
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5353
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 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(), ATExecDropExpression(), ATExecDropIdentity(), ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), ATParseTransformCmd(), ATRewriteTables(), AttachPartitionEnsureIndexes(), CloneFkReferencing(), CommitSubTransaction(), CommitTransactionCommand(), copy_table_data(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyActionTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), CreateTrigger(), DefineCollation(), DefineDomain(), DefineQueryRewrite(), DefineRelation(), DefineVirtualRelation(), deleteOneObject(), DelRoleMems(), DropRole(), EventTriggerDDLCommandEnd(), EventTriggerDDLCommandStart(), EventTriggerInvoke(), EventTriggerSQLDrop(), EventTriggerTableRewrite(), exec_eval_simple_expr(), exec_execute_message(), exec_parse_message(), exec_simple_query(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), execute_sql_string(), ExplainOnePlan(), fmgr_sql(), ImportForeignSchema(), index_build(), index_create(), IndexSetParentIndex(), InitTempTableNamespace(), inv_create(), inv_drop(), inv_truncate(), inv_write(), LogicalRepSyncTableStart(), make_new_heap(), makeConfigurationDependencies(), moveArrayTypeName(), OperatorShellMake(), OperatorUpd(), pg_import_system_collations(), PortalRunMulti(), ProcedureCreate(), ProcessUtilitySlow(), recordExtensionInitPrivWorker(), reindex_relation(), ReindexRelationConcurrently(), RelationSetNewRelfilenode(), RenumberEnumType(), replorigin_create(), replorigin_drop(), ri_PerformCheck(), SetMatViewPopulatedState(), shdepReassignOwned(), SPI_cursor_open_internal(), standard_ProcessUtility(), StoreConstraints(), StorePartitionBound(), tryAttachPartitionForeignKey(), vacuum(), and validatePartitionedIndex().

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:610
bool IsInParallelMode(void)
Definition: xact.c:996
#define ERROR
Definition: elog.h:43
#define IsParallelWorker()
Definition: parallel.h:61
#define InvalidCommandId
Definition: c.h:530
#define ereport(elevel,...)
Definition: elog.h:144
static void AtCCI_LocalCache(void)
Definition: xact.c:1448
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:546
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
static CommandId currentCommandId
Definition: xact.c:241
static bool currentCommandIdUsed
Definition: xact.c:242

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

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

2914 {
2916 
2917  if (s->chain)
2919 
2920  switch (s->blockState)
2921  {
2922  /*
2923  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2924  * StartTransactionCommand didn't set the STARTED state
2925  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2926  * by EndParallelWorkerTransaction(), not this function.
2927  */
2928  case TBLOCK_DEFAULT:
2930  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2932  break;
2933 
2934  /*
2935  * If we aren't in a transaction block, just do our usual
2936  * transaction commit, and return to the idle state.
2937  */
2938  case TBLOCK_STARTED:
2941  break;
2942 
2943  /*
2944  * We are completing a "BEGIN TRANSACTION" command, so we change
2945  * to the "transaction block in progress" state and return. (We
2946  * assume the BEGIN did nothing to the database, so we need no
2947  * CommandCounterIncrement.)
2948  */
2949  case TBLOCK_BEGIN:
2951  break;
2952 
2953  /*
2954  * This is the case when we have finished executing a command
2955  * someplace within a transaction block. We increment the command
2956  * counter and return.
2957  */
2958  case TBLOCK_INPROGRESS:
2960  case TBLOCK_SUBINPROGRESS:
2962  break;
2963 
2964  /*
2965  * We are completing a "COMMIT" command. Do it and return to the
2966  * idle state.
2967  */
2968  case TBLOCK_END:
2971  if (s->chain)
2972  {
2973  StartTransaction();
2975  s->chain = false;
2977  }
2978  break;
2979 
2980  /*
2981  * Here we are in the middle of a transaction block but one of the
2982  * commands caused an abort so we do nothing but remain in the
2983  * abort state. Eventually we will get a ROLLBACK command.
2984  */
2985  case TBLOCK_ABORT:
2986  case TBLOCK_SUBABORT:
2987  break;
2988 
2989  /*
2990  * Here we were in an aborted transaction block and we just got
2991  * the ROLLBACK command from the user, so clean up the
2992  * already-aborted transaction and return to the idle state.
2993  */
2994  case TBLOCK_ABORT_END:
2997  if (s->chain)
2998  {
2999  StartTransaction();
3001  s->chain = false;
3003  }
3004  break;
3005 
3006  /*
3007  * Here we were in a perfectly good transaction block but the user
3008  * told us to ROLLBACK anyway. We have to abort the transaction
3009  * and then clean up.
3010  */
3011  case TBLOCK_ABORT_PENDING:
3012  AbortTransaction();
3015  if (s->chain)
3016  {
3017  StartTransaction();
3019  s->chain = false;
3021  }
3022  break;
3023 
3024  /*
3025  * We are completing a "PREPARE TRANSACTION" command. Do it and
3026  * return to the idle state.
3027  */
3028  case TBLOCK_PREPARE:
3031  break;
3032 
3033  /*
3034  * We were just issued a SAVEPOINT inside a transaction block.
3035  * Start a subtransaction. (DefineSavepoint already did
3036  * PushTransaction, so as to have someplace to put the SUBBEGIN
3037  * state.)
3038  */
3039  case TBLOCK_SUBBEGIN:
3042  break;
3043 
3044  /*
3045  * We were issued a RELEASE command, so we end the current
3046  * subtransaction and return to the parent transaction. The parent
3047  * might be ended too, so repeat till we find an INPROGRESS
3048  * transaction or subtransaction.
3049  */
3050  case TBLOCK_SUBRELEASE:
3051  do
3052  {
3054  s = CurrentTransactionState; /* changed by pop */
3055  } while (s->blockState == TBLOCK_SUBRELEASE);
3056 
3059  break;
3060 
3061  /*
3062  * We were issued a COMMIT, so we end the current subtransaction
3063  * hierarchy and perform final commit. We do this by rolling up
3064  * any subtransactions into their parent, which leads to O(N^2)
3065  * operations with respect to resource owners - this isn't that
3066  * bad until we approach a thousands of savepoints but is
3067  * necessary for correctness should after triggers create new
3068  * resource owners.
3069  */
3070  case TBLOCK_SUBCOMMIT:
3071  do
3072  {
3074  s = CurrentTransactionState; /* changed by pop */
3075  } while (s->blockState == TBLOCK_SUBCOMMIT);
3076  /* If we had a COMMIT command, finish off the main xact too */
3077  if (s->blockState == TBLOCK_END)
3078  {
3079  Assert(s->parent == NULL);
3082  }
3083  else if (s->blockState == TBLOCK_PREPARE)
3084  {
3085  Assert(s->parent == NULL);
3088  }
3089  else
3090  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3092  break;
3093 
3094  /*
3095  * The current already-failed subtransaction is ending due to a
3096  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3097  * examine the parent (which could be in any of several states).
3098  */
3099  case TBLOCK_SUBABORT_END:
3102  break;
3103 
3104  /*
3105  * As above, but it's not dead yet, so abort first.
3106  */
3111  break;
3112 
3113  /*
3114  * The current subtransaction is the target of a ROLLBACK TO
3115  * command. Abort and pop it, then start a new subtransaction
3116  * with the same name.
3117  */
3118  case TBLOCK_SUBRESTART:
3119  {
3120  char *name;
3121  int savepointLevel;
3122 
3123  /* save name and keep Cleanup from freeing it */
3124  name = s->name;
3125  s->name = NULL;
3126  savepointLevel = s->savepointLevel;
3127 
3130 
3131  DefineSavepoint(NULL);
3132  s = CurrentTransactionState; /* changed by push */
3133  s->name = name;
3134  s->savepointLevel = savepointLevel;
3135 
3136  /* This is the same as TBLOCK_SUBBEGIN case */
3140  }
3141  break;
3142 
3143  /*
3144  * Same as above, but the subtransaction had already failed, so we
3145  * don't need AbortSubTransaction.
3146  */
3148  {
3149  char *name;
3150  int savepointLevel;
3151 
3152  /* save name and keep Cleanup from freeing it */
3153  name = s->name;
3154  s->name = NULL;
3155  savepointLevel = s->savepointLevel;
3156 
3158 
3159  DefineSavepoint(NULL);
3160  s = CurrentTransactionState; /* changed by push */
3161  s->name = name;
3162  s->savepointLevel = savepointLevel;
3163 
3164  /* This is the same as TBLOCK_SUBBEGIN case */
3168  }
3169  break;
3170  }
3171 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
#define AssertState(condition)
Definition: c.h:741
void CommitTransactionCommand(void)
Definition: xact.c:2913
void DefineSavepoint(const char *name)
Definition: xact.c:4053
TBlockState blockState
Definition: xact.c:178
static void PrepareTransaction(void)
Definition: xact.c:2293
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4783
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2763
struct TransactionStateData * parent
Definition: xact.c:193
void SaveTransactionCharacteristics(void)
Definition: xact.c:2893
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5353
static void AbortSubTransaction(void)
Definition: xact.c:4892
void CommandCounterIncrement(void)
Definition: xact.c:1005
static void AbortTransaction(void)
Definition: xact.c:2581
static void StartTransaction(void)
Definition: xact.c:1886
#define Assert(condition)
Definition: c.h:738
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2901
static void CleanupSubTransaction(void)
Definition: xact.c:5041
const char * name
Definition: encode.c:521
#define elog(elevel,...)
Definition: elog.h:214
static void CommitTransaction(void)
Definition: xact.c:2045
static void StartSubTransaction(void)
Definition: xact.c:4746

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4031 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5290 of file xact.c.

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

Referenced by ParallelWorkerMain().

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

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

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

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 963 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

Referenced by _bt_begin_parallel(), begin_parallel_vacuum(), 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:738

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5167 of file xact.c.

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

Referenced by InitializeParallelDSM().

5168 {
5169  TransactionState s;
5170  Size nxids = 0;
5172 
5173  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5174  {
5176  nxids = add_size(nxids, 1);
5177  nxids = add_size(nxids, s->nChildXids);
5178  }
5179 
5180  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5181 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
uint32 TransactionId
Definition: c.h:513
#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:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
#define SerializedTransactionStateHeaderSize
Definition: xact.c:214
size_t Size
Definition: c.h:466

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 976 of file xact.c.

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

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

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:978
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:738

◆ 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:61
#define Assert(condition)
Definition: c.h:738
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:513
PGPROC * MyProc
Definition: proc.c:67
#define InvalidTransactionId
Definition: transam.h:31
uint32 LocalTransactionId
Definition: c.h:515
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:409
#define Assert(condition)
Definition: c.h:738
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 3459 of file xact.c.

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

Referenced by vacuum().

3460 {
3461  /*
3462  * Return true on same conditions that would make
3463  * PreventInTransactionBlock error out
3464  */
3465  if (IsTransactionBlock())
3466  return true;
3467 
3468  if (IsSubTransaction())
3469  return true;
3470 
3471  if (!isTopLevel)
3472  return true;
3473 
3476  return true;
3477 
3478  return false;
3479 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
TBlockState blockState
Definition: xact.c:178
bool IsTransactionBlock(void)
Definition: xact.c:4650
bool IsSubTransaction(void)
Definition: xact.c:4723

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

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

4651 {
4653 
4655  return false;
4656 
4657  return true;
4658 }
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(), InitializeClientEncoding(), LogLogicalMessage(), maybe_reread_subscription(), pg_attribute_noreturn(), pg_do_encoding_conversion(), PrepareClientEncoding(), PrepareTempTablespaces(), process_syncing_tables_for_apply(), process_syncing_tables_for_sync(), RelationClearRelation(), RelationIdGetRelation(), RelationInitPhysicalAddr(), RelationReloadNailed(), replorigin_create(), replorigin_drop(), report_untranslatable_char(), SearchCatCacheInternal(), SetMultiXactIdLimit(), SetTransactionIdLimit(), SnapBuildClearExportedSnapshot(), and SocketBackend().

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

◆ ParseCommitRecord()

void ParseCommitRecord ( uint8  info,
xl_xact_commit xlrec,
xl_xact_parsed_commit parsed 
)

Definition at line 34 of file xactdesc.c.

References xl_xact_dbinfo::dbId, xl_xact_parsed_commit::dbId, MinSizeOfXactCommit, MinSizeOfXactInvals, MinSizeOfXactRelfilenodes, MinSizeOfXactSubxacts, xl_xact_invals::msgs, xl_xact_parsed_commit::msgs, xl_xact_invals::nmsgs, xl_xact_parsed_commit::nmsgs, xl_xact_relfilenodes::nrels, xl_xact_parsed_commit::nrels, xl_xact_subxacts::nsubxacts, xl_xact_parsed_commit::nsubxacts, xl_xact_origin::origin_lsn, xl_xact_parsed_commit::origin_lsn, xl_xact_origin::origin_timestamp, xl_xact_parsed_commit::origin_timestamp, strlcpy(), xl_xact_subxacts::subxacts, xl_xact_parsed_commit::subxacts, xl_xact_dbinfo::tsId, xl_xact_parsed_commit::tsId, xl_xact_parsed_commit::twophase_gid, xl_xact_parsed_commit::twophase_xid, xl_xact_commit::xact_time, xl_xact_parsed_commit::xact_time, XACT_XINFO_HAS_DBINFO, XACT_XINFO_HAS_GID, XACT_XINFO_HAS_INVALS, XACT_XINFO_HAS_ORIGIN, XACT_XINFO_HAS_RELFILENODES, XACT_XINFO_HAS_SUBXACTS, XACT_XINFO_HAS_TWOPHASE, xl_xact_twophase::xid, xl_xact_xinfo::xinfo, xl_xact_parsed_commit::xinfo, XLOG_XACT_HAS_INFO, xl_xact_relfilenodes::xnodes, and xl_xact_parsed_commit::xnodes.

Referenced by DecodeXactOp(), recoveryStopsAfter(), recoveryStopsBefore(), xact_desc_commit(), and xact_redo().

35 {
36  char *data = ((char *) xlrec) + MinSizeOfXactCommit;
37 
38  memset(parsed, 0, sizeof(*parsed));
39 
40  parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
41  * present */
42 
43  parsed->xact_time = xlrec->xact_time;
44 
45  if (info & XLOG_XACT_HAS_INFO)
46  {
47  xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
48 
49  parsed->xinfo = xl_xinfo->xinfo;
50 
51  data += sizeof(xl_xact_xinfo);
52  }
53 
54  if (parsed->xinfo & XACT_XINFO_HAS_DBINFO)
55  {
56  xl_xact_dbinfo *xl_dbinfo = (xl_xact_dbinfo *) data;
57 
58  parsed->dbId = xl_dbinfo->dbId;
59  parsed->tsId = xl_dbinfo->tsId;
60 
61  data += sizeof(xl_xact_dbinfo);
62  }
63 
64  if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
65  {
66  xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
67 
68  parsed->nsubxacts = xl_subxacts->nsubxacts;
69  parsed->subxacts = xl_subxacts->subxacts;
70 
71  data += MinSizeOfXactSubxacts;
72  data += parsed->nsubxacts * sizeof(TransactionId);
73  }
74 
75  if (parsed->xinfo & XACT_XINFO_HAS_RELFILENODES)
76  {
77  xl_xact_relfilenodes *xl_relfilenodes = (xl_xact_relfilenodes *) data;
78 
79  parsed->nrels = xl_relfilenodes->nrels;
80  parsed->xnodes = xl_relfilenodes->xnodes;
81 
83  data += xl_relfilenodes->nrels * sizeof(RelFileNode);
84  }
85 
86  if (parsed->xinfo & XACT_XINFO_HAS_INVALS)
87  {
88  xl_xact_invals *xl_invals = (xl_xact_invals *) data;
89 
90  parsed->nmsgs = xl_invals->nmsgs;
91  parsed->msgs = xl_invals->msgs;
92 
93  data += MinSizeOfXactInvals;
94  data += xl_invals->nmsgs * sizeof(SharedInvalidationMessage);
95  }
96 
97  if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
98  {
99  xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
100 
101  parsed->twophase_xid = xl_twophase->xid;
102 
103  data += sizeof(xl_xact_twophase);
104 
105  if (parsed->xinfo & XACT_XINFO_HAS_GID)
106  {
107  strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid));
108  data += strlen(data) + 1;
109  }
110  }
111 
112  /* Note: no alignment is guaranteed after this point */
113 
114  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
115  {
116  xl_xact_origin xl_origin;
117 
118  /* no alignment is guaranteed, so copy onto stack */
119  memcpy(&xl_origin, data, sizeof(xl_origin));
120 
121  parsed->origin_lsn = xl_origin.origin_lsn;
122  parsed->origin_timestamp = xl_origin.origin_timestamp;
123 
124  data += sizeof(xl_xact_origin);
125  }
126 }
RelFileNode * xnodes
Definition: xact.h:330
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:167
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:513
TimestampTz origin_timestamp
Definition: xact.h:341
TransactionId xid
Definition: xact.h:256
TransactionId twophase_xid
Definition: xact.h:335
TransactionId * subxacts
Definition: xact.h:327
#define XLOG_XACT_HAS_INFO
Definition: xact.h:156
SharedInvalidationMessage * msgs
Definition: xact.h:333
XLogRecPtr origin_lsn
Definition: xact.h:340
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:163
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:245
Oid tsId
Definition: xact.h:230
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:243
#define MinSizeOfXactSubxacts
Definition: xact.h:238
#define MinSizeOfXactCommit
Definition: xact.h:278
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:250
struct RelFileNode RelFileNode
TimestampTz xact_time
Definition: xact.h:267
struct xl_xact_twophase xl_xact_twophase
#define MinSizeOfXactInvals
Definition: xact.h:252
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:162
XLogRecPtr origin_lsn
Definition: xact.h:261
uint32 xinfo
Definition: xact.h:224
int nsubxacts
Definition: xact.h:235
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:166
Oid dbId
Definition: xact.h:229
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz origin_timestamp
Definition: xact.h:262
#define XACT_XINFO_HAS_INVALS
Definition: xact.h:165
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:164
int nmsgs
Definition: xact.h:249
#define XACT_XINFO_HAS_GID
Definition: xact.h:169
char twophase_gid[GIDSIZE]
Definition: xact.h:336
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:236
TimestampTz xact_time
Definition: xact.h:320

◆ ParsePrepareRecord()

void ParsePrepareRecord ( uint8  info,
xl_xact_prepare xlrec,
xl_xact_parsed_prepare parsed 
)

Definition at line 216 of file xactdesc.c.

References xl_xact_parsed_commit::abortnodes, xl_xact_prepare::database, xl_xact_parsed_commit::dbId, xl_xact_prepare::gidlen, MAXALIGN, xl_xact_parsed_commit::msgs, xl_xact_prepare::nabortrels, xl_xact_parsed_commit::nabortrels, xl_xact_prepare::ncommitrels, xl_xact_prepare::ninvalmsgs, xl_xact_parsed_commit::nmsgs, xl_xact_parsed_commit::nrels, xl_xact_prepare::nsubxacts, xl_xact_parsed_commit::nsubxacts, xl_xact_prepare::origin_lsn, xl_xact_parsed_commit::origin_lsn, xl_xact_prepare::origin_timestamp, xl_xact_parsed_commit::origin_timestamp, xl_xact_prepare::prepared_at, xl_xact_parsed_commit::subxacts, xl_xact_parsed_commit::twophase_gid, xl_xact_parsed_commit::twophase_xid, xl_xact_parsed_commit::xact_time, xl_xact_prepare::xid, and xl_xact_parsed_commit::xnodes.

Referenced by xact_desc_prepare().

217 {
218  char *bufptr;
219 
220  bufptr = ((char *) xlrec) + MAXALIGN(sizeof(xl_xact_prepare));
221 
222  memset(parsed, 0, sizeof(*parsed));
223 
224  parsed->xact_time = xlrec->prepared_at;
225  parsed->origin_lsn = xlrec->origin_lsn;
226  parsed->origin_timestamp = xlrec->origin_timestamp;
227  parsed->twophase_xid = xlrec->xid;
228  parsed->dbId = xlrec->database;
229  parsed->nsubxacts = xlrec->nsubxacts;
230  parsed->nrels = xlrec->ncommitrels;
231  parsed->nabortrels = xlrec->nabortrels;
232  parsed->nmsgs = xlrec->ninvalmsgs;
233 
234  strncpy(parsed->twophase_gid, bufptr, xlrec->gidlen);
235  bufptr += MAXALIGN(xlrec->gidlen);
236 
237  parsed->subxacts = (TransactionId *) bufptr;
238  bufptr += MAXALIGN(xlrec->nsubxacts * sizeof(TransactionId));
239 
240  parsed->xnodes = (RelFileNode *) bufptr;
241  bufptr += MAXALIGN(xlrec->ncommitrels * sizeof(RelFileNode));
242 
243  parsed->abortnodes = (RelFileNode *) bufptr;
244  bufptr += MAXALIGN(xlrec->nabortrels * sizeof(RelFileNode));
245 
246  parsed->msgs = (SharedInvalidationMessage *) bufptr;
247  bufptr += MAXALIGN(xlrec->ninvalmsgs * sizeof(SharedInvalidationMessage));
248 }
RelFileNode * xnodes
Definition: xact.h:330
int32 ninvalmsgs
Definition: xact.h:306
int32 nsubxacts
Definition: xact.h:303
uint32 TransactionId
Definition: c.h:513
int32 ncommitrels
Definition: xact.h:304
TimestampTz origin_timestamp
Definition: xact.h:341
TransactionId twophase_xid
Definition: xact.h:335
TransactionId * subxacts
Definition: xact.h:327
SharedInvalidationMessage * msgs
Definition: xact.h:333
XLogRecPtr origin_lsn
Definition: xact.h:340
uint16 gidlen
Definition: xact.h:308
Oid database
Definition: xact.h:300
int32 nabortrels
Definition: xact.h:305
XLogRecPtr origin_lsn
Definition: xact.h:309
RelFileNode * abortnodes
Definition: xact.h:338
TimestampTz origin_timestamp
Definition: xact.h:310
#define MAXALIGN(LEN)
Definition: c.h:691
TransactionId xid
Definition: xact.h:299
TimestampTz prepared_at
Definition: xact.h:301
char twophase_gid[GIDSIZE]
Definition: xact.h:336
TimestampTz xact_time
Definition: xact.h:320

◆ PrepareTransactionBlock()

bool PrepareTransactionBlock ( const char *  gid)

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

3673 {
3674  TransactionState s;
3675  bool result;
3676 
3677  /* Set up to commit the current transaction */
3678  result = EndTransactionBlock(false);
3679 
3680  /* If successful, change outer tblock state to PREPARE */
3681  if (result)
3682  {
3684 
3685  while (s->parent != NULL)
3686  s = s->parent;
3687 
3688  if (s->blockState == TBLOCK_END)
3689  {
3690  /* Save GID where PrepareTransaction can find it again */
3692 
3694  }
3695  else
3696  {
3697  /*
3698  * ignore case where we are not in a transaction;
3699  * EndTransactionBlock already issued a warning.
3700  */
3703  /* Don't send back a PREPARE result tag... */
3704  result = false;
3705  }
3706  }
3707 
3708  return result;
3709 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
bool EndTransactionBlock(bool chain)
Definition: xact.c:3724
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:738
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 3346 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().

3347 {
3348  /*
3349  * xact block already started?
3350  */
3351  if (IsTransactionBlock())
3352  ereport(ERROR,
3353  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3354  /* translator: %s represents an SQL statement name */
3355  errmsg("%s cannot run inside a transaction block",
3356  stmtType)));
3357 
3358  /*
3359  * subtransaction?
3360  */
3361  if (IsSubTransaction())
3362  ereport(ERROR,
3363  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3364  /* translator: %s represents an SQL statement name */
3365  errmsg("%s cannot run inside a subtransaction",
3366  stmtType)));
3367 
3368  /*
3369  * inside a function call?
3370  */
3371  if (!isTopLevel)
3372  ereport(ERROR,
3373  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3374  /* translator: %s represents an SQL statement name */
3375  errmsg("%s cannot be executed from a function", stmtType)));
3376 
3377  /* If we got past IsTransactionBlock test, should be in default state */
3380  elog(FATAL, "cannot prevent transaction chain");
3381  /* all okay */
3382 }
static TransactionState CurrentTransactionState
Definition: xact.c:234
int errcode(int sqlerrcode)
Definition: elog.c:610
TBlockState blockState
Definition: xact.c:178
bool IsTransactionBlock(void)
Definition: xact.c:4650
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define ereport(elevel,...)
Definition: elog.h:144
bool IsSubTransaction(void)
Definition: xact.c:4723
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ RegisterSubXactCallback()

void RegisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

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

3550 {
3551  SubXactCallbackItem *item;
3552 
3553  item = (SubXactCallbackItem *)
3555  item->callback = callback;
3556  item->arg = arg;
3557  item->next = SubXact_callbacks;
3558  SubXact_callbacks = item;
3559 }
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 3494 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().

3495 {
3496  XactCallbackItem *item;
3497 
3498  item = (XactCallbackItem *)
3500  item->callback = callback;
3501  item->arg = arg;
3502  item->next = Xact_callbacks;
3503  Xact_callbacks = item;
3504 }
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 4445 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().

4446 {
4448 
4449  /*
4450  * Workers synchronize transaction state at the beginning of each parallel
4451  * operation, so we can't account for commit of subtransactions after that
4452  * point. This should not happen anyway. Code calling this would
4453  * typically have called BeginInternalSubTransaction() first, failing
4454  * there.
4455  */
4456  if (IsInParallelMode())
4457  ereport(ERROR,
4458  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4459  errmsg("cannot commit subtransactions during a parallel operation")));
4460 
4461  if (s->blockState != TBLOCK_SUBINPROGRESS)
4462  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4464  Assert(s->state == TRANS_INPROGRESS);
4467  s = CurrentTransactionState; /* changed by pop */
4468  Assert(s->state == TRANS_INPROGRESS);
4469 }
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:610
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:4783
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5353
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:738
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214

◆ ReleaseSavepoint()

void ReleaseSavepoint ( const char *  name)

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

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

◆ RequireTransactionBlock()

void RequireTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3412 of file xact.c.

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

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

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2901 of file xact.c.

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

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

2902 {
2906 }
bool XactDeferrable
Definition: xact.c:80
static bool save_XactDeferrable
Definition: xact.c:2890
bool XactReadOnly
Definition: xact.c:77
static bool save_XactReadOnly
Definition: xact.c:2889
int XactIsoLevel
Definition: xact.c:74
static int save_XactIsoLevel
Definition: xact.c:2888

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

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

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

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

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

◆ SaveTransactionCharacteristics()

void SaveTransactionCharacteristics ( void  )

Definition at line 2893 of file xact.c.

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

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

2894 {
2898 }
bool XactDeferrable
Definition: xact.c:80
static bool save_XactDeferrable
Definition: xact.c:2890
bool XactReadOnly
Definition: xact.c:77
static bool save_XactReadOnly
Definition: xact.c:2889
int XactIsoLevel
Definition: xact.c:74
static int save_XactIsoLevel
Definition: xact.c:2888

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

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

5196 {
5197  TransactionState s;
5198  Size nxids = 0;
5199  Size i = 0;
5200  TransactionId *workspace;
5202 
5203  result = (SerializedTransactionState *) start_address;
5204 
5205  result->xactIsoLevel = XactIsoLevel;
5206  result->xactDeferrable = XactDeferrable;
5208  result->currentFullTransactionId =
5211 
5212  /*
5213  * If we're running in a parallel worker and launching a parallel worker
5214  * of our own, we can just pass along the information that was passed to
5215  * us.
5216  */
5217  if (nParallelCurrentXids > 0)
5218  {
5220  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5222  return;
5223  }
5224 
5225  /*
5226  * OK, we need to generate a sorted list of XIDs that our workers should
5227  * view as current. First, figure out how many there are.
5228  */
5229  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5230  {
5232  nxids = add_size(nxids, 1);
5233  nxids = add_size(nxids, s->nChildXids);
5234  }
5236  <= maxsize);
5237 
5238  /* Copy them to our scratch space. */
5239  workspace = palloc(nxids * sizeof(TransactionId));
5240  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5241  {
5243  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5244  memcpy(&workspace[i], s->childXids,
5245  s->nChildXids * sizeof(TransactionId));
5246  i += s->nChildXids;
5247  }
5248  Assert(i == nxids);
5249 
5250  /* Sort them. */
5251  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5252 
5253  /* Copy data into output area. */
5254  result->nParallelCurrentXids = nxids;
5255  memcpy(&result->parallelCurrentXids[0], workspace,
5256  nxids * sizeof(TransactionId));
5257 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:107
bool XactDeferrable
Definition: xact.c:80
static TransactionState CurrentTransactionState
Definition: xact.c:234
uint32 TransactionId
Definition: c.h:513
#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:498
TransactionId * ParallelCurrentXids
Definition: xact.c:109
#define Assert(condition)
Definition: c.h:738
#define SerializedTransactionStateHeaderSize
Definition: xact.c:214
size_t Size
Definition: c.h:466
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:479
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:61
#define Assert(condition)
Definition: c.h:738

◆ 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:61
#define Assert(condition)
Definition: c.h:738
static TimestampTz xactStartTimestamp
Definition: xact.c:251

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

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

5266 {
5268 
5270  StartTransaction();
5271 
5272  tstate = (SerializedTransactionState *) tstatespace;
5273  XactIsoLevel = tstate->xactIsoLevel;
5274  XactDeferrable = tstate->xactDeferrable;
5277  tstate->currentFullTransactionId;
5281 
5283 }
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:738
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 2812 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().

2813 {
2815 
2816  switch (s->blockState)
2817  {
2818  /*
2819  * if we aren't in a transaction block, we just do our usual start
2820  * transaction.
2821  */
2822  case TBLOCK_DEFAULT:
2823  StartTransaction();
2825  break;
2826 
2827  /*
2828  * We are somewhere in a transaction block or subtransaction and
2829  * about to start a new command. For now we do nothing, but
2830  * someday we may do command-local resource initialization. (Note
2831  * that any needed CommandCounterIncrement was done by the
2832  * previous CommitTransactionCommand.)
2833  */
2834  case TBLOCK_INPROGRESS:
2836  case TBLOCK_SUBINPROGRESS:
2837  break;
2838 
2839  /*
2840  * Here we are in a failed transaction block (one of the commands
2841  * caused an abort) so we do nothing but remain in the abort
2842  * state. Eventually we will get a ROLLBACK command which will
2843  * get us out of this state. (It is up to other code to ensure
2844  * that no commands other than ROLLBACK will be processed in these
2845  * states.)
2846  */
2847  case TBLOCK_ABORT:
2848  case TBLOCK_SUBABORT:
2849  break;
2850 
2851  /* These cases are invalid. */
2852  case TBLOCK_STARTED:
2853  case TBLOCK_BEGIN:
2855  case TBLOCK_SUBBEGIN:
2856  case TBLOCK_END:
2857  case TBLOCK_SUBRELEASE:
2858  case TBLOCK_SUBCOMMIT:
2859  case TBLOCK_ABORT_END:
2860  case TBLOCK_SUBABORT_END:
2861  case TBLOCK_ABORT_PENDING:
2863  case TBLOCK_SUBRESTART:
2865  case TBLOCK_PREPARE:
2866  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2868  break;
2869  }
2870 
2871  /*
2872  * We must switch to CurTransactionContext before returning. This is
2873  * already done if we called StartTransaction, otherwise not.
2874  */
2875  Assert(CurTransactionContext != NULL);
2877 }
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:5353
static void StartTransaction(void)
Definition: xact.c:1886
#define Assert(condition)
Definition: c.h:738
#define elog(elevel,...)
Definition: elog.h:214

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

4683 {
4685 
4686  switch (s->blockState)
4687  {
4688  case TBLOCK_DEFAULT:
4689  case TBLOCK_STARTED:
4690  return 'I'; /* idle --- not in transaction */
4691  case TBLOCK_BEGIN:
4692  case TBLOCK_SUBBEGIN:
4693  case TBLOCK_INPROGRESS:
4696  case TBLOCK_SUBINPROGRESS:
4697  case TBLOCK_END:
4698  case TBLOCK_SUBRELEASE:
4699  case TBLOCK_SUBCOMMIT:
4700  case TBLOCK_PREPARE:
4701  return 'T'; /* in transaction */
4702  case TBLOCK_ABORT:
4703  case TBLOCK_SUBABORT:
4704  case TBLOCK_ABORT_END:
4705  case TBLOCK_SUBABORT_END:
4706  case TBLOCK_ABORT_PENDING:
4708  case TBLOCK_SUBRESTART:
4710  return 'E'; /* in failed transaction */
4711  }
4712 
4713  /* should never get here */
4714  elog(FATAL, "invalid transaction block state: %s",
4716  return 0; /* keep compiler quiet */
4717 }
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:5353
#define elog(elevel,...)
Definition: elog.h:214

◆ 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(), PredicateLockTID(), 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:513
#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 3562 of file xact.c.

References SubXactCallbackItem::next, and pfree().

3563 {
3564  SubXactCallbackItem *item;
3565  SubXactCallbackItem *prev;
3566 
3567  prev = NULL;
3568  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3569  {
3570  if (item->callback == callback && item->arg == arg)
3571  {
3572  if (prev)
3573  prev->next = item->next;
3574  else
3575  SubXact_callbacks = item->next;
3576  pfree(item);
3577  break;
3578  }
3579  }
3580 }
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 3507 of file xact.c.

References XactCallbackItem::next, and pfree().

3508 {
3509  XactCallbackItem *item;
3510  XactCallbackItem *prev;
3511 
3512  prev = NULL;
3513  for (item = Xact_callbacks; item; prev = item, item = item->next)
3514  {
3515  if (item->callback == callback && item->arg == arg)
3516  {
3517  if (prev)
3518  prev->next = item->next;
3519  else
3520  Xact_callbacks = item->next;
3521  pfree(item);
3522  break;
3523  }
3524  }
3525 }
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 3884 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().

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

◆ WarnNoTransactionBlock()

void WarnNoTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3406 of file xact.c.

References CheckTransactionBlock().

Referenced by ExecSetVariableStmt(), and standard_ProcessUtility().

3407 {
3408  CheckTransactionBlock(isTopLevel, false, stmtType);
3409 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3421

◆ xact_desc()

void xact_desc ( StringInfo  buf,
XLogReaderState record 
)