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

◆ 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

◆ xl_xact_relfilenodes

◆ xl_xact_subxacts

◆ xl_xact_twophase

◆ 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:4852
static void AbortTransaction(void)
Definition: xact.c:2564
static void CleanupSubTransaction(void)
Definition: xact.c:5001

◆ AbortOutOfAnyTransaction()

void AbortOutOfAnyTransaction ( void  )

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

4506 {
4508 
4509  /* Ensure we're not running in a doomed memory context */
4510  AtAbort_Memory();
4511 
4512  /*
4513  * Get out of any transaction or nested transaction
4514  */
4515  do
4516  {
4517  switch (s->blockState)
4518  {
4519  case TBLOCK_DEFAULT:
4520  if (s->state == TRANS_DEFAULT)
4521  {
4522  /* Not in a transaction, do nothing */
4523  }
4524  else
4525  {
4526  /*
4527  * We can get here after an error during transaction start
4528  * (state will be TRANS_START). Need to clean up the
4529  * incompletely started transaction. First, adjust the
4530  * low-level state to suppress warning message from
4531  * AbortTransaction.
4532  */
4533  if (s->state == TRANS_START)
4534  s->state = TRANS_INPROGRESS;
4535  AbortTransaction();
4537  }
4538  break;
4539  case TBLOCK_STARTED:
4540  case TBLOCK_BEGIN:
4541  case TBLOCK_INPROGRESS:
4544  case TBLOCK_END:
4545  case TBLOCK_ABORT_PENDING:
4546  case TBLOCK_PREPARE:
4547  /* In a transaction, so clean up */
4548  AbortTransaction();
4551  break;
4552  case TBLOCK_ABORT:
4553  case TBLOCK_ABORT_END:
4554 
4555  /*
4556  * AbortTransaction is already done, still need Cleanup.
4557  * However, if we failed partway through running ROLLBACK,
4558  * there will be an active portal running that command, which
4559  * we need to shut down before doing CleanupTransaction.
4560  */
4561  AtAbort_Portals();
4564  break;
4565 
4566  /*
4567  * In a subtransaction, so clean it up and abort parent too
4568  */
4569  case TBLOCK_SUBBEGIN:
4570  case TBLOCK_SUBINPROGRESS:
4571  case TBLOCK_SUBRELEASE:
4572  case TBLOCK_SUBCOMMIT:
4574  case TBLOCK_SUBRESTART:
4577  s = CurrentTransactionState; /* changed by pop */
4578  break;
4579 
4580  case TBLOCK_SUBABORT:
4581  case TBLOCK_SUBABORT_END:
4583  /* As above, but AbortSubTransaction already done */
4584  if (s->curTransactionOwner)
4585  {
4586  /* As in TBLOCK_ABORT, might have a live portal to zap */
4591  }
4593  s = CurrentTransactionState; /* changed by pop */
4594  break;
4595  }
4596  } while (s->blockState != TBLOCK_DEFAULT);
4597 
4598  /* Should be out of all subxacts now */
4599  Assert(s->parent == NULL);
4600 
4601  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4602  AtCleanup_Memory();
4603 }
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:4852
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:5001

◆ BeginImplicitTransactionBlock()

void BeginImplicitTransactionBlock ( void  )

Definition at line 3965 of file xact.c.

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

Referenced by exec_simple_query().

