PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xact.c File Reference
#include "postgres.h"
#include <time.h>
#include <unistd.h>
#include "access/commit_ts.h"
#include "access/multixact.h"
#include "access/parallel.h"
#include "access/subtrans.h"
#include "access/transam.h"
#include "access/twophase.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/storage.h"
#include "commands/async.h"
#include "commands/tablecmds.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "libpq/be-fsstubs.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/logical.h"
#include "replication/logicallauncher.h"
#include "replication/origin.h"
#include "replication/syncrep.h"
#include "replication/walsender.h"
#include "storage/condition_variable.h"
#include "storage/fd.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/sinvaladt.h"
#include "storage/smgr.h"
#include "utils/builtins.h"
#include "utils/catcache.h"
#include "utils/combocid.h"
#include "utils/guc.h"
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/relmapper.h"
#include "utils/snapmgr.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"
#include "pg_trace.h"
Include dependency graph for xact.c:

Go to the source code of this file.

Data Structures

struct  TransactionStateData
 
struct  XactCallbackItem
 
struct  SubXactCallbackItem
 

Typedefs

typedef enum TransState TransState
 
typedef enum TBlockState TBlockState
 
typedef struct TransactionStateData TransactionStateData
 
typedef TransactionStateDataTransactionState
 
typedef struct XactCallbackItem XactCallbackItem
 
typedef struct SubXactCallbackItem SubXactCallbackItem
 

Enumerations

enum  TransState {
  TRANS_DEFAULT, TRANS_START, TRANS_INPROGRESS, TRANS_COMMIT,
  TRANS_ABORT, TRANS_PREPARE
}
 
enum  TBlockState {
  TBLOCK_DEFAULT, TBLOCK_STARTED, TBLOCK_BEGIN, TBLOCK_INPROGRESS,
  TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_END, TBLOCK_ABORT,
  TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_PREPARE, TBLOCK_SUBBEGIN,
  TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBCOMMIT, TBLOCK_SUBABORT,
  TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBRESTART, TBLOCK_SUBABORT_RESTART
}
 

Functions

static void AssignTransactionId (TransactionState s)
 
static void AbortTransaction (void)
 
static void AtAbort_Memory (void)
 
static void AtCleanup_Memory (void)
 
static void AtAbort_ResourceOwner (void)
 
static void AtCCI_LocalCache (void)
 
static void AtCommit_Memory (void)
 
static void AtStart_Cache (void)
 
static void AtStart_Memory (void)
 
static void AtStart_ResourceOwner (void)
 
static void CallXactCallbacks (XactEvent event)
 
static void CallSubXactCallbacks (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
 
static void CleanupTransaction (void)
 
static void CheckTransactionChain (bool isTopLevel, bool throwError, const char *stmtType)
 
static void CommitTransaction (void)
 
static TransactionId RecordTransactionAbort (bool isSubXact)
 
static void StartTransaction (void)
 
static void StartSubTransaction (void)
 
static void CommitSubTransaction (void)
 
static void AbortSubTransaction (void)
 
static void CleanupSubTransaction (void)
 
static void PushTransaction (void)
 
static void PopTransaction (void)
 
static void AtSubAbort_Memory (void)
 
static void AtSubCleanup_Memory (void)
 
static void AtSubAbort_ResourceOwner (void)
 
static void AtSubCommit_Memory (void)
 
static void AtSubStart_Memory (void)
 
static void AtSubStart_ResourceOwner (void)
 
static void ShowTransactionState (const char *str)
 
static void ShowTransactionStateRec (const char *str, TransactionState state)
 
static const char * BlockStateAsString (TBlockState blockState)
 
static const char * TransStateAsString (TransState state)
 
bool IsTransactionState (void)
 
bool IsAbortedTransactionBlockState (void)
 
TransactionId GetTopTransactionId (void)
 
TransactionId GetTopTransactionIdIfAny (void)
 
TransactionId GetCurrentTransactionId (void)
 
TransactionId GetCurrentTransactionIdIfAny (void)
 
void MarkCurrentTransactionIdLoggedIfAny (void)
 
TransactionId GetStableLatestTransactionId (void)
 
SubTransactionId GetCurrentSubTransactionId (void)
 
bool SubTransactionIsActive (SubTransactionId subxid)
 
CommandId GetCurrentCommandId (bool used)
 
TimestampTz GetCurrentTransactionStartTimestamp (void)
 
TimestampTz GetCurrentStatementStartTimestamp (void)
 
TimestampTz GetCurrentTransactionStopTimestamp (void)
 
void SetCurrentStatementStartTimestamp (void)
 
static void SetCurrentTransactionStopTimestamp (void)
 
int GetCurrentTransactionNestLevel (void)
 
bool TransactionIdIsCurrentTransactionId (TransactionId xid)
 
bool TransactionStartedDuringRecovery (void)
 
void EnterParallelMode (void)
 
void ExitParallelMode (void)
 
bool IsInParallelMode (void)
 
void CommandCounterIncrement (void)
 
void ForceSyncCommit (void)
 
static TransactionId RecordTransactionCommit (void)
 
static void AtSubCommit_childXids (void)
 
static void AtSubAbort_childXids (void)
 
static void PrepareTransaction (void)
 
void StartTransactionCommand (void)
 
void CommitTransactionCommand (void)
 
void AbortCurrentTransaction (void)
 
void PreventTransactionChain (bool isTopLevel, const char *stmtType)
 
void WarnNoTransactionChain (bool isTopLevel, const char *stmtType)
 
void RequireTransactionChain (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)
 
void BeginTransactionBlock (void)
 
bool PrepareTransactionBlock (char *gid)
 
bool EndTransactionBlock (void)
 
void UserAbortTransactionBlock (void)
 
void BeginImplicitTransactionBlock (void)
 
void EndImplicitTransactionBlock (void)
 
void DefineSavepoint (char *name)
 
void ReleaseSavepoint (List *options)
 
void RollbackToSavepoint (List *options)
 
void BeginInternalSubTransaction (char *name)
 
void ReleaseCurrentSubTransaction (void)
 
void RollbackAndReleaseCurrentSubTransaction (void)
 
void AbortOutOfAnyTransaction (void)
 
bool IsTransactionBlock (void)
 
bool IsTransactionOrTransactionBlock (void)
 
char TransactionBlockStatusCode (void)
 
bool IsSubTransaction (void)
 
Size EstimateTransactionStateSpace (void)
 
void SerializeTransactionState (Size maxsize, char *start_address)
 
void StartParallelWorkerTransaction (char *tstatespace)
 
void EndParallelWorkerTransaction (void)
 
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)
 
static void xact_redo_commit (xl_xact_parsed_commit *parsed, TransactionId xid, XLogRecPtr lsn, RepOriginId origin_id)
 
static void xact_redo_abort (xl_xact_parsed_abort *parsed, TransactionId xid)
 
void xact_redo (XLogReaderState *record)
 

Variables

int DefaultXactIsoLevel = XACT_READ_COMMITTED
 
int XactIsoLevel
 
bool DefaultXactReadOnly = false
 
bool XactReadOnly
 
bool DefaultXactDeferrable = false
 
bool XactDeferrable
 
int synchronous_commit = SYNCHRONOUS_COMMIT_ON
 
TransactionId XactTopTransactionId = InvalidTransactionId
 
int nParallelCurrentXids = 0
 
TransactionIdParallelCurrentXids
 
int MyXactFlags
 
static TransactionStateData TopTransactionStateData
 
static int nUnreportedXids
 
static TransactionId unreportedXids [PGPROC_MAX_CACHED_SUBXIDS]
 
static TransactionState CurrentTransactionState = &TopTransactionStateData
 
static SubTransactionId currentSubTransactionId
 
static CommandId currentCommandId
 
static bool currentCommandIdUsed
 
static TimestampTz xactStartTimestamp
 
static TimestampTz stmtStartTimestamp
 
static TimestampTz xactStopTimestamp
 
static char * prepareGID
 
static bool forceSyncCommit = false
 
static MemoryContext TransactionAbortContext = NULL
 
static XactCallbackItemXact_callbacks = NULL
 
static SubXactCallbackItemSubXact_callbacks = NULL
 

Typedef Documentation

Definition at line 195 of file xact.c.

Enumeration Type Documentation

Enumerator
TBLOCK_DEFAULT 
TBLOCK_STARTED 
TBLOCK_BEGIN 
TBLOCK_INPROGRESS 
TBLOCK_IMPLICIT_INPROGRESS 
TBLOCK_PARALLEL_INPROGRESS 
TBLOCK_END 
TBLOCK_ABORT 
TBLOCK_ABORT_END 
TBLOCK_ABORT_PENDING 
TBLOCK_PREPARE 
TBLOCK_SUBBEGIN 
TBLOCK_SUBINPROGRESS 
TBLOCK_SUBRELEASE 
TBLOCK_SUBCOMMIT 
TBLOCK_SUBABORT 
TBLOCK_SUBABORT_END 
TBLOCK_SUBABORT_PENDING 
TBLOCK_SUBRESTART 
TBLOCK_SUBABORT_RESTART 

Definition at line 139 of file xact.c.

140 {
141  /* not-in-transaction-block states */
142  TBLOCK_DEFAULT, /* idle */
143  TBLOCK_STARTED, /* running single-query transaction */
144 
145  /* transaction block states */
146  TBLOCK_BEGIN, /* starting transaction block */
147  TBLOCK_INPROGRESS, /* live transaction */
148  TBLOCK_IMPLICIT_INPROGRESS, /* live transaction after implicit BEGIN */
149  TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
150  TBLOCK_END, /* COMMIT received */
151  TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
152  TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
153  TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
154  TBLOCK_PREPARE, /* live xact, PREPARE received */
155 
156  /* subtransaction states */
157  TBLOCK_SUBBEGIN, /* starting a subtransaction */
158  TBLOCK_SUBINPROGRESS, /* live subtransaction */
159  TBLOCK_SUBRELEASE, /* RELEASE received */
160  TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
161  TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
162  TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
163  TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
164  TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
165  TBLOCK_SUBABORT_RESTART /* failed subxact, ROLLBACK TO received */
166 } TBlockState;
TBlockState
Definition: xact.c:139
enum TransState
Enumerator
TRANS_DEFAULT 
TRANS_START 
TRANS_INPROGRESS 
TRANS_COMMIT 
TRANS_ABORT 
TRANS_PREPARE 

Definition at line 123 of file xact.c.

124 {
125  TRANS_DEFAULT, /* idle */
126  TRANS_START, /* transaction starting */
127  TRANS_INPROGRESS, /* inside a valid transaction */
128  TRANS_COMMIT, /* commit in progress */
129  TRANS_ABORT, /* abort in progress */
130  TRANS_PREPARE /* prepare in progress */
131 } TransState;
TransState
Definition: xact.c:123

Function Documentation

void AbortCurrentTransaction ( void  )

Definition at line 2984 of file xact.c.

References AbortCurrentTransaction(), AbortSubTransaction(), AbortTransaction(), TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, TransactionStateData::state, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, TRANS_DEFAULT, TRANS_INPROGRESS, and TRANS_START.

Referenced by AbortCurrentTransaction(), AutoVacLauncherMain(), PostgresMain(), ReorderBufferCommit(), ReorderBufferImmediateInvalidation(), and SnapBuildClearExportedSnapshot().

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

Definition at line 4343 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, AtAbort_Memory(), AtAbort_Portals(), AtCleanup_Memory(), AtSubAbort_Portals(), TransactionStateData::blockState, CleanupSubTransaction(), CleanupTransaction(), CurrentTransactionState, TransactionStateData::curTransactionOwner, TransactionStateData::parent, TransactionStateData::state, TransactionStateData::subTransactionId, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, TRANS_DEFAULT, TRANS_INPROGRESS, and TRANS_START.

Referenced by do_autovacuum(), perform_work_item(), RemoveTempRelationsCallback(), and ShutdownPostgres().

