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_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_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 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

Definition at line 144 of file xact.h.

Referenced by DecodeXactOp(), EndPrepare(), xact_identify(), xact_redo(), and XlogReadTwoPhaseData().

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

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

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

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

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

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 3989 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

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

◆ BeginTransactionBlock()

void BeginTransactionBlock ( void  )

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

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

◆ CommandCounterIncrement()

void CommandCounterIncrement ( void  )

Definition at line 1003 of file xact.c.

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

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

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

◆ CommitTransactionCommand()

void CommitTransactionCommand ( void  )

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

2896 {
2898 
2899  if (s->chain)
2901 
2902  switch (s->blockState)
2903  {
2904  /*
2905  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2906  * StartTransactionCommand didn't set the STARTED state
2907  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2908  * by EndParallelWorkerTransaction(), not this function.
2909  */
2910  case TBLOCK_DEFAULT:
2912  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2914  break;
2915 
2916  /*
2917  * If we aren't in a transaction block, just do our usual
2918  * transaction commit, and return to the idle state.
2919  */
2920  case TBLOCK_STARTED:
2923  break;
2924 
2925  /*
2926  * We are completing a "BEGIN TRANSACTION" command, so we change
2927  * to the "transaction block in progress" state and return. (We
2928  * assume the BEGIN did nothing to the database, so we need no
2929  * CommandCounterIncrement.)
2930  */
2931  case TBLOCK_BEGIN:
2933  break;
2934 
2935  /*
2936  * This is the case when we have finished executing a command
2937  * someplace within a transaction block. We increment the command
2938  * counter and return.
2939  */
2940  case TBLOCK_INPROGRESS:
2942  case TBLOCK_SUBINPROGRESS:
2944  break;
2945 
2946  /*
2947  * We are completing a "COMMIT" command. Do it and return to the
2948  * idle state.
2949  */
2950  case TBLOCK_END:
2953  if (s->chain)
2954  {
2955  StartTransaction();
2957  s->chain = false;
2959  }
2960  break;
2961 
2962  /*
2963  * Here we are in the middle of a transaction block but one of the
2964  * commands caused an abort so we do nothing but remain in the
2965  * abort state. Eventually we will get a ROLLBACK command.
2966  */
2967  case TBLOCK_ABORT:
2968  case TBLOCK_SUBABORT:
2969  break;
2970 
2971  /*
2972  * Here we were in an aborted transaction block and we just got
2973  * the ROLLBACK command from the user, so clean up the
2974  * already-aborted transaction and return to the idle state.
2975  */
2976  case TBLOCK_ABORT_END:
2979  if (s->chain)
2980  {
2981  StartTransaction();
2983  s->chain = false;
2985  }
2986  break;
2987 
2988  /*
2989  * Here we were in a perfectly good transaction block but the user
2990  * told us to ROLLBACK anyway. We have to abort the transaction
2991  * and then clean up.
2992  */
2993  case TBLOCK_ABORT_PENDING:
2994  AbortTransaction();
2997  if (s->chain)
2998  {
2999  StartTransaction();
3001  s->chain = false;
3003  }
3004  break;
3005 
3006  /*
3007  * We are completing a "PREPARE TRANSACTION" command. Do it and
3008  * return to the idle state.
3009  */
3010  case TBLOCK_PREPARE:
3013  break;
3014 
3015  /*
3016  * We were just issued a SAVEPOINT inside a transaction block.
3017  * Start a subtransaction. (DefineSavepoint already did
3018  * PushTransaction, so as to have someplace to put the SUBBEGIN
3019  * state.)
3020  */
3021  case TBLOCK_SUBBEGIN:
3024  break;
3025 
3026  /*
3027  * We were issued a RELEASE command, so we end the current
3028  * subtransaction and return to the parent transaction. The parent
3029  * might be ended too, so repeat till we find an INPROGRESS
3030  * transaction or subtransaction.
3031  */
3032  case TBLOCK_SUBRELEASE:
3033  do
3034  {
3036  s = CurrentTransactionState; /* changed by pop */
3037  } while (s->blockState == TBLOCK_SUBRELEASE);
3038 
3041  break;
3042 
3043  /*
3044  * We were issued a COMMIT, so we end the current subtransaction
3045  * hierarchy and perform final commit. We do this by rolling up
3046  * any subtransactions into their parent, which leads to O(N^2)
3047  * operations with respect to resource owners - this isn't that
3048  * bad until we approach a thousands of savepoints but is
3049  * necessary for correctness should after triggers create new
3050  * resource owners.
3051  */
3052  case TBLOCK_SUBCOMMIT:
3053  do
3054  {
3056  s = CurrentTransactionState; /* changed by pop */
3057  } while (s->blockState == TBLOCK_SUBCOMMIT);
3058  /* If we had a COMMIT command, finish off the main xact too */
3059  if (s->blockState == TBLOCK_END)
3060  {
3061  Assert(s->parent == NULL);
3064  }
3065  else if (s->blockState == TBLOCK_PREPARE)
3066  {
3067  Assert(s->parent == NULL);
3070  }
3071  else
3072  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
3074  break;
3075 
3076  /*
3077  * The current already-failed subtransaction is ending due to a
3078  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
3079  * examine the parent (which could be in any of several states).
3080  */
3081  case TBLOCK_SUBABORT_END:
3084  break;
3085 
3086  /*
3087  * As above, but it's not dead yet, so abort first.
3088  */
3093  break;
3094 
3095  /*
3096  * The current subtransaction is the target of a ROLLBACK TO
3097  * command. Abort and pop it, then start a new subtransaction
3098  * with the same name.
3099  */
3100  case TBLOCK_SUBRESTART:
3101  {
3102  char *name;
3103  int savepointLevel;
3104 
3105  /* save name and keep Cleanup from freeing it */
3106  name = s->name;
3107  s->name = NULL;
3108  savepointLevel = s->savepointLevel;
3109 
3112 
3113  DefineSavepoint(NULL);
3114  s = CurrentTransactionState; /* changed by push */
3115  s->name = name;
3116  s->savepointLevel = savepointLevel;
3117 
3118  /* This is the same as TBLOCK_SUBBEGIN case */
3122  }
3123  break;
3124 
3125  /*
3126  * Same as above, but the subtransaction had already failed, so we
3127  * don't need AbortSubTransaction.
3128  */
3130  {
3131  char *name;
3132  int savepointLevel;
3133 
3134  /* save name and keep Cleanup from freeing it */
3135  name = s->name;
3136  s->name = NULL;
3137  savepointLevel = s->savepointLevel;
3138 
3140 
3141  DefineSavepoint(NULL);
3142  s = CurrentTransactionState; /* changed by push */
3143  s->name = name;
3144  s->savepointLevel = savepointLevel;
3145 
3146  /* This is the same as TBLOCK_SUBBEGIN case */
3150  }
3151  break;
3152  }
3153 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
#define AssertState(condition)
Definition: c.h:735
void CommitTransactionCommand(void)
Definition: xact.c:2895
void DefineSavepoint(const char *name)
Definition: xact.c:4036
TBlockState blockState
Definition: xact.c:179
static void PrepareTransaction(void)
Definition: xact.c:2283
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4766
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2745
struct TransactionStateData * parent
Definition: xact.c:194
void SaveTransactionCharacteristics(void)
Definition: xact.c:2875
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5336
static void AbortSubTransaction(void)
Definition: xact.c:4875
void CommandCounterIncrement(void)
Definition: xact.c:1003
static void AbortTransaction(void)
Definition: xact.c:2564
static void StartTransaction(void)
Definition: xact.c:1884
#define Assert(condition)
Definition: c.h:732
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2883
static void CleanupSubTransaction(void)
Definition: xact.c:5024
const char * name
Definition: encode.c:521
#define elog(elevel,...)
Definition: elog.h:226
static void CommitTransaction(void)
Definition: xact.c:2043
static void StartSubTransaction(void)
Definition: xact.c:4729

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

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

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 4014 of file xact.c.

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

Referenced by exec_simple_query().

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

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5273 of file xact.c.

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

Referenced by ParallelWorkerMain().

5274 {
5278 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
#define Assert(condition)
Definition: c.h:732
static void CommitTransaction(void)
Definition: xact.c:2043

◆ EndTransactionBlock()

bool EndTransactionBlock ( bool  chain)

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

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

◆ EnterParallelMode()

void EnterParallelMode ( void  )

Definition at line 961 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

962 {
964 
965  Assert(s->parallelModeLevel >= 0);
966 
967  ++s->parallelModeLevel;
968 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:732

◆ EstimateTransactionStateSpace()

Size EstimateTransactionStateSpace ( void  )

Definition at line 5150 of file xact.c.

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

Referenced by InitializeParallelDSM().

5151 {
5152  TransactionState s;
5153  Size nxids = 0;
5155 
5156  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5157  {
5159  nxids = add_size(nxids, 1);
5160  nxids = add_size(nxids, s->nChildXids);
5161  }
5162 
5163  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5164 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
uint32 TransactionId
Definition: c.h:507
#define FullTransactionIdIsValid(x)
Definition: transam.h:51
struct TransactionStateData * parent
Definition: xact.c:194
FullTransactionId fullTransactionId
Definition: xact.c:174
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define SerializedTransactionStateHeaderSize
Definition: xact.c:215
size_t Size
Definition: c.h:466

◆ ExitParallelMode()

void ExitParallelMode ( void  )

Definition at line 974 of file xact.c.

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

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

975 {
977 
978  Assert(s->parallelModeLevel > 0);
980 
981  --s->parallelModeLevel;
982 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
bool ParallelContextActive(void)
Definition: parallel.c:945
int parallelModeLevel
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:732

◆ ForceSyncCommit()

void ForceSyncCommit ( void  )

Definition at line 1051 of file xact.c.

References forceSyncCommit.

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

1052 {
1053  forceSyncCommit = true;
1054 }
static bool forceSyncCommit
Definition: xact.c:265

◆ GetCurrentCommandId()

CommandId GetCurrentCommandId ( bool  used)

Definition at line 746 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and IsParallelWorker.

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

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

◆ GetCurrentFullTransactionId()

FullTransactionId GetCurrentFullTransactionId ( void  )

Definition at line 481 of file xact.c.

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

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

◆ GetCurrentFullTransactionIdIfAny()

FullTransactionId GetCurrentFullTransactionIdIfAny ( void  )

Definition at line 499 of file xact.c.

References TransactionStateData::fullTransactionId.

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

◆ GetCurrentStatementStartTimestamp()

TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 792 of file xact.c.

References stmtStartTimestamp.

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

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

◆ GetCurrentSubTransactionId()

◆ GetCurrentTransactionId()

◆ GetCurrentTransactionIdIfAny()

TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 440 of file xact.c.

References TransactionStateData::fullTransactionId, and XidFromFullTransactionId.

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

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

◆ GetCurrentTransactionNestLevel()

◆ GetCurrentTransactionStartTimestamp()

TimestampTz GetCurrentTransactionStartTimestamp ( void  )

◆ GetCurrentTransactionStopTimestamp()

TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 804 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

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

◆ GetStableLatestTransactionId()

TransactionId GetStableLatestTransactionId ( void  )

Definition at line 526 of file xact.c.

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

Referenced by xid_age().

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

◆ GetTopFullTransactionId()

FullTransactionId GetTopFullTransactionId ( void  )

Definition at line 452 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XactTopFullTransactionId.

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

◆ GetTopFullTransactionIdIfAny()

FullTransactionId GetTopFullTransactionIdIfAny ( void  )

Definition at line 468 of file xact.c.

References XactTopFullTransactionId.

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

◆ GetTopTransactionId()

TransactionId GetTopTransactionId ( void  )

Definition at line 395 of file xact.c.

References AssignTransactionId(), FullTransactionIdIsValid, and XidFromFullTransactionId.

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

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

◆ GetTopTransactionIdIfAny()

◆ IsAbortedTransactionBlockState()

◆ IsInParallelMode()

◆ IsInTransactionBlock()

bool IsInTransactionBlock ( bool  isTopLevel)

Definition at line 3442 of file xact.c.

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

Referenced by vacuum().

3443 {
3444  /*
3445  * Return true on same conditions that would make
3446  * PreventInTransactionBlock error out
3447  */
3448  if (IsTransactionBlock())
3449  return true;
3450 
3451  if (IsSubTransaction())
3452  return true;
3453 
3454  if (!isTopLevel)
3455  return true;
3456 
3459  return true;
3460 
3461  return false;
3462 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179
bool IsTransactionBlock(void)
Definition: xact.c:4633
bool IsSubTransaction(void)
Definition: xact.c:4706

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

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

4634 {
4636 
4638  return false;
4639 
4640  return true;
4641 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ IsTransactionOrTransactionBlock()

bool IsTransactionOrTransactionBlock ( void  )

◆ IsTransactionState()

bool IsTransactionState ( void  )

Definition at line 356 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

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

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

◆ MarkCurrentTransactionIdLoggedIfAny()

void MarkCurrentTransactionIdLoggedIfAny ( void  )

Definition at line 510 of file xact.c.

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

Referenced by XLogInsertRecord().

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

◆ ParseAbortRecord()

void ParseAbortRecord ( uint8  info,
xl_xact_abort xlrec,
xl_xact_parsed_abort parsed 
)

Definition at line 129 of file xactdesc.c.

References xl_xact_dbinfo::dbId, xl_xact_parsed_abort::dbId, MinSizeOfXactAbort, MinSizeOfXactRelfilenodes, MinSizeOfXactSubxacts, xl_xact_relfilenodes::nrels, xl_xact_parsed_abort::nrels, xl_xact_subxacts::nsubxacts, xl_xact_parsed_abort::nsubxacts, xl_xact_origin::origin_lsn, xl_xact_parsed_abort::origin_lsn, xl_xact_origin::origin_timestamp, xl_xact_parsed_abort::origin_timestamp, strlcpy(), xl_xact_subxacts::subxacts, xl_xact_parsed_abort::subxacts, xl_xact_dbinfo::tsId, xl_xact_parsed_abort::tsId, xl_xact_parsed_abort::twophase_gid, xl_xact_parsed_abort::twophase_xid, xl_xact_abort::xact_time, xl_xact_parsed_abort::xact_time, XACT_XINFO_HAS_DBINFO, XACT_XINFO_HAS_GID, XACT_XINFO_HAS_ORIGIN, XACT_XINFO_HAS_RELFILENODES, XACT_XINFO_HAS_SUBXACTS, XACT_XINFO_HAS_TWOPHASE, xl_xact_twophase::xid, xl_xact_xinfo::xinfo, xl_xact_parsed_abort::xinfo, XLOG_XACT_HAS_INFO, xl_xact_relfilenodes::xnodes, and xl_xact_parsed_abort::xnodes.

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

130 {
131  char *data = ((char *) xlrec) + MinSizeOfXactAbort;
132 
133  memset(parsed, 0, sizeof(*parsed));
134 
135  parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
136  * present */
137 
138  parsed->xact_time = xlrec->xact_time;
139 
140  if (info & XLOG_XACT_HAS_INFO)
141  {
142  xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
143 
144  parsed->xinfo = xl_xinfo->xinfo;
145 
146  data += sizeof(xl_xact_xinfo);
147  }
148 
149  if (parsed->xinfo & XACT_XINFO_HAS_DBINFO)
150  {
151  xl_xact_dbinfo *xl_dbinfo = (xl_xact_dbinfo *) data;
152 
153  parsed->dbId = xl_dbinfo->dbId;
154  parsed->tsId = xl_dbinfo->tsId;
155 
156  data += sizeof(xl_xact_dbinfo);
157  }
158 
159  if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
160  {
161  xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
162 
163  parsed->nsubxacts = xl_subxacts->nsubxacts;
164  parsed->subxacts = xl_subxacts->subxacts;
165 
166  data += MinSizeOfXactSubxacts;
167  data += parsed->nsubxacts * sizeof(TransactionId);
168  }
169 
170  if (parsed->xinfo & XACT_XINFO_HAS_RELFILENODES)
171  {
172  xl_xact_relfilenodes *xl_relfilenodes = (xl_xact_relfilenodes *) data;
173 
174  parsed->nrels = xl_relfilenodes->nrels;
175  parsed->xnodes = xl_relfilenodes->xnodes;
176 
178  data += xl_relfilenodes->nrels * sizeof(RelFileNode);
179  }
180 
181  if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
182  {
183  xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
184 
185  parsed->twophase_xid = xl_twophase->xid;
186 
187  data += sizeof(xl_xact_twophase);
188 
189  if (parsed->xinfo & XACT_XINFO_HAS_GID)
190  {
191  strlcpy(parsed->twophase_gid, data, sizeof(parsed->twophase_gid));
192  data += strlen(data) + 1;
193  }
194  }
195 
196  /* Note: no alignment is guaranteed after this point */
197 
198  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
199  {
200  xl_xact_origin xl_origin;
201 
202  /* no alignment is guaranteed, so copy onto stack */
203  memcpy(&xl_origin, data, sizeof(xl_origin));
204 
205  parsed->origin_lsn = xl_origin.origin_lsn;
206  parsed->origin_timestamp = xl_origin.origin_timestamp;
207 
208  data += sizeof(xl_xact_origin);
209  }
210 }
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:167
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:507
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:337
uint32 xinfo
Definition: xact.h:224
XLogRecPtr origin_lsn
Definition: xact.h:345
RelFileNode * xnodes
Definition: xact.h:340
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:342
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
TimestampTz xact_time
Definition: xact.h:330
TimestampTz origin_timestamp
Definition: xact.h:262
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:164
TimestampTz origin_timestamp
Definition: xact.h:346
#define XACT_XINFO_HAS_GID
Definition: xact.h:169
char twophase_gid[GIDSIZE]
Definition: xact.h:343
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:312
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:167
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:507
TimestampTz origin_timestamp
Definition: xact.h:323
TransactionId xid
Definition: xact.h:256
TransactionId twophase_xid
Definition: xact.h:317
TransactionId * subxacts
Definition: xact.h:309
#define XLOG_XACT_HAS_INFO
Definition: xact.h:156
SharedInvalidationMessage * msgs
Definition: xact.h:315
XLogRecPtr origin_lsn
Definition: xact.h:322
#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:318
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:236
TimestampTz xact_time
Definition: xact.h:302

◆ PrepareTransactionBlock()

bool PrepareTransactionBlock ( const char *  gid)

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

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

◆ PreventInTransactionBlock()

void PreventInTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

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

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

◆ RegisterSubXactCallback()

void RegisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

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

3533 {
3534  SubXactCallbackItem *item;
3535 
3536  item = (SubXactCallbackItem *)
3538  item->callback = callback;
3539  item->arg = arg;
3540  item->next = SubXact_callbacks;
3541  SubXact_callbacks = item;
3542 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:299
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:294
SubXactCallback callback
Definition: xact.c:295
MemoryContext TopMemoryContext
Definition: mcxt.c:44
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
void * arg

◆ RegisterXactCallback()

void RegisterXactCallback ( XactCallback  callback,
void *  arg 
)

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

3478 {
3479  XactCallbackItem *item;
3480 
3481  item = (XactCallbackItem *)
3483  item->callback = callback;
3484  item->arg = arg;
3485  item->next = Xact_callbacks;
3486  Xact_callbacks = item;
3487 }
struct XactCallbackItem * next
Definition: xact.c:282
void * arg
Definition: xact.c:284
XactCallback callback
Definition: xact.c:283
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static XactCallbackItem * Xact_callbacks
Definition: xact.c:287
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
void * arg

◆ ReleaseCurrentSubTransaction()

void ReleaseCurrentSubTransaction ( void  )

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

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

◆ ReleaseSavepoint()

void ReleaseSavepoint ( const char *  name)

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

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

◆ RequireTransactionBlock()

void RequireTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3394 of file xact.c.

References CheckTransactionBlock().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

3395 {
3396  CheckTransactionBlock(isTopLevel, true, stmtType);
3397 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3403

◆ RestoreTransactionCharacteristics()

void RestoreTransactionCharacteristics ( void  )

Definition at line 2883 of file xact.c.

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

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

2884 {
2888 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2872
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2871
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2870

◆ RollbackAndReleaseCurrentSubTransaction()

void RollbackAndReleaseCurrentSubTransaction ( void  )

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

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

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

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

◆ SaveTransactionCharacteristics()

void SaveTransactionCharacteristics ( void  )

Definition at line 2875 of file xact.c.

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

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

2876 {
2880 }
bool XactDeferrable
Definition: xact.c:81
static bool save_XactDeferrable
Definition: xact.c:2872
bool XactReadOnly
Definition: xact.c:78
static bool save_XactReadOnly
Definition: xact.c:2871
int XactIsoLevel
Definition: xact.c:75
static int save_XactIsoLevel
Definition: xact.c:2870

◆ SerializeTransactionState()

void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

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

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

◆ SetCurrentStatementStartTimestamp()

void SetCurrentStatementStartTimestamp ( void  )

Definition at line 818 of file xact.c.

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

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

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

◆ SetParallelStartTimestamps()

void SetParallelStartTimestamps ( TimestampTz  xact_ts,
TimestampTz  stmt_ts 
)

Definition at line 772 of file xact.c.

References Assert, IsParallelWorker, stmtStartTimestamp, and xactStartTimestamp.

Referenced by ParallelWorkerMain().

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

◆ StartParallelWorkerTransaction()

void StartParallelWorkerTransaction ( char *  tstatespace)

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

5249 {
5251 
5253  StartTransaction();
5254 
5255  tstate = (SerializedTransactionState *) tstatespace;
5256  XactIsoLevel = tstate->xactIsoLevel;
5257  XactDeferrable = tstate->xactDeferrable;
5260  tstate->currentFullTransactionId;
5264 
5266 }
FullTransactionId XactTopFullTransactionId
Definition: xact.c:108
bool XactDeferrable
Definition: xact.c:81
static TransactionState CurrentTransactionState
Definition: xact.c:235
TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.c:211
int nParallelCurrentXids
Definition: xact.c:109
TBlockState blockState
Definition: xact.c:179
CommandId currentCommandId
Definition: xact.c:209
FullTransactionId fullTransactionId
Definition: xact.c:174
static void StartTransaction(void)
Definition: xact.c:1884
TransactionId * ParallelCurrentXids
Definition: xact.c:110
#define Assert(condition)
Definition: c.h:732
int XactIsoLevel
Definition: xact.c:75
static CommandId currentCommandId
Definition: xact.c:242
FullTransactionId topFullTransactionId
Definition: xact.c:207
FullTransactionId currentFullTransactionId
Definition: xact.c:208

◆ StartTransactionCommand()

void StartTransactionCommand ( void  )

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

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

◆ SubTransactionIsActive()

bool SubTransactionIsActive ( SubTransactionId  subxid)

Definition at line 722 of file xact.c.

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

Referenced by fmgr_sql().

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

◆ TransactionBlockStatusCode()

char TransactionBlockStatusCode ( void  )

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

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

◆ TransactionIdIsCurrentTransactionId()

bool TransactionIdIsCurrentTransactionId ( TransactionId  xid)

Definition at line 854 of file xact.c.

References TransactionStateData::childXids, TransactionStateData::fullTransactionId, FullTransactionIdIsValid, 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(), RI_FKey_fk_upd_check_required(), SnapBuildWaitSnapshot(), test_lockmode_for_conflict(), TransactionIdIsInProgress(), and txid_status().

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

◆ UnregisterSubXactCallback()

void UnregisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3545 of file xact.c.

References SubXactCallbackItem::next, and pfree().

3546 {
3547  SubXactCallbackItem *item;
3548  SubXactCallbackItem *prev;
3549 
3550  prev = NULL;
3551  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3552  {
3553  if (item->callback == callback && item->arg == arg)
3554  {
3555  if (prev)
3556  prev->next = item->next;
3557  else
3558  SubXact_callbacks = item->next;
3559  pfree(item);
3560  break;
3561  }
3562  }
3563 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:299
void pfree(void *pointer)
Definition: mcxt.c:1056
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:294
void * arg

◆ UnregisterXactCallback()

void UnregisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3490 of file xact.c.

References XactCallbackItem::next, and pfree().

3491 {
3492  XactCallbackItem *item;
3493  XactCallbackItem *prev;
3494 
3495  prev = NULL;
3496  for (item = Xact_callbacks; item; prev = item, item = item->next)
3497  {
3498  if (item->callback == callback && item->arg == arg)
3499  {
3500  if (prev)
3501  prev->next = item->next;
3502  else
3503  Xact_callbacks = item->next;
3504  pfree(item);
3505  break;
3506  }
3507  }
3508 }
struct XactCallbackItem * next
Definition: xact.c:282
void pfree(void *pointer)
Definition: mcxt.c:1056
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
static XactCallbackItem * Xact_callbacks
Definition: xact.c:287
void * arg

◆ UserAbortTransactionBlock()

void UserAbortTransactionBlock ( bool  chain)

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

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

◆ WarnNoTransactionBlock()

void WarnNoTransactionBlock ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3388 of file xact.c.

References CheckTransactionBlock().

Referenced by ExecSetVariableStmt(), and standard_ProcessUtility().

3389 {
3390  CheckTransactionBlock(isTopLevel, false, stmtType);
3391 }
static void CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3403

◆ xact_desc()

void xact_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 308 of file xactdesc.c.

References appendStringInfo(), xact_desc_abort(), xact_desc_assignment(), xact_desc_commit(), XLOG_XACT_ABORT, XLOG_XACT_ABORT_PREPARED, XLOG_XACT_ASSIGNMENT, XLOG_XACT_COMMIT, XLOG_XACT_COMMIT_PREPARED, XLOG_XACT_OPMASK, XLogRecGetData, XLogRecGetInfo, XLogRecGetOrigin, and xl_xact_assignment::xtop.

309 {
310  char *rec = XLogRecGetData(record);
311  uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
312 
313  if (info == XLOG_XACT_COMMIT || info == XLOG_XACT_COMMIT_PREPARED)
314  {
315  xl_xact_commit *xlrec = (xl_xact_commit *) rec;
316 
317  xact_desc_commit(buf, XLogRecGetInfo(record), xlrec,
318  XLogRecGetOrigin(record));
319  }
320  else if (info == XLOG_XACT_ABORT || info == XLOG_XACT_ABORT_PREPARED)
321  {
322  xl_xact_abort *xlrec = (xl_xact_abort *) rec;
323 
324  xact_desc_abort(buf, XLogRecGetInfo(record), xlrec);
325  }
326  else if (info == XLOG_XACT_ASSIGNMENT)
327  {
328  xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
329 
330  /*
331  * Note that we ignore the WAL record's xid, since we're more
332  * interested in the top-level xid that issued the record and which
333  * xids are being reported here.
334  */
335  appendStringInfo(buf, "xtop %u: ", xlrec->xtop);
336  xact_desc_assignment(buf, xlrec);
337  }
338 }
#define XLOG_XACT_COMMIT
Definition: xact.h:143
unsigned char uint8
Definition: c.h:356
TransactionId xtop
Definition: xact.h:193
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:245
#define XLogRecGetData(decoder)
Definition: xlogreader.h:246
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define XLOG_XACT_ABORT_PREPARED
Definition: xact.h:147
static void xact_desc_abort(StringInfo buf, uint8 info, xl_xact_abort *xlrec)
Definition: xactdesc.c:264
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:242
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:148
#define XLOG_XACT_ABORT
Definition: xact.h:145
#define XLOG_XACT_OPMASK
Definition: xact.h:153
static void xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
Definition: xactdesc.c:297
#define XLOG_XACT_COMMIT_PREPARED
Definition: xact.h:146
static void xact_desc_commit(StringInfo buf, uint8 info, xl_xact_commit *xlrec, RepOriginId origin_id)
Definition: xactdesc.c:213

◆ xact_identify()

const char* xact_identify ( uint8  info)

Definition at line 341 of file xactdesc.c.

References XLOG_XACT_ABORT, XLOG_XACT_ABORT_PREPARED, XLOG_XACT_ASSIGNMENT, XLOG_XACT_COMMIT, XLOG_XACT_COMMIT_PREPARED, XLOG_XACT_OPMASK, and XLOG_XACT_PREPARE.

342 {
343  const char *id = NULL;
344 
345  switch (info & XLOG_XACT_OPMASK)
346  {
347  case XLOG_XACT_COMMIT:
348  id = "COMMIT";
349  break;
350  case XLOG_XACT_PREPARE:
351  id = "PREPARE";
352  break;
353  case XLOG_XACT_ABORT:
354  id = "ABORT";
355  break;
357  id = "COMMIT_PREPARED";
358  break;
360  id = "ABORT_PREPARED";
361  break;
363  id = "ASSIGNMENT";
364  break;
365  }
366 
367  return id;
368 }
#define XLOG_XACT_COMMIT
Definition: xact.h:143
#define XLOG_XACT_PREPARE
Definition: xact.h:144
#define XLOG_XACT_ABORT_PREPARED
Definition: xact.h:147
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:148
#define XLOG_XACT_ABORT
Definition: xact.h:145
#define XLOG_XACT_OPMASK
Definition: xact.h:153
#define XLOG_XACT_COMMIT_PREPARED
Definition: xact.h:146

◆ xact_redo()

void xact_redo ( XLogReaderState record)

Definition at line 5928 of file xact.c.

References Assert, elog, XLogReaderState::EndRecPtr, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), xl_xact_assignment::nsubxacts, PANIC, ParseAbortRecord(), ParseCommitRecord(), PrepareRedoAdd(), PrepareRedoRemove(), ProcArrayApplyXidAssignment(), XLogReaderState::ReadRecPtr, STANDBY_INITIALIZED, standbyState, xl_xact_parsed_commit::twophase_xid, xl_xact_parsed_abort::twophase_xid, xact_redo_abort(), xact_redo_commit(), XLOG_XACT_ABORT, XLOG_XACT_ABORT_PREPARED, XLOG_XACT_ASSIGNMENT, XLOG_XACT_COMMIT, XLOG_XACT_COMMIT_PREPARED, XLOG_XACT_OPMASK, XLOG_XACT_PREPARE, XLogRecGetData, XLogRecGetInfo, XLogRecGetOrigin, XLogRecGetXid, XLogRecHasAnyBlockRefs, xl_xact_assignment::xsub, and xl_xact_assignment::xtop.

5929 {
5930  uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
5931 
5932  /* Backup blocks are not used in xact records */
5933  Assert(!XLogRecHasAnyBlockRefs(record));
5934 
5935  if (info == XLOG_XACT_COMMIT)
5936  {
5937  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5938  xl_xact_parsed_commit parsed;
5939 
5940  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5941  xact_redo_commit(&parsed, XLogRecGetXid(record),
5942  record->EndRecPtr, XLogRecGetOrigin(record));
5943  }
5944  else if (info == XLOG_XACT_COMMIT_PREPARED)
5945  {
5946  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5947  xl_xact_parsed_commit parsed;
5948 
5949  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5950  xact_redo_commit(&parsed, parsed.twophase_xid,
5951  record->EndRecPtr, XLogRecGetOrigin(record));
5952