3966 {
3968 
3969  /*
3970  * If we are in STARTED state (that is, no transaction block is open),
3971  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3972  * block.
3973  *
3974  * For caller convenience, we consider all other transaction states as
3975  * legal here; otherwise the caller would need its own state check, which
3976  * seems rather pointless.
3977  */
3978  if (s->blockState == TBLOCK_STARTED)
3980 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ BeginInternalSubTransaction()

void BeginInternalSubTransaction ( const char *  name)

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

4334 {
4336 
4337  /*
4338  * Workers synchronize transaction state at the beginning of each parallel
4339  * operation, so we can't account for new subtransactions after that
4340  * point. We might be able to make an exception for the type of
4341  * subtransaction established by this function, which is typically used in
4342  * contexts where we're going to release or roll back the subtransaction
4343  * before proceeding further, so that no enduring change to the
4344  * transaction state occurs. For now, however, we prohibit this case along
4345  * with all the others.
4346  */
4347  if (IsInParallelMode())
4348  ereport(ERROR,
4349  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4350  errmsg("cannot start subtransactions during a parallel operation")));
4351 
4352  switch (s->blockState)
4353  {
4354  case TBLOCK_STARTED:
4355  case TBLOCK_INPROGRESS:
4357  case TBLOCK_END:
4358  case TBLOCK_PREPARE:
4359  case TBLOCK_SUBINPROGRESS:
4360  /* Normal subtransaction start */
4361  PushTransaction();
4362  s = CurrentTransactionState; /* changed by push */
4363 
4364  /*
4365  * Savepoint names, like the TransactionState block itself, live
4366  * in TopTransactionContext.
4367  */
4368  if (name)
4370  break;
4371 
4372  /* These cases are invalid. */
4373  case TBLOCK_DEFAULT:
4374  case TBLOCK_BEGIN:
4376  case TBLOCK_SUBBEGIN:
4377  case TBLOCK_SUBRELEASE:
4378  case TBLOCK_SUBCOMMIT:
4379  case TBLOCK_ABORT:
4380  case TBLOCK_SUBABORT:
4381  case TBLOCK_ABORT_END:
4382  case TBLOCK_SUBABORT_END:
4383  case TBLOCK_ABORT_PENDING:
4385  case TBLOCK_SUBRESTART:
4387  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4389  break;
4390  }
4391 
4394 }
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:5313
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5034
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:1148
#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:5313
#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(), 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:4012
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:4743
#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:5313
static void AbortSubTransaction(void)
Definition: xact.c:4852
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:5001
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:4705

◆ DefineSavepoint()

void DefineSavepoint ( const char *  name)

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

4013 {
4015 
4016  /*
4017  * Workers synchronize transaction state at the beginning of each parallel
4018  * operation, so we can't account for new subtransactions after that
4019  * point. (Note that this check will certainly error out if s->blockState
4020  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4021  * below.)
4022  */
4023  if (IsInParallelMode())
4024  ereport(ERROR,
4025  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4026  errmsg("cannot define savepoints during a parallel operation")));
4027 
4028  switch (s->blockState)
4029  {
4030  case TBLOCK_INPROGRESS:
4031  case TBLOCK_SUBINPROGRESS:
4032  /* Normal subtransaction start */
4033  PushTransaction();
4034  s = CurrentTransactionState; /* changed by push */
4035 
4036  /*
4037  * Savepoint names, like the TransactionState block itself, live
4038  * in TopTransactionContext.
4039  */
4040  if (name)
4042  break;
4043 
4044  /*
4045  * We disallow savepoint commands in implicit transaction blocks.
4046  * There would be no great difficulty in allowing them so far as
4047  * this module is concerned, but a savepoint seems inconsistent
4048  * with exec_simple_query's behavior of abandoning the whole query
4049  * string upon error. Also, the point of an implicit transaction
4050  * block (as opposed to a regular one) is to automatically close
4051  * after an error, so it's hard to see how a savepoint would fit
4052  * into that.
4053  *
4054  * The error messages for this are phrased as if there were no
4055  * active transaction block at all, which is historical but
4056  * perhaps could be improved.
4057  */
4059  ereport(ERROR,
4060  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4061  /* translator: %s represents an SQL statement name */
4062  errmsg("%s can only be used in transaction blocks",
4063  "SAVEPOINT")));
4064  break;
4065 
4066  /* These cases are invalid. */
4067  case TBLOCK_DEFAULT:
4068  case TBLOCK_STARTED:
4069  case TBLOCK_BEGIN:
4071  case TBLOCK_SUBBEGIN:
4072  case TBLOCK_END:
4073  case TBLOCK_SUBRELEASE:
4074  case TBLOCK_SUBCOMMIT:
4075  case TBLOCK_ABORT:
4076  case TBLOCK_SUBABORT:
4077  case TBLOCK_ABORT_END:
4078  case TBLOCK_SUBABORT_END:
4079  case TBLOCK_ABORT_PENDING:
4081  case TBLOCK_SUBRESTART:
4083  case TBLOCK_PREPARE:
4084  elog(FATAL, "DefineSavepoint: unexpected state %s",
4086  break;
4087  }
4088 }
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:5313
#define ereport(elevel, rest)
Definition: elog.h:141
static void PushTransaction(void)
Definition: xact.c:5034
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:1148
#define elog(elevel,...)
Definition: elog.h:226

◆ EndImplicitTransactionBlock()

void EndImplicitTransactionBlock ( void  )