4344 {
4346 
4347  /* Ensure we're not running in a doomed memory context */
4348  AtAbort_Memory();
4349 
4350  /*
4351  * Get out of any transaction or nested transaction
4352  */
4353  do
4354  {
4355  switch (s->blockState)
4356  {
4357  case TBLOCK_DEFAULT:
4358  if (s->state == TRANS_DEFAULT)
4359  {
4360  /* Not in a transaction, do nothing */
4361  }
4362  else
4363  {
4364  /*
4365  * We can get here after an error during transaction start
4366  * (state will be TRANS_START). Need to clean up the
4367  * incompletely started transaction. First, adjust the
4368  * low-level state to suppress warning message from
4369  * AbortTransaction.
4370  */
4371  if (s->state == TRANS_START)
4372  s->state = TRANS_INPROGRESS;
4373  AbortTransaction();
4375  }
4376  break;
4377  case TBLOCK_STARTED:
4378  case TBLOCK_BEGIN:
4379  case TBLOCK_INPROGRESS:
4382  case TBLOCK_END:
4383  case TBLOCK_ABORT_PENDING:
4384  case TBLOCK_PREPARE:
4385  /* In a transaction, so clean up */
4386  AbortTransaction();
4389  break;
4390  case TBLOCK_ABORT:
4391  case TBLOCK_ABORT_END:
4392 
4393  /*
4394  * AbortTransaction is already done, still need Cleanup.
4395  * However, if we failed partway through running ROLLBACK,
4396  * there will be an active portal running that command, which
4397  * we need to shut down before doing CleanupTransaction.
4398  */
4399  AtAbort_Portals();
4402  break;
4403 
4404  /*
4405  * In a subtransaction, so clean it up and abort parent too
4406  */
4407  case TBLOCK_SUBBEGIN:
4408  case TBLOCK_SUBINPROGRESS:
4409  case TBLOCK_SUBRELEASE:
4410  case TBLOCK_SUBCOMMIT:
4412  case TBLOCK_SUBRESTART:
4415  s = CurrentTransactionState; /* changed by pop */
4416  break;
4417 
4418  case TBLOCK_SUBABORT:
4419  case TBLOCK_SUBABORT_END:
4421  /* As above, but AbortSubTransaction already done */
4422  if (s->curTransactionOwner)
4423  {
4424  /* As in TBLOCK_ABORT, might have a live portal to zap */
4429  }
4431  s = CurrentTransactionState; /* changed by pop */
4432  break;
4433  }
4434  } while (s->blockState != TBLOCK_DEFAULT);
4435 
4436  /* Should be out of all subxacts now */
4437  Assert(s->parent == NULL);
4438 
4439  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4440  AtCleanup_Memory();
4441 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
static void AtCleanup_Memory(void)
Definition: xact.c:1724
void AtAbort_Portals(void)
Definition: portalmem.c:746
TBlockState blockState
Definition: xact.c:178
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
static void CleanupTransaction(void)
Definition: xact.c:2624
struct TransactionStateData * parent
Definition: xact.c:192
SubTransactionId subTransactionId
Definition: xact.c:174
static void AbortSubTransaction(void)
Definition: xact.c:4689
static void AtAbort_Memory(void)
Definition: xact.c:1634
static void AbortTransaction(void)
Definition: xact.c:2444
#define Assert(condition)
Definition: c.h:681
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:901
static void CleanupSubTransaction(void)
Definition: xact.c:4834
static void AbortSubTransaction ( void  )
static

Definition at line 4689 of file xact.c.

References AbortBufferIO(), AfterTriggerEndSubXact(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubAbort_childXids(), AtSubAbort_Memory(), AtSubAbort_Notify(), AtSubAbort_Portals(), AtSubAbort_ResourceOwner(), AtSubAbort_smgr(), AtSubAbort_Snapshot(), CallSubXactCallbacks(), CurrentTransactionState, TransactionStateData::curTransactionOwner, elog, TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, IsInParallelMode(), LockErrorCleanup(), LWLockReleaseAll(), TransactionStateData::nestingLevel, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PG_SETMASK, pgstat_progress_end_command(), pgstat_report_wait_end(), TransactionStateData::prevSecContext, TransactionStateData::prevUser, TransactionStateData::prevXactReadOnly, RecordTransactionAbort(), reschedule_timeouts(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerRelease(), RESUME_INTERRUPTS, SetUserIdAndSecContext(), ShowTransactionState(), TransactionStateData::state, TransactionStateData::subTransactionId, SUBXACT_EVENT_ABORT_SUB, TRANS_ABORT, TRANS_INPROGRESS, TransactionStateData::transactionId, TransactionIdIsValid, TransStateAsString(), UnBlockSig, UnlockBuffers(), WARNING, XactReadOnly, and XLogResetInsertion().

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), CommitTransactionCommand(), and RollbackAndReleaseCurrentSubTransaction().

4690 {
4692 
4693  /* Prevent cancel/die interrupt while cleaning up */
4694  HOLD_INTERRUPTS();
4695 
4696  /* Make sure we have a valid memory context and resource owner */
4699 
4700  /*
4701  * Release any LW locks we might be holding as quickly as possible.
4702  * (Regular locks, however, must be held till we finish aborting.)
4703  * Releasing LW locks is critical since we might try to grab them again
4704  * while cleaning up!
4705  *
4706  * FIXME This may be incorrect --- Are there some locks we should keep?
4707  * Buffer locks, for example? I don't think so but I'm not sure.
4708  */
4709  LWLockReleaseAll();
4710 
4713  AbortBufferIO();
4714  UnlockBuffers();
4715 
4716  /* Reset WAL record construction state */
4718 
4719  /*
4720  * Also clean up any open wait for lock, since the lock manager will choke
4721  * if we try to wait for another lock before doing this.
4722  */
4723  LockErrorCleanup();
4724 
4725  /*
4726  * If any timeout events are still active, make sure the timeout interrupt
4727  * is scheduled. This covers possible loss of a timeout interrupt due to
4728  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4729  * We delay this till after LockErrorCleanup so that we don't uselessly
4730  * reschedule lock or deadlock check timeouts.
4731  */
4733 
4734  /*
4735  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4736  * handler. We do this fairly early in the sequence so that the timeout
4737  * infrastructure will be functional if needed while aborting.
4738  */
4740 
4741  /*
4742  * check the current transaction state
4743  */
4744  ShowTransactionState("AbortSubTransaction");
4745 
4746  if (s->state != TRANS_INPROGRESS)
4747  elog(WARNING, "AbortSubTransaction while in %s state",
4749 
4750  s->state = TRANS_ABORT;
4751 
4752  /*
4753  * Reset user ID which might have been changed transiently. (See notes in
4754  * AbortTransaction.)
4755  */
4757 
4758  /* Exit from parallel mode, if necessary. */
4759  if (IsInParallelMode())
4760  {
4762  s->parallelModeLevel = 0;
4763  }
4764 
4765  /*
4766  * We can skip all this stuff if the subxact failed before creating a
4767  * ResourceOwner...
4768  */
4769  if (s->curTransactionOwner)
4770  {
4771  AfterTriggerEndSubXact(false);
4777  s->parent->subTransactionId);
4779 
4780  /* Advertise the fact that we aborted in pg_xact. */
4781  (void) RecordTransactionAbort(true);
4782 
4783  /* Post-abort cleanup */
4786 
4788  s->parent->subTransactionId);
4789 
4792  false, false);
4794  s->parent->subTransactionId);
4795  AtEOSubXact_Inval(false);
4798  false, false);
4801  false, false);
4802  AtSubAbort_smgr();
4803 
4804  AtEOXact_GUC(false, s->gucNestLevel);
4805  AtEOSubXact_SPI(false, s->subTransactionId);
4807  s->parent->subTransactionId);
4809  s->parent->subTransactionId);
4811  s->parent->subTransactionId);
4813  AtEOSubXact_PgStat(false, s->nestingLevel);
4815  }
4816 
4817  /*
4818  * Restore the upper transaction's read-only state, too. This should be
4819  * redundant with GUC's cleanup but we may as well do it for consistency
4820  * with the commit case.
4821  */
4823 
4825 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1679
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3075
static TransactionState CurrentTransactionState
Definition: xact.c:233
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:396
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1856
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:216
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:408
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:1023
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
bool IsInParallelMode(void)
Definition: xact.c:906
static void AtSubAbort_Memory(void)
Definition: xact.c:1654
void XLogResetInsertion(void)
Definition: xloginsert.c:193
struct TransactionStateData * parent
Definition: xact.c:192
static void ShowTransactionState(const char *str)
Definition: xact.c:5096
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5088
SubTransactionId subTransactionId
Definition: xact.c:174
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void AtSubAbort_childXids(void)
Definition: xact.c:1692
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1244
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2136
void LockErrorCleanup(void)
Definition: proc.c:686
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:683
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3518
void AtSubAbort_Notify(void)
Definition: async.c:1671
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:13022
void pgstat_progress_end_command(void)
Definition: pgstat.c:3144
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2618
TransactionId transactionId
Definition: xact.c:173
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3392
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:191
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5200
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:901
void AtSubAbort_smgr(void)
Definition: storage.c:471
bool prevXactReadOnly
Definition: xact.c:188
void AbortBufferIO(void)
Definition: bufmgr.c:3974
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4842
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:988
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:115
#define elog
Definition: elog.h:219
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:3943
void LWLockReleaseAll(void)
Definition: lwlock.c:1820
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:916
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1523
static void AbortTransaction ( void  )
static

Definition at line 2444 of file xact.c.

References AbortBufferIO(), AfterTriggerEndXact(), Assert, AtAbort_Memory(), AtAbort_Notify(), AtAbort_Portals(), AtAbort_ResourceOwner(), AtAbort_Twophase(), AtEOXact_ApplyLauncher(), AtEOXact_Buffers(), AtEOXact_ComboCid(), AtEOXact_Files(), AtEOXact_GUC(), AtEOXact_HashTables(), AtEOXact_Inval(), AtEOXact_LargeObject(), AtEOXact_MultiXact(), AtEOXact_Namespace(), AtEOXact_on_commit_actions(), AtEOXact_Parallel(), AtEOXact_PgStat(), AtEOXact_RelationCache(), AtEOXact_RelationMap(), AtEOXact_SMgr(), AtEOXact_SPI(), TransactionStateData::blockState, CallXactCallbacks(), ConditionVariableCancelSleep(), CurrentTransactionState, elog, HOLD_INTERRUPTS, InvalidTransactionId, IsInParallelMode(), LockErrorCleanup(), LWLockReleaseAll(), PGPROC::lxid, MyProc, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PG_SETMASK, pgstat_progress_end_command(), pgstat_report_wait_end(), pgstat_report_xact_timestamp(), TransactionStateData::prevSecContext, TransactionStateData::prevUser, ProcArrayEndTransaction(), RecordTransactionAbort(), reschedule_timeouts(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerRelease(), RESUME_INTERRUPTS, SetUserIdAndSecContext(), smgrDoPendingDeletes(), TransactionStateData::state, TBLOCK_PARALLEL_INPROGRESS, TopTransactionResourceOwner, TRANS_ABORT, TRANS_INPROGRESS, TRANS_PREPARE, TransStateAsString(), UnBlockSig, UnlockBuffers(), WARNING, XACT_EVENT_ABORT, XACT_EVENT_PARALLEL_ABORT, XactLastRecEnd, XLogResetInsertion(), and XLogSetAsyncXactLSN().

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), and CommitTransactionCommand().

2445 {
2447  TransactionId latestXid;
2448  bool is_parallel_worker;
2449 
2450  /* Prevent cancel/die interrupt while cleaning up */
2451  HOLD_INTERRUPTS();
2452 
2453  /* Make sure we have a valid memory context and resource owner */
2454  AtAbort_Memory();
2456 
2457  /*
2458  * Release any LW locks we might be holding as quickly as possible.
2459  * (Regular locks, however, must be held till we finish aborting.)
2460  * Releasing LW locks is critical since we might try to grab them again
2461  * while cleaning up!
2462  */
2463  LWLockReleaseAll();
2464 
2465  /* Clear wait information and command progress indicator */
2468 
2469  /* Clean up buffer I/O and buffer context locks, too */
2470  AbortBufferIO();
2471  UnlockBuffers();
2472 
2473  /* Reset WAL record construction state */
2475 
2476  /* Cancel condition variable sleep */
2478 
2479  /*
2480  * Also clean up any open wait for lock, since the lock manager will choke
2481  * if we try to wait for another lock before doing this.
2482  */
2483  LockErrorCleanup();
2484 
2485  /*
2486  * If any timeout events are still active, make sure the timeout interrupt
2487  * is scheduled. This covers possible loss of a timeout interrupt due to
2488  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2489  * We delay this till after LockErrorCleanup so that we don't uselessly
2490  * reschedule lock or deadlock check timeouts.
2491  */
2493 
2494  /*
2495  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2496  * handler. We do this fairly early in the sequence so that the timeout
2497  * infrastructure will be functional if needed while aborting.
2498  */
2500 
2501  /*
2502  * check the current transaction state
2503  */
2504  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2505  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2506  elog(WARNING, "AbortTransaction while in %s state",
2508  Assert(s->parent == NULL);
2509 
2510  /*
2511  * set the current transaction state information appropriately during the
2512  * abort processing
2513  */
2514  s->state = TRANS_ABORT;
2515 
2516  /*
2517  * Reset user ID which might have been changed transiently. We need this
2518  * to clean up in case control escaped out of a SECURITY DEFINER function
2519  * or other local change of CurrentUserId; therefore, the prior value of
2520  * SecurityRestrictionContext also needs to be restored.
2521  *
2522  * (Note: it is not necessary to restore session authorization or role
2523  * settings here because those can only be changed via GUC, and GUC will
2524  * take care of rolling them back if need be.)
2525  */
2527 
2528  /* If in parallel mode, clean up workers and exit parallel mode. */
2529  if (IsInParallelMode())
2530  {
2531  AtEOXact_Parallel(false);
2532  s->parallelModeLevel = 0;
2533  }
2534 
2535  /*
2536  * do abort processing
2537  */
2538  AfterTriggerEndXact(false); /* 'false' means it's abort */
2539  AtAbort_Portals();
2540  AtEOXact_LargeObject(false);
2541  AtAbort_Notify();
2542  AtEOXact_RelationMap(false);
2543  AtAbort_Twophase();
2544 
2545  /*
2546  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2547  * far as assigning an XID to advertise). But if we're inside a parallel
2548  * worker, skip this; the user backend must be the one to write the abort
2549  * record.
2550  */
2551  if (!is_parallel_worker)
2552  latestXid = RecordTransactionAbort(false);
2553  else
2554  {
2555  latestXid = InvalidTransactionId;
2556 
2557  /*
2558  * Since the parallel master won't get our value of XactLastRecEnd in
2559  * this case, we nudge WAL-writer ourselves in this case. See related
2560  * comments in RecordTransactionAbort for why this matters.
2561  */
2563  }
2564 
2565  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2566 
2567  /*
2568  * Let others know about no transaction in progress by me. Note that this
2569  * must be done _before_ releasing locks we hold and _after_
2570  * RecordTransactionAbort.
2571  */
2572  ProcArrayEndTransaction(MyProc, latestXid);
2573 
2574  /*
2575  * Post-abort cleanup. See notes in CommitTransaction() concerning
2576  * ordering. We can skip all of it if the transaction failed before
2577  * creating a resource owner.
2578  */
2579  if (TopTransactionResourceOwner != NULL)
2580  {
2581  if (is_parallel_worker)
2583  else
2585 
2588  false, true);
2589  AtEOXact_Buffers(false);
2590  AtEOXact_RelationCache(false);
2591  AtEOXact_Inval(false);
2595  false, true);
2598  false, true);
2599  smgrDoPendingDeletes(false);
2600 
2601  AtEOXact_GUC(false, 1);
2602  AtEOXact_SPI(false);
2604  AtEOXact_Namespace(false, is_parallel_worker);
2605  AtEOXact_SMgr();
2606  AtEOXact_Files();
2608  AtEOXact_HashTables(false);
2609  AtEOXact_PgStat(false);
2610  AtEOXact_ApplyLauncher(false);
2612  }
2613 
2614  /*
2615  * State remains TRANS_ABORT until CleanupTransaction().
2616  */
2618 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:833
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:306
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1666
static TransactionState CurrentTransactionState
Definition: xact.c:233
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:929
uint32 TransactionId
Definition: c.h:391
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:396
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3196
XLogRecPtr XactLastRecEnd
Definition: xlog.c:339
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2924
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2415
void AtEOXact_RelationMap(bool isCommit)
Definition: relmapper.c:457
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3337
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:12980
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:396
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void reschedule_timeouts(void)
Definition: timeout.c:408
void AtAbort_Portals(void)
Definition: portalmem.c:746
TBlockState blockState
Definition: xact.c:178
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
TransState state
Definition: xact.c:177
void AtEOXact_SMgr(void)
Definition: smgr.c:798
bool IsInParallelMode(void)
Definition: xact.c:906
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4746
void XLogResetInsertion(void)
Definition: xloginsert.c:193
void AtEOXact_Files(void)
Definition: fd.c:2649
struct TransactionStateData * parent
Definition: xact.c:192
void AtAbort_Twophase(void)
Definition: twophase.c:319
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5088
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1244
void LockErrorCleanup(void)
Definition: proc.c:686
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3518
static void AtAbort_Memory(void)
Definition: xact.c:1634
void pgstat_progress_end_command(void)
Definition: pgstat.c:3144
void AtAbort_Notify(void)
Definition: async.c:1589
void AtEOXact_ComboCid(void)
Definition: combocid.c:183
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2623
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:681
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5200
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:188
void AtEOXact_MultiXact(void)
Definition: multixact.c:1662
void AbortBufferIO(void)
Definition: bufmgr.c:3974
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:115
#define elog
Definition: elog.h:219
void LWLockReleaseAll(void)
Definition: lwlock.c:1820
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1830
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:642
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:935
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:3888
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1523
void AtEOXact_PgStat(bool isCommit)
Definition: pgstat.c:2053
LocalTransactionId lxid
Definition: proc.h:106
static void AssignTransactionId ( TransactionState  s)
static

