PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xact.h File Reference
#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 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_ACCESSEDTEMPREL   (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_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 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)
 
void MarkCurrentTransactionIdLoggedIfAny (void)
 
bool SubTransactionIsActive (SubTransactionId subxid)
 
CommandId GetCurrentCommandId (bool used)
 
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 CommitTransactionCommand (void)
 
void AbortCurrentTransaction (void)
 
void BeginTransactionBlock (void)
 
bool EndTransactionBlock (void)
 
bool PrepareTransactionBlock (char *gid)
 
void UserAbortTransactionBlock (void)
 
void ReleaseSavepoint (List *options)
 
void DefineSavepoint (char *name)
 
void RollbackToSavepoint (List *options)
 
void BeginInternalSubTransaction (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 PreventTransactionChain (bool isTopLevel, const char *stmtType)
 
void RequireTransactionChain (bool isTopLevel, const char *stmtType)
 
void WarnNoTransactionChain (bool isTopLevel, const char *stmtType)
 
bool IsInTransactionChain (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)
 
XLogRecPtr XactLogAbortRecord (TimestampTz abort_time, int nsubxacts, TransactionId *subxacts, int nrels, RelFileNode *rels, int xactflags, TransactionId twophase_xid)
 
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 DefaultXactDeferrable
 
bool XactDeferrable
 
int synchronous_commit
 
int MyXactFlags
 

Macro Definition Documentation

#define MinSizeOfXactAbort   sizeof(xl_xact_abort)

Definition at line 279 of file xact.h.

Referenced by ParseAbortRecord(), and XactLogAbortRecord().

#define MinSizeOfXactAssignment   offsetof(xl_xact_assignment, xsub)

Definition at line 187 of file xact.h.

Referenced by AssignTransactionId().

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

Definition at line 266 of file xact.h.

Referenced by ParseCommitRecord().

#define MinSizeOfXactInvals   offsetof(xl_xact_invals, msgs)

Definition at line 241 of file xact.h.

Referenced by ParseCommitRecord(), and XactLogCommitRecord().

#define MinSizeOfXactRelfilenodes   offsetof(xl_xact_relfilenodes, xnodes)
#define MinSizeOfXactSubxacts   offsetof(xl_xact_subxacts, subxacts)
#define SYNCHRONOUS_COMMIT_ON   SYNCHRONOUS_COMMIT_REMOTE_FLUSH

Definition at line 68 of file xact.h.

#define XACT_COMPLETION_APPLY_FEEDBACK   (1U << 29)

Definition at line 168 of file xact.h.

Referenced by XactLogCommitRecord().

#define XACT_COMPLETION_FORCE_SYNC_COMMIT   (1U << 31)

Definition at line 170 of file xact.h.

Referenced by XactLogCommitRecord().

#define XACT_COMPLETION_UPDATE_RELCACHE_FILE   (1U << 30)

Definition at line 169 of file xact.h.

Referenced by XactLogCommitRecord().

#define XACT_FLAGS_ACCESSEDTEMPREL   (1U << 0)
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK   (1U << 1)
#define XACT_READ_COMMITTED   1

Definition at line 29 of file xact.h.

Referenced by check_XactIsoLevel(), InitPostgres(), and show_XactIsoLevel().

#define XACT_READ_UNCOMMITTED   0

Definition at line 28 of file xact.h.

Referenced by check_XactIsoLevel(), and show_XactIsoLevel().

#define XACT_REPEATABLE_READ   2
#define XACT_SERIALIZABLE   3

Definition at line 31 of file xact.h.

Referenced by check_XactIsoLevel(), ImportSnapshot(), and show_XactIsoLevel().

#define XACT_XINFO_HAS_AE_LOCKS   (1U << 6)

Definition at line 158 of file xact.h.

Referenced by xact_redo_abort(), xact_redo_commit(), XactLogAbortRecord(), and XactLogCommitRecord().

#define XACT_XINFO_HAS_DBINFO   (1U << 0)

Definition at line 152 of file xact.h.

Referenced by ParseCommitRecord(), and XactLogCommitRecord().

#define XACT_XINFO_HAS_INVALS   (1U << 3)

Definition at line 155 of file xact.h.

Referenced by ParseCommitRecord(), and XactLogCommitRecord().

#define XACT_XINFO_HAS_ORIGIN   (1U << 5)
#define XACT_XINFO_HAS_RELFILENODES   (1U << 2)
#define XACT_XINFO_HAS_SUBXACTS   (1U << 1)
#define XACT_XINFO_HAS_TWOPHASE   (1U << 4)
#define XactCompletionApplyFeedback (   xinfo)    ((xinfo & XACT_COMPLETION_APPLY_FEEDBACK) != 0)

Definition at line 173 of file xact.h.

Referenced by xact_redo_commit().

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

Definition at line 177 of file xact.h.

Referenced by xact_desc_commit(), and xact_redo_commit().

#define XactCompletionRelcacheInitFileInval (   xinfo)    ((xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE) != 0)

Definition at line 175 of file xact.h.

Referenced by xact_desc_commit(), and xact_redo_commit().

#define XLOG_XACT_ABORT   0x20
#define XLOG_XACT_ABORT_PREPARED   0x40
#define XLOG_XACT_ASSIGNMENT   0x50

Definition at line 138 of file xact.h.

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

#define XLOG_XACT_COMMIT_PREPARED   0x30
#define XLOG_XACT_HAS_INFO   0x80
#define XLOG_XACT_PREPARE   0x10

Definition at line 134 of file xact.h.

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

Typedef Documentation

typedef void(* SubXactCallback)(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)

Definition at line 120 of file xact.h.

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

Definition at line 110 of file xact.h.

Enumeration Type Documentation

Enumerator
SUBXACT_EVENT_START_SUB 
SUBXACT_EVENT_COMMIT_SUB 
SUBXACT_EVENT_ABORT_SUB 
SUBXACT_EVENT_PRE_COMMIT_SUB 

Definition at line 112 of file xact.h.

Enumerator
SYNCHRONOUS_COMMIT_OFF 
SYNCHRONOUS_COMMIT_LOCAL_FLUSH 
SYNCHRONOUS_COMMIT_REMOTE_WRITE 
SYNCHRONOUS_COMMIT_REMOTE_FLUSH 
SYNCHRONOUS_COMMIT_REMOTE_APPLY 

Definition at line 57 of file xact.h.

58 {
59  SYNCHRONOUS_COMMIT_OFF, /* asynchronous commit */
60  SYNCHRONOUS_COMMIT_LOCAL_FLUSH, /* wait for local flush only */
61  SYNCHRONOUS_COMMIT_REMOTE_WRITE, /* wait for local flush and remote
62  * write */
63  SYNCHRONOUS_COMMIT_REMOTE_FLUSH, /* wait for local and remote flush */
64  SYNCHRONOUS_COMMIT_REMOTE_APPLY /* wait for local flush and remote apply */
SyncCommitLevel
Definition: xact.h:57
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 98 of file xact.h.

Function Documentation

void AbortCurrentTransaction ( void  )

Definition at line 2987 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_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 AbortCurrentTransaction(), AutoVacLauncherMain(), PostgresMain(), ReorderBufferCommit(), ReorderBufferImmediateInvalidation(), and SnapBuildClearExportedSnapshot().

2988 {
2990 
2991  switch (s->blockState)
2992  {
2993  case TBLOCK_DEFAULT:
2994  if (s->state == TRANS_DEFAULT)
2995  {
2996  /* we are idle, so nothing to do */
2997  }
2998  else
2999  {
3000  /*
3001  * We can get here after an error during transaction start
3002  * (state will be TRANS_START). Need to clean up the
3003  * incompletely started transaction. First, adjust the
3004  * low-level state to suppress warning message from
3005  * AbortTransaction.
3006  */
3007  if (s->state == TRANS_START)
3008  s->state = TRANS_INPROGRESS;
3009  AbortTransaction();
3011  }
3012  break;
3013 
3014  /*
3015  * if we aren't in a transaction block, we just do the basic abort
3016  * & cleanup transaction.
3017  */
3018  case TBLOCK_STARTED:
3019  AbortTransaction();
3022  break;
3023 
3024  /*
3025  * If we are in TBLOCK_BEGIN it means something screwed up right
3026  * after reading "BEGIN TRANSACTION". We assume that the user
3027  * will interpret the error as meaning the BEGIN failed to get him
3028  * into a transaction block, so we should abort and return to idle
3029  * state.
3030  */
3031  case TBLOCK_BEGIN:
3032  AbortTransaction();
3035  break;
3036 
3037  /*
3038  * We are somewhere in a transaction block and we've gotten a
3039  * failure, so we abort the transaction and set up the persistent
3040  * ABORT state. We will stay in ABORT until we get a ROLLBACK.
3041  */
3042  case TBLOCK_INPROGRESS:
3044  AbortTransaction();
3045  s->blockState = TBLOCK_ABORT;
3046  /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
3047  break;
3048 
3049  /*
3050  * Here, we failed while trying to COMMIT. Clean up the
3051  * transaction and return to idle state (we do not want to stay in
3052  * the transaction).
3053  */
3054  case TBLOCK_END:
3055  AbortTransaction();
3058  break;
3059 
3060  /*
3061  * Here, we are already in an aborted transaction state and are
3062  * waiting for a ROLLBACK, but for some reason we failed again! So
3063  * we just remain in the abort state.
3064  */
3065  case TBLOCK_ABORT:
3066  case TBLOCK_SUBABORT:
3067  break;
3068 
3069  /*
3070  * We are in a failed transaction and we got the ROLLBACK command.
3071  * We have already aborted, we just need to cleanup and go to idle
3072  * state.
3073  */
3074  case TBLOCK_ABORT_END:
3077  break;
3078 
3079  /*
3080  * We are in a live transaction and we got a ROLLBACK command.
3081  * Abort, cleanup, go to idle state.
3082  */
3083  case TBLOCK_ABORT_PENDING:
3084  AbortTransaction();
3087  break;
3088 
3089  /*
3090  * Here, we failed while trying to PREPARE. Clean up the
3091  * transaction and return to idle state (we do not want to stay in
3092  * the transaction).
3093  */
3094  case TBLOCK_PREPARE:
3095  AbortTransaction();
3098  break;
3099 
3100  /*
3101  * We got an error inside a subtransaction. Abort just the
3102  * subtransaction, and go to the persistent SUBABORT state until
3103  * we get ROLLBACK.
3104  */
3105  case TBLOCK_SUBINPROGRESS:
3108  break;
3109 
3110  /*
3111  * If we failed while trying to create a subtransaction, clean up
3112  * the broken subtransaction and abort the parent. The same
3113  * applies if we get a failure while ending a subtransaction.
3114  */
3115  case TBLOCK_SUBBEGIN:
3116  case TBLOCK_SUBRELEASE:
3117  case TBLOCK_SUBCOMMIT:
3119  case TBLOCK_SUBRESTART:
3123  break;
3124 
3125  /*
3126  * Same as above, except the Abort() was already done.
3127  */
3128  case TBLOCK_SUBABORT_END:
3132  break;
3133  }
3134 }
void AbortCurrentTransaction(void)
Definition: xact.c:2987
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
TransState state
Definition: xact.c:176
static void CleanupTransaction(void)
Definition: xact.c:2629
static void AbortSubTransaction(void)
Definition: xact.c:4546
static void AbortTransaction(void)
Definition: xact.c:2448
static void CleanupSubTransaction(void)
Definition: xact.c:4691
void AbortOutOfAnyTransaction ( void  )

Definition at line 4223 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, NULL, TransactionStateData::parent, TransactionStateData::state, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, 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().

4224 {
4226 
4227  /*
4228  * Get out of any transaction or nested transaction
4229  */
4230  do
4231  {
4232  switch (s->blockState)
4233  {
4234  case TBLOCK_DEFAULT:
4235  if (s->state == TRANS_DEFAULT)
4236  {
4237  /* Not in a transaction, do nothing */
4238  }
4239  else
4240  {
4241  /*
4242  * We can get here after an error during transaction start
4243  * (state will be TRANS_START). Need to clean up the
4244  * incompletely started transaction. First, adjust the
4245  * low-level state to suppress warning message from
4246  * AbortTransaction.
4247  */
4248  if (s->state == TRANS_START)
4249  s->state = TRANS_INPROGRESS;
4250  AbortTransaction();
4252  }
4253  break;
4254  case TBLOCK_STARTED:
4255  case TBLOCK_BEGIN:
4256  case TBLOCK_INPROGRESS:
4258  case TBLOCK_END:
4259  case TBLOCK_ABORT_PENDING:
4260  case TBLOCK_PREPARE:
4261  /* In a transaction, so clean up */
4262  AbortTransaction();
4265  break;
4266  case TBLOCK_ABORT:
4267  case TBLOCK_ABORT_END:
4268  /* AbortTransaction already done, still need Cleanup */
4271  break;
4272 
4273  /*
4274  * In a subtransaction, so clean it up and abort parent too
4275  */
4276  case TBLOCK_SUBBEGIN:
4277  case TBLOCK_SUBINPROGRESS:
4278  case TBLOCK_SUBRELEASE:
4279  case TBLOCK_SUBCOMMIT:
4281  case TBLOCK_SUBRESTART:
4284  s = CurrentTransactionState; /* changed by pop */
4285  break;
4286 
4287  case TBLOCK_SUBABORT:
4288  case TBLOCK_SUBABORT_END:
4290  /* As above, but AbortSubTransaction already done */
4292  s = CurrentTransactionState; /* changed by pop */
4293  break;
4294  }
4295  } while (s->blockState != TBLOCK_DEFAULT);
4296 
4297  /* Should be out of all subxacts now */
4298  Assert(s->parent == NULL);
4299 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
TransState state
Definition: xact.c:176
static void CleanupTransaction(void)
Definition: xact.c:2629
struct TransactionStateData * parent
Definition: xact.c:191
static void AbortSubTransaction(void)
Definition: xact.c:4546
static void AbortTransaction(void)
Definition: xact.c:2448
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static void CleanupSubTransaction(void)
Definition: xact.c:4691
void BeginInternalSubTransaction ( char *  name)

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

4055 {
4057 
4058  /*
4059  * Workers synchronize transaction state at the beginning of each parallel
4060  * operation, so we can't account for new subtransactions after that
4061  * point. We might be able to make an exception for the type of
4062  * subtransaction established by this function, which is typically used in
4063  * contexts where we're going to release or roll back the subtransaction
4064  * before proceeding further, so that no enduring change to the
4065  * transaction state occurs. For now, however, we prohibit this case along
4066  * with all the others.
4067  */
4068  if (IsInParallelMode())
4069  ereport(ERROR,
4070  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4071  errmsg("cannot start subtransactions during a parallel operation")));
4072 
4073  switch (s->blockState)
4074  {
4075  case TBLOCK_STARTED:
4076  case TBLOCK_INPROGRESS:
4077  case TBLOCK_END:
4078  case TBLOCK_PREPARE:
4079  case TBLOCK_SUBINPROGRESS:
4080  /* Normal subtransaction start */
4081  PushTransaction();
4082  s = CurrentTransactionState; /* changed by push */
4083 
4084  /*
4085  * Savepoint names, like the TransactionState block itself, live
4086  * in TopTransactionContext.
4087  */
4088  if (name)
4090  break;
4091 
4092  /* These cases are invalid. */
4093  case TBLOCK_DEFAULT:
4094  case TBLOCK_BEGIN:
4096  case TBLOCK_SUBBEGIN:
4097  case TBLOCK_SUBRELEASE:
4098  case TBLOCK_SUBCOMMIT:
4099  case TBLOCK_ABORT:
4100  case TBLOCK_SUBABORT:
4101  case TBLOCK_ABORT_END:
4102  case TBLOCK_SUBABORT_END:
4103  case TBLOCK_ABORT_PENDING:
4105  case TBLOCK_SUBRESTART:
4107  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4109  break;
4110  }
4111 
4114 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
MemoryContext TopTransactionContext
Definition: mcxt.c:48
void CommitTransactionCommand(void)
Definition: xact.c:2748
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
bool IsInParallelMode(void)
Definition: xact.c:913
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
static void PushTransaction(void)
Definition: xact.c:4724
void StartTransactionCommand(void)
Definition: xact.c:2678
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1064
#define elog
Definition: elog.h:219
void BeginTransactionBlock ( void  )

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

3417 {
3419 
3420  switch (s->blockState)
3421  {
3422  /*
3423  * We are not inside a transaction block, so allow one to begin.
3424  */
3425  case TBLOCK_STARTED:
3426  s->blockState = TBLOCK_BEGIN;
3427  break;
3428 
3429  /*
3430  * Already a transaction block in progress.
3431  */
3432  case TBLOCK_INPROGRESS:
3434  case TBLOCK_SUBINPROGRESS:
3435  case TBLOCK_ABORT:
3436  case TBLOCK_SUBABORT:
3437  ereport(WARNING,
3438  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3439  errmsg("there is already a transaction in progress")));
3440  break;
3441 
3442  /* These cases are invalid. */
3443  case TBLOCK_DEFAULT:
3444  case TBLOCK_BEGIN:
3445  case TBLOCK_SUBBEGIN:
3446  case TBLOCK_END:
3447  case TBLOCK_SUBRELEASE:
3448  case TBLOCK_SUBCOMMIT:
3449  case TBLOCK_ABORT_END:
3450  case TBLOCK_SUBABORT_END:
3451  case TBLOCK_ABORT_PENDING:
3453  case TBLOCK_SUBRESTART:
3455  case TBLOCK_PREPARE:
3456  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3458  break;
3459  }
3460 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void CommandCounterIncrement ( void  )

Definition at line 922 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(), AddRoleMems(), AlterPublicationOptions(), AlterRole(), apply_handle_delete(), apply_handle_insert(), apply_handle_update(), ATAddCheckConstraint(), ATExecAddColumn(), ATExecAlterColumnType(), ATExecCmd(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropIdentity(), ATExecSetTableSpace(), CommitSubTransaction(), CommitTransactionCommand(), create_ctas_internal(), create_toast_table(), CreateFKCheckTrigger(), createForeignKeyTriggers(), CreateForeignTable(), CreatePublication(), CreateRole(), CreateSchemaCommand(), 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(), InitTempTableNamespace(), inv_create(), inv_drop(), inv_truncate(), inv_write(), LogicalRepSyncTableStart(), make_new_heap(), makeConfigurationDependencies(), moveArrayTypeName(), OperatorShellMake(), OperatorUpd(), pg_import_system_collations(), PortalRunMulti(), PreCommit_on_commit_actions(), ProcedureCreate(), ProcessUtilitySlow(), recordExtensionInitPrivWorker(), reindex_relation(), RelationSetNewRelfilenode(), RenumberEnumType(), replorigin_create(), replorigin_drop(), ri_PerformCheck(), SetMatViewPopulatedState(), shdepReassignOwned(), SPI_cursor_open_internal(), and StoreConstraints().

923 {
924  /*
925  * If the current value of the command counter hasn't been "used" to mark
926  * tuples, we need not increment it, since there's no need to distinguish
927  * a read-only command from others. This helps postpone command counter
928  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
929  */
931  {
932  /*
933  * Workers synchronize transaction state at the beginning of each
934  * parallel operation, so we can't account for new commands after that
935  * point.
936  */
938  elog(ERROR, "cannot start commands during a parallel operation");
939 
940  currentCommandId += 1;
942  {
943  currentCommandId -= 1;
944  ereport(ERROR,
945  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
946  errmsg("cannot have more than 2^32-2 commands in a transaction")));
947  }
948  currentCommandIdUsed = false;
949 
950  /* Propagate new command ID into static snapshots */
952 
953  /*
954  * Make any catalog changes done by the just-completed command visible
955  * in the local syscache. We obviously don't need to do this after a
956  * read-only command. (But see hacks in inval.c to make real sure we
957  * don't think a command that queued inval messages was read-only.)
958  */
960  }
961 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsInParallelMode(void)
Definition: xact.c:913
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define IsParallelWorker()
Definition: parallel.h:52
#define InvalidCommandId
Definition: c.h:414
static void AtCCI_LocalCache(void)
Definition: xact.c:1365
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:544
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static CommandId currentCommandId
Definition: xact.c:239
static bool currentCommandIdUsed
Definition: xact.c:240
void CommitTransactionCommand ( void  )

Definition at line 2748 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, AssertState, TransactionStateData::blockState, BlockStateAsString(), CleanupSubTransaction(), CleanupTransaction(), CommandCounterIncrement(), CommitSubTransaction(), CommitTransaction(), CommitTransactionCommand(), CurrentTransactionState, DefineSavepoint(), elog, ERROR, FATAL, TransactionStateData::name, name, NULL, TransactionStateData::parent, PrepareTransaction(), TransactionStateData::savepointLevel, StartSubTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, 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 apply_handle_commit(), ApplyWorkerMain(), BeginInternalSubTransaction(), BootstrapModeMain(), cluster(), CommitTransactionCommand(), DefineIndex(), do_autovacuum(), 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(), RemoveTempRelationsCallback(), vacuum(), and vacuum_rel().

2749 {
2751 
2752  switch (s->blockState)
2753  {
2754  /*
2755  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2756  * StartTransactionCommand didn't set the STARTED state
2757  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2758  * by EndParallelWorkerTransaction(), not this function.
2759  */
2760  case TBLOCK_DEFAULT:
2762  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2764  break;
2765 
2766  /*
2767  * If we aren't in a transaction block, just do our usual
2768  * transaction commit, and return to the idle state.
2769  */
2770  case TBLOCK_STARTED:
2773  break;
2774 
2775  /*
2776  * We are completing a "BEGIN TRANSACTION" command, so we change
2777  * to the "transaction block in progress" state and return. (We
2778  * assume the BEGIN did nothing to the database, so we need no
2779  * CommandCounterIncrement.)
2780  */
2781  case TBLOCK_BEGIN:
2783  break;
2784 
2785  /*
2786  * This is the case when we have finished executing a command
2787  * someplace within a transaction block. We increment the command
2788  * counter and return.
2789  */
2790  case TBLOCK_INPROGRESS:
2791  case TBLOCK_SUBINPROGRESS:
2793  break;
2794 
2795  /*
2796  * We are completing a "COMMIT" command. Do it and return to the
2797  * idle state.
2798  */
2799  case TBLOCK_END:
2802  break;
2803 
2804  /*
2805  * Here we are in the middle of a transaction block but one of the
2806  * commands caused an abort so we do nothing but remain in the
2807  * abort state. Eventually we will get a ROLLBACK command.
2808  */
2809  case TBLOCK_ABORT:
2810  case TBLOCK_SUBABORT:
2811  break;
2812 
2813  /*
2814  * Here we were in an aborted transaction block and we just got
2815  * the ROLLBACK command from the user, so clean up the
2816  * already-aborted transaction and return to the idle state.
2817  */
2818  case TBLOCK_ABORT_END:
2821  break;
2822 
2823  /*
2824  * Here we were in a perfectly good transaction block but the user
2825  * told us to ROLLBACK anyway. We have to abort the transaction
2826  * and then clean up.
2827  */
2828  case TBLOCK_ABORT_PENDING:
2829  AbortTransaction();
2832  break;
2833 
2834  /*
2835  * We are completing a "PREPARE TRANSACTION" command. Do it and
2836  * return to the idle state.
2837  */
2838  case TBLOCK_PREPARE:
2841  break;
2842 
2843  /*
2844  * We were just issued a SAVEPOINT inside a transaction block.
2845  * Start a subtransaction. (DefineSavepoint already did
2846  * PushTransaction, so as to have someplace to put the SUBBEGIN
2847  * state.)
2848  */
2849  case TBLOCK_SUBBEGIN:
2852  break;
2853 
2854  /*
2855  * We were issued a RELEASE command, so we end the current
2856  * subtransaction and return to the parent transaction. The parent
2857  * might be ended too, so repeat till we find an INPROGRESS
2858  * transaction or subtransaction.
2859  */
2860  case TBLOCK_SUBRELEASE:
2861  do
2862  {
2864  s = CurrentTransactionState; /* changed by pop */
2865  } while (s->blockState == TBLOCK_SUBRELEASE);
2866 
2869  break;
2870 
2871  /*
2872  * We were issued a COMMIT, so we end the current subtransaction
2873  * hierarchy and perform final commit. We do this by rolling up
2874  * any subtransactions into their parent, which leads to O(N^2)
2875  * operations with respect to resource owners - this isn't that
2876  * bad until we approach a thousands of savepoints but is
2877  * necessary for correctness should after triggers create new
2878  * resource owners.
2879  */
2880  case TBLOCK_SUBCOMMIT:
2881  do
2882  {
2884  s = CurrentTransactionState; /* changed by pop */
2885  } while (s->blockState == TBLOCK_SUBCOMMIT);
2886  /* If we had a COMMIT command, finish off the main xact too */
2887  if (s->blockState == TBLOCK_END)
2888  {
2889  Assert(s->parent == NULL);
2892  }
2893  else if (s->blockState == TBLOCK_PREPARE)
2894  {
2895  Assert(s->parent == NULL);
2898  }
2899  else
2900  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2902  break;
2903 
2904  /*
2905  * The current already-failed subtransaction is ending due to a
2906  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2907  * examine the parent (which could be in any of several states).
2908  */
2909  case TBLOCK_SUBABORT_END:
2912  break;
2913 
2914  /*
2915  * As above, but it's not dead yet, so abort first.
2916  */
2921  break;
2922 
2923  /*
2924  * The current subtransaction is the target of a ROLLBACK TO
2925  * command. Abort and pop it, then start a new subtransaction
2926  * with the same name.
2927  */
2928  case TBLOCK_SUBRESTART:
2929  {
2930  char *name;
2931  int savepointLevel;
2932 
2933  /* save name and keep Cleanup from freeing it */
2934  name = s->name;
2935  s->name = NULL;
2936  savepointLevel = s->savepointLevel;
2937 
2940 
2942  s = CurrentTransactionState; /* changed by push */
2943  s->name = name;
2944  s->savepointLevel = savepointLevel;
2945 
2946  /* This is the same as TBLOCK_SUBBEGIN case */
2950  }
2951  break;
2952 
2953  /*
2954  * Same as above, but the subtransaction had already failed, so we
2955  * don't need AbortSubTransaction.
2956  */
2958  {
2959  char *name;
2960  int savepointLevel;
2961 
2962  /* save name and keep Cleanup from freeing it */
2963  name = s->name;
2964  s->name = NULL;
2965  savepointLevel = s->savepointLevel;
2966 
2968 
2970  s = CurrentTransactionState; /* changed by push */
2971  s->name = name;
2972  s->savepointLevel = savepointLevel;
2973 
2974  /* This is the same as TBLOCK_SUBBEGIN case */
2978  }
2979  break;
2980  }
2981 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
#define AssertState(condition)
Definition: c.h:678
void CommitTransactionCommand(void)
Definition: xact.c:2748
TBlockState blockState
Definition: xact.c:177
static void PrepareTransaction(void)
Definition: xact.c:2179
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4438
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2629
struct TransactionStateData * parent
Definition: xact.c:191
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
static void AbortSubTransaction(void)
Definition: xact.c:4546
void DefineSavepoint(char *name)
Definition: xact.c:3749
void CommandCounterIncrement(void)
Definition: xact.c:922
static void AbortTransaction(void)
Definition: xact.c:2448
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static void CleanupSubTransaction(void)
Definition: xact.c:4691
const char * name
Definition: encode.c:521
static void CommitTransaction(void)
Definition: xact.c:1940
#define elog
Definition: elog.h:219
static void StartSubTransaction(void)
Definition: xact.c:4400
void DefineSavepoint ( char *  name)

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

3750 {
3752 
3753  /*
3754  * Workers synchronize transaction state at the beginning of each parallel
3755  * operation, so we can't account for new subtransactions after that
3756  * point. (Note that this check will certainly error out if s->blockState
3757  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3758  * below.)
3759  */
3760  if (IsInParallelMode())
3761  ereport(ERROR,
3762  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3763  errmsg("cannot define savepoints during a parallel operation")));
3764 
3765  switch (s->blockState)
3766  {
3767  case TBLOCK_INPROGRESS:
3768  case TBLOCK_SUBINPROGRESS:
3769  /* Normal subtransaction start */
3770  PushTransaction();
3771  s = CurrentTransactionState; /* changed by push */
3772 
3773  /*
3774  * Savepoint names, like the TransactionState block itself, live
3775  * in TopTransactionContext.
3776  */
3777  if (name)
3779  break;
3780 
3781  /* These cases are invalid. */
3782  case TBLOCK_DEFAULT:
3783  case TBLOCK_STARTED:
3784  case TBLOCK_BEGIN:
3786  case TBLOCK_SUBBEGIN:
3787  case TBLOCK_END:
3788  case TBLOCK_SUBRELEASE:
3789  case TBLOCK_SUBCOMMIT:
3790  case TBLOCK_ABORT:
3791  case TBLOCK_SUBABORT:
3792  case TBLOCK_ABORT_END:
3793  case TBLOCK_SUBABORT_END:
3794  case TBLOCK_ABORT_PENDING:
3796  case TBLOCK_SUBRESTART:
3798  case TBLOCK_PREPARE:
3799  elog(FATAL, "DefineSavepoint: unexpected state %s",
3801  break;
3802  }
3803 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
MemoryContext TopTransactionContext
Definition: mcxt.c:48
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
bool IsInParallelMode(void)
Definition: xact.c:913
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
static void PushTransaction(void)
Definition: xact.c:4724
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1064
#define elog
Definition: elog.h:219
void EndParallelWorkerTransaction ( void  )

Definition at line 4941 of file xact.c.

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

Referenced by ParallelWorkerMain().

4942 {
4946 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
#define Assert(condition)
Definition: c.h:675
static void CommitTransaction(void)
Definition: xact.c:1940
bool EndTransactionBlock ( void  )

Definition at line 3526 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), FATAL, NULL, TransactionStateData::parent, result, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, 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().

3527 {
3529  bool result = false;
3530 
3531  switch (s->blockState)
3532  {
3533  /*
3534  * We are in a transaction block, so tell CommitTransactionCommand
3535  * to COMMIT.
3536  */
3537  case TBLOCK_INPROGRESS:
3538  s->blockState = TBLOCK_END;
3539  result = true;
3540  break;
3541 
3542  /*
3543  * We are in a failed transaction block. Tell
3544  * CommitTransactionCommand it's time to exit the block.
3545  */
3546  case TBLOCK_ABORT:
3548  break;
3549 
3550  /*
3551  * We are in a live subtransaction block. Set up to subcommit all
3552  * open subtransactions and then commit the main transaction.
3553  */
3554  case TBLOCK_SUBINPROGRESS:
3555  while (s->parent != NULL)
3556  {
3557  if (s->blockState == TBLOCK_SUBINPROGRESS)
3559  else
3560  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3562  s = s->parent;
3563  }
3564  if (s->blockState == TBLOCK_INPROGRESS)
3565  s->blockState = TBLOCK_END;
3566  else
3567  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3569  result = true;
3570  break;
3571 
3572  /*
3573  * Here we are inside an aborted subtransaction. Treat the COMMIT
3574  * as ROLLBACK: set up to abort everything and exit the main
3575  * transaction.
3576  */
3577  case TBLOCK_SUBABORT:
3578  while (s->parent != NULL)
3579  {
3580  if (s->blockState == TBLOCK_SUBINPROGRESS)
3582  else if (s->blockState == TBLOCK_SUBABORT)
3584  else
3585  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3587  s = s->parent;
3588  }
3589  if (s->blockState == TBLOCK_INPROGRESS)
3591  else if (s->blockState == TBLOCK_ABORT)
3593  else
3594  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3596  break;
3597 
3598  /*
3599  * The user issued COMMIT when not inside a transaction. Issue a
3600  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3601  * CommitTransactionCommand() will then close the transaction and
3602  * put us back into the default state.
3603  */
3604  case TBLOCK_STARTED:
3605  ereport(WARNING,
3606  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3607  errmsg("there is no transaction in progress")));
3608  result = true;
3609  break;
3610 
3611  /*
3612  * The user issued a COMMIT that somehow ran inside a parallel
3613  * worker. We can't cope with that.
3614  */
3616  ereport(FATAL,
3617  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3618  errmsg("cannot commit during a parallel operation")));
3619  break;
3620 
3621  /* These cases are invalid. */
3622  case TBLOCK_DEFAULT:
3623  case TBLOCK_BEGIN:
3624  case TBLOCK_SUBBEGIN:
3625  case TBLOCK_END:
3626  case TBLOCK_SUBRELEASE:
3627  case TBLOCK_SUBCOMMIT:
3628  case TBLOCK_ABORT_END:
3629  case TBLOCK_SUBABORT_END:
3630  case TBLOCK_ABORT_PENDING:
3632  case TBLOCK_SUBRESTART:
3634  case TBLOCK_PREPARE:
3635  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3637  break;
3638  }
3639 
3640  return result;
3641 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
TBlockState blockState
Definition: xact.c:177
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:191
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void EnterParallelMode ( void  )

Definition at line 880 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

881 {
883 
884  Assert(s->parallelModeLevel >= 0);
885 
886  ++s->parallelModeLevel;
887 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
int parallelModeLevel
Definition: xact.c:190
#define Assert(condition)
Definition: c.h:675
Size EstimateTransactionStateSpace ( void  )

Definition at line 4817 of file xact.c.

References add_size(), mul_size(), TransactionStateData::nChildXids, nParallelCurrentXids, NULL, TransactionStateData::parent, TransactionStateData::transactionId, and TransactionIdIsValid.

Referenced by InitializeParallelDSM().

4818 {
4819  TransactionState s;
4820  Size nxids = 6; /* iso level, deferrable, top & current XID,
4821  * command counter, XID count */
4822 
4823  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4824  {
4826  nxids = add_size(nxids, 1);
4827  nxids = add_size(nxids, s->nChildXids);
4828  }
4829 
4830  nxids = add_size(nxids, nParallelCurrentXids);
4831  return mul_size(nxids, sizeof(TransactionId));
4832 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
uint32 TransactionId
Definition: c.h:397
int nParallelCurrentXids
Definition: xact.c:108
struct TransactionStateData * parent
Definition: xact.c:191
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
TransactionId transactionId
Definition: xact.c:172
#define NULL
Definition: c.h:229
size_t Size
Definition: c.h:356
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ExitParallelMode ( void  )

Definition at line 893 of file xact.c.

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

Referenced by ExecutePlan(), and ParallelWorkerMain().

894 {
896 
897  Assert(s->parallelModeLevel > 0);
899 
900  --s->parallelModeLevel;
901 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
bool ParallelContextActive(void)
Definition: parallel.c:668
int parallelModeLevel
Definition: xact.c:190
#define Assert(condition)
Definition: c.h:675
void ForceSyncCommit ( void  )

Definition at line 970 of file xact.c.

References forceSyncCommit.

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

971 {
972  forceSyncCommit = true;
973 }
static bool forceSyncCommit
Definition: xact.c:262
CommandId GetCurrentCommandId ( bool  used)

Definition at line 687 of file xact.c.

References Assert, currentCommandId, currentCommandIdUsed, and TransactionStateData::parallelModeLevel.

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

688 {
689  /* this is global to a transaction, not subtransaction-local */
690  if (used)
691  {
692  /*
693  * Forbid setting currentCommandIdUsed in parallel mode, because we
694  * have no provision for communicating this back to the master. We
695  * could relax this restriction when currentCommandIdUsed was already
696  * true at the start of the parallel operation.
697  */
699  currentCommandIdUsed = true;
700  }
701  return currentCommandId;
702 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
int parallelModeLevel
Definition: xact.c:190
#define Assert(condition)
Definition: c.h:675
static CommandId currentCommandId
Definition: xact.c:239
static bool currentCommandIdUsed
Definition: xact.c:240
TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 717 of file xact.c.

References stmtStartTimestamp.

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

718 {
719  return stmtStartTimestamp;
720 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:250
TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 434 of file xact.c.

References TransactionStateData::transactionId.

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

435 {
437 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TransactionId transactionId
Definition: xact.c:172
TimestampTz GetCurrentTransactionStartTimestamp ( void  )
TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 729 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

730 {
731  if (xactStopTimestamp != 0)
732  return xactStopTimestamp;
733  return GetCurrentTimestamp();
734 }
static TimestampTz xactStopTimestamp
Definition: xact.c:251
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
TransactionId GetStableLatestTransactionId ( void  )

Definition at line 461 of file xact.c.

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

Referenced by xid_age().

462 {
464  static TransactionId stablexid = InvalidTransactionId;
465 
466  if (lxid != MyProc->lxid)
467  {
468  lxid = MyProc->lxid;
469  stablexid = GetTopTransactionIdIfAny();
470  if (!TransactionIdIsValid(stablexid))
471  stablexid = ReadNewTransactionId();
472  }
473 
474  Assert(TransactionIdIsValid(stablexid));
475 
476  return stablexid;
477 }
uint32 TransactionId
Definition: c.h:397
PGPROC * MyProc
Definition: proc.c:67
#define InvalidTransactionId
Definition: transam.h:31
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
uint32 LocalTransactionId
Definition: c.h:399
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:404
#define Assert(condition)
Definition: c.h:675
#define InvalidLocalTransactionId
Definition: lock.h:69
#define TransactionIdIsValid(xid)
Definition: transam.h:41
LocalTransactionId lxid
Definition: proc.h:105
TransactionId GetTopTransactionId ( void  )

Definition at line 389 of file xact.c.

References AssignTransactionId(), TransactionIdIsValid, and XactTopTransactionId.

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

390 {
393  return XactTopTransactionId;
394 }
TransactionId XactTopTransactionId
Definition: xact.c:107
static TransactionStateData TopTransactionStateData
Definition: xact.c:201
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void AssignTransactionId(TransactionState s)
Definition: xact.c:489
bool IsInTransactionChain ( bool  isTopLevel)

Definition at line 3271 of file xact.c.

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

Referenced by vacuum().

3272 {
3273  /*
3274  * Return true on same conditions that would make PreventTransactionChain
3275  * error out
3276  */
3277  if (IsTransactionBlock())
3278  return true;
3279 
3280  if (IsSubTransaction())
3281  return true;
3282 
3283  if (!isTopLevel)
3284  return true;
3285 
3288  return true;
3289 
3290  return false;
3291 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
bool IsTransactionBlock(void)
Definition: xact.c:4305
bool IsSubTransaction(void)
Definition: xact.c:4377
bool IsTransactionBlock ( void  )

Definition at line 4305 of file xact.c.

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

Referenced by CheckTransactionChain(), CreateReplicationSlot(), exec_replication_command(), IsInTransactionChain(), and PreventTransactionChain().

4306 {
4308 
4310  return false;
4311 
4312  return true;
4313 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
bool IsTransactionOrTransactionBlock ( void  )

Definition at line 4323 of file xact.c.

References TransactionStateData::blockState, CurrentTransactionState, and TBLOCK_DEFAULT.

Referenced by PostgresMain(), ProcessCatchupInterrupt(), ProcessNotifyInterrupt(), RecoveryConflictInterrupt(), ReorderBufferCommit(), ReorderBufferImmediateInvalidation(), SnapBuildExportSnapshot(), and StartupDecodingContext().

4324 {
4326 
4327  if (s->blockState == TBLOCK_DEFAULT)
4328  return false;
4329 
4330  return true;
4331 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
bool IsTransactionState ( void  )

Definition at line 350 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

Referenced by apply_handle_commit(), apply_handle_origin(), check_client_encoding(), check_default_tablespace(), check_role(), check_session_authorization(), check_temp_tablespaces(), check_transaction_read_only(), check_TSCurrentConfig(), check_XactIsoLevel(), CreateInitDecodingContext(), ensure_transaction(), 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(), replorigin_create(), replorigin_drop(), SearchCatCache(), SetMultiXactIdLimit(), SetTransactionIdLimit(), SnapBuildClearExportedSnapshot(), and SocketBackend().

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

Definition at line 445 of file xact.c.

References TransactionStateData::didLogXid, TransactionStateData::transactionId, and TransactionIdIsValid.

Referenced by XLogInsertRecord().

446 {
449 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TransactionId transactionId
Definition: xact.c:172
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ParseAbortRecord ( uint8  info,
xl_xact_abort xlrec,
xl_xact_parsed_abort parsed 
)

Definition at line 122 of file xactdesc.c.

References MinSizeOfXactAbort, MinSizeOfXactRelfilenodes, MinSizeOfXactSubxacts, xl_xact_relfilenodes::nrels, xl_xact_parsed_abort::nrels, xl_xact_subxacts::nsubxacts, xl_xact_parsed_abort::nsubxacts, xl_xact_subxacts::subxacts, xl_xact_parsed_abort::subxacts, xl_xact_parsed_abort::twophase_xid, xl_xact_abort::xact_time, xl_xact_parsed_abort::xact_time, 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().

123 {
124  char *data = ((char *) xlrec) + MinSizeOfXactAbort;
125 
126  memset(parsed, 0, sizeof(*parsed));
127 
128  parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
129  * present */
130 
131  parsed->xact_time = xlrec->xact_time;
132 
133  if (info & XLOG_XACT_HAS_INFO)
134  {
135  xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
136 
137  parsed->xinfo = xl_xinfo->xinfo;
138 
139  data += sizeof(xl_xact_xinfo);
140  }
141 
142  if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
143  {
144  xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
145 
146  parsed->nsubxacts = xl_subxacts->nsubxacts;
147  parsed->subxacts = xl_subxacts->subxacts;
148 
149  data += MinSizeOfXactSubxacts;
150  data += parsed->nsubxacts * sizeof(TransactionId);
151  }
152 
153  if (parsed->xinfo & XACT_XINFO_HAS_RELFILENODES)
154  {
155  xl_xact_relfilenodes *xl_relfilenodes = (xl_xact_relfilenodes *) data;
156 
157  parsed->nrels = xl_relfilenodes->nrels;
158  parsed->xnodes = xl_relfilenodes->xnodes;
159 
161  data += xl_relfilenodes->nrels * sizeof(RelFileNode);
162  }
163 
164  if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
165  {
166  xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
167 
168  parsed->twophase_xid = xl_twophase->xid;
169 
170  data += sizeof(xl_xact_twophase);
171  }
172 }
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:397
TransactionId xid
Definition: xact.h:245
#define MinSizeOfXactAbort
Definition: xact.h:279
#define XLOG_XACT_HAS_INFO
Definition: xact.h:146
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:153
#define MinSizeOfXactRelfilenodes
Definition: xact.h:234
TimestampTz xact_time
Definition: xact.h:270
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:232
#define MinSizeOfXactSubxacts
Definition: xact.h:227
struct RelFileNode RelFileNode
struct xl_xact_twophase xl_xact_twophase
TransactionId * subxacts
Definition: xact.h:316
uint32 xinfo
Definition: xact.h:213
RelFileNode * xnodes
Definition: xact.h:319
int nsubxacts
Definition: xact.h:224
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:156
TransactionId twophase_xid
Definition: xact.h:321
TimestampTz xact_time
Definition: xact.h:312
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:154
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:225
void ParseCommitRecord ( uint8  info,
xl_xact_commit xlrec,
xl_xact_parsed_commit parsed 
)

Definition at line 35 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, xl_xact_subxacts::subxacts, xl_xact_parsed_commit::subxacts, xl_xact_dbinfo::tsId, xl_xact_parsed_commit::tsId, xl_xact_parsed_commit::twophase_xid, xl_xact_commit::xact_time, xl_xact_parsed_commit::xact_time, XACT_XINFO_HAS_DBINFO, 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().

36 {
37  char *data = ((char *) xlrec) + MinSizeOfXactCommit;
38 
39  memset(parsed, 0, sizeof(*parsed));
40 
41  parsed->xinfo = 0; /* default, if no XLOG_XACT_HAS_INFO is
42  * present */
43 
44  parsed->xact_time = xlrec->xact_time;
45 
46  if (info & XLOG_XACT_HAS_INFO)
47  {
48  xl_xact_xinfo *xl_xinfo = (xl_xact_xinfo *) data;
49 
50  parsed->xinfo = xl_xinfo->xinfo;
51 
52  data += sizeof(xl_xact_xinfo);
53  }
54 
55  if (parsed->xinfo & XACT_XINFO_HAS_DBINFO)
56  {
57  xl_xact_dbinfo *xl_dbinfo = (xl_xact_dbinfo *) data;
58 
59  parsed->dbId = xl_dbinfo->dbId;
60  parsed->tsId = xl_dbinfo->tsId;
61 
62  data += sizeof(xl_xact_dbinfo);
63  }
64 
65  if (parsed->xinfo & XACT_XINFO_HAS_SUBXACTS)
66  {
67  xl_xact_subxacts *xl_subxacts = (xl_xact_subxacts *) data;
68 
69  parsed->nsubxacts = xl_subxacts->nsubxacts;
70  parsed->subxacts = xl_subxacts->subxacts;
71 
72  data += MinSizeOfXactSubxacts;
73  data += parsed->nsubxacts * sizeof(TransactionId);
74  }
75 
76  if (parsed->xinfo & XACT_XINFO_HAS_RELFILENODES)
77  {
78  xl_xact_relfilenodes *xl_relfilenodes = (xl_xact_relfilenodes *) data;
79 
80  parsed->nrels = xl_relfilenodes->nrels;
81  parsed->xnodes = xl_relfilenodes->xnodes;
82 
84  data += xl_relfilenodes->nrels * sizeof(RelFileNode);
85  }
86 
87  if (parsed->xinfo & XACT_XINFO_HAS_INVALS)
88  {
89  xl_xact_invals *xl_invals = (xl_xact_invals *) data;
90 
91  parsed->nmsgs = xl_invals->nmsgs;
92  parsed->msgs = xl_invals->msgs;
93 
94  data += MinSizeOfXactInvals;
95  data += xl_invals->nmsgs * sizeof(SharedInvalidationMessage);
96  }
97 
98  if (parsed->xinfo & XACT_XINFO_HAS_TWOPHASE)
99  {
100  xl_xact_twophase *xl_twophase = (xl_xact_twophase *) data;
101 
102  parsed->twophase_xid = xl_twophase->xid;
103 
104  data += sizeof(xl_xact_twophase);
105  }
106 
107  if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
108  {
109  xl_xact_origin xl_origin;
110 
111  /* we're only guaranteed 4 byte alignment, so copy onto stack */
112  memcpy(&xl_origin, data, sizeof(xl_origin));
113 
114  parsed->origin_lsn = xl_origin.origin_lsn;
115  parsed->origin_timestamp = xl_origin.origin_timestamp;
116 
117  data += sizeof(xl_xact_origin);
118  }
119 }
RelFileNode * xnodes
Definition: xact.h:299
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:157
struct xl_xact_xinfo xl_xact_xinfo
uint32 TransactionId
Definition: c.h:397
TimestampTz origin_timestamp
Definition: xact.h:307
TransactionId xid
Definition: xact.h:245
TransactionId twophase_xid
Definition: xact.h:304
TransactionId * subxacts
Definition: xact.h:296
#define XLOG_XACT_HAS_INFO
Definition: xact.h:146
SharedInvalidationMessage * msgs
Definition: xact.h:302
XLogRecPtr origin_lsn
Definition: xact.h:306
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:153
struct xl_xact_dbinfo xl_xact_dbinfo
#define MinSizeOfXactRelfilenodes
Definition: xact.h:234
Oid tsId
Definition: xact.h:219
struct xl_xact_origin xl_xact_origin
RelFileNode xnodes[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:232
#define MinSizeOfXactSubxacts
Definition: xact.h:227
#define MinSizeOfXactCommit
Definition: xact.h:266
SharedInvalidationMessage msgs[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:239
struct RelFileNode RelFileNode
TimestampTz xact_time
Definition: xact.h:256
struct xl_xact_twophase xl_xact_twophase
#define MinSizeOfXactInvals
Definition: xact.h:241
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:152
XLogRecPtr origin_lsn
Definition: xact.h:250
uint32 xinfo
Definition: xact.h:213
int nsubxacts
Definition: xact.h:224
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:156
Oid dbId
Definition: xact.h:218
TimestampTz origin_timestamp
Definition: xact.h:251
#define XACT_XINFO_HAS_INVALS
Definition: xact.h:155
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:154
int nmsgs
Definition: xact.h:238
TransactionId subxacts[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:225
TimestampTz xact_time
Definition: xact.h:288
bool PrepareTransactionBlock ( char *  gid)

Definition at line 3475 of file xact.c.

References Assert, TransactionStateData::blockState, CurrentTransactionState, EndTransactionBlock(), MemoryContextStrdup(), NULL, TransactionStateData::parent, prepareGID, result, TBLOCK_END, TBLOCK_PREPARE, TBLOCK_STARTED, and TopTransactionContext.

Referenced by standard_ProcessUtility().

3476 {
3477  TransactionState s;
3478  bool result;
3479 
3480  /* Set up to commit the current transaction */
3481  result = EndTransactionBlock();
3482 
3483  /* If successful, change outer tblock state to PREPARE */
3484  if (result)
3485  {
3487 
3488  while (s->parent != NULL)
3489  s = s->parent;
3490 
3491  if (s->blockState == TBLOCK_END)
3492  {
3493  /* Save GID where PrepareTransaction can find it again */
3495 
3497  }
3498  else
3499  {
3500  /*
3501  * ignore case where we are not in a transaction;
3502  * EndTransactionBlock already issued a warning.
3503  */
3505  /* Don't send back a PREPARE result tag... */
3506  result = false;
3507  }
3508  }
3509 
3510  return result;
3511 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
MemoryContext TopTransactionContext
Definition: mcxt.c:48
return result
Definition: formatting.c:1633
bool EndTransactionBlock(void)
Definition: xact.c:3526
TBlockState blockState
Definition: xact.c:177
struct TransactionStateData * parent
Definition: xact.c:191
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1064
static char * prepareGID
Definition: xact.c:257
void PreventTransactionChain ( bool  isTopLevel,
const char *  stmtType 
)

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

3156 {
3157  /*
3158  * xact block already started?
3159  */
3160  if (IsTransactionBlock())
3161  ereport(ERROR,
3162  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3163  /* translator: %s represents an SQL statement name */
3164  errmsg("%s cannot run inside a transaction block",
3165  stmtType)));
3166 
3167  /*
3168  * subtransaction?
3169  */
3170  if (IsSubTransaction())
3171  ereport(ERROR,
3172  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3173  /* translator: %s represents an SQL statement name */
3174  errmsg("%s cannot run inside a subtransaction",
3175  stmtType)));
3176 
3177  /*
3178  * inside a function call?
3179  */
3180  if (!isTopLevel)
3181  ereport(ERROR,
3182  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3183  /* translator: %s represents an SQL statement name */
3184  errmsg("%s cannot be executed from a function or multi-command string",
3185  stmtType)));
3186 
3187  /* If we got past IsTransactionBlock test, should be in default state */
3190  elog(FATAL, "cannot prevent transaction chain");
3191  /* all okay */
3192 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
bool IsTransactionBlock(void)
Definition: xact.c:4305
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
bool IsSubTransaction(void)
Definition: xact.c:4377
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void RegisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

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

3362 {
3363  SubXactCallbackItem *item;
3364 
3365  item = (SubXactCallbackItem *)
3367  item->callback = callback;
3368  item->arg = arg;
3369  item->next = SubXact_callbacks;
3370  SubXact_callbacks = item;
3371 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:293
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:288
SubXactCallback callback
Definition: xact.c:289
MemoryContext TopMemoryContext
Definition: mcxt.c:43
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
void * arg
void RegisterXactCallback ( XactCallback  callback,
void *  arg 
)

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

3307 {
3308  XactCallbackItem *item;
3309 
3310  item = (XactCallbackItem *)
3312  item->callback = callback;
3313  item->arg = arg;
3314  item->next = Xact_callbacks;
3315  Xact_callbacks = item;
3316 }
struct XactCallbackItem * next
Definition: xact.c:276
void * arg
Definition: xact.c:278
XactCallback callback
Definition: xact.c:277
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static XactCallbackItem * Xact_callbacks
Definition: xact.c:281
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:707
void * arg
void ReleaseCurrentSubTransaction ( void  )

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

4125 {
4127 
4128  /*
4129  * Workers synchronize transaction state at the beginning of each parallel
4130  * operation, so we can't account for commit of subtransactions after that
4131  * point. This should not happen anyway. Code calling this would
4132  * typically have called BeginInternalSubTransaction() first, failing
4133  * there.
4134  */
4135  if (IsInParallelMode())
4136  ereport(ERROR,
4137  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4138  errmsg("cannot commit subtransactions during a parallel operation")));
4139 
4140  if (s->blockState != TBLOCK_SUBINPROGRESS)
4141  elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
4143  Assert(s->state == TRANS_INPROGRESS);
4146  s = CurrentTransactionState; /* changed by pop */
4147  Assert(s->state == TRANS_INPROGRESS);
4148 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
TransState state
Definition: xact.c:176
bool IsInParallelMode(void)
Definition: xact.c:913
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4438
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void ReleaseSavepoint ( List options)

Definition at line 3812 of file xact.c.

References DefElem::arg, Assert, TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), lfirst, TransactionStateData::name, name, NULL, TransactionStateData::parent, PointerIsValid, TransactionStateData::savepointLevel, strVal, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, 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().

3813 {
3815  TransactionState target,
3816  xact;
3817  ListCell *cell;
3818  char *name = NULL;
3819 
3820  /*
3821  * Workers synchronize transaction state at the beginning of each parallel
3822  * operation, so we can't account for transaction state change after that
3823  * point. (Note that this check will certainly error out if s->blockState
3824  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3825  * below.)
3826  */
3827  if (IsInParallelMode())
3828  ereport(ERROR,
3829  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3830  errmsg("cannot release savepoints during a parallel operation")));
3831 
3832  switch (s->blockState)
3833  {
3834  /*
3835  * We can't rollback to a savepoint if there is no savepoint
3836  * defined.
3837  */
3838  case TBLOCK_INPROGRESS:
3839  ereport(ERROR,
3840  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3841  errmsg("no such savepoint")));
3842  break;
3843 
3844  /*
3845  * We are in a non-aborted subtransaction. This is the only valid
3846  * case.
3847  */
3848  case TBLOCK_SUBINPROGRESS:
3849  break;
3850 
3851  /* These cases are invalid. */
3852  case TBLOCK_DEFAULT:
3853  case TBLOCK_STARTED:
3854  case TBLOCK_BEGIN:
3856  case TBLOCK_SUBBEGIN:
3857  case TBLOCK_END:
3858  case TBLOCK_SUBRELEASE:
3859  case TBLOCK_SUBCOMMIT:
3860  case TBLOCK_ABORT:
3861  case TBLOCK_SUBABORT:
3862  case TBLOCK_ABORT_END:
3863  case TBLOCK_SUBABORT_END:
3864  case TBLOCK_ABORT_PENDING:
3866  case TBLOCK_SUBRESTART:
3868  case TBLOCK_PREPARE:
3869  elog(FATAL, "ReleaseSavepoint: unexpected state %s",
3871  break;
3872  }
3873 
3874  foreach(cell, options)
3875  {
3876  DefElem *elem = lfirst(cell);
3877 
3878  if (strcmp(elem->defname, "savepoint_name") == 0)
3879  name = strVal(elem->arg);
3880  }
3881 
3882  Assert(PointerIsValid(name));
3883 
3884  for (target = s; PointerIsValid(target); target = target->parent)
3885  {
3886  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3887  break;
3888  }
3889 
3890  if (!PointerIsValid(target))
3891  ereport(ERROR,
3892  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3893  errmsg("no such savepoint")));
3894 
3895  /* disallow crossing savepoint level boundaries */
3896  if (target->savepointLevel != s->savepointLevel)
3897  ereport(ERROR,
3898  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3899  errmsg("no such savepoint")));
3900 
3901  /*
3902  * Mark "commit pending" all subtransactions up to the target
3903  * subtransaction. The actual commits will happen when control gets to
3904  * CommitTransactionCommand.
3905  */
3906  xact = CurrentTransactionState;
3907  for (;;)
3908  {
3910  xact->blockState = TBLOCK_SUBRELEASE;
3911  if (xact == target)
3912  break;
3913  xact = xact->parent;
3914  Assert(PointerIsValid(xact));
3915  }
3916 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
bool IsInParallelMode(void)
Definition: xact.c:913
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:191
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:720
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
#define elog
Definition: elog.h:219
#define PointerIsValid(pointer)
Definition: c.h:526
void RequireTransactionChain ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3209 of file xact.c.

References CheckTransactionChain().

Referenced by PerformCursorOpen(), and standard_ProcessUtility().

3210 {
3211  CheckTransactionChain(isTopLevel, true, stmtType);
3212 }
static void CheckTransactionChain(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3232
void RollbackAndReleaseCurrentSubTransaction ( void  )

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

4159 {
4161 
4162  /*
4163  * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
4164  * during parallel operations. That's because we may be in the master,
4165  * recovering from an error thrown while we were in parallel mode. We
4166  * won't reach here in a worker, because BeginInternalSubTransaction()
4167  * will have failed.
4168  */
4169 
4170  switch (s->blockState)
4171  {
4172  /* Must be in a subtransaction */
4173  case TBLOCK_SUBINPROGRESS:
4174  case TBLOCK_SUBABORT:
4175  break;
4176 
4177  /* These cases are invalid. */
4178  case TBLOCK_DEFAULT:
4179  case TBLOCK_STARTED:
4180  case TBLOCK_BEGIN:
4182  case TBLOCK_SUBBEGIN:
4183  case TBLOCK_INPROGRESS:
4184  case TBLOCK_END:
4185  case TBLOCK_SUBRELEASE:
4186  case TBLOCK_SUBCOMMIT:
4187  case TBLOCK_ABORT:
4188  case TBLOCK_ABORT_END:
4189  case TBLOCK_SUBABORT_END:
4190  case TBLOCK_ABORT_PENDING:
4192  case TBLOCK_SUBRESTART:
4194  case TBLOCK_PREPARE:
4195  elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
4197  break;
4198  }
4199 
4200  /*
4201  * Abort the current subtransaction, if needed.
4202  */
4203  if (s->blockState == TBLOCK_SUBINPROGRESS)
4205 
4206  /* And clean it up, too */
4208 
4209  s = CurrentTransactionState; /* changed by pop */
4211  s->blockState == TBLOCK_INPROGRESS ||
4212  s->blockState == TBLOCK_STARTED);
4213 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
#define AssertState(condition)
Definition: c.h:678
TBlockState blockState
Definition: xact.c:177
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
static void AbortSubTransaction(void)
Definition: xact.c:4546
static void CleanupSubTransaction(void)
Definition: xact.c:4691
#define elog
Definition: elog.h:219
void RollbackToSavepoint ( List options)

Definition at line 3925 of file xact.c.

References DefElem::arg, Assert, TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), lfirst, TransactionStateData::name, name, NULL, TransactionStateData::parent, PointerIsValid, TransactionStateData::savepointLevel, strVal, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, 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().

3926 {
3928  TransactionState target,
3929  xact;
3930  ListCell *cell;
3931  char *name = NULL;
3932 
3933  /*
3934  * Workers synchronize transaction state at the beginning of each parallel
3935  * operation, so we can't account for transaction state change after that
3936  * point. (Note that this check will certainly error out if s->blockState
3937  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3938  * below.)
3939  */
3940  if (IsInParallelMode())
3941  ereport(ERROR,
3942  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3943  errmsg("cannot rollback to savepoints during a parallel operation")));
3944 
3945  switch (s->blockState)
3946  {
3947  /*
3948  * We can't rollback to a savepoint if there is no savepoint
3949  * defined.
3950  */
3951  case TBLOCK_INPROGRESS:
3952  case TBLOCK_ABORT:
3953  ereport(ERROR,
3954  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
3955  errmsg("no such savepoint")));
3956  break;
3957 
3958  /*
3959  * There is at least one savepoint, so proceed.
3960  */
3961  case TBLOCK_SUBINPROGRESS:
3962  case TBLOCK_SUBABORT:
3963  break;
3964 
3965  /* These cases are invalid. */
3966  case TBLOCK_DEFAULT:
3967  case TBLOCK_STARTED:
3968  case TBLOCK_BEGIN:
3970  case TBLOCK_SUBBEGIN:
3971  case TBLOCK_END:
3972  case TBLOCK_SUBRELEASE:
3973  case TBLOCK_SUBCOMMIT:
3974  case TBLOCK_ABORT_END:
3975  case TBLOCK_SUBABORT_END:
3976  case TBLOCK_ABORT_PENDING:
3978  case TBLOCK_SUBRESTART:
3980  case TBLOCK_PREPARE:
3981  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
3983  break;
3984  }
3985 
3986  foreach(cell, options)
3987  {
3988  DefElem *elem = lfirst(cell);
3989 
3990  if (strcmp(elem->defname, "savepoint_name") == 0)
3991  name = strVal(elem->arg);
3992  }
3993 
3994  Assert(PointerIsValid(name));
3995 
3996  for (target = s; PointerIsValid(target); target = target->parent)
3997  {
3998  if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
3999  break;
4000  }
4001 
4002  if (!PointerIsValid(target))
4003  ereport(ERROR,
4004  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4005  errmsg("no such savepoint")));
4006 
4007  /* disallow crossing savepoint level boundaries */
4008  if (target->savepointLevel != s->savepointLevel)
4009  ereport(ERROR,
4010  (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
4011  errmsg("no such savepoint")));
4012 
4013  /*
4014  * Mark "abort pending" all subtransactions up to the target
4015  * subtransaction. The actual aborts will happen when control gets to
4016  * CommitTransactionCommand.
4017  */
4018  xact = CurrentTransactionState;
4019  for (;;)
4020  {
4021  if (xact == target)
4022  break;
4023  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4025  else if (xact->blockState == TBLOCK_SUBABORT)
4027  else
4028  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4030  xact = xact->parent;
4031  Assert(PointerIsValid(xact));
4032  }
4033 
4034  /* And mark the target as "restart pending" */
4035  if (xact->blockState == TBLOCK_SUBINPROGRESS)
4036  xact->blockState = TBLOCK_SUBRESTART;
4037  else if (xact->blockState == TBLOCK_SUBABORT)
4039  else
4040  elog(FATAL, "RollbackToSavepoint: unexpected state %s",
4042 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
bool IsInParallelMode(void)
Definition: xact.c:913
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:191
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
Node * arg
Definition: parsenodes.h:720
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * defname
Definition: parsenodes.h:719
#define elog
Definition: elog.h:219
#define PointerIsValid(pointer)
Definition: c.h:526
void SerializeTransactionState ( Size  maxsize,
char *  start_address 
)

Definition at line 4850 of file xact.c.

References add_size(), Assert, TransactionStateData::childXids, currentCommandId, i, TransactionStateData::nChildXids, nParallelCurrentXids, NULL, palloc(), ParallelCurrentXids, TransactionStateData::parent, qsort, result, TransactionStateData::transactionId, TransactionIdIsValid, XactDeferrable, XactIsoLevel, XactTopTransactionId, and xidComparator().

Referenced by InitializeParallelDSM().

4851 {
4852  TransactionState s;
4853  Size nxids = 0;
4854  Size i = 0;
4855  Size c = 0;
4856  TransactionId *workspace;
4857  TransactionId *result = (TransactionId *) start_address;
4858 
4859  result[c++] = (TransactionId) XactIsoLevel;
4860  result[c++] = (TransactionId) XactDeferrable;
4861  result[c++] = XactTopTransactionId;
4862  result[c++] = CurrentTransactionState->transactionId;
4863  result[c++] = (TransactionId) currentCommandId;
4864  Assert(maxsize >= c * sizeof(TransactionId));
4865 
4866  /*
4867  * If we're running in a parallel worker and launching a parallel worker
4868  * of our own, we can just pass along the information that was passed to
4869  * us.
4870  */
4871  if (nParallelCurrentXids > 0)
4872  {
4873  result[c++] = nParallelCurrentXids;
4874  Assert(maxsize >= (nParallelCurrentXids + c) * sizeof(TransactionId));
4875  memcpy(&result[c], ParallelCurrentXids,
4877  return;
4878  }
4879 
4880  /*
4881  * OK, we need to generate a sorted list of XIDs that our workers should
4882  * view as current. First, figure out how many there are.
4883  */
4884  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4885  {
4887  nxids = add_size(nxids, 1);
4888  nxids = add_size(nxids, s->nChildXids);
4889  }
4890  Assert((c + 1 + nxids) * sizeof(TransactionId) <= maxsize);
4891 
4892  /* Copy them to our scratch space. */
4893  workspace = palloc(nxids * sizeof(TransactionId));
4894  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4895  {
4897  workspace[i++] = s->transactionId;
4898  memcpy(&workspace[i], s->childXids,
4899  s->nChildXids * sizeof(TransactionId));
4900  i += s->nChildXids;
4901  }
4902  Assert(i == nxids);
4903 
4904  /* Sort them. */
4905  qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
4906 
4907  /* Copy data into output area. */
4908  result[c++] = (TransactionId) nxids;
4909  memcpy(&result[c], workspace, nxids * sizeof(TransactionId));
4910 }
TransactionId XactTopTransactionId
Definition: xact.c:107
bool XactDeferrable
Definition: xact.c:80
static TransactionState CurrentTransactionState
Definition: xact.c:232
uint32 TransactionId
Definition: c.h:397
return result
Definition: formatting.c:1633
int nParallelCurrentXids
Definition: xact.c:108
TransactionId * childXids
Definition: xact.c:182
struct TransactionStateData * parent
Definition: xact.c:191
char * c
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
TransactionId transactionId
Definition: xact.c:172
TransactionId * ParallelCurrentXids
Definition: xact.c:109
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
size_t Size
Definition: c.h:356
int XactIsoLevel
Definition: xact.c:74
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define qsort(a, b, c, d)
Definition: port.h:440
static CommandId currentCommandId
Definition: xact.c:239
#define TransactionIdIsValid(xid)
Definition: transam.h:41
int xidComparator(const void *arg1, const void *arg2)
Definition: xid.c:138
void SetCurrentStatementStartTimestamp ( void  )

Definition at line 740 of file xact.c.

References GetCurrentTimestamp(), and stmtStartTimestamp.

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

741 {
743 }
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
static TimestampTz stmtStartTimestamp
Definition: xact.c:250
void StartParallelWorkerTransaction ( char *  tstatespace)

Definition at line 4918 of file xact.c.

References Assert, TransactionStateData::blockState, currentCommandId, nParallelCurrentXids, ParallelCurrentXids, StartTransaction(), TBLOCK_DEFAULT, TBLOCK_PARALLEL_INPROGRESS, TransactionStateData::transactionId, XactDeferrable, XactIsoLevel, and XactTopTransactionId.

Referenced by ParallelWorkerMain().

4919 {
4920  TransactionId *tstate = (TransactionId *) tstatespace;
4921 
4923  StartTransaction();
4924 
4925  XactIsoLevel = (int) tstate[0];
4926  XactDeferrable = (bool) tstate[1];
4927  XactTopTransactionId = tstate[2];
4929  currentCommandId = tstate[4];
4930  nParallelCurrentXids = (int) tstate[5];
4931  ParallelCurrentXids = &tstate[6];
4932 
4934 }
TransactionId XactTopTransactionId
Definition: xact.c:107
bool XactDeferrable
Definition: xact.c:80
static TransactionState CurrentTransactionState
Definition: xact.c:232
uint32 TransactionId
Definition: c.h:397
int nParallelCurrentXids
Definition: xact.c:108
char bool
Definition: c.h:202
TBlockState blockState
Definition: xact.c:177
TransactionId transactionId
Definition: xact.c:172
static void StartTransaction(void)
Definition: xact.c:1802
TransactionId * ParallelCurrentXids
Definition: xact.c:109
#define Assert(condition)
Definition: c.h:675
int XactIsoLevel
Definition: xact.c:74
static CommandId currentCommandId
Definition: xact.c:239
void StartTransactionCommand ( void  )

Definition at line 2678 of file xact.c.

References Assert, TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, CurTransactionContext, elog, ERROR, MemoryContextSwitchTo(), NULL, StartTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, 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 ApplyWorkerMain(), BeginInternalSubTransaction(), BootstrapModeMain(), cluster(), DefineIndex(), do_autovacuum(), ensure_transaction(), 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(), RemoveTempRelationsCallback(), ReorderBufferCommit(), SnapBuildExportSnapshot(), start_xact_command(), vacuum(), and vacuum_rel().

2679 {
2681 
2682  switch (s->blockState)
2683  {
2684  /*
2685  * if we aren't in a transaction block, we just do our usual start
2686  * transaction.
2687  */
2688  case TBLOCK_DEFAULT:
2689  StartTransaction();
2691  break;
2692 
2693  /*
2694  * We are somewhere in a transaction block or subtransaction and
2695  * about to start a new command. For now we do nothing, but
2696  * someday we may do command-local resource initialization. (Note
2697  * that any needed CommandCounterIncrement was done by the
2698  * previous CommitTransactionCommand.)
2699  */
2700  case TBLOCK_INPROGRESS:
2701  case TBLOCK_SUBINPROGRESS:
2702  break;
2703 
2704  /*
2705  * Here we are in a failed transaction block (one of the commands
2706  * caused an abort) so we do nothing but remain in the abort
2707  * state. Eventually we will get a ROLLBACK command which will
2708  * get us out of this state. (It is up to other code to ensure
2709  * that no commands other than ROLLBACK will be processed in these
2710  * states.)
2711  */
2712  case TBLOCK_ABORT:
2713  case TBLOCK_SUBABORT:
2714  break;
2715 
2716  /* These cases are invalid. */
2717  case TBLOCK_STARTED:
2718  case TBLOCK_BEGIN:
2720  case TBLOCK_SUBBEGIN:
2721  case TBLOCK_END:
2722  case TBLOCK_SUBRELEASE:
2723  case TBLOCK_SUBCOMMIT:
2724  case TBLOCK_ABORT_END:
2725  case TBLOCK_SUBABORT_END:
2726  case TBLOCK_ABORT_PENDING:
2728  case TBLOCK_SUBRESTART:
2730  case TBLOCK_PREPARE:
2731  elog(ERROR, "StartTransactionCommand: unexpected state %s",
2733  break;
2734  }
2735 
2736  /*
2737  * We must switch to CurTransactionContext before returning. This is
2738  * already done if we called StartTransaction, otherwise not.
2739  */
2742 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
TBlockState blockState
Definition: xact.c:177
#define ERROR
Definition: elog.h:43
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
static void StartTransaction(void)
Definition: xact.c:1802
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define elog
Definition: elog.h:219
bool SubTransactionIsActive ( SubTransactionId  subxid)

Definition at line 663 of file xact.c.

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

Referenced by fmgr_sql().

664 {
666 
667  for (s = CurrentTransactionState; s != NULL; s = s->parent)
668  {
669  if (s->state == TRANS_ABORT)
670  continue;
671  if (s->subTransactionId == subxid)
672  return true;
673  }
674  return false;
675 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TransState state
Definition: xact.c:176
struct TransactionStateData * parent
Definition: xact.c:191
SubTransactionId subTransactionId
Definition: xact.c:173
#define NULL
Definition: c.h:229
char TransactionBlockStatusCode ( void  )

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

4338 {
4340 
4341  switch (s->blockState)
4342  {
4343  case TBLOCK_DEFAULT:
4344  case TBLOCK_STARTED:
4345  return 'I'; /* idle --- not in transaction */
4346  case TBLOCK_BEGIN:
4347  case TBLOCK_SUBBEGIN:
4348  case TBLOCK_INPROGRESS:
4350  case TBLOCK_SUBINPROGRESS:
4351  case TBLOCK_END:
4352  case TBLOCK_SUBRELEASE:
4353  case TBLOCK_SUBCOMMIT:
4354  case TBLOCK_PREPARE:
4355  return 'T'; /* in transaction */
4356  case TBLOCK_ABORT:
4357  case TBLOCK_SUBABORT:
4358  case TBLOCK_ABORT_END:
4359  case TBLOCK_SUBABORT_END:
4360  case TBLOCK_ABORT_PENDING:
4362  case TBLOCK_SUBRESTART:
4364  return 'E'; /* in failed transaction */
4365  }
4366 
4367  /* should never get here */
4368  elog(FATAL, "invalid transaction block state: %s",
4370  return 0; /* keep compiler quiet */
4371 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define elog
Definition: elog.h:219
bool TransactionIdIsCurrentTransactionId ( TransactionId  xid)

Definition at line 773 of file xact.c.

References TransactionStateData::childXids, TransactionStateData::nChildXids, nParallelCurrentXids, NULL, ParallelCurrentXids, TransactionStateData::parent, TransactionStateData::state, TRANS_ABORT, TransactionStateData::transactionId, TransactionIdEquals, TransactionIdIsNormal, TransactionIdIsValid, and TransactionIdPrecedes().

Referenced by acquire_sample_rows(), compute_new_xmax_infomask(), copy_heap_data(), Do_MultiXactIdWait(), DoesMultiXactIdConflict(), EvalPlanQualFetch(), ExecCheckHeapTupleVisible(), ExecOnConflictUpdate(), FreezeMultiXactId(), funny_dup17(), heap_delete(), heap_lock_tuple(), heap_update(), HeapTupleHeaderAdjustCmax(), HeapTupleHeaderGetCmax(), HeapTupleHeaderGetCmin(), HeapTupleHeaderIsOnlyLocked(), HeapTupleSatisfiesDirty(), HeapTupleSatisfiesMVCC(), HeapTupleSatisfiesSelf(), HeapTupleSatisfiesToast(), HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVacuum(), IndexBuildHeapRangeScan(), MultiXactIdIsRunning(), RI_FKey_fk_upd_check_required(), SnapBuildWaitSnapshot(), test_lockmode_for_conflict(), TransactionIdIsInProgress(), and txid_status().

774 {
776 
777  /*
778  * We always say that BootstrapTransactionId is "not my transaction ID"
779  * even when it is (ie, during bootstrap). Along with the fact that
780  * transam.c always treats BootstrapTransactionId as already committed,
781  * this causes the tqual.c routines to see all tuples as committed, which
782  * is what we need during bootstrap. (Bootstrap mode only inserts tuples,
783  * it never updates or deletes them, so all tuples can be presumed good
784  * immediately.)
785  *
786  * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
787  * not my transaction ID, so we can just return "false" immediately for
788  * any non-normal XID.
789  */
790  if (!TransactionIdIsNormal(xid))
791  return false;
792 
793  /*
794  * In parallel workers, the XIDs we must consider as current are stored in
795  * ParallelCurrentXids rather than the transaction-state stack. Note that
796  * the XIDs in this array are sorted numerically rather than according to
797  * transactionIdPrecedes order.
798  */
799  if (nParallelCurrentXids > 0)
800  {
801  int low,
802  high;
803 
804  low = 0;
805  high = nParallelCurrentXids - 1;
806  while (low <= high)
807  {
808  int middle;
809  TransactionId probe;
810 
811  middle = low + (high - low) / 2;
812  probe = ParallelCurrentXids[middle];
813  if (probe == xid)
814  return true;
815  else if (probe < xid)
816  low = middle + 1;
817  else
818  high = middle - 1;
819  }
820  return false;
821  }
822 
823  /*
824  * We will return true for the Xid of the current subtransaction, any of
825  * its subcommitted children, any of its parents, or any of their
826  * previously subcommitted children. However, a transaction being aborted
827  * is no longer "current", even though it may still have an entry on the
828  * state stack.
829  */
830  for (s = CurrentTransactionState; s != NULL; s = s->parent)
831  {
832  int low,
833  high;
834 
835  if (s->state == TRANS_ABORT)
836  continue;
838  continue; /* it can't have any child XIDs either */
839  if (TransactionIdEquals(xid, s->transactionId))
840  return true;
841  /* As the childXids array is ordered, we can use binary search */
842  low = 0;
843  high = s->nChildXids - 1;
844  while (low <= high)
845  {
846  int middle;
847  TransactionId probe;
848 
849  middle = low + (high - low) / 2;
850  probe = s->childXids[middle];
851  if (TransactionIdEquals(probe, xid))
852  return true;
853  else if (TransactionIdPrecedes(probe, xid))
854  low = middle + 1;
855  else
856  high = middle - 1;
857  }
858  }
859 
860  return false;
861 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
uint32 TransactionId
Definition: c.h:397
int nParallelCurrentXids
Definition: xact.c:108
TransactionId * childXids
Definition: xact.c:182
TransState state
Definition: xact.c:176
struct TransactionStateData * parent
Definition: xact.c:191
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
TransactionId transactionId
Definition: xact.c:172
TransactionId * ParallelCurrentXids
Definition: xact.c:109
#define NULL
Definition: c.h:229
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
void UnregisterSubXactCallback ( SubXactCallback  callback,
void *  arg 
)

Definition at line 3374 of file xact.c.

References SubXactCallbackItem::next, NULL, and pfree().

3375 {
3376  SubXactCallbackItem *item;
3377  SubXactCallbackItem *prev;
3378 
3379  prev = NULL;
3380  for (item = SubXact_callbacks; item; prev = item, item = item->next)
3381  {
3382  if (item->callback == callback && item->arg == arg)
3383  {
3384  if (prev)
3385  prev->next = item->next;
3386  else
3387  SubXact_callbacks = item->next;
3388  pfree(item);
3389  break;
3390  }
3391  }
3392 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:293
void pfree(void *pointer)
Definition: mcxt.c:950
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
struct SubXactCallbackItem * next
Definition: xact.c:288
#define NULL
Definition: c.h:229
void * arg
void UnregisterXactCallback ( XactCallback  callback,
void *  arg 
)

Definition at line 3319 of file xact.c.

References XactCallbackItem::next, NULL, and pfree().

3320 {
3321  XactCallbackItem *item;
3322  XactCallbackItem *prev;
3323 
3324  prev = NULL;
3325  for (item = Xact_callbacks; item; prev = item, item = item->next)
3326  {
3327  if (item->callback == callback && item->arg == arg)
3328  {
3329  if (prev)
3330  prev->next = item->next;
3331  else
3332  Xact_callbacks = item->next;
3333  pfree(item);
3334  break;
3335  }
3336  }
3337 }
struct XactCallbackItem * next
Definition: xact.c:276
void pfree(void *pointer)
Definition: mcxt.c:950
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
#define NULL
Definition: c.h:229
static XactCallbackItem * Xact_callbacks
Definition: xact.c:281
void * arg
void UserAbortTransactionBlock ( void  )

Definition at line 3650 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), FATAL, NULL, TransactionStateData::parent, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, 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().

3651 {
3653 
3654  switch (s->blockState)
3655  {
3656  /*
3657  * We are inside a transaction block and we got a ROLLBACK command
3658  * from the user, so tell CommitTransactionCommand to abort and
3659  * exit the transaction block.
3660  */
3661  case TBLOCK_INPROGRESS:
3663  break;
3664 
3665  /*
3666  * We are inside a failed transaction block and we got a ROLLBACK
3667  * command from the user. Abort processing is already done, so
3668  * CommitTransactionCommand just has to cleanup and go back to
3669  * idle state.
3670  */
3671  case TBLOCK_ABORT:
3673  break;
3674 
3675  /*
3676  * We are inside a subtransaction. Mark everything up to top
3677  * level as exitable.
3678  */
3679  case TBLOCK_SUBINPROGRESS:
3680  case TBLOCK_SUBABORT:
3681  while (s->parent != NULL)
3682  {
3683  if (s->blockState == TBLOCK_SUBINPROGRESS)
3685  else if (s->blockState == TBLOCK_SUBABORT)
3687  else
3688  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3690  s = s->parent;
3691  }
3692  if (s->blockState == TBLOCK_INPROGRESS)
3694  else if (s->blockState == TBLOCK_ABORT)
3696  else
3697  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3699  break;
3700 
3701  /*
3702  * The user issued ABORT when not inside a transaction. Issue a
3703  * WARNING and go to abort state. The upcoming call to
3704  * CommitTransactionCommand() will then put us back into the
3705  * default state.
3706  */
3707  case TBLOCK_STARTED:
3708  ereport(WARNING,
3709  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3710  errmsg("there is no transaction in progress")));
3712  break;
3713 
3714  /*
3715  * The user issued an ABORT that somehow ran inside a parallel
3716  * worker. We can't cope with that.
3717  */
3719  ereport(FATAL,
3720  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3721  errmsg("cannot abort during a parallel operation")));
3722  break;
3723 
3724  /* These cases are invalid. */
3725  case TBLOCK_DEFAULT:
3726  case TBLOCK_BEGIN:
3727  case TBLOCK_SUBBEGIN:
3728  case TBLOCK_END:
3729  case TBLOCK_SUBRELEASE:
3730  case TBLOCK_SUBCOMMIT:
3731  case TBLOCK_ABORT_END:
3732  case TBLOCK_SUBABORT_END:
3733  case TBLOCK_ABORT_PENDING:
3735  case TBLOCK_SUBRESTART:
3737  case TBLOCK_PREPARE:
3738  elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
3740  break;
3741  }
3742 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:177
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:191
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5004
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void WarnNoTransactionChain ( bool  isTopLevel,
const char *  stmtType 
)

Definition at line 3203 of file xact.c.

References CheckTransactionChain().

Referenced by ExecSetVariableStmt(), and standard_ProcessUtility().

3204 {
3205  CheckTransactionChain(isTopLevel, false, stmtType);
3206 }
static void CheckTransactionChain(bool isTopLevel, bool throwError, const char *stmtType)
Definition: xact.c:3232
void xact_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 270 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.

271 {
272  char *rec = XLogRecGetData(record);
273  uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
274 
275  if (info == XLOG_XACT_COMMIT || info == XLOG_XACT_COMMIT_PREPARED)
276  {
277  xl_xact_commit *xlrec = (xl_xact_commit *) rec;
278 
279  xact_desc_commit(buf, XLogRecGetInfo(record), xlrec,
280  XLogRecGetOrigin(record));
281  }
282  else if (info == XLOG_XACT_ABORT || info == XLOG_XACT_ABORT_PREPARED)
283  {
284  xl_xact_abort *xlrec = (xl_xact_abort *) rec;
285 
286  xact_desc_abort(buf, XLogRecGetInfo(record), xlrec);
287  }
288  else if (info == XLOG_XACT_ASSIGNMENT)
289  {
290  xl_xact_assignment *xlrec = (xl_xact_assignment *) rec;
291 
292  /*
293  * Note that we ignore the WAL record's xid, since we're more
294  * interested in the top-level xid that issued the record and which
295  * xids are being reported here.
296  */
297  appendStringInfo(buf, "xtop %u: ", xlrec->xtop);
298  xact_desc_assignment(buf, xlrec);
299  }
300 }
#define XLOG_XACT_COMMIT
Definition: xact.h:133
unsigned char uint8
Definition: c.h:266
TransactionId xtop
Definition: xact.h:182
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:219
#define XLogRecGetData(decoder)
Definition: xlogreader.h:220
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define XLOG_XACT_ABORT_PREPARED
Definition: xact.h:137
static void xact_desc_abort(StringInfo buf, uint8 info, xl_xact_abort *xlrec)
Definition: xactdesc.c:226
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:216
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:138
#define XLOG_XACT_ABORT
Definition: xact.h:135
#define XLOG_XACT_OPMASK
Definition: xact.h:143
static void xact_desc_assignment(StringInfo buf, xl_xact_assignment *xlrec)
Definition: xactdesc.c:259
#define XLOG_XACT_COMMIT_PREPARED
Definition: xact.h:136
static void xact_desc_commit(StringInfo buf, uint8 info, xl_xact_commit *xlrec, RepOriginId origin_id)
Definition: xactdesc.c:175
const char* xact_identify ( uint8  info)

Definition at line 303 of file xactdesc.c.

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

304 {
305  const char *id = NULL;
306 
307  switch (info & XLOG_XACT_OPMASK)
308  {
309  case XLOG_XACT_COMMIT:
310  id = "COMMIT";
311  break;
312  case XLOG_XACT_PREPARE:
313  id = "PREPARE";
314  break;
315  case XLOG_XACT_ABORT:
316  id = "ABORT";
317  break;
319  id = "COMMIT_PREPARED";
320  break;
322  id = "ABORT_PREPARED";
323  break;
325  id = "ASSIGNMENT";
326  break;
327  }
328 
329  return id;
330 }
#define XLOG_XACT_COMMIT
Definition: xact.h:133
#define XLOG_XACT_PREPARE
Definition: xact.h:134
#define XLOG_XACT_ABORT_PREPARED
Definition: xact.h:137
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:138
#define NULL
Definition: c.h:229
#define XLOG_XACT_ABORT
Definition: xact.h:135
#define XLOG_XACT_OPMASK
Definition: xact.h:143
#define XLOG_XACT_COMMIT_PREPARED
Definition: xact.h:136
void xact_redo ( XLogReaderState record)

Definition at line 5597 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.

5598 {
5599  uint8 info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
5600 
5601  /* Backup blocks are not used in xact records */
5602  Assert(!XLogRecHasAnyBlockRefs(record));
5603 
5604  if (info == XLOG_XACT_COMMIT)
5605  {
5606  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5607  xl_xact_parsed_commit parsed;
5608 
5609  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5610  xact_redo_commit(&parsed, XLogRecGetXid(record),
5611  record->EndRecPtr, XLogRecGetOrigin(record));
5612  }
5613  else if (info == XLOG_XACT_COMMIT_PREPARED)
5614  {
5615  xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
5616  xl_xact_parsed_commit parsed;
5617 
5618  ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
5619  xact_redo_commit(&parsed, parsed.twophase_xid,
5620  record->EndRecPtr, XLogRecGetOrigin(record));
5621 
5622  /* Delete TwoPhaseState gxact entry and/or 2PC file. */
5623  LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
5624  PrepareRedoRemove(parsed.twophase_xid, false);
5625  LWLockRelease(TwoPhaseStateLock);
5626  }
5627  else if (info == XLOG_XACT_ABORT)
5628  {
5629  xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
5630  xl_xact_parsed_abort parsed;
5631 
5632  ParseAbortRecord(XLogRecGetInfo(record), xlrec, &parsed);
5633  xact_redo_abort(&parsed, XLogRecGetXid(record));
5634  }
5635  else if (info == XLOG_XACT_ABORT_PREPARED)
5636  {
5637  xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
5638  xl_xact_parsed_abort parsed;
5639 
5640  ParseAbortRecord(XLogRecGetInfo(record), xlrec, &parsed);
5641  xact_redo_abort(&parsed, parsed.twophase_xid);
5642 
5643  /* Delete TwoPhaseState gxact entry and/or 2PC file. */
5644  LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
5645  PrepareRedoRemove(parsed.twophase_xid, false);
5646  LWLockRelease(TwoPhaseStateLock);
5647  }
5648  else if (info == XLOG_XACT_PREPARE)
5649  {
5650  /*
5651  * Store xid and start/end pointers of the WAL record in TwoPhaseState
5652  * gxact entry.
5653  */
5654  LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
5656  record->ReadRecPtr,
5657  record->EndRecPtr);
5658  LWLockRelease(TwoPhaseStateLock);
5659  }
5660  else if (info == XLOG_XACT_ASSIGNMENT)
5661  {
5663 
5666  xlrec->nsubxacts, xlrec->xsub);
5667  }
5668  else
5669  elog(PANIC, "xact_redo: unknown op code %u", info);
5670 }
#define XLOG_XACT_COMMIT
Definition: xact.h:133
void PrepareRedoRemove(TransactionId xid, bool giveWarning)
Definition: twophase.c:2374
#define XLOG_XACT_PREPARE
Definition: xact.h:134
static void xact_redo_commit(xl_xact_parsed_commit *parsed, TransactionId xid, XLogRecPtr lsn, RepOriginId origin_id)
Definition: xact.c:5344
TransactionId twophase_xid
Definition: xact.h:304
unsigned char uint8
Definition: c.h:266
TransactionId xtop
Definition: xact.h:182
TransactionId xsub[FLEXIBLE_ARRAY_MEMBER]
Definition: xact.h:184
#define PANIC
Definition: elog.h:53
#define XLogRecGetOrigin(decoder)
Definition: xlogreader.h:219
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define XLogRecGetData(decoder)
Definition: xlogreader.h:220
void ParseCommitRecord(uint8 info, xl_xact_commit *xlrec, xl_xact_parsed_commit *parsed)
Definition: xactdesc.c:35
#define XLOG_XACT_ABORT_PREPARED
Definition: xact.h:137
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:114
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:216
void PrepareRedoAdd(char *buf, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
Definition: twophase.c:2312
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:218
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:138
static void xact_redo_abort(xl_xact_parsed_abort *parsed, TransactionId xid)
Definition: xact.c:5517
TransactionId twophase_xid
Definition: xact.h:321
#define Assert(condition)
Definition: c.h:675
#define XLOG_XACT_ABORT
Definition: xact.h:135
#define XLOG_XACT_OPMASK
Definition: xact.h:143
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void ParseAbortRecord(uint8 info, xl_xact_abort *xlrec, xl_xact_parsed_abort *parsed)
Definition: xactdesc.c:122
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:222
void ProcArrayApplyXidAssignment(TransactionId topxid, int nsubxids, TransactionId *subxids)
Definition: procarray.c:914
#define elog
Definition: elog.h:219
#define XLOG_XACT_COMMIT_PREPARED
Definition: xact.h:136
HotStandbyState standbyState
Definition: xlog.c:195
int xactGetCommittedChildren ( TransactionId **  ptr)

Definition at line 5085 of file xact.c.

References TransactionStateData::childXids, CurrentTransactionState, TransactionStateData::nChildXids, and NULL.

Referenced by ExportSnapshot(), RecordTransactionAbort(), RecordTransactionCommit(), and StartPrepare().

5086 {
5088 
5089  if (s->nChildXids == 0)
5090  *ptr = NULL;
5091  else
5092  *ptr = s->childXids;
5093 
5094  return s->nChildXids;
5095 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TransactionId * childXids
Definition: xact.c:182
#define NULL
Definition: c.h:229
XLogRecPtr XactLogAbortRecord ( TimestampTz  abort_time,
int  nsubxacts,
TransactionId subxacts,
int  nrels,
RelFileNode rels,
int  xactflags,
TransactionId  twophase_xid 
)

Definition at line 5256 of file xact.c.

References Assert, CritSectionCount, MinSizeOfXactAbort, MinSizeOfXactRelfilenodes, MinSizeOfXactSubxacts, xl_xact_relfilenodes::nrels, xl_xact_subxacts::nsubxacts, TransactionIdIsValid, XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK, xl_xact_abort::xact_time, XACT_XINFO_HAS_AE_LOCKS, XACT_XINFO_HAS_RELFILENODES, XACT_XINFO_HAS_SUBXACTS, XACT_XINFO_HAS_TWOPHASE, xl_xact_twophase::xid, xl_xact_xinfo::xinfo, XLOG_XACT_ABORT, XLOG_XACT_ABORT_PREPARED, XLOG_XACT_HAS_INFO, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by RecordTransactionAbort(), and RecordTransactionAbortPrepared().

5260 {
5261  xl_xact_abort xlrec;
5262  xl_xact_xinfo xl_xinfo;
5263  xl_xact_subxacts xl_subxacts;
5264  xl_xact_relfilenodes xl_relfilenodes;
5265  xl_xact_twophase xl_twophase;
5266 
5267  uint8 info;
5268 
5269  Assert(CritSectionCount > 0);
5270 
5271  xl_xinfo.xinfo = 0;
5272 
5273  /* decide between a plain and 2pc abort */
5274  if (!TransactionIdIsValid(twophase_xid))
5275  info = XLOG_XACT_ABORT;
5276  else
5277  info = XLOG_XACT_ABORT_PREPARED;
5278 
5279 
5280  /* First figure out and collect all the information needed */
5281 
5282  xlrec.xact_time = abort_time;
5283 
5284  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5285  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5286 
5287  if (nsubxacts > 0)
5288  {
5289  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5290  xl_subxacts.nsubxacts = nsubxacts;
5291  }
5292 
5293  if (nrels > 0)
5294  {
5295  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5296  xl_relfilenodes.nrels = nrels;
5297  }
5298 
5299  if (TransactionIdIsValid(twophase_xid))
5300  {
5301  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5302  xl_twophase.xid = twophase_xid;
5303  }
5304 
5305  if (xl_xinfo.xinfo != 0)
5306  info |= XLOG_XACT_HAS_INFO;
5307 
5308  /* Then include all the collected data into the abort record. */
5309 
5310  XLogBeginInsert();
5311 
5312  XLogRegisterData((char *) (&xlrec), MinSizeOfXactAbort);
5313 
5314  if (xl_xinfo.xinfo != 0)
5315  XLogRegisterData((char *) (&xl_xinfo), sizeof(xl_xinfo));
5316 
5317  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5318  {
5319  XLogRegisterData((char *) (&xl_subxacts),
5321  XLogRegisterData((char *) subxacts,
5322  nsubxacts * sizeof(TransactionId));
5323  }
5324 
5325  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5326  {
5327  XLogRegisterData((char *) (&xl_relfilenodes),
5329  XLogRegisterData((char *) rels,
5330  nrels * sizeof(RelFileNode));
5331  }
5332 
5333  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5334  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5335 
5336  return XLogInsert(RM_XACT_ID, info);
5337 }
uint32 TransactionId
Definition: c.h:397
TransactionId xid
Definition: xact.h:245
unsigned char uint8
Definition: c.h:266
#define MinSizeOfXactAbort
Definition: xact.h:279
#define XLOG_XACT_HAS_INFO
Definition: xact.h:146
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:153
#define MinSizeOfXactRelfilenodes
Definition: xact.h:234
TimestampTz xact_time
Definition: xact.h:270
#define XACT_XINFO_HAS_AE_LOCKS
Definition: xact.h:158
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK
Definition: xact.h:92
#define MinSizeOfXactSubxacts
Definition: xact.h:227
#define XLOG_XACT_ABORT_PREPARED
Definition: xact.h:137
volatile uint32 CritSectionCount
Definition: globals.c:37
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
uint32 xinfo
Definition: xact.h:213
int nsubxacts
Definition: xact.h:224
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:156
#define Assert(condition)
Definition: c.h:675
#define XLOG_XACT_ABORT
Definition: xact.h:135
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:154
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void XLogBeginInsert(void)
Definition: xloginsert.c:120
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 
)

Definition at line 5109 of file xact.c.

References Assert, CritSectionCount, xl_xact_dbinfo::dbId, forceSyncCommit, InvalidRepOriginId, MinSizeOfXactInvals, MinSizeOfXactRelfilenodes, MinSizeOfXactSubxacts, MyDatabaseId, MyDatabaseTableSpace, xl_xact_invals::nmsgs, xl_xact_relfilenodes::nrels, xl_xact_subxacts::nsubxacts, xl_xact_origin::origin_lsn, xl_xact_origin::origin_timestamp, replorigin_session_origin, replorigin_session_origin_lsn, replorigin_session_origin_timestamp, synchronous_commit, SYNCHRONOUS_COMMIT_REMOTE_APPLY, TransactionIdIsValid, xl_xact_dbinfo::tsId, XACT_COMPLETION_APPLY_FEEDBACK, XACT_COMPLETION_FORCE_SYNC_COMMIT, XACT_COMPLETION_UPDATE_RELCACHE_FILE, XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK, xl_xact_commit::xact_time, XACT_XINFO_HAS_AE_LOCKS, XACT_XINFO_HAS_DBINFO, 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, XLOG_INCLUDE_ORIGIN, XLOG_XACT_COMMIT, XLOG_XACT_COMMIT_PREPARED, XLOG_XACT_HAS_INFO, XLogBeginInsert(), XLogInsert(), XLogLogicalInfoActive, XLogRegisterData(), and XLogSetRecordFlags().

Referenced by RecordTransactionCommit(), and RecordTransactionCommitPrepared().

5115 {
5116  xl_xact_commit xlrec;
5117  xl_xact_xinfo xl_xinfo;
5118  xl_xact_dbinfo xl_dbinfo;
5119  xl_xact_subxacts xl_subxacts;
5120  xl_xact_relfilenodes xl_relfilenodes;
5121  xl_xact_invals xl_invals;
5122  xl_xact_twophase xl_twophase;
5123  xl_xact_origin xl_origin;
5124 
5125  uint8 info;
5126 
5127  Assert(CritSectionCount > 0);
5128 
5129  xl_xinfo.xinfo = 0;
5130 
5131  /* decide between a plain and 2pc commit */
5132  if (!TransactionIdIsValid(twophase_xid))
5133  info = XLOG_XACT_COMMIT;
5134  else
5136 
5137  /* First figure out and collect all the information needed */
5138 
5139  xlrec.xact_time = commit_time;
5140 
5141  if (relcacheInval)
5143  if (forceSyncCommit)
5145  if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
5146  xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
5147 
5148  /*
5149  * Check if the caller would like to ask standbys for immediate feedback
5150  * once this commit is applied.
5151  */
5154 
5155  /*
5156  * Relcache invalidations requires information about the current database
5157  * and so does logical decoding.
5158  */
5159  if (nmsgs > 0 || XLogLogicalInfoActive())
5160  {
5161  xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
5162  xl_dbinfo.dbId = MyDatabaseId;
5163  xl_dbinfo.tsId = MyDatabaseTableSpace;
5164  }
5165 
5166  if (nsubxacts > 0)
5167  {
5168  xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
5169  xl_subxacts.nsubxacts = nsubxacts;
5170  }
5171 
5172  if (nrels > 0)
5173  {
5174  xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
5175  xl_relfilenodes.nrels = nrels;
5176  }
5177 
5178  if (nmsgs > 0)
5179  {
5180  xl_xinfo.xinfo |= XACT_XINFO_HAS_INVALS;
5181  xl_invals.nmsgs = nmsgs;
5182  }
5183 
5184  if (TransactionIdIsValid(twophase_xid))
5185  {
5186  xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
5187  xl_twophase.xid = twophase_xid;
5188  }
5189 
5190  /* dump transaction origin information */
5192  {
5193  xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
5194 
5197  }
5198 
5199  if (xl_xinfo.xinfo != 0)
5200  info |= XLOG_XACT_HAS_INFO;
5201 
5202  /* Then include all the collected data into the commit record. */
5203 
5204  XLogBeginInsert();
5205 
5206  XLogRegisterData((char *) (&xlrec), sizeof(xl_xact_commit));
5207 
5208  if (xl_xinfo.xinfo != 0)
5209  XLogRegisterData((char *) (&xl_xinfo.xinfo), sizeof(xl_xinfo.xinfo));
5210 
5211  if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
5212  XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
5213 
5214  if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
5215  {
5216  XLogRegisterData((char *) (&xl_subxacts),
5218  XLogRegisterData((char *) subxacts,
5219  nsubxacts * sizeof(TransactionId));
5220  }
5221 
5222  if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
5223  {
5224  XLogRegisterData((char *) (&xl_relfilenodes),
5226  XLogRegisterData((char *) rels,
5227  nrels * sizeof(RelFileNode));
5228  }
5229 
5230  if (xl_xinfo.xinfo & XACT_XINFO_HAS_INVALS)
5231  {
5232  XLogRegisterData((char *) (&xl_invals), MinSizeOfXactInvals);
5233  XLogRegisterData((char *) msgs,
5234  nmsgs * sizeof(SharedInvalidationMessage));
5235  }
5236 
5237  if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
5238  XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
5239 
5240  if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
5241  XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
5242 
5243  /* we allow filtering by xacts */
5245 
5246  return XLogInsert(RM_XACT_ID, info);
5247 }
#define XACT_COMPLETION_APPLY_FEEDBACK
Definition: xact.h:168
#define XACT_COMPLETION_UPDATE_RELCACHE_FILE
Definition: xact.h:169
#define XLOG_XACT_COMMIT
Definition: xact.h:133
#define XACT_XINFO_HAS_ORIGIN
Definition: xact.h:157
uint32 TransactionId
Definition: c.h:397
TransactionId xid
Definition: xact.h:245
unsigned char uint8
Definition: c.h:266
#define XLOG_XACT_HAS_INFO
Definition: xact.h:146
#define XLOG_INCLUDE_ORIGIN
Definition: xlog.h:191
#define XACT_XINFO_HAS_SUBXACTS
Definition: xact.h:153
#define MinSizeOfXactRelfilenodes
Definition: xact.h:234
Oid tsId
Definition: xact.h:219
Oid MyDatabaseTableSpace
Definition: globals.c:79
static bool forceSyncCommit
Definition: xact.c:262
#define XACT_XINFO_HAS_AE_LOCKS
Definition: xact.h:158
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK
Definition: xact.h:92
#define MinSizeOfXactSubxacts
Definition: xact.h:227
#define XLogLogicalInfoActive()
Definition: xlog.h:162
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:151
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:152
TimestampTz xact_time
Definition: xact.h:256
#define MinSizeOfXactInvals
Definition: xact.h:241
void XLogSetRecordFlags(uint8 flags)
Definition: xloginsert.c:397
volatile uint32 CritSectionCount
Definition: globals.c:37
#define XACT_XINFO_HAS_DBINFO
Definition: xact.h:152
int synchronous_commit
Definition: xact.c:82
XLogRecPtr origin_lsn
Definition: xact.h:250
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
uint32 xinfo
Definition: xact.h:213
Oid MyDatabaseId
Definition: globals.c:77
int nsubxacts
Definition: xact.h:224
#define XACT_XINFO_HAS_TWOPHASE
Definition: xact.h:156
Oid dbId
Definition: xact.h:218
#define XACT_COMPLETION_FORCE_SYNC_COMMIT
Definition: xact.h:170
#define Assert(condition)
Definition: c.h:675
RepOriginId replorigin_session_origin
Definition: origin.c:150
#define InvalidRepOriginId
Definition: origin.h:34
TimestampTz origin_timestamp
Definition: xact.h:251
#define XACT_XINFO_HAS_INVALS
Definition: xact.h:155
#define XACT_XINFO_HAS_RELFILENODES
Definition: xact.h:154
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define XLOG_XACT_COMMIT_PREPARED
Definition: xact.h:136
void XLogBeginInsert(void)
Definition: xloginsert.c:120
int nmsgs
Definition: xact.h:238

Variable Documentation

bool DefaultXactDeferrable

Definition at line 79 of file xact.c.

Referenced by StartTransaction().

int DefaultXactIsoLevel

Definition at line 73 of file xact.c.

Referenced by check_XactIsoLevel(), and StartTransaction().

bool DefaultXactReadOnly

Definition at line 76 of file xact.c.

Referenced by StartTransaction().

int synchronous_commit