Definition at line 3990 of file xact.c.

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

Referenced by exec_simple_query().

3991 {
3993 
3994  /*
3995  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
3996  * allowing CommitTransactionCommand to commit whatever happened during
3997  * the implicit transaction block as though it were a single statement.
3998  *
3999  * For caller convenience, we consider all other transaction states as
4000  * legal here; otherwise the caller would need its own state check, which
4001  * seems rather pointless.
4002  */
4005 }
static TransactionState CurrentTransactionState
Definition: xact.c:235
TBlockState blockState
Definition: xact.c:179

◆ EndParallelWorkerTransaction()

void EndParallelWorkerTransaction ( void  )

Definition at line 5250 of file xact.c.

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

Referenced by ParallelWorkerMain().

5251 {
5255 }
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(), 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  * In an implicit transaction block, commit, but issue a warning
3725  * because there was no explicit BEGIN before this.
3726  */
3728  ereport(WARNING,
3729  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3730  errmsg("there is no transaction in progress")));
3731  s->blockState = TBLOCK_END;
3732  result = true;
3733  break;
3734 
3735  /*
3736  * We are in a failed transaction block. Tell
3737  * CommitTransactionCommand it's time to exit the block.
3738  */
3739  case TBLOCK_ABORT:
3741  break;
3742 
3743  /*
3744  * We are in a live subtransaction block. Set up to subcommit all
3745  * open subtransactions and then commit the main transaction.
3746  */
3747  case TBLOCK_SUBINPROGRESS:
3748  while (s->parent != NULL)
3749  {
3750  if (s->blockState == TBLOCK_SUBINPROGRESS)
3752  else
3753  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3755  s = s->parent;
3756  }
3757  if (s->blockState == TBLOCK_INPROGRESS)
3758  s->blockState = TBLOCK_END;
3759  else
3760  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3762  result = true;
3763  break;
3764 
3765  /*
3766  * Here we are inside an aborted subtransaction. Treat the COMMIT
3767  * as ROLLBACK: set up to abort everything and exit the main
3768  * transaction.
3769  */
3770  case TBLOCK_SUBABORT:
3771  while (s->parent != NULL)
3772  {
3773  if (s->blockState == TBLOCK_SUBINPROGRESS)
3775  else if (s->blockState == TBLOCK_SUBABORT)
3777  else
3778  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3780  s = s->parent;
3781  }
3782  if (s->blockState == TBLOCK_INPROGRESS)
3784  else if (s->blockState == TBLOCK_ABORT)
3786  else
3787  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3789  break;
3790 
3791  /*
3792  * The user issued COMMIT when not inside a transaction. Issue a
3793  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3794  * CommitTransactionCommand() will then close the transaction and
3795  * put us back into the default state.
3796  */
3797  case TBLOCK_STARTED:
3798  ereport(WARNING,
3799  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3800  errmsg("there is no transaction in progress")));
3801  result = true;
3802  break;
3803 
3804  /*
3805  * The user issued a COMMIT that somehow ran inside a parallel
3806  * worker. We can't cope with that.
3807  */
3809  ereport(FATAL,
3810  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3811  errmsg("cannot commit during a parallel operation")));
3812  break;
3813 
3814  /* These cases are invalid. */
3815  case TBLOCK_DEFAULT:
3816  case TBLOCK_BEGIN:
3817  case TBLOCK_SUBBEGIN:
3818  case TBLOCK_END:
3819  case TBLOCK_SUBRELEASE:
3820  case TBLOCK_SUBCOMMIT:
3821  case TBLOCK_ABORT_END:
3822  case TBLOCK_SUBABORT_END:
3823  case TBLOCK_ABORT_PENDING:
3825  case TBLOCK_SUBRESTART:
3827  case TBLOCK_PREPARE:
3828  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3830  break;
3831  }
3832 
3834  s->blockState == TBLOCK_END ||
3835  s->blockState == TBLOCK_ABORT_END ||
3837 
3838  s->chain = chain;
3839 
3840  return result;
3841 }
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
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5313
#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 5127 of file xact.c.

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

Referenced by InitializeParallelDSM().

5128 {
5129  TransactionState s;
5130  Size nxids = 0;
5132 
5133  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5134  {
5136  nxids = add_size(nxids, 1);
5137  nxids = add_size(nxids, s->nChildXids);
5138  }
5139 
5140  return add_size(size, mul_size(sizeof(TransactionId), nxids));
5141 }
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:1591