Definition at line 490 of file xact.c.

References Assert, CurrentResourceOwner, TransactionStateData::curTransactionOwner, TransactionStateData::didLogXid, elog, ERROR, GetNewTransactionId(), GetTopTransactionId(), IsInParallelMode(), IsParallelWorker, MinSizeOfXactAssignment, TransactionStateData::nestingLevel, xl_xact_assignment::nsubxacts, nUnreportedXids, palloc(), TransactionStateData::parent, pfree(), PGPROC_MAX_CACHED_SUBXIDS, RegisterPredicateLockingXid(), TransactionStateData::state, SubTransSetParent(), TRANS_INPROGRESS, TransactionStateData::transactionId, TransactionIdIsValid, unreportedXids, XactLockTableInsert(), XactTopTransactionId, XLOG_XACT_ASSIGNMENT, XLogBeginInsert(), XLogInsert(), XLogLogicalInfoActive, XLogRegisterData(), XLogStandbyInfoActive, and xl_xact_assignment::xtop.

Referenced by GetCurrentTransactionId(), and GetTopTransactionId().

491 {
492  bool isSubXact = (s->parent != NULL);
493  ResourceOwner currentOwner;
494  bool log_unknown_top = false;
495 
496  /* Assert that caller didn't screw up */
499 
500  /*
501  * Workers synchronize transaction state at the beginning of each parallel
502  * operation, so we can't account for new XIDs at this point.
503  */
505  elog(ERROR, "cannot assign XIDs during a parallel operation");
506 
507  /*
508  * Ensure parent(s) have XIDs, so that a child always has an XID later
509  * than its parent. Musn't recurse here, or we might get a stack overflow
510  * if we're at the bottom of a huge stack of subtransactions none of which
511  * have XIDs yet.
512  */
513  if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
514  {
515  TransactionState p = s->parent;
516  TransactionState *parents;
517  size_t parentOffset = 0;
518 
519  parents = palloc(sizeof(TransactionState) * s->nestingLevel);
520  while (p != NULL && !TransactionIdIsValid(p->transactionId))
521  {
522  parents[parentOffset++] = p;
523  p = p->parent;
524  }
525 
526  /*
527  * This is technically a recursive call, but the recursion will never
528  * be more than one layer deep.
529  */
530  while (parentOffset != 0)
531  AssignTransactionId(parents[--parentOffset]);
532 
533  pfree(parents);
534  }
535 
536  /*
537  * When wal_level=logical, guarantee that a subtransaction's xid can only
538  * be seen in the WAL stream if its toplevel xid has been logged before.
539  * If necessary we log an xact_assignment record with fewer than
540  * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
541  * for a transaction even though it appears in a WAL record, we just might
542  * superfluously log something. That can happen when an xid is included
543  * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
544  * xl_standby_locks.
545  */
546  if (isSubXact && XLogLogicalInfoActive() &&
548  log_unknown_top = true;
549 
550  /*
551  * Generate a new Xid and record it in PG_PROC and pg_subtrans.
552  *
553  * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
554  * shared storage other than PG_PROC; because if there's no room for it in
555  * PG_PROC, the subtrans entry is needed to ensure that other backends see
556  * the Xid as "running". See GetNewTransactionId.
557  */
558  s->transactionId = GetNewTransactionId(isSubXact);
559  if (!isSubXact)
561 
562  if (isSubXact)
564 
565  /*
566  * If it's a top-level transaction, the predicate locking system needs to
567  * be told about it too.
568  */
569  if (!isSubXact)
571 
572  /*
573  * Acquire lock on the transaction XID. (We assume this cannot block.) We
574  * have to ensure that the lock is assigned to the transaction's own
575  * ResourceOwner.
576  */
577  currentOwner = CurrentResourceOwner;
579 
581 
582  CurrentResourceOwner = currentOwner;
583 
584  /*
585  * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
586  * top-level transaction we issue a WAL record for the assignment. We
587  * include the top-level xid and all the subxids that have not yet been
588  * reported using XLOG_XACT_ASSIGNMENT records.
589  *
590  * This is required to limit the amount of shared memory required in a hot
591  * standby server to keep track of in-progress XIDs. See notes for
592  * RecordKnownAssignedTransactionIds().
593  *
594  * We don't keep track of the immediate parent of each subxid, only the
595  * top-level transaction that each subxact belongs to. This is correct in
596  * recovery only because aborted subtransactions are separately WAL
597  * logged.
598  *
599  * This is correct even for the case where several levels above us didn't
600  * have an xid assigned as we recursed up to them beforehand.
601  */
602  if (isSubXact && XLogStandbyInfoActive())
603  {
605  nUnreportedXids++;
606 
607  /*
608  * ensure this test matches similar one in
609  * RecoverPreparedTransactions()
610  */
612  log_unknown_top)
613  {
614  xl_xact_assignment xlrec;
615 
616  /*
617  * xtop is always set by now because we recurse up transaction
618  * stack to the highest unassigned xid and then come back down
619  */
620  xlrec.xtop = GetTopTransactionId();
622  xlrec.nsubxacts = nUnreportedXids;
623 
624  XLogBeginInsert();
625  XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
627  nUnreportedXids * sizeof(TransactionId));
628 
629  (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
630 
631  nUnreportedXids = 0;
632  /* mark top, not current xact as having been logged */
634  }
635  }
636 }
TransactionId XactTopTransactionId
Definition: xact.c:107
uint32 TransactionId
Definition: c.h:391
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
TransactionId GetNewTransactionId(bool isSubXact)
Definition: varsup.c:48
TransactionId xtop
Definition: xact.h:182
TransactionId GetTopTransactionId(void)
Definition: xact.c:390
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
void pfree(void *pointer)
Definition: mcxt.c:949
bool IsInParallelMode(void)
Definition: xact.c:906
#define ERROR
Definition: elog.h:43
#define XLogLogicalInfoActive()
Definition: xlog.h:163
struct TransactionStateData * parent
Definition: xact.c:192
#define MinSizeOfXactAssignment
Definition: xact.h:187
#define IsParallelWorker()
Definition: parallel.h:52
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define XLOG_XACT_ASSIGNMENT
Definition: xact.h:138
void XactLockTableInsert(TransactionId xid)
Definition: lmgr.c:513
#define XLogStandbyInfoActive()
Definition: xlog.h:160
static int nUnreportedXids
Definition: xact.c:230
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
TransactionId transactionId
Definition: xact.c:173
#define Assert(condition)
Definition: c.h:681
static TransactionStateData TopTransactionStateData
Definition: xact.c:202
void RegisterPredicateLockingXid(TransactionId xid)
Definition: predicate.c:1880
void * palloc(Size size)
Definition: mcxt.c:848
static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: xact.c:231
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void AssignTransactionId(TransactionState s)
Definition: xact.c:490
void SubTransSetParent(TransactionId xid, TransactionId parent)
Definition: subtrans.c:74
void XLogBeginInsert(void)
Definition: xloginsert.c:120
static void AtAbort_Memory ( void  )
static

Definition at line 1634 of file xact.c.

References MemoryContextSwitchTo(), and TopMemoryContext.

Referenced by AbortOutOfAnyTransaction(), and AbortTransaction().

1635 {
1636  /*
1637  * Switch into TransactionAbortContext, which should have some free space
1638  * even if nothing else does. We'll work in this context until we've
1639  * finished cleaning up.
1640  *
1641  * It is barely possible to get here when we've not been able to create
1642  * TransactionAbortContext yet; if so use TopMemoryContext.
1643  */
1644  if (TransactionAbortContext != NULL)
1646  else
1648 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:270
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static void AtAbort_ResourceOwner ( void  )
static

Definition at line 1666 of file xact.c.

References CurrentResourceOwner, and TopTransactionResourceOwner.

Referenced by AbortTransaction().

1667 {
1668  /*
1669  * Make sure we have a valid ResourceOwner, if possible (else it will be
1670  * NULL, which is OK)
1671  */
1673 }
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static void AtCCI_LocalCache ( void  )
static

Definition at line 1358 of file xact.c.

References AtCCI_RelationMap(), and CommandEndInvalidationMessages().

Referenced by CommandCounterIncrement().

1359 {
1360  /*
1361  * Make any pending relation map changes visible. We must do this before
1362  * processing local sinval messages, so that the map changes will get
1363  * reflected into the relcache when relcache invals are processed.
1364  */
1366 
1367  /*
1368  * Make catalog changes visible to me for the next command.
1369  */
1371 }
void CommandEndInvalidationMessages(void)
Definition: inval.c:1065
void AtCCI_RelationMap(void)
Definition: relmapper.c:423
static void AtCleanup_Memory ( void  )
static

Definition at line 1724 of file xact.c.

References Assert, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), TransactionStateData::parent, TopMemoryContext, and TopTransactionContext.

Referenced by AbortOutOfAnyTransaction(), and CleanupTransaction().