◆ 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:245
#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:4609
bool IsSubTransaction(void)
Definition: xact.c:4682

◆ IsSubTransaction()

◆ IsTransactionBlock()

bool IsTransactionBlock ( void  )

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

4610 {
4612 
4614  return false;
4615 
4616  return true;
4617 }
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:1148
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:4609
#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:4682
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:771
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:771
void * arg

◆ ReleaseCurrentSubTransaction()

void ReleaseCurrentSubTransaction ( void  )

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

4405 {
4407 
4408  /*
4409  * Workers synchronize transaction state at the beginning of each parallel
4410  * operation, so we can't account for commit of subtransactions after that
4411  * point. This should not happen anyway. Code calling this would
4412  * typically have called BeginInternalSubTransaction() first, failing
4413  * there.
4414  */
4415  if (IsInParallelMode())
4416  ereport(ERROR,
4417  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4418  errmsg("cannot commit subtransactions during a parallel operation")));
4419 
4420  if (s->blockState != TBLOCK_SUBINPROGRESS)
4421  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4423  Assert(s->state == TRANS_INPROGRESS);
4426  s = CurrentTransactionState; /* changed by pop */
4427  Assert(s->state == TRANS_INPROGRESS);
4428 }
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:4743
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5313
#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 4097 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().

4098 {
4100  TransactionState target,
4101  xact;
4102 
4103  /*
4104  * Workers synchronize transaction state at the beginning of each parallel
4105  * operation, so we can't account for transaction state change after that
4106  * point. (Note that this check will certainly error out if s->blockState
4107  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4108  * below.)
4109  */
4110  if (IsInParallelMode())
4111  ereport(ERROR,
4112  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4113  errmsg("cannot release savepoints during a parallel operation")));
4114 
4115  switch (s->blockState)
4116  {
4117  /*
4118  * We can't release a savepoint if there is no savepoint defined.
4119  */
4120  case TBLOCK_INPROGRESS:
4121  ereport(ERROR,
4122  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4123  errmsg("savepoint \"%s\" does not exist", name)));
4124  break;
4125 
4127  /* See comment about implicit transactions in DefineSavepoint */
4128  ereport(ERROR,
4129  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4130  /* translator: %s represents an SQL statement name */
4131  errmsg("%s can only be used in transaction blocks",
4132  "RELEASE SAVEPOINT")));
4133  break;
4134 
4135  /*
4136  * We are in a non-aborted subtransaction. This is the only valid
4137  * case.
4138  */
4139  case TBLOCK_SUBINPROGRESS:
4140  break;
4141 
4142  /* These cases are invalid. */
4143  case TBLOCK_DEFAULT:
4144  case TBLOCK_STARTED:
4145  case TBLOCK_BEGIN:
4147  case TBLOCK_SUBBEGIN:
4148  case TBLOCK_END:
4149  case TBLOCK_SUBRELEASE:
4150  case TBLOCK_SUBCOMMIT:
4151  case TBLOCK_ABORT:
4152  case TBLOCK_SUBABORT:
4153  case TBLOCK_ABORT_END:
4154  case TBLOCK_SUBABORT_END:
4155  case TBLOCK_ABORT_PENDING:
4157  case TBLOCK_SUBRESTART:
4159  case TBLOCK_PREPARE:
4160  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
4162  break;
4163  }
4164 
4165  for (target = s; PointerIsValid(target); target = target->parent)
4166  {
4167  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4168  break;
4169  }
4170 
4171  if (!PointerIsValid(target))
4172  ereport(ERROR,
4173  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4174  errmsg("savepoint \"%s\" does not exist", name)));
4175 
4176  /* disallow crossing savepoint level boundaries */
4177  if (target->savepointLevel != s->savepointLevel)
4178  ereport(ERROR,
4179  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4180  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4181 
4182  /*
4183  * Mark "commit pending" all subtransactions up to the target
4184  * subtransaction. The actual commits will happen when control gets to
4185  * CommitTransactionCommand.
4186  */
4187  xact = CurrentTransactionState;
4188  for (;;)
4189  {
4191  xact->blockState = TBLOCK_SUBRELEASE;
4192  if (xact == target)
4193  break;
4194  xact = xact->parent;
4195  Assert(PointerIsValid(xact));
4196  }
4197 }
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:5313
#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 4438 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().

4439 {
4441 
4442  /*
4443  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4444  * during parallel operations. That's because we may be in the master,
4445  * recovering from an error thrown while we were in parallel mode. We
4446  * won't reach here in a worker, because BeginInternalSubTransaction()
4447  * will have failed.
4448  */
4449 
4450  switch (s->blockState)
4451  {
4452  /* Must be in a subtransaction */
4453  case TBLOCK_SUBINPROGRESS:
4454  case TBLOCK_SUBABORT:
4455  break;
4456 
4457  /* These cases are invalid. */
4458  case TBLOCK_DEFAULT:
4459  case TBLOCK_STARTED:
4460  case TBLOCK_BEGIN:
4463  case TBLOCK_SUBBEGIN:
4464  case TBLOCK_INPROGRESS:
4465  case TBLOCK_END:
4466  case TBLOCK_SUBRELEASE:
4467  case TBLOCK_SUBCOMMIT:
4468  case TBLOCK_ABORT:
4469  case TBLOCK_ABORT_END:
4470  case TBLOCK_SUBABORT_END:
4471  case TBLOCK_ABORT_PENDING:
4473  case TBLOCK_SUBRESTART:
4475  case TBLOCK_PREPARE:
4476  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4478  break;
4479  }
4480 
4481  /*
4482  * Abort the current subtransaction, if needed.
4483  */
4484  if (s->blockState == TBLOCK_SUBINPROGRESS)
4486 
4487  /* And clean it up, too */
4489 
4490  s = CurrentTransactionState; /* changed by pop */
4492  s->blockState == TBLOCK_INPROGRESS ||
4494  s->blockState == TBLOCK_STARTED);
4495 }
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:5313
static void AbortSubTransaction(void)
Definition: xact.c:4852
static void CleanupSubTransaction(void)
Definition: xact.c:5001
#define elog(elevel,...)
Definition: elog.h:226

◆ RollbackToSavepoint()

void RollbackToSavepoint ( const char *  name)

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

4207 {
4209  TransactionState target,
4210  xact;
4211 
4212  /*
4213  * Workers synchronize transaction state at the beginning of each parallel
4214  * operation, so we can't account for transaction state change after that
4215  * point. (Note that this check will certainly error out if s->blockState
4216  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
4217  * below.)
4218  */
4219  if (IsInParallelMode())
4220  ereport(ERROR,
4221  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4222  errmsg("cannot rollback to savepoints during a parallel operation")));
4223 
4224  switch (s->blockState)
4225  {
4226  /*
4227  * We can't rollback to a savepoint if there is no savepoint
4228  * defined.
4229  */
4230  case TBLOCK_INPROGRESS:
4231  case TBLOCK_ABORT:
4232  ereport(ERROR,
4233  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4234  errmsg("savepoint \"%s\" does not exist", name)));
4235  break;
4236 
4238  /* See comment about implicit transactions in DefineSavepoint */
4239  ereport(ERROR,
4240  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
4241  /* translator: %s represents an SQL statement name */
4242  errmsg("%s can only be used in transaction blocks",
4243  "ROLLBACK TO SAVEPOINT")));
4244  break;
4245 
4246  /*
4247  * There is at least one savepoint, so proceed.
4248  */
4249  case TBLOCK_SUBINPROGRESS:
4250  case TBLOCK_SUBABORT:
4251  break;
4252 
4253  /* These cases are invalid. */
4254  case TBLOCK_DEFAULT:
4255  case TBLOCK_STARTED:
4256  case TBLOCK_BEGIN:
4258  case TBLOCK_SUBBEGIN:
4259  case TBLOCK_END:
4260  case TBLOCK_SUBRELEASE:
4261  case TBLOCK_SUBCOMMIT:
4262  case TBLOCK_ABORT_END:
4263  case TBLOCK_SUBABORT_END:
4264  case TBLOCK_ABORT_PENDING:
4266  case TBLOCK_SUBRESTART:
4268  case TBLOCK_PREPARE:
4269  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4271  break;
4272  }
4273 
4274  for (target = s; PointerIsValid(target); target = target->parent)
4275  {
4276  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
4277  break;
4278  }
4279 
4280  if (!PointerIsValid(target))
4281  ereport(ERROR,
4282  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4283  errmsg("savepoint \"%s\" does not exist", name)));
4284 
4285  /* disallow crossing savepoint level boundaries */
4286  if (target->savepointLevel != s->savepointLevel)
4287  ereport(ERROR,
4288  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4289  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
4290 
4291  /*
4292  * Mark "abort pending" all subtransactions up to the target
4293  * subtransaction. The actual aborts will happen when control gets to
4294  * CommitTransactionCommand.
4295  */
4296  xact = CurrentTransactionState;
4297  for (;;)
4298  {
4299  if (xact == target)
4300  break;
4301  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4303  else if (xact->blockState == TBLOCK_SUBABORT)
4305  else
4306  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4308  xact = xact->parent;
4309  Assert(PointerIsValid(xact));
4310  }
4311 
4312  /* And mark the target as "restart pending" */
4313  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4314  xact->blockState = TBLOCK_SUBRESTART;
4315  else if (xact->blockState == TBLOCK_SUBABORT)
4317  else
4318  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4320 }
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:5313
#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 5155 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().