1725 {
1727 
1728  /*
1729  * Now that we're "out" of a transaction, have the system allocate things
1730  * in the top memory context instead of per-transaction contexts.
1731  */
1733 
1734  /*
1735  * Clear the special abort context for next time.
1736  */
1737  if (TransactionAbortContext != NULL)
1739 
1740  /*
1741  * Release all transaction-local memory.
1742  */
1743  if (TopTransactionContext != NULL)
1745  TopTransactionContext = NULL;
1746  CurTransactionContext = NULL;
1748 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
static MemoryContext TransactionAbortContext
Definition: xact.c:270
struct TransactionStateData * parent
Definition: xact.c:192
MemoryContext curTransactionContext
Definition: xact.c:181
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:681
static void AtCommit_Memory ( void  )
static

Definition at line 1377 of file xact.c.

References Assert, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextSwitchTo(), TopMemoryContext, and TopTransactionContext.

Referenced by CommitTransaction(), and PrepareTransaction().

1378 {
1379  /*
1380  * Now that we're "out" of a transaction, have the system allocate things
1381  * in the top memory context instead of per-transaction contexts.
1382  */
1384 
1385  /*
1386  * Release all transaction-local memory.
1387  */
1388  Assert(TopTransactionContext != NULL);
1390  TopTransactionContext = NULL;
1391  CurTransactionContext = NULL;
1393 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
MemoryContext curTransactionContext
Definition: xact.c:181
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define Assert(condition)
Definition: c.h:681
static void AtStart_Cache ( void  )
static

Definition at line 978 of file xact.c.

References AcceptInvalidationMessages().

Referenced by StartTransaction().

979 {
981 }
void AcceptInvalidationMessages(void)
Definition: inval.c:679
static void AtStart_Memory ( void  )
static

Definition at line 987 of file xact.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextSwitchTo(), TopMemoryContext, and TopTransactionContext.

Referenced by StartTransaction().

988 {
990 
991  /*
992  * If this is the first time through, create a private context for
993  * AbortTransaction to work in. By reserving some space now, we can
994  * insulate AbortTransaction from out-of-memory scenarios. Like
995  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
996  * size, so that space will be reserved immediately.
997  */
998  if (TransactionAbortContext == NULL)
1001  "TransactionAbortContext",
1002  32 * 1024,
1003  32 * 1024,
1004  32 * 1024);
1005 
1006  /*
1007  * We shouldn't have a transaction context already.
1008  */
1009  Assert(TopTransactionContext == NULL);
1010 
1011  /*
1012  * Create a toplevel context for the transaction.
1013  */
1016  "TopTransactionContext",
1018 
1019  /*
1020  * In a top-level transaction, CurTransactionContext is the same as
1021  * TopTransactionContext.
1022  */
1025 
1026  /* Make the CurTransactionContext active. */
1028 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
static MemoryContext TransactionAbortContext
Definition: xact.c:270
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
MemoryContext curTransactionContext
Definition: xact.c:181
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
#define Assert(condition)
Definition: c.h:681
static void AtStart_ResourceOwner ( void  )
static

Definition at line 1034 of file xact.c.

References Assert, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, ResourceOwnerCreate(), and TopTransactionResourceOwner.

Referenced by StartTransaction().

1035 {
1037 
1038  /*
1039  * We shouldn't have a transaction resource owner already.
1040  */
1042 
1043  /*
1044  * Create a toplevel resource owner for the transaction.
1045  */
1046  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1047 
1051 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:182
#define Assert(condition)
Definition: c.h:681
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
static void AtSubAbort_childXids ( void  )
static

Definition at line 1692 of file xact.c.

References TransactionStateData::childXids, CurrentTransactionState, TransactionStateData::maxChildXids, TransactionStateData::nChildXids, and pfree().

Referenced by AbortSubTransaction().

1693 {
1695 
1696  /*
1697  * We keep the child-XID arrays in TopTransactionContext (see
1698  * AtSubCommit_childXids). This means we'd better free the array
1699  * explicitly at abort to avoid leakage.
1700  */
1701  if (s->childXids != NULL)
1702  pfree(s->childXids);
1703  s->childXids = NULL;
1704  s->nChildXids = 0;
1705  s->maxChildXids = 0;
1706 
1707  /*
1708  * We could prune the unreportedXids array here. But we don't bother. That
1709  * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
1710  * would likely introduce more CPU time into the more common paths, so we
1711  * choose not to do that.
1712  */
1713 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TransactionId * childXids
Definition: xact.c:183
void pfree(void *pointer)
Definition: mcxt.c:949
static void AtSubAbort_Memory ( void  )
static

Definition at line 1654 of file xact.c.

References Assert, and MemoryContextSwitchTo().

Referenced by AbortSubTransaction().

1655 {
1657 
1659 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:270
#define Assert(condition)
Definition: c.h:681
static void AtSubAbort_ResourceOwner ( void  )
static

Definition at line 1679 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, and TransactionStateData::curTransactionOwner.

Referenced by AbortSubTransaction().

1680 {
1682 
1683  /* Make sure we have a valid ResourceOwner */
1685 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:182
static void AtSubCleanup_Memory ( void  )
static

Definition at line 1760 of file xact.c.

References Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), and TransactionStateData::parent.

Referenced by CleanupSubTransaction().

1761 {
1763 
1764  Assert(s->parent != NULL);
1765 
1766  /* Make sure we're not in an about-to-be-deleted context */
1769 
1770  /*
1771  * Clear the special abort context for next time.
1772  */
1773  if (TransactionAbortContext != NULL)
1775 
1776  /*
1777  * Delete the subxact local memory contexts. Its CurTransactionContext can
1778  * go too (note this also kills CurTransactionContexts from any children
1779  * of the subxact).
1780  */
1781  if (s->curTransactionContext)
1783  s->curTransactionContext = NULL;
1784 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
static MemoryContext TransactionAbortContext
Definition: xact.c:270
struct TransactionStateData * parent
Definition: xact.c:192
MemoryContext curTransactionContext
Definition: xact.c:181
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define Assert(condition)
Definition: c.h:681
static void AtSubCommit_childXids ( void  )
static

Definition at line 1433 of file xact.c.

References Assert, TransactionStateData::childXids, CurrentTransactionState, ereport, errcode(), errmsg(), ERROR, MaxAllocSize, TransactionStateData::maxChildXids, MemoryContextAlloc(), Min, TransactionStateData::nChildXids, TransactionStateData::parent, pfree(), repalloc(), TopTransactionContext, and TransactionStateData::transactionId.

Referenced by CommitSubTransaction().

1434 {
1436  int new_nChildXids;
1437 
1438  Assert(s->parent != NULL);
1439 
1440  /*
1441  * The parent childXids array will need to hold my XID and all my
1442  * childXids, in addition to the XIDs already there.
1443  */
1444  new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
1445 
1446  /* Allocate or enlarge the parent array if necessary */
1447  if (s->parent->maxChildXids < new_nChildXids)
1448  {
1449  int new_maxChildXids;
1450  TransactionId *new_childXids;
1451 
1452  /*
1453  * Make it 2x what's needed right now, to avoid having to enlarge it
1454  * repeatedly. But we can't go above MaxAllocSize. (The latter limit
1455  * is what ensures that we don't need to worry about integer overflow
1456  * here or in the calculation of new_nChildXids.)
1457  */
1458  new_maxChildXids = Min(new_nChildXids * 2,
1459  (int) (MaxAllocSize / sizeof(TransactionId)));
1460 
1461  if (new_maxChildXids < new_nChildXids)
1462  ereport(ERROR,
1463  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1464  errmsg("maximum number of committed subtransactions (%d) exceeded",
1465  (int) (MaxAllocSize / sizeof(TransactionId)))));
1466 
1467  /*
1468  * We keep the child-XID arrays in TopTransactionContext; this avoids
1469  * setting up child-transaction contexts for what might be just a few
1470  * bytes of grandchild XIDs.
1471  */
1472  if (s->parent->childXids == NULL)
1473  new_childXids =
1475  new_maxChildXids * sizeof(TransactionId));
1476  else
1477  new_childXids = repalloc(s->parent->childXids,
1478  new_maxChildXids * sizeof(TransactionId));
1479 
1480  s->parent->childXids = new_childXids;
1481  s->parent->maxChildXids = new_maxChildXids;
1482  }
1483 
1484  /*
1485  * Copy all my XIDs to parent's array.
1486  *
1487  * Note: We rely on the fact that the XID of a child always follows that
1488  * of its parent. By copying the XID of this subtransaction before the
1489  * XIDs of its children, we ensure that the array stays ordered. Likewise,
1490  * all XIDs already in the array belong to subtransactions started and
1491  * subcommitted before us, so their XIDs must precede ours.
1492  */
1494 
1495  if (s->nChildXids > 0)
1496  memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
1497  s->childXids,
1498  s->nChildXids * sizeof(TransactionId));
1499 
1500  s->parent->nChildXids = new_nChildXids;
1501 
1502  /* Release child's array to avoid leakage */
1503  if (s->childXids != NULL)
1504  pfree(s->childXids);
1505  /* We must reset these to avoid double-free if fail later in commit */
1506  s->childXids = NULL;
1507  s->nChildXids = 0;
1508  s->maxChildXids = 0;
1509 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
MemoryContext TopTransactionContext
Definition: mcxt.c:48
uint32 TransactionId
Definition: c.h:391
#define Min(x, y)
Definition: c.h:812
int errcode(int sqlerrcode)
Definition: elog.c:575
TransactionId * childXids
Definition: xact.c:183
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
struct TransactionStateData * parent
Definition: xact.c:192
#define ereport(elevel, rest)
Definition: elog.h:122
#define MaxAllocSize
Definition: memutils.h:40
TransactionId transactionId
Definition: xact.c:173
#define Assert(condition)
Definition: c.h:681
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
static void AtSubCommit_Memory ( void  )
static

Definition at line 1404 of file xact.c.

References Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, MemoryContextDelete(), MemoryContextIsEmpty(), MemoryContextSwitchTo(), and TransactionStateData::parent.

Referenced by CommitSubTransaction().

1405 {
1407 
1408  Assert(s->parent != NULL);
1409 
1410  /* Return to parent transaction level's memory context. */
1413 
1414  /*
1415  * Ordinarily we cannot throw away the child's CurTransactionContext,
1416  * since the data it contains will be needed at upper commit. However, if
1417  * there isn't actually anything in it, we can throw it away. This avoids
1418  * a small memory leak in the common case of "trivial" subxacts.
1419  */
1421  {
1423  s->curTransactionContext = NULL;
1424  }
1425 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
bool MemoryContextIsEmpty(MemoryContext context)
Definition: mcxt.c:414
struct TransactionStateData * parent
Definition: xact.c:192
MemoryContext curTransactionContext
Definition: xact.c:181
#define Assert(condition)
Definition: c.h:681
static void AtSubStart_Memory ( void  )
static

Definition at line 1062 of file xact.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, and MemoryContextSwitchTo().

Referenced by StartSubTransaction().

1063 {
1065 
1066  Assert(CurTransactionContext != NULL);
1067 
1068  /*
1069  * Create a CurTransactionContext, which will be used to hold data that
1070  * survives subtransaction commit but disappears on subtransaction abort.
1071  * We make it a child of the immediate parent's CurTransactionContext.
1072  */
1074  "CurTransactionContext",
1077 
1078  /* Make the CurTransactionContext active. */
1080 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
MemoryContext curTransactionContext
Definition: xact.c:181
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
#define Assert(condition)
Definition: c.h:681
static void AtSubStart_ResourceOwner ( void  )
static

Definition at line 1086 of file xact.c.

References Assert, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, TransactionStateData::parent, and ResourceOwnerCreate().

Referenced by StartSubTransaction().

1087 {
1089 
1090  Assert(s->parent != NULL);
1091 
1092  /*
1093  * Create a resource owner for the subtransaction. We make it a child of
1094  * the immediate parent's resource owner.
1095  */
1096  s->curTransactionOwner =
1098  "SubTransaction");
1099 
1102 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:182
struct TransactionStateData * parent
Definition: xact.c:192
#define Assert(condition)
Definition: c.h:681
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
void BeginImplicitTransactionBlock ( void  )

Definition at line 3779 of file xact.c.

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

Referenced by exec_simple_query().

3780 {
3782 
3783  /*
3784  * If we are in STARTED state (that is, no transaction block is open),
3785  * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
3786  * block.
3787  *
3788  * For caller convenience, we consider all other transaction states as
3789  * legal here; otherwise the caller would need its own state check, which
3790  * seems rather pointless.
3791  */
3792  if (s->blockState == TBLOCK_STARTED)
3794 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TBlockState blockState
Definition: xact.c:178
void BeginInternalSubTransaction ( char *  name)

Definition at line 4171 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CommitTransactionCommand(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), MemoryContextStrdup(), TransactionStateData::name, PushTransaction(), StartTransactionCommand(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and TopTransactionContext.

Referenced by exec_stmt_block(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_fetchrow(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), pltcl_returnnext(), pltcl_subtrans_begin(), pltcl_subtransaction(), PLy_spi_subtransaction_begin(), PLy_subtransaction_enter(), ReorderBufferCommit(), and ReorderBufferImmediateInvalidation().

4172 {
4174 
4175  /*
4176  * Workers synchronize transaction state at the beginning of each parallel
4177  * operation, so we can't account for new subtransactions after that
4178  * point. We might be able to make an exception for the type of
4179  * subtransaction established by this function, which is typically used in
4180  * contexts where we're going to release or roll back the subtransaction
4181  * before proceeding further, so that no enduring change to the
4182  * transaction state occurs. For now, however, we prohibit this case along
4183  * with all the others.
4184  */
4185  if (IsInParallelMode())
4186  ereport(ERROR,
4187  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
4188  errmsg("cannot start subtransactions during a parallel operation")));
4189 
4190  switch (s->blockState)
4191  {
4192  case TBLOCK_STARTED:
4193  case TBLOCK_INPROGRESS:
4195  case TBLOCK_END:
4196  case TBLOCK_PREPARE:
4197  case TBLOCK_SUBINPROGRESS:
4198  /* Normal subtransaction start */
4199  PushTransaction();
4200  s = CurrentTransactionState; /* changed by push */
4201 
4202  /*
4203  * Savepoint names, like the TransactionState block itself, live
4204  * in TopTransactionContext.
4205  */
4206  if (name)
4208  break;
4209 
4210  /* These cases are invalid. */
4211  case TBLOCK_DEFAULT:
4212  case TBLOCK_BEGIN:
4214  case TBLOCK_SUBBEGIN:
4215  case TBLOCK_SUBRELEASE:
4216  case TBLOCK_SUBCOMMIT:
4217  case TBLOCK_ABORT:
4218  case TBLOCK_SUBABORT:
4219  case TBLOCK_ABORT_END:
4220  case TBLOCK_SUBABORT_END:
4221  case TBLOCK_ABORT_PENDING:
4223  case TBLOCK_SUBRESTART:
4225  elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
4227  break;
4228  }
4229 
4232 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
MemoryContext TopTransactionContext
Definition: mcxt.c:48
void CommitTransactionCommand(void)
Definition: xact.c:2744
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:906
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5147
#define ereport(elevel, rest)
Definition: elog.h:122
static void PushTransaction(void)
Definition: xact.c:4867
void StartTransactionCommand(void)
Definition: xact.c:2673
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:1063
#define elog
Definition: elog.h:219
void BeginTransactionBlock ( void  )

Definition at line 3413 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), FATAL, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and WARNING.

Referenced by standard_ProcessUtility().

3414 {
3416 
3417  switch (s->blockState)
3418  {
3419  /*
3420  * We are not inside a transaction block, so allow one to begin.
3421  */
3422  case TBLOCK_STARTED:
3423  s->blockState = TBLOCK_BEGIN;
3424  break;
3425 
3426  /*
3427  * BEGIN converts an implicit transaction block to a regular one.
3428  * (Note that we allow this even if we've already done some
3429  * commands, which is a bit odd but matches historical practice.)
3430  */
3432  s->blockState = TBLOCK_BEGIN;
3433  break;
3434 
3435  /*
3436  * Already a transaction block in progress.
3437  */
3438  case TBLOCK_INPROGRESS:
3440  case TBLOCK_SUBINPROGRESS:
3441  case TBLOCK_ABORT:
3442  case TBLOCK_SUBABORT:
3443  ereport(WARNING,
3444  (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
3445  errmsg("there is already a transaction in progress")));
3446  break;
3447 
3448  /* These cases are invalid. */
3449  case TBLOCK_DEFAULT:
3450  case TBLOCK_BEGIN:
3451  case TBLOCK_SUBBEGIN:
3452  case TBLOCK_END:
3453  case TBLOCK_SUBRELEASE:
3454  case TBLOCK_SUBCOMMIT:
3455  case TBLOCK_ABORT_END:
3456  case TBLOCK_SUBABORT_END:
3457  case TBLOCK_ABORT_PENDING:
3459  case TBLOCK_SUBRESTART:
3461  case TBLOCK_PREPARE:
3462  elog(FATAL, "BeginTransactionBlock: unexpected state %s",
3464  break;
3465  }
3466 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:178
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5147
#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
static const char * BlockStateAsString ( TBlockState  blockState)
static

Definition at line 5147 of file xact.c.

References TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, and TBLOCK_SUBRESTART.

Referenced by BeginInternalSubTransaction(), BeginTransactionBlock(), CommitTransactionCommand(), DefineSavepoint(), EndTransactionBlock(), ReleaseCurrentSubTransaction(), ReleaseSavepoint(), RollbackAndReleaseCurrentSubTransaction(), RollbackToSavepoint(), ShowTransactionStateRec(), StartTransactionCommand(), TransactionBlockStatusCode(), and UserAbortTransactionBlock().

5148 {
5149  switch (blockState)
5150  {
5151  case TBLOCK_DEFAULT:
5152  return "DEFAULT";
5153  case TBLOCK_STARTED:
5154  return "STARTED";
5155  case TBLOCK_BEGIN:
5156  return "BEGIN";
5157  case TBLOCK_INPROGRESS:
5158  return "INPROGRESS";
5160  return "IMPLICIT_INPROGRESS";
5162  return "PARALLEL_INPROGRESS";
5163  case TBLOCK_END:
5164  return "END";
5165  case TBLOCK_ABORT:
5166  return "ABORT";
5167  case TBLOCK_ABORT_END:
5168  return "ABORT END";
5169  case TBLOCK_ABORT_PENDING:
5170  return "ABORT PEND";
5171  case TBLOCK_PREPARE:
5172  return "PREPARE";
5173  case TBLOCK_SUBBEGIN:
5174  return "SUB BEGIN";
5175  case TBLOCK_SUBINPROGRESS:
5176  return "SUB INPROGRS";
5177  case TBLOCK_SUBRELEASE:
5178  return "SUB RELEASE";
5179  case TBLOCK_SUBCOMMIT:
5180  return "SUB COMMIT";
5181  case TBLOCK_SUBABORT:
5182  return "SUB ABORT";
5183  case TBLOCK_SUBABORT_END:
5184  return "SUB ABORT END";
5186  return "SUB ABRT PEND";
5187  case TBLOCK_SUBRESTART:
5188  return "SUB RESTART";
5190  return "SUB AB RESTRT";
5191  }
5192  return "UNRECOGNIZED";
5193 }
static void CallSubXactCallbacks ( SubXactEvent  event,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)
static

Definition at line 3392 of file xact.c.

References SubXactCallbackItem::arg, SubXactCallbackItem::callback, and SubXactCallbackItem::next.

Referenced by AbortSubTransaction(), CommitSubTransaction(), and StartSubTransaction().

3395 {
3396  SubXactCallbackItem *item;
3397 
3398  for (item = SubXact_callbacks; item; item = item->next)
3399  item->callback(event, mySubid, parentSubid, item->arg);
3400 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:294
struct SubXactCallbackItem * next
Definition: xact.c:289
SubXactCallback callback
Definition: xact.c:290
static void CallXactCallbacks ( XactEvent  event)
static

Definition at line 3337 of file xact.c.

References XactCallbackItem::arg, XactCallbackItem::callback, and XactCallbackItem::next.

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

3338 {
3339  XactCallbackItem *item;
3340 
3341  for (item = Xact_callbacks; item; item = item->next)
3342  item->callback(event, item->arg);
3343 }
struct XactCallbackItem * next
Definition: xact.c:277
void * arg
Definition: xact.c:279
XactCallback callback
Definition: xact.c:278
static XactCallbackItem * Xact_callbacks
Definition: xact.c:282
static void CheckTransactionChain ( bool  isTopLevel,
bool  throwError,
const char *  stmtType 
)
static

Definition at line 3229 of file xact.c.

References ereport, errcode(), errmsg(), ERROR, IsSubTransaction(), IsTransactionBlock(), and WARNING.

Referenced by RequireTransactionChain(), and WarnNoTransactionChain().

3230 {
3231  /*
3232  * xact block already started?
3233  */
3234  if (IsTransactionBlock())
3235  return;
3236 
3237  /*
3238  * subtransaction?
3239  */
3240  if (IsSubTransaction())
3241  return;
3242 
3243  /*
3244  * inside a function call?
3245  */
3246  if (!isTopLevel)
3247  return;
3248 
3249  ereport(throwError ? ERROR : WARNING,
3250  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3251  /* translator: %s represents an SQL statement name */
3252  errmsg("%s can only be used in transaction blocks",
3253  stmtType)));
3254  return;
3255 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsTransactionBlock(void)
Definition: xact.c:4447
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
bool IsSubTransaction(void)
Definition: xact.c:4520
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void CleanupSubTransaction ( void  )
static

Definition at line 4834 of file xact.c.

References AtSubCleanup_Memory(), AtSubCleanup_Portals(), CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, TransactionStateData::parent, PopTransaction(), ResourceOwnerDelete(), ShowTransactionState(), TransactionStateData::state, TransactionStateData::subTransactionId, TRANS_ABORT, TRANS_DEFAULT, TransStateAsString(), and WARNING.

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), CommitTransactionCommand(), and RollbackAndReleaseCurrentSubTransaction().

4835 {
4837 
4838  ShowTransactionState("CleanupSubTransaction");
4839 
4840  if (s->state != TRANS_ABORT)
4841  elog(WARNING, "CleanupSubTransaction while in %s state",
4843 
4845 
4848  if (s->curTransactionOwner)
4850  s->curTransactionOwner = NULL;
4851 
4853 
4854  s->state = TRANS_DEFAULT;
4855 
4856  PopTransaction();
4857 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
static void PopTransaction(void)
Definition: xact.c:4926
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:673
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
struct TransactionStateData * parent
Definition: xact.c:192
static void ShowTransactionState(const char *str)
Definition: xact.c:5096
SubTransactionId subTransactionId
Definition: xact.c:174
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1014
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1760
static const char * TransStateAsString(TransState state)
Definition: xact.c:5200
#define elog
Definition: elog.h:219
static void CleanupTransaction ( void  )
static

Definition at line 2624 of file xact.c.

References AtCleanup_Memory(), AtCleanup_Portals(), AtEOXact_Snapshot(), TransactionStateData::childXids, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, FATAL, TransactionStateData::gucNestLevel, InvalidSubTransactionId, InvalidTransactionId, TransactionStateData::maxChildXids, TransactionStateData::nChildXids, TransactionStateData::nestingLevel, nParallelCurrentXids, TransactionStateData::parallelModeLevel, ResourceOwnerDelete(), TransactionStateData::state, TransactionStateData::subTransactionId, TopTransactionResourceOwner, TRANS_ABORT, TRANS_DEFAULT, TransactionStateData::transactionId, TransStateAsString(), and XactTopTransactionId.

Referenced by AbortCurrentTransaction(), AbortOutOfAnyTransaction(), and CommitTransactionCommand().

2625 {
2627 
2628  /*
2629  * State should still be TRANS_ABORT from AbortTransaction().
2630  */
2631  if (s->state != TRANS_ABORT)
2632  elog(FATAL, "CleanupTransaction: unexpected state %s",
2634 
2635  /*
2636  * do abort cleanup processing
2637  */
2638  AtCleanup_Portals(); /* now safe to release portal memory */
2639  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2640 
2641  CurrentResourceOwner = NULL; /* and resource owner */
2644  s->curTransactionOwner = NULL;
2647 
2648  AtCleanup_Memory(); /* and transaction memory */
2649 
2652  s->nestingLevel = 0;
2653  s->gucNestLevel = 0;
2654  s->childXids = NULL;
2655  s->nChildXids = 0;
2656  s->maxChildXids = 0;
2657  s->parallelModeLevel = 0;
2658 
2661 
2662  /*
2663  * done with abort processing, set current transaction state back to
2664  * default
2665  */
2666  s->state = TRANS_DEFAULT;
2667 }
TransactionId XactTopTransactionId
Definition: xact.c:107
static TransactionState CurrentTransactionState
Definition: xact.c:233
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:673
static void AtCleanup_Memory(void)
Definition: xact.c:1724
int nParallelCurrentXids
Definition: xact.c:108
void AtCleanup_Portals(void)
Definition: portalmem.c:818
TransactionId * childXids
Definition: xact.c:183
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
#define FATAL
Definition: elog.h:52
#define InvalidTransactionId
Definition: transam.h:31
SubTransactionId subTransactionId
Definition: xact.c:174
TransactionId transactionId
Definition: xact.c:173
int parallelModeLevel
Definition: xact.c:191
static const char * TransStateAsString(TransState state)
Definition: xact.c:5200
#define InvalidSubTransactionId
Definition: c.h:397
#define elog
Definition: elog.h:219
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1059
void CommandCounterIncrement ( void  )

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

916 {
917  /*
918  * If the current value of the command counter hasn't been "used" to mark
919  * tuples, we need not increment it, since there's no need to distinguish
920  * a read-only command from others. This helps postpone command counter
921  * overflow, and keeps no-op CommandCounterIncrement operations cheap.
922  */
924  {
925  /*
926  * Workers synchronize transaction state at the beginning of each
927  * parallel operation, so we can't account for new commands after that
928  * point.
929  */
931  elog(ERROR, "cannot start commands during a parallel operation");
932 
933  currentCommandId += 1;
935  {
936  currentCommandId -= 1;
937  ereport(ERROR,
938  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
939  errmsg("cannot have more than 2^32-2 commands in a transaction")));
940  }
941  currentCommandIdUsed = false;
942 
943  /* Propagate new command ID into static snapshots */
945 
946  /*
947  * Make any catalog changes done by the just-completed command visible
948  * in the local syscache. We obviously don't need to do this after a
949  * read-only command. (But see hacks in inval.c to make real sure we
950  * don't think a command that queued inval messages was read-only.)
951  */
953  }
954 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsInParallelMode(void)
Definition: xact.c:906
#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:408
static void AtCCI_LocalCache(void)
Definition: xact.c:1358
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:240
static bool currentCommandIdUsed
Definition: xact.c:241
static void CommitSubTransaction ( void  )
static

Definition at line 4581 of file xact.c.

References AfterTriggerEndSubXact(), AtEOSubXact_Files(), AtEOSubXact_HashTables(), AtEOSubXact_Inval(), AtEOSubXact_LargeObject(), AtEOSubXact_Namespace(), AtEOSubXact_on_commit_actions(), AtEOSubXact_Parallel(), AtEOSubXact_PgStat(), AtEOSubXact_RelationCache(), AtEOSubXact_SPI(), AtEOXact_GUC(), AtSubCommit_childXids(), AtSubCommit_Memory(), AtSubCommit_Notify(), AtSubCommit_Portals(), AtSubCommit_smgr(), AtSubCommit_Snapshot(), CallSubXactCallbacks(), CommandCounterIncrement(), CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, TransactionStateData::gucNestLevel, IsInParallelMode(), TransactionStateData::nestingLevel, TransactionStateData::parallelModeLevel, TransactionStateData::parent, PopTransaction(), TransactionStateData::prevXactReadOnly, RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), ResourceOwnerRelease(), ShowTransactionState(), TransactionStateData::state, TransactionStateData::subTransactionId, SUBXACT_EVENT_COMMIT_SUB, SUBXACT_EVENT_PRE_COMMIT_SUB, TRANS_COMMIT, TRANS_DEFAULT, TRANS_INPROGRESS, TransactionStateData::transactionId, TransactionIdIsValid, TransStateAsString(), WARNING, XactLockTableDelete(), and XactReadOnly.

Referenced by CommitTransactionCommand(), and ReleaseCurrentSubTransaction().

4582 {
4584 
4585  ShowTransactionState("CommitSubTransaction");
4586 
4587  if (s->state != TRANS_INPROGRESS)
4588  elog(WARNING, "CommitSubTransaction while in %s state",
4590 
4591  /* Pre-commit processing goes here */
4592 
4594  s->parent->subTransactionId);
4595 
4596  /* If in parallel mode, clean up workers and exit parallel mode. */
4597  if (IsInParallelMode())
4598  {
4600  s->parallelModeLevel = 0;
4601  }
4602 
4603  /* Do the actual "commit", such as it is */
4604  s->state = TRANS_COMMIT;
4605 
4606  /* Must CCI to ensure commands of subtransaction are seen as done */
4608 
4609  /*
4610  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4611  * perform that step, if required, as part of the atomic update of the
4612  * whole transaction tree at top level commit or abort.
4613  */
4614 
4615  /* Post-commit cleanup */
4618  AfterTriggerEndSubXact(true);
4623  s->parent->subTransactionId);
4625 
4627  s->parent->subTransactionId);
4628 
4631  true, false);
4633  s->parent->subTransactionId);
4634  AtEOSubXact_Inval(true);
4635  AtSubCommit_smgr();
4636 
4637  /*
4638  * The only lock we actually release here is the subtransaction XID lock.
4639  */
4643 
4644  /*
4645  * Other locks should get transferred to their parent resource owner.
4646  */
4649  true, false);
4652  true, false);
4653 
4654  AtEOXact_GUC(true, s->gucNestLevel);
4657  s->parent->subTransactionId);
4659  s->parent->subTransactionId);
4661  s->parent->subTransactionId);
4663  AtEOSubXact_PgStat(true, s->nestingLevel);
4665 
4666  /*
4667  * We need to restore the upper transaction's read-only state, in case the
4668  * upper is read-write while the child is read-only; GUC will incorrectly
4669  * think it should leave the child state in place.
4670  */
4672 
4676  s->curTransactionOwner = NULL;
4677 
4679 
4680  s->state = TRANS_DEFAULT;
4681 
4682  PopTransaction();
4683 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3075
static TransactionState CurrentTransactionState
Definition: xact.c:233
static void AtSubCommit_Memory(void)
Definition: xact.c:1404
static void PopTransaction(void)
Definition: xact.c:4926
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void AtEOSubXact_HashTables(bool isCommit, int nestDepth)
Definition: dynahash.c:1856
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:673
void AtSubCommit_smgr(void)
Definition: storage.c:451
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:216
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:1002
static void AtSubCommit_childXids(void)
Definition: xact.c:1433
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
bool IsInParallelMode(void)
Definition: xact.c:906
struct TransactionStateData * parent
Definition: xact.c:192
void AtSubCommit_Notify(void)
Definition: async.c:1639
static void ShowTransactionState(const char *str)
Definition: xact.c:5096
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5088
SubTransactionId subTransactionId
Definition: xact.c:174
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2136
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:683
#define WARNING
Definition: elog.h:40
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:13022
void CommandCounterIncrement(void)
Definition: xact.c:915
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2618
TransactionId transactionId
Definition: xact.c:173
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3392
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:191
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5200
bool prevXactReadOnly
Definition: xact.c:188
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4842
void AtEOSubXact_Inval(bool isCommit)
Definition: inval.c:988
void XactLockTableDelete(TransactionId xid)
Definition: lmgr.c:530
#define elog
Definition: elog.h:219
void AtEOSubXact_Namespace(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: namespace.c:3943
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:916
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:867
static void CommitTransaction ( void  )
static

Definition at line 1933 of file xact.c.

References AfterTriggerEndXact(), AfterTriggerFireDeferred(), Assert, AtCommit_Memory(), AtCommit_Notify(), AtEOXact_ApplyLauncher(), AtEOXact_Buffers(), AtEOXact_ComboCid(), AtEOXact_Files(), AtEOXact_GUC(), AtEOXact_HashTables(), AtEOXact_Inval(), AtEOXact_LargeObject(), AtEOXact_MultiXact(), AtEOXact_Namespace(), AtEOXact_on_commit_actions(), AtEOXact_Parallel(), AtEOXact_PgStat(), AtEOXact_RelationCache(), AtEOXact_RelationMap(), AtEOXact_SMgr(), AtEOXact_Snapshot(), AtEOXact_SPI(), TransactionStateData::blockState, CallXactCallbacks(), TransactionStateData::childXids, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, EnterParallelMode(), TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, InvalidSubTransactionId, InvalidTransactionId, IsInParallelMode(), PGPROC::lxid, TransactionStateData::maxChildXids, MyProc, TransactionStateData::nChildXids, TransactionStateData::nestingLevel, nParallelCurrentXids, TransactionStateData::parallelModeLevel, ParallelWorkerReportLastRecEnd(), TransactionStateData::parent, pgstat_report_xact_timestamp(), PreCommit_CheckForSerializationFailure(), PreCommit_Notify(), PreCommit_on_commit_actions(), PreCommit_Portals(), ProcArrayEndTransaction(), RecordTransactionCommit(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), ResourceOwnerRelease(), RESUME_INTERRUPTS, ShowTransactionState(), smgrDoPendingDeletes(), TransactionStateData::state, TransactionStateData::subTransactionId, TBLOCK_PARALLEL_INPROGRESS, TopTransactionResourceOwner, TRANS_COMMIT, TRANS_DEFAULT, TRANS_INPROGRESS, TransactionStateData::transactionId, TransStateAsString(), WARNING, XACT_EVENT_COMMIT, XACT_EVENT_PARALLEL_COMMIT, XACT_EVENT_PARALLEL_PRE_COMMIT, XACT_EVENT_PRE_COMMIT, XactLastRecEnd, and XactTopTransactionId.

Referenced by CommitTransactionCommand(), EndParallelWorkerTransaction(), EndRestoreBlobs(), restore_toc_entry(), and RestoreArchive().

1934 {
1936  TransactionId latestXid;
1937  bool is_parallel_worker;
1938 
1939  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
1940 
1941  /* Enforce parallel mode restrictions during parallel worker commit. */
1942  if (is_parallel_worker)
1944 
1945  ShowTransactionState("CommitTransaction");
1946 
1947  /*
1948  * check the current transaction state
1949  */
1950  if (s->state != TRANS_INPROGRESS)
1951  elog(WARNING, "CommitTransaction while in %s state",
1953  Assert(s->parent == NULL);
1954 
1955  /*
1956  * Do pre-commit processing that involves calling user-defined code, such
1957  * as triggers. Since closing cursors could queue trigger actions,
1958  * triggers could open cursors, etc, we have to keep looping until there's
1959  * nothing left to do.
1960  */
1961  for (;;)
1962  {
1963  /*
1964  * Fire all currently pending deferred triggers.
1965  */
1967 
1968  /*
1969  * Close open portals (converting holdable ones into static portals).
1970  * If there weren't any, we are done ... otherwise loop back to check
1971  * if they queued deferred triggers. Lather, rinse, repeat.
1972  */
1973  if (!PreCommit_Portals(false))
1974  break;
1975  }
1976 
1979 
1980  /*
1981  * The remaining actions cannot call any user-defined code, so it's safe
1982  * to start shutting down within-transaction services. But note that most
1983  * of this stuff could still throw an error, which would switch us into
1984  * the transaction-abort path.
1985  */
1986 
1987  /* If we might have parallel workers, clean them up now. */
1988  if (IsInParallelMode())
1989  AtEOXact_Parallel(true);
1990 
1991  /* Shut down the deferred-trigger manager */
1992  AfterTriggerEndXact(true);
1993 
1994  /*
1995  * Let ON COMMIT management do its thing (must happen after closing
1996  * cursors, to avoid dangling-reference problems)
1997  */
1999 
2000  /* close large objects before lower-level cleanup */
2001  AtEOXact_LargeObject(true);
2002 
2003  /*
2004  * Mark serializable transaction as complete for predicate locking
2005  * purposes. This should be done as late as we can put it and still allow
2006  * errors to be raised for failure patterns found at commit.
2007  */
2009 
2010  /*
2011  * Insert notifications sent by NOTIFY commands into the queue. This
2012  * should be late in the pre-commit sequence to minimize time spent
2013  * holding the notify-insertion lock.
2014  */
2015  PreCommit_Notify();
2016 
2017  /* Prevent cancel/die interrupt while cleaning up */
2018  HOLD_INTERRUPTS();
2019 
2020  /* Commit updates to the relation map --- do this as late as possible */
2021  AtEOXact_RelationMap(true);
2022 
2023  /*
2024  * set the current transaction state information appropriately during
2025  * commit processing
2026  */
2027  s->state = TRANS_COMMIT;
2028  s->parallelModeLevel = 0;
2029 
2030  if (!is_parallel_worker)
2031  {
2032  /*
2033  * We need to mark our XIDs as committed in pg_xact. This is where we
2034  * durably commit.
2035  */
2036  latestXid = RecordTransactionCommit();
2037  }
2038  else
2039  {
2040  /*
2041  * We must not mark our XID committed; the parallel master is
2042  * responsible for that.
2043  */
2044  latestXid = InvalidTransactionId;
2045 
2046  /*
2047  * Make sure the master will know about any WAL we wrote before it
2048  * commits.
2049  */
2051  }
2052 
2053  TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
2054 
2055  /*
2056  * Let others know about no transaction in progress by me. Note that this
2057  * must be done _before_ releasing locks we hold and _after_
2058  * RecordTransactionCommit.
2059  */
2060  ProcArrayEndTransaction(MyProc, latestXid);
2061 
2062  /*
2063  * This is all post-commit cleanup. Note that if an error is raised here,
2064  * it's too late to abort the transaction. This should be just
2065  * noncritical resource releasing.
2066  *
2067  * The ordering of operations is not entirely random. The idea is:
2068  * release resources visible to other backends (eg, files, buffer pins);
2069  * then release locks; then release backend-local resources. We want to
2070  * release locks at the point where any backend waiting for us will see
2071  * our transaction as being fully cleaned up.
2072  *
2073  * Resources that can be associated with individual queries are handled by
2074  * the ResourceOwner mechanism. The other calls here are for backend-wide
2075  * state.
2076  */
2077 
2078  CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
2079  : XACT_EVENT_COMMIT);
2080 
2083  true, true);
2084 
2085  /* Check we've released all buffer pins */
2086  AtEOXact_Buffers(true);
2087 
2088  /* Clean up the relation cache */
2089  AtEOXact_RelationCache(true);
2090 
2091  /*
2092  * Make catalog changes visible to all backends. This has to happen after
2093  * relcache references are dropped (see comments for
2094  * AtEOXact_RelationCache), but before locks are released (if anyone is
2095  * waiting for lock on a relation we've modified, we want them to know
2096  * about the catalog change before they start using the relation).
2097  */
2098  AtEOXact_Inval(true);
2099 
2101 
2104  true, true);
2107  true, true);
2108 
2109  /*
2110  * Likewise, dropping of files deleted during the transaction is best done
2111  * after releasing relcache and buffer pins. (This is not strictly
2112  * necessary during commit, since such pins should have been released
2113  * already, but this ordering is definitely critical during abort.) Since
2114  * this may take many seconds, also delay until after releasing locks.
2115  * Other backends will observe the attendant catalog changes and not
2116  * attempt to access affected files.
2117  */
2118  smgrDoPendingDeletes(true);
2119 
2120  AtCommit_Notify();
2121  AtEOXact_GUC(true, 1);
2122  AtEOXact_SPI(true);
2124  AtEOXact_Namespace(true, is_parallel_worker);
2125  AtEOXact_SMgr();
2126  AtEOXact_Files();
2128  AtEOXact_HashTables(true);
2129  AtEOXact_PgStat(true);
2130  AtEOXact_Snapshot(true, false);
2131  AtEOXact_ApplyLauncher(true);
2133 
2134  CurrentResourceOwner = NULL;
2136  s->curTransactionOwner = NULL;
2139 
2140  AtCommit_Memory();
2141 
2144  s->nestingLevel = 0;
2145  s->gucNestLevel = 0;
2146  s->childXids = NULL;
2147  s->nChildXids = 0;
2148  s->maxChildXids = 0;
2149 
2152 
2153  /*
2154  * done with commit processing, set current transaction state back to
2155  * default
2156  */
2157  s->state = TRANS_DEFAULT;
2158 
2160 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:833
TransactionId XactTopTransactionId
Definition: xact.c:107
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:306
static TransactionState CurrentTransactionState
Definition: xact.c:233
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:929
uint32 TransactionId
Definition: c.h:391
static void AtCommit_Memory(void)
Definition: xact.c:1377
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3196
XLogRecPtr XactLastRecEnd
Definition: xlog.c:339
void AtEOXact_RelationCache(bool isCommit)
Definition: relcache.c:2924
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2415
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void AtEOXact_RelationMap(bool isCommit)
Definition: relmapper.c:457
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:673
static void CallXactCallbacks(XactEvent event)
Definition: xact.c:3337
int nParallelCurrentXids
Definition: xact.c:108
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:12980
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:396
static TransactionId RecordTransactionCommit(void)
Definition: xact.c:1118
TBlockState blockState
Definition: xact.c:178
TransactionId * childXids
Definition: xact.c:183
void PreCommit_CheckForSerializationFailure(void)
Definition: predicate.c:4712
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
void PreCommit_Notify(void)
Definition: async.c:775
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
void AtEOXact_SMgr(void)
Definition: smgr.c:798
bool IsInParallelMode(void)
Definition: xact.c:906
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4746
void AtEOXact_Files(void)
Definition: fd.c:2649
struct TransactionStateData * parent
Definition: xact.c:192
static void ShowTransactionState(const char *str)
Definition: xact.c:5096
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5088
#define InvalidTransactionId
Definition: transam.h:31
SubTransactionId subTransactionId
Definition: xact.c:174
#define WARNING
Definition: elog.h:40
TransactionId transactionId
Definition: xact.c:173
void ParallelWorkerReportLastRecEnd(XLogRecPtr last_xlog_end)
Definition: parallel.c:1161
void AtEOXact_ComboCid(void)
Definition: combocid.c:183
void AfterTriggerFireDeferred(void)
Definition: trigger.c:4690
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:681
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5200
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:188
void AtEOXact_MultiXact(void)
Definition: multixact.c:1662
void EnterParallelMode(void)
Definition: xact.c:873
#define InvalidSubTransactionId
Definition: c.h:397
bool PreCommit_Portals(bool isPrepare)
Definition: portalmem.c:630
void PreCommit_on_commit_actions(void)
Definition: tablecmds.c:12909
void AtCommit_Notify(void)
Definition: async.c:874
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:115
#define elog
Definition: elog.h:219
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1830
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:642
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:935
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:3888
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1059
void AtEOXact_PgStat(bool isCommit)
Definition: pgstat.c:2053
LocalTransactionId lxid
Definition: proc.h:106
void CommitTransactionCommand ( void  )

Definition at line 2744 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, TransactionStateData::parent, PrepareTransaction(), TransactionStateData::savepointLevel, StartSubTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, and TBLOCK_SUBRESTART.

Referenced by apply_handle_commit(), ApplyWorkerMain(), autoprewarm_database_main(), 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().

2745 {
2747 
2748  switch (s->blockState)
2749  {
2750  /*
2751  * These shouldn't happen. TBLOCK_DEFAULT means the previous
2752  * StartTransactionCommand didn't set the STARTED state
2753  * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
2754  * by EndParallelWorkerTransaction(), not this function.
2755  */
2756  case TBLOCK_DEFAULT:
2758  elog(FATAL, "CommitTransactionCommand: unexpected state %s",
2760  break;
2761 
2762  /*
2763  * If we aren't in a transaction block, just do our usual
2764  * transaction commit, and return to the idle state.
2765  */
2766  case TBLOCK_STARTED:
2769  break;
2770 
2771  /*
2772  * We are completing a "BEGIN TRANSACTION" command, so we change
2773  * to the "transaction block in progress" state and return. (We
2774  * assume the BEGIN did nothing to the database, so we need no
2775  * CommandCounterIncrement.)
2776  */
2777  case TBLOCK_BEGIN:
2779  break;
2780 
2781  /*
2782  * This is the case when we have finished executing a command
2783  * someplace within a transaction block. We increment the command
2784  * counter and return.
2785  */
2786  case TBLOCK_INPROGRESS:
2788  case TBLOCK_SUBINPROGRESS:
2790  break;
2791 
2792  /*
2793  * We are completing a "COMMIT" command. Do it and return to the
2794  * idle state.
2795  */
2796  case TBLOCK_END:
2799  break;
2800 
2801  /*
2802  * Here we are in the middle of a transaction block but one of the
2803  * commands caused an abort so we do nothing but remain in the
2804  * abort state. Eventually we will get a ROLLBACK command.
2805  */
2806  case TBLOCK_ABORT:
2807  case TBLOCK_SUBABORT:
2808  break;
2809 
2810  /*
2811  * Here we were in an aborted transaction block and we just got
2812  * the ROLLBACK command from the user, so clean up the
2813  * already-aborted transaction and return to the idle state.
2814  */
2815  case TBLOCK_ABORT_END:
2818  break;
2819 
2820  /*
2821  * Here we were in a perfectly good transaction block but the user
2822  * told us to ROLLBACK anyway. We have to abort the transaction
2823  * and then clean up.
2824  */
2825  case TBLOCK_ABORT_PENDING:
2826  AbortTransaction();
2829  break;
2830 
2831  /*
2832  * We are completing a "PREPARE TRANSACTION" command. Do it and
2833  * return to the idle state.
2834  */
2835  case TBLOCK_PREPARE:
2838  break;
2839 
2840  /*
2841  * We were just issued a SAVEPOINT inside a transaction block.
2842  * Start a subtransaction. (DefineSavepoint already did
2843  * PushTransaction, so as to have someplace to put the SUBBEGIN
2844  * state.)
2845  */
2846  case TBLOCK_SUBBEGIN:
2849  break;
2850 
2851  /*
2852  * We were issued a RELEASE command, so we end the current
2853  * subtransaction and return to the parent transaction. The parent
2854  * might be ended too, so repeat till we find an INPROGRESS
2855  * transaction or subtransaction.
2856  */
2857  case TBLOCK_SUBRELEASE:
2858  do
2859  {
2861  s = CurrentTransactionState; /* changed by pop */
2862  } while (s->blockState == TBLOCK_SUBRELEASE);
2863 
2866  break;
2867 
2868  /*
2869  * We were issued a COMMIT, so we end the current subtransaction
2870  * hierarchy and perform final commit. We do this by rolling up
2871  * any subtransactions into their parent, which leads to O(N^2)
2872  * operations with respect to resource owners - this isn't that
2873  * bad until we approach a thousands of savepoints but is
2874  * necessary for correctness should after triggers create new
2875  * resource owners.
2876  */
2877  case TBLOCK_SUBCOMMIT:
2878  do
2879  {
2881  s = CurrentTransactionState; /* changed by pop */
2882  } while (s->blockState == TBLOCK_SUBCOMMIT);
2883  /* If we had a COMMIT command, finish off the main xact too */
2884  if (s->blockState == TBLOCK_END)
2885  {
2886  Assert(s->parent == NULL);
2889  }
2890  else if (s->blockState == TBLOCK_PREPARE)
2891  {
2892  Assert(s->parent == NULL);
2895  }
2896  else
2897  elog(ERROR, "CommitTransactionCommand: unexpected state %s",
2899  break;
2900 
2901  /*
2902  * The current already-failed subtransaction is ending due to a
2903  * ROLLBACK or ROLLBACK TO command, so pop it and recursively
2904  * examine the parent (which could be in any of several states).
2905  */
2906  case TBLOCK_SUBABORT_END:
2909  break;
2910 
2911  /*
2912  * As above, but it's not dead yet, so abort first.
2913  */
2918  break;
2919 
2920  /*
2921  * The current subtransaction is the target of a ROLLBACK TO
2922  * command. Abort and pop it, then start a new subtransaction
2923  * with the same name.
2924  */
2925  case TBLOCK_SUBRESTART:
2926  {
2927  char *name;
2928  int savepointLevel;
2929 
2930  /* save name and keep Cleanup from freeing it */
2931  name = s->name;
2932  s->name = NULL;
2933  savepointLevel = s->savepointLevel;
2934 
2937 
2938  DefineSavepoint(NULL);
2939  s = CurrentTransactionState; /* changed by push */
2940  s->name = name;
2941  s->savepointLevel = savepointLevel;
2942 
2943  /* This is the same as TBLOCK_SUBBEGIN case */
2947  }
2948  break;
2949 
2950  /*
2951  * Same as above, but the subtransaction had already failed, so we
2952  * don't need AbortSubTransaction.
2953  */
2955  {
2956  char *name;
2957  int savepointLevel;
2958 
2959  /* save name and keep Cleanup from freeing it */
2960  name = s->name;
2961  s->name = NULL;
2962  savepointLevel = s->savepointLevel;
2963 
2965 
2966  DefineSavepoint(NULL);
2967  s = CurrentTransactionState; /* changed by push */
2968  s->name = name;
2969  s->savepointLevel = savepointLevel;
2970 
2971  /* This is the same as TBLOCK_SUBBEGIN case */
2975  }
2976  break;
2977  }
2978 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
#define AssertState(condition)
Definition: c.h:684
void CommitTransactionCommand(void)
Definition: xact.c:2744
TBlockState blockState
Definition: xact.c:178
static void PrepareTransaction(void)
Definition: xact.c:2169
#define ERROR
Definition: elog.h:43
static void CommitSubTransaction(void)
Definition: xact.c:4581
#define FATAL
Definition: elog.h:52
static void CleanupTransaction(void)
Definition: xact.c:2624
struct TransactionStateData * parent
Definition: xact.c:192
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5147
static void AbortSubTransaction(void)
Definition: xact.c:4689
void DefineSavepoint(char *name)
Definition: xact.c:3826
void CommandCounterIncrement(void)
Definition: xact.c:915
static void AbortTransaction(void)
Definition: xact.c:2444
#define Assert(condition)
Definition: c.h:681
static void CleanupSubTransaction(void)
Definition: xact.c:4834
const char * name
Definition: encode.c:521
static void CommitTransaction(void)
Definition: xact.c:1933
#define elog
Definition: elog.h:219
static void StartSubTransaction(void)
Definition: xact.c:4543
void DefineSavepoint ( char *  name)

Definition at line 3826 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), ERROR, FATAL, IsInParallelMode(), MemoryContextStrdup(), TransactionStateData::name, PushTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and TopTransactionContext.

Referenced by CommitTransactionCommand(), and standard_ProcessUtility().

3827 {
3829 
3830  /*
3831  * Workers synchronize transaction state at the beginning of each parallel
3832  * operation, so we can't account for new subtransactions after that
3833  * point. (Note that this check will certainly error out if s->blockState
3834  * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
3835  * below.)
3836  */
3837  if (IsInParallelMode())
3838  ereport(ERROR,
3839  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3840  errmsg("cannot define savepoints during a parallel operation")));
3841 
3842  switch (s->blockState)
3843  {
3844  case TBLOCK_INPROGRESS:
3845  case TBLOCK_SUBINPROGRESS:
3846  /* Normal subtransaction start */
3847  PushTransaction();
3848  s = CurrentTransactionState; /* changed by push */
3849 
3850  /*
3851  * Savepoint names, like the TransactionState block itself, live
3852  * in TopTransactionContext.
3853  */
3854  if (name)
3856  break;
3857 
3858  /*
3859  * We disallow savepoint commands in implicit transaction blocks.
3860  * There would be no great difficulty in allowing them so far as
3861  * this module is concerned, but a savepoint seems inconsistent
3862  * with exec_simple_query's behavior of abandoning the whole query
3863  * string upon error. Also, the point of an implicit transaction
3864  * block (as opposed to a regular one) is to automatically close
3865  * after an error, so it's hard to see how a savepoint would fit
3866  * into that.
3867  *
3868  * The error messages for this are phrased as if there were no
3869  * active transaction block at all, which is historical but
3870  * perhaps could be improved.
3871  */
3873  ereport(ERROR,
3874  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3875  /* translator: %s represents an SQL statement name */
3876  errmsg("%s can only be used in transaction blocks",
3877  "SAVEPOINT")));
3878  break;
3879 
3880  /* These cases are invalid. */
3881  case TBLOCK_DEFAULT:
3882  case TBLOCK_STARTED:
3883  case TBLOCK_BEGIN:
3885  case TBLOCK_SUBBEGIN:
3886  case TBLOCK_END:
3887  case TBLOCK_SUBRELEASE:
3888  case TBLOCK_SUBCOMMIT:
3889  case TBLOCK_ABORT:
3890  case TBLOCK_SUBABORT:
3891  case TBLOCK_ABORT_END:
3892  case TBLOCK_SUBABORT_END:
3893  case TBLOCK_ABORT_PENDING:
3895  case TBLOCK_SUBRESTART:
3897  case TBLOCK_PREPARE:
3898  elog(FATAL, "DefineSavepoint: unexpected state %s",
3900  break;
3901  }
3902 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
MemoryContext TopTransactionContext
Definition: mcxt.c:48
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:178
bool IsInParallelMode(void)
Definition: xact.c:906
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5147
#define ereport(elevel, rest)
Definition: elog.h:122
static void PushTransaction(void)
Definition: xact.c:4867
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:1063
#define elog
Definition: elog.h:219
void EndImplicitTransactionBlock ( void  )

Definition at line 3804 of file xact.c.

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

Referenced by exec_simple_query().

3805 {
3807 
3808  /*
3809  * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
3810  * allowing CommitTransactionCommand to commit whatever happened during
3811  * the implicit transaction block as though it were a single statement.
3812  *
3813  * For caller convenience, we consider all other transaction states as
3814  * legal here; otherwise the caller would need its own state check, which
3815  * seems rather pointless.
3816  */
3819 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TBlockState blockState
Definition: xact.c:178
void EndParallelWorkerTransaction ( void  )

Definition at line 5084 of file xact.c.

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

Referenced by ParallelWorkerMain().

5085 {
5089 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TBlockState blockState
Definition: xact.c:178
#define Assert(condition)
Definition: c.h:681
static void CommitTransaction(void)
Definition: xact.c:1933
bool EndTransactionBlock ( void  )

Definition at line 3533 of file xact.c.

References TransactionStateData::blockState, BlockStateAsString(), CurrentTransactionState, elog, ereport, errcode(), errmsg(), FATAL, TransactionStateData::parent, TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, TBLOCK_SUBRESTART, and WARNING.

Referenced by PrepareTransactionBlock(), and standard_ProcessUtility().

3534 {
3536  bool result = false;
3537 
3538  switch (s->blockState)
3539  {
3540  /*
3541  * We are in a transaction block, so tell CommitTransactionCommand
3542  * to COMMIT.
3543  */
3544  case TBLOCK_INPROGRESS:
3545  s->blockState = TBLOCK_END;
3546  result = true;
3547  break;
3548 
3549  /*
3550  * In an implicit transaction block, commit, but issue a warning
3551  * because there was no explicit BEGIN before this.
3552  */
3554  ereport(WARNING,
3555  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3556  errmsg("there is no transaction in progress")));
3557  s->blockState = TBLOCK_END;
3558  result = true;
3559  break;
3560 
3561  /*
3562  * We are in a failed transaction block. Tell
3563  * CommitTransactionCommand it's time to exit the block.
3564  */
3565  case TBLOCK_ABORT:
3567  break;
3568 
3569  /*
3570  * We are in a live subtransaction block. Set up to subcommit all
3571  * open subtransactions and then commit the main transaction.
3572  */
3573  case TBLOCK_SUBINPROGRESS:
3574  while (s->parent != NULL)
3575  {
3576  if (s->blockState == TBLOCK_SUBINPROGRESS)
3578  else
3579  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3581  s = s->parent;
3582  }
3583  if (s->blockState == TBLOCK_INPROGRESS)
3584  s->blockState = TBLOCK_END;
3585  else
3586  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3588  result = true;
3589  break;
3590 
3591  /*
3592  * Here we are inside an aborted subtransaction. Treat the COMMIT
3593  * as ROLLBACK: set up to abort everything and exit the main
3594  * transaction.
3595  */
3596  case TBLOCK_SUBABORT:
3597  while (s->parent != NULL)
3598  {
3599  if (s->blockState == TBLOCK_SUBINPROGRESS)
3601  else if (s->blockState == TBLOCK_SUBABORT)
3603  else
3604  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3606  s = s->parent;
3607  }
3608  if (s->blockState == TBLOCK_INPROGRESS)
3610  else if (s->blockState == TBLOCK_ABORT)
3612  else
3613  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3615  break;
3616 
3617  /*
3618  * The user issued COMMIT when not inside a transaction. Issue a
3619  * WARNING, staying in TBLOCK_STARTED state. The upcoming call to
3620  * CommitTransactionCommand() will then close the transaction and
3621  * put us back into the default state.
3622  */
3623  case TBLOCK_STARTED:
3624  ereport(WARNING,
3625  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3626  errmsg("there is no transaction in progress")));
3627  result = true;
3628  break;
3629 
3630  /*
3631  * The user issued a COMMIT that somehow ran inside a parallel
3632  * worker. We can't cope with that.
3633  */
3635  ereport(FATAL,
3636  (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3637  errmsg("cannot commit during a parallel operation")));
3638  break;
3639 
3640  /* These cases are invalid. */
3641  case TBLOCK_DEFAULT:
3642  case TBLOCK_BEGIN:
3643  case TBLOCK_SUBBEGIN:
3644  case TBLOCK_END:
3645  case TBLOCK_SUBRELEASE:
3646  case TBLOCK_SUBCOMMIT:
3647  case TBLOCK_ABORT_END:
3648  case TBLOCK_SUBABORT_END:
3649  case TBLOCK_ABORT_PENDING:
3651  case TBLOCK_SUBRESTART:
3653  case TBLOCK_PREPARE:
3654  elog(FATAL, "EndTransactionBlock: unexpected state %s",
3656  break;
3657  }
3658 
3659  return result;
3660 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
int errcode(int sqlerrcode)
Definition: elog.c:575
TBlockState blockState
Definition: xact.c:178
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:192
static const char * BlockStateAsString(TBlockState blockState)
Definition: xact.c:5147
#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 EnterParallelMode ( void  )

Definition at line 873 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

874 {
876 
877  Assert(s->parallelModeLevel >= 0);
878 
879  ++s->parallelModeLevel;
880 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:681
Size EstimateTransactionStateSpace ( void  )

Definition at line 4960 of file xact.c.

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

Referenced by InitializeParallelDSM().

4961 {
4962  TransactionState s;
4963  Size nxids = 6; /* iso level, deferrable, top & current XID,
4964  * command counter, XID count */
4965 
4966  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4967  {
4969  nxids = add_size(nxids, 1);
4970  nxids = add_size(nxids, s->nChildXids);
4971  }
4972 
4973  nxids = add_size(nxids, nParallelCurrentXids);
4974  return mul_size(nxids, sizeof(TransactionId));
4975 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
uint32 TransactionId
Definition: c.h:391
int nParallelCurrentXids
Definition: xact.c:108
struct TransactionStateData * parent
Definition: xact.c:192
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:173
size_t Size
Definition: c.h:350
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ExitParallelMode ( void  )

Definition at line 886 of file xact.c.

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

Referenced by ExecutePlan(), and ParallelWorkerMain().

887 {
889 
890  Assert(s->parallelModeLevel > 0);
892 
893  --s->parallelModeLevel;
894 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
bool ParallelContextActive(void)
Definition: parallel.c:698
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:681
void ForceSyncCommit ( void  )

Definition at line 963 of file xact.c.

References forceSyncCommit.

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

964 {
965  forceSyncCommit = true;
966 }
static bool forceSyncCommit
Definition: xact.c:263
CommandId GetCurrentCommandId ( bool  used)

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

681 {
682  /* this is global to a transaction, not subtransaction-local */
683  if (used)
684  {
685  /*
686  * Forbid setting currentCommandIdUsed in parallel mode, because we
687  * have no provision for communicating this back to the master. We
688  * could relax this restriction when currentCommandIdUsed was already
689  * true at the start of the parallel operation.
690  */
692  currentCommandIdUsed = true;
693  }
694  return currentCommandId;
695 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
int parallelModeLevel
Definition: xact.c:191
#define Assert(condition)
Definition: c.h:681
static CommandId currentCommandId
Definition: xact.c:240
static bool currentCommandIdUsed
Definition: xact.c:241
TimestampTz GetCurrentStatementStartTimestamp ( void  )

Definition at line 710 of file xact.c.

References stmtStartTimestamp.

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

711 {
712  return stmtStartTimestamp;
713 }
static TimestampTz stmtStartTimestamp
Definition: xact.c:251
TransactionId GetCurrentTransactionIdIfAny ( void  )

Definition at line 435 of file xact.c.

References TransactionStateData::transactionId.

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

436 {
438 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TransactionId transactionId
Definition: xact.c:173
TimestampTz GetCurrentTransactionStartTimestamp ( void  )
TimestampTz GetCurrentTransactionStopTimestamp ( void  )

Definition at line 722 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

723 {
724  if (xactStopTimestamp != 0)
725  return xactStopTimestamp;
726  return GetCurrentTimestamp();
727 }
static TimestampTz xactStopTimestamp
Definition: xact.c:252
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
TransactionId GetStableLatestTransactionId ( void  )

Definition at line 462 of file xact.c.

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

Referenced by xid_age().

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

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

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

Definition at line 3268 of file xact.c.

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

Referenced by vacuum().

3269 {
3270  /*
3271  * Return true on same conditions that would make PreventTransactionChain
3272  * error out
3273  */
3274  if (IsTransactionBlock())
3275  return true;
3276 
3277  if (IsSubTransaction())
3278  return true;
3279 
3280  if (!isTopLevel)
3281  return true;
3282 
3285  return true;
3286 
3287  return false;
3288 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TBlockState blockState
Definition: xact.c:178
bool IsTransactionBlock(void)
Definition: xact.c:4447
bool IsSubTransaction(void)
Definition: xact.c:4520
bool IsTransactionBlock ( void  )

Definition at line 4447 of file xact.c.

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

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

4448 {
4450 
4452  return false;
4453 
4454  return true;
4455 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TBlockState blockState
Definition: xact.c:178
bool IsTransactionOrTransactionBlock ( void  )

Definition at line 4465 of file xact.c.

References TransactionStateData::blockState, CurrentTransactionState, and TBLOCK_DEFAULT.

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

4466 {
4468 
4469  if (s->blockState == TBLOCK_DEFAULT)
4470  return false;
4471 
4472  return true;
4473 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TBlockState blockState
Definition: xact.c:178
bool IsTransactionState ( void  )

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

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

Definition at line 446 of file xact.c.

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

Referenced by XLogInsertRecord().

447 {
450 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
TransactionId transactionId
Definition: xact.c:173
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void PopTransaction ( void  )
static

Definition at line 4926 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, CurTransactionContext, TransactionStateData::curTransactionContext, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, FATAL, MemoryContextSwitchTo(), TransactionStateData::name, TransactionStateData::parent, pfree(), TransactionStateData::state, TRANS_DEFAULT, TransStateAsString(), and WARNING.

Referenced by CleanupSubTransaction(), and CommitSubTransaction().

4927 {
4929 
4930  if (s->state != TRANS_DEFAULT)
4931  elog(WARNING, "PopTransaction while in %s state",
4933 
4934  if (s->parent == NULL)
4935  elog(FATAL, "PopTransaction with no parent");
4936 
4938 
4939  /* Let's just make sure CurTransactionContext is good */
4942 
4943  /* Ditto for ResourceOwner links */
4946 
4947  /* Free the old child structure */
4948  if (s->name)
4949  pfree(s->name);
4950  pfree(s);
4951 }
static TransactionState CurrentTransactionState
Definition: xact.c:233
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurTransactionContext
Definition: mcxt.c:49
TransState state
Definition: xact.c:177
ResourceOwner curTransactionOwner
Definition: xact.c:182
void pfree(void *pointer)
Definition: mcxt.c:949
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:192
MemoryContext curTransactionContext
Definition: xact.c:181
#define WARNING
Definition: elog.h:40
static const char * TransStateAsString(TransState state)
Definition: xact.c:5200
#define elog
Definition: elog.h:219
static void PrepareTransaction ( void  )
static

Definition at line 2169 of file xact.c.

References AfterTriggerEndXact(), AfterTriggerFireDeferred(), Assert, AtCommit_Memory(), AtEOXact_Buffers(), AtEOXact_ComboCid(), AtEOXact_Files(), AtEOXact_GUC(), AtEOXact_HashTables(), AtEOXact_LargeObject(), AtEOXact_Namespace(), AtEOXact_on_commit_actions(), AtEOXact_RelationCache(), AtEOXact_SMgr(), AtEOXact_Snapshot(), AtEOXact_SPI(), AtPrepare_Locks(), AtPrepare_MultiXact(), AtPrepare_Notify(), AtPrepare_PgStat(), AtPrepare_PredicateLocks(), AtPrepare_RelationMap(), BufmgrCommit(), CallXactCallbacks(), TransactionStateData::childXids, CurrentResourceOwner, CurrentTransactionState, TransactionStateData::curTransactionOwner, CurTransactionResourceOwner, elog, EndPrepare(), ereport, errcode(), errmsg(), ERROR, GetCurrentTimestamp(), GetCurrentTransactionId(), GetUserId(), TransactionStateData::gucNestLevel, HOLD_INTERRUPTS, InvalidSubTransactionId, InvalidTransactionId, IsInParallelMode(), MarkAsPreparing(), TransactionStateData::maxChildXids, MyDatabaseId, MyProc, MyXactFlags, TransactionStateData::nChildXids, TransactionStateData::nestingLevel, nParallelCurrentXids, TransactionStateData::parent, pgstat_report_xact_timestamp(), PostPrepare_Inval(), PostPrepare_Locks(), PostPrepare_MultiXact(), PostPrepare_PgStat(), PostPrepare_PredicateLocks(), PostPrepare_smgr(), PostPrepare_Twophase(), PreCommit_CheckForSerializationFailure(), PreCommit_on_commit_actions(), PreCommit_Portals(), prepareGID, ProcArrayClearTransaction(), RESOURCE_RELEASE_AFTER_LOCKS, RESOURCE_RELEASE_BEFORE_LOCKS, RESOURCE_RELEASE_LOCKS, ResourceOwnerDelete(), ResourceOwnerRelease(), RESUME_INTERRUPTS, ShowTransactionState(), StartPrepare(), TransactionStateData::state, TransactionStateData::subTransactionId, TopTransactionResourceOwner, TRANS_DEFAULT, TRANS_INPROGRESS, TRANS_PREPARE, TransactionStateData::transactionId, TransStateAsString(), WARNING, XACT_EVENT_PRE_PREPARE, XACT_EVENT_PREPARE, XACT_FLAGS_ACCESSEDTEMPREL, XactHasExportedSnapshots(), XactLastRecEnd, XactManipulatesLogicalReplicationWorkers(), and XactTopTransactionId.

Referenced by CommitTransactionCommand().

2170 {
2173  GlobalTransaction gxact;
2174  TimestampTz prepared_at;
2175 
2177 
2178  ShowTransactionState("PrepareTransaction");
2179 
2180  /*
2181  * check the current transaction state
2182  */
2183  if (s->state != TRANS_INPROGRESS)
2184  elog(WARNING, "PrepareTransaction while in %s state",
2186  Assert(s->parent == NULL);
2187 
2188  /*
2189  * Do pre-commit processing that involves calling user-defined code, such
2190  * as triggers. Since closing cursors could queue trigger actions,
2191  * triggers could open cursors, etc, we have to keep looping until there's
2192  * nothing left to do.
2193  */
2194  for (;;)
2195  {
2196  /*
2197  * Fire all currently pending deferred triggers.
2198  */
2200 
2201  /*
2202  * Close open portals (converting holdable ones into static portals).
2203  * If there weren't any, we are done ... otherwise loop back to check
2204  * if they queued deferred triggers. Lather, rinse, repeat.
2205  */
2206  if (!PreCommit_Portals(true))
2207  break;
2208  }
2209 
2211 
2212  /*
2213  * The remaining actions cannot call any user-defined code, so it's safe
2214  * to start shutting down within-transaction services. But note that most
2215  * of this stuff could still throw an error, which would switch us into
2216  * the transaction-abort path.
2217  */
2218 
2219  /* Shut down the deferred-trigger manager */
2220  AfterTriggerEndXact(true);
2221 
2222  /*
2223  * Let ON COMMIT management do its thing (must happen after closing
2224  * cursors, to avoid dangling-reference problems)
2225  */
2227 
2228  /* close large objects before lower-level cleanup */
2229  AtEOXact_LargeObject(true);
2230 
2231  /*
2232  * Mark serializable transaction as complete for predicate locking
2233  * purposes. This should be done as late as we can put it and still allow
2234  * errors to be raised for failure patterns found at commit.
2235  */
2237 
2238  /* NOTIFY will be handled below */
2239 
2240  /*
2241  * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
2242  * this transaction. Having the prepared xact hold locks on another
2243  * backend's temp table seems a bad idea --- for instance it would prevent
2244  * the backend from exiting. There are other problems too, such as how to
2245  * clean up the source backend's local buffers and ON COMMIT state if the
2246  * prepared xact includes a DROP of a temp table.
2247  *
2248  * We must check this after executing any ON COMMIT actions, because they
2249  * might still access a temp relation.
2250  *
2251  * XXX In principle this could be relaxed to allow some useful special
2252  * cases, such as a temp table created and dropped all within the
2253  * transaction. That seems to require much more bookkeeping though.
2254  */
2256  ereport(ERROR,
2257  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2258  errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
2259 
2260  /*
2261  * Likewise, don't allow PREPARE after pg_export_snapshot. This could be
2262  * supported if we added cleanup logic to twophase.c, but for now it
2263  * doesn't seem worth the trouble.
2264  */
2266  ereport(ERROR,
2267  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2268  errmsg("cannot PREPARE a transaction that has exported snapshots")));
2269 
2270  /*
2271  * Don't allow PREPARE but for transaction that has/might kill logical
2272  * replication workers.
2273  */
2275  ereport(ERROR,
2276  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2277  errmsg("cannot PREPARE a transaction that has manipulated logical replication workers")));
2278 
2279  /* Prevent cancel/die interrupt while cleaning up */
2280  HOLD_INTERRUPTS();
2281 
2282  /*
2283  * set the current transaction state information appropriately during
2284  * prepare processing
2285  */
2286  s->state = TRANS_PREPARE;
2287 
2288  prepared_at = GetCurrentTimestamp();
2289 
2290  /* Tell bufmgr and smgr to prepare for commit */
2291  BufmgrCommit();
2292 
2293  /*
2294  * Reserve the GID for this transaction. This could fail if the requested
2295  * GID is invalid or already in use.
2296  */
2297  gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
2298  GetUserId(), MyDatabaseId);
2299  prepareGID = NULL;
2300 
2301  /*
2302  * Collect data for the 2PC state file. Note that in general, no actual
2303  * state change should happen in the called modules during this step,
2304  * since it's still possible to fail before commit, and in that case we
2305  * want transaction abort to be able to clean up. (In particular, the
2306  * AtPrepare routines may error out if they find cases they cannot
2307  * handle.) State cleanup should happen in the PostPrepare routines
2308  * below. However, some modules can go ahead and clear state here because
2309  * they wouldn't do anything with it during abort anyway.
2310  *
2311  * Note: because the 2PC state file records will be replayed in the same
2312  * order they are made, the order of these calls has to match the order in
2313  * which we want things to happen during COMMIT PREPARED or ROLLBACK
2314  * PREPARED; in particular, pay attention to whether things should happen
2315  * before or after releasing the transaction's locks.
2316  */
2317  StartPrepare(gxact);
2318 
2319  AtPrepare_Notify();
2320  AtPrepare_Locks();
2322  AtPrepare_PgStat();
2325 
2326  /*
2327  * Here is where we really truly prepare.
2328  *
2329  * We have to record transaction prepares even if we didn't make any
2330  * updates, because the transaction manager might get confused if we lose
2331  * a global transaction.
2332  */
2333  EndPrepare(gxact);
2334 
2335  /*
2336  * Now we clean up backend-internal state and release internal resources.
2337  */
2338 
2339  /* Reset XactLastRecEnd until the next transaction writes something */
2340  XactLastRecEnd = 0;
2341 
2342  /*
2343  * Let others know about no transaction in progress by me. This has to be
2344  * done *after* the prepared transaction has been marked valid, else
2345  * someone may think it is unlocked and recyclable.
2346  */
2348 
2349  /*
2350  * In normal commit-processing, this is all non-critical post-transaction
2351  * cleanup. When the transaction is prepared, however, it's important
2352  * that the locks and other per-backend resources are transferred to the
2353  * prepared transaction's PGPROC entry. Note that if an error is raised
2354  * here, it's too late to abort the transaction. XXX: This probably should
2355  * be in a critical section, to force a PANIC if any of this fails, but
2356  * that cure could be worse than the disease.
2357  */
2358 
2360 
2363  true, true);
2364 
2365  /* Check we've released all buffer pins */
2366  AtEOXact_Buffers(true);
2367 
2368  /* Clean up the relation cache */
2369  AtEOXact_RelationCache(true);
2370 
2371