5156 {
5157  TransactionState s;
5158  Size nxids = 0;
5159  Size i = 0;
5160  TransactionId *workspace;
5162 
5163  result = (SerializedTransactionState *) start_address;
5164 
5165  result->xactIsoLevel = XactIsoLevel;
5166  result->xactDeferrable = XactDeferrable;
5168  result->currentFullTransactionId =
5171 
5172  /*
5173  * If we're running in a parallel worker and launching a parallel worker
5174  * of our own, we can just pass along the information that was passed to
5175  * us.
5176  */
5177  if (nParallelCurrentXids > 0)
5178  {
5180  memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
5182  return;
5183  }
5184 
5185  /*
5186  * OK, we need to generate a sorted list of XIDs that our workers should
5187  * view as current. First, figure out how many there are.
5188  */
5189  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5190  {
5192  nxids = add_size(nxids, 1);
5193  nxids = add_size(nxids, s->nChildXids);
5194  }
5196  <= maxsize);
5197 
5198  /* Copy them to our scratch space. */
5199  workspace = palloc(nxids * sizeof(TransactionId));
5200  for (s = CurrentTransactionState; s != NULL; s = s->parent)
5201  {
5203  workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
5204  memcpy(&workspace[i], s->childXids,
5205  s->nChildXids * sizeof(TransactionId));
5206  i += s->nChildXids;
5207  }
5208  Assert(i == nxids);
5209 
5210  /* Sort them. */
5211  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
5212 
5213  /* Copy data into output area. */
5214  result->nParallelCurrentXids = nxids;
5215  memcpy(&result->parallelCurrentXids[0], workspace,
5216  nxids * sizeof(TransactionId));
5217 }
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:924
int i
#define qsort(a, b, c, d)
Definition: port.h:491
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:1591
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 5225 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().

5226 {
5228 
5230  StartTransaction();
5231 
5232  tstate = (SerializedTransactionState *) tstatespace;
5233  XactIsoLevel = tstate->xactIsoLevel;
5234  XactDeferrable = tstate->xactDeferrable;
5237  tstate->currentFullTransactionId;
5241 
5243 }
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:5313
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 4641 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().

4642 {
4644 
4645  switch (s->blockState)
4646  {
4647  case TBLOCK_DEFAULT:
4648  case TBLOCK_STARTED:
4649  return 'I'; /* idle --- not in transaction */
4650  case TBLOCK_BEGIN:
4651  case TBLOCK_SUBBEGIN:
4652  case TBLOCK_INPROGRESS:
4655  case TBLOCK_SUBINPROGRESS:
4656  case TBLOCK_END:
4657  case TBLOCK_SUBRELEASE:
4658  case TBLOCK_SUBCOMMIT:
4659  case TBLOCK_PREPARE:
4660  return 'T'; /* in transaction */
4661  case TBLOCK_ABORT:
4662  case TBLOCK_SUBABORT:
4663  case TBLOCK_ABORT_END:
4664  case TBLOCK_SUBABORT_END:
4665  case TBLOCK_ABORT_PENDING:
4667  case TBLOCK_SUBRESTART:
4669  return 'E'; /* in failed transaction */
4670  }
4671 
4672  /* should never get here */
4673  elog(FATAL, "invalid transaction block state: %s",
4675  return 0; /* keep compiler quiet */
4676 }
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:5313
#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:1031
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:1031
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 3850 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), TransactionStateData::chain, CurrentTransactionState, elog, ereport, errcode(), errmsg(), 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().

3851 {
3853 
3854  switch (s->blockState)
3855  {
3856  /*
3857  * We are inside a transaction block and we got a ROLLBACK command
3858  * from the user, so tell CommitTransactionCommand to abort and
3859  * exit the transaction block.
3860  */
3861  case TBLOCK_INPROGRESS:
3863  break;
3864 
3865  /*
3866  * We are inside a failed transaction block and we got a ROLLBACK
3867  * command from the user. Abort processing is already done, so
3868  * CommitTransactionCommand just has to cleanup and go back to
3869  * idle state.
3870  */
3871  case TBLOCK_ABORT:
3873  break;
3874 
3875  /*
3876  * We are inside a subtransaction. Mark everything up to top
3877  * level as exitable.
3878  */
3879  case TBLOCK_SUBINPROGRESS:
3880  case TBLOCK_SUBABORT:
3881  while (s->parent != NULL)
3882  {
3883  if (s->blockState == TBLOCK_SUBINPROGRESS)
3885  else if (s->blockState == TBLOCK_SUBABORT)
3887  else
3888  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3890  s = s->parent;
3891  }
3892  if (s->blockState == TBLOCK_INPROGRESS)
3894  else if (s->blockState == TBLOCK_ABORT)
3896  else
3897  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3899  break;
3900 
3901  /*
3902  * The user issued ABORT when not inside a transaction. Issue a
3903  * WARNING and go to abort state. The upcoming call to
3904  * CommitTransactionCommand() will then put us back into the
3905  * default state.
3906  *
3907  * We do the same thing with ABORT inside an implicit transaction,
3908  * although in this case we might be rolling back actual database
3909  * state changes. (It's debatable whether we should issue a
3910  * WARNING in this case, but we have done so historically.)
3911  */
3912  case TBLOCK_STARTED:
3914  ereport(WARNING,
3915  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3916  errmsg("there is no transaction in progress")));
3918  break;
3919 
3920  /*
3921  * The user issued an ABORT that somehow ran inside a parallel
3922  * worker. We can't cope with that.
3923  */
3925  ereport(FATAL,
3926  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3927  errmsg("cannot abort during a parallel operation")));
3928  break;
3929 
3930  /* These cases are invalid. */
3931  case TBLOCK_DEFAULT:
3932  case TBLOCK_BEGIN:
3933  case TBLOCK_SUBBEGIN:
3934  case TBLOCK_END:
3935  case TBLOCK_SUBRELEASE:
3936  case TBLOCK_SUBCOMMIT:
3937  case TBLOCK_ABORT_END:
3938  case TBLOCK_SUBABORT_END:
3939  case TBLOCK_ABORT_PENDING:
3941  case TBLOCK_SUBRESTART:
3943  case TBLOCK_PREPARE:
3944  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3946  break;
3947  }
3948 
3951 
3952  s->chain = chain;
3953 }
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
struct TransactionStateData * parent
Definition: xact.c:194
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5313
#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:232
#define XLogRecGetData(decoder)
Definition: xlogreader.h:233
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:229
#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 5905 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.

5906 {
5907  uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
5908 
5909  /* Backup blocks are not used in xact records */
5910  Assert(!XLogRecHasAnyBlockRefs(record));
5911 
5912  if (info == XLOG_XACT_COMMIT)
5913  {
5914  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5915  xl_xact_parsed_commit parsed;
5916 
5917  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5918  xact_redo_commit(&parsed, XLogRecGetXid(record),
5919  record->EndRecPtr, XLogRecGetOrigin(record));
5920  }
5921  else if (info == XLOG_XACT_COMMIT_PREPARED)
5922  {
5923  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5924  xl_xact_parsed_commit parsed;
5925 
5926  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5927  xact_redo_commit(&parsed, parsed.twophase_xid,
5928  record->EndRecPtr, XLogRecGetOrigin(record));
5929 
5930  /* Delete TwoPhaseState gxact entry and/or 2PC file. */
5931  LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
5932  PrepareRedoRemove(parsed.twophase_xid, false);
5933  LWLockRelease(TwoPhaseStateLock);
5934  }
5935  else if (info == XLOG_XACT_ABORT)
5936  {
5937  xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
5938  xl_xact_parsed_abort parsed;
5939 
5940  ParseAbortRecord(XLogRecGetInfo(record), xlrec, &parsed);
5941  xact_redo_abort(&parsed, XLogRecGetXid(record));
5942  }
5943  else if (info == XLOG_XACT_ABORT_PREPARED)
5944  {
5945