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_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 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 194 of file xact.c.

Enumeration Type Documentation

Enumerator
TBLOCK_DEFAULT 
TBLOCK_STARTED 
TBLOCK_BEGIN 
TBLOCK_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_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
149  TBLOCK_END, /* COMMIT received */
150  TBLOCK_ABORT, /* failed xact, awaiting ROLLBACK */
151  TBLOCK_ABORT_END, /* failed xact, ROLLBACK received */
152  TBLOCK_ABORT_PENDING, /* live xact, ROLLBACK received */
153  TBLOCK_PREPARE, /* live xact, PREPARE received */
154 
155  /* subtransaction states */
156  TBLOCK_SUBBEGIN, /* starting a subtransaction */
157  TBLOCK_SUBINPROGRESS, /* live subtransaction */
158  TBLOCK_SUBRELEASE, /* RELEASE received */
159  TBLOCK_SUBCOMMIT, /* COMMIT received while TBLOCK_SUBINPROGRESS */
160  TBLOCK_SUBABORT, /* failed subxact, awaiting ROLLBACK */
161  TBLOCK_SUBABORT_END, /* failed subxact, ROLLBACK received */
162  TBLOCK_SUBABORT_PENDING, /* live subxact, ROLLBACK received */
163  TBLOCK_SUBRESTART, /* live subxact, ROLLBACK TO received */
164  TBLOCK_SUBABORT_RESTART /* failed subxact, ROLLBACK TO received */
165 } 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 2989 of file xact.c.

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

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

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

Definition at line 4225 of file xact.c.

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

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

4226 {
4228 
4229  /* Ensure we're not running in a doomed memory context */
4230  AtAbort_Memory();
4231 
4232  /*
4233  * Get out of any transaction or nested transaction
4234  */
4235  do
4236  {
4237  switch (s->blockState)
4238  {
4239  case TBLOCK_DEFAULT:
4240  if (s->state == TRANS_DEFAULT)
4241  {
4242  /* Not in a transaction, do nothing */
4243  }
4244  else
4245  {
4246  /*
4247  * We can get here after an error during transaction start
4248  * (state will be TRANS_START). Need to clean up the
4249  * incompletely started transaction. First, adjust the
4250  * low-level state to suppress warning message from
4251  * AbortTransaction.
4252  */
4253  if (s->state == TRANS_START)
4254  s->state = TRANS_INPROGRESS;
4255  AbortTransaction();
4257  }
4258  break;
4259  case TBLOCK_STARTED:
4260  case TBLOCK_BEGIN:
4261  case TBLOCK_INPROGRESS:
4263  case TBLOCK_END:
4264  case TBLOCK_ABORT_PENDING:
4265  case TBLOCK_PREPARE:
4266  /* In a transaction, so clean up */
4267  AbortTransaction();
4270  break;
4271  case TBLOCK_ABORT:
4272  case TBLOCK_ABORT_END:
4273 
4274  /*
4275  * AbortTransaction is already done, still need Cleanup.
4276  * However, if we failed partway through running ROLLBACK,
4277  * there will be an active portal running that command, which
4278  * we need to shut down before doing CleanupTransaction.
4279  */
4280  AtAbort_Portals();
4283  break;
4284 
4285  /*
4286  * In a subtransaction, so clean it up and abort parent too
4287  */
4288  case TBLOCK_SUBBEGIN:
4289  case TBLOCK_SUBINPROGRESS:
4290  case TBLOCK_SUBRELEASE:
4291  case TBLOCK_SUBCOMMIT:
4293  case TBLOCK_SUBRESTART:
4296  s = CurrentTransactionState; /* changed by pop */
4297  break;
4298 
4299  case TBLOCK_SUBABORT:
4300  case TBLOCK_SUBABORT_END:
4302  /* As above, but AbortSubTransaction already done */
4303  if (s->curTransactionOwner)
4304  {
4305  /* As in TBLOCK_ABORT, might have a live portal to zap */
4310  }
4312  s = CurrentTransactionState; /* changed by pop */
4313  break;
4314  }
4315  } while (s->blockState != TBLOCK_DEFAULT);
4316 
4317  /* Should be out of all subxacts now */
4318  Assert(s->parent == NULL);
4319 
4320  /* If we didn't actually have anything to do, revert to TopMemoryContext */
4321  AtCleanup_Memory();
4322 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
static void AtCleanup_Memory(void)
Definition: xact.c:1731
void AtAbort_Portals(void)
Definition: portalmem.c:746
TBlockState blockState
Definition: xact.c:177
TransState state
Definition: xact.c:176
ResourceOwner curTransactionOwner
Definition: xact.c:181
static void CleanupTransaction(void)
Definition: xact.c:2631
struct TransactionStateData * parent
Definition: xact.c:191
SubTransactionId subTransactionId
Definition: xact.c:173
static void AbortSubTransaction(void)
Definition: xact.c:4569
static void AtAbort_Memory(void)
Definition: xact.c:1641
static void AbortTransaction(void)
Definition: xact.c:2451
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner myXactOwner, ResourceOwner parentXactOwner)
Definition: portalmem.c:901
static void CleanupSubTransaction(void)
Definition: xact.c:4714
static void AbortSubTransaction ( void  )
static

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

4570 {
4572 
4573  /* Prevent cancel/die interrupt while cleaning up */
4574  HOLD_INTERRUPTS();
4575 
4576  /* Make sure we have a valid memory context and resource owner */
4579 
4580  /*
4581  * Release any LW locks we might be holding as quickly as possible.
4582  * (Regular locks, however, must be held till we finish aborting.)
4583  * Releasing LW locks is critical since we might try to grab them again
4584  * while cleaning up!
4585  *
4586  * FIXME This may be incorrect --- Are there some locks we should keep?
4587  * Buffer locks, for example? I don't think so but I'm not sure.
4588  */
4589  LWLockReleaseAll();
4590 
4593  AbortBufferIO();
4594  UnlockBuffers();
4595 
4596  /* Reset WAL record construction state */
4598 
4599  /*
4600  * Also clean up any open wait for lock, since the lock manager will choke
4601  * if we try to wait for another lock before doing this.
4602  */
4603  LockErrorCleanup();
4604 
4605  /*
4606  * If any timeout events are still active, make sure the timeout interrupt
4607  * is scheduled. This covers possible loss of a timeout interrupt due to
4608  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
4609  * We delay this till after LockErrorCleanup so that we don't uselessly
4610  * reschedule lock or deadlock check timeouts.
4611  */
4613 
4614  /*
4615  * Re-enable signals, in case we got here by longjmp'ing out of a signal
4616  * handler. We do this fairly early in the sequence so that the timeout
4617  * infrastructure will be functional if needed while aborting.
4618  */
4620 
4621  /*
4622  * check the current transaction state
4623  */
4624  ShowTransactionState("AbortSubTransaction");
4625 
4626  if (s->state != TRANS_INPROGRESS)
4627  elog(WARNING, "AbortSubTransaction while in %s state",
4629 
4630  s->state = TRANS_ABORT;
4631 
4632  /*
4633  * Reset user ID which might have been changed transiently. (See notes in
4634  * AbortTransaction.)
4635  */
4637 
4638  /* Exit from parallel mode, if necessary. */
4639  if (IsInParallelMode())
4640  {
4642  s->parallelModeLevel = 0;
4643  }
4644 
4645  /*
4646  * We can skip all this stuff if the subxact failed before creating a
4647  * ResourceOwner...
4648  */
4649  if (s->curTransactionOwner)
4650  {
4651  AfterTriggerEndSubXact(false);
4657  s->parent->subTransactionId);
4659 
4660  /* Advertise the fact that we aborted in pg_xact. */
4661  (void) RecordTransactionAbort(true);
4662 
4663  /* Post-abort cleanup */
4666 
4668  s->parent->subTransactionId);
4669 
4672  false, false);
4674  s->parent->subTransactionId);
4675  AtEOSubXact_Inval(false);
4678  false, false);
4681  false, false);
4682  AtSubAbort_smgr();
4683 
4684  AtEOXact_GUC(false, s->gucNestLevel);
4685  AtEOSubXact_SPI(false, s->subTransactionId);
4687  s->parent->subTransactionId);
4689  s->parent->subTransactionId);
4691  s->parent->subTransactionId);
4693  AtEOSubXact_PgStat(false, s->nestingLevel);
4695  }
4696 
4697  /*
4698  * Restore the upper transaction's read-only state, too. This should be
4699  * redundant with GUC's cleanup but we may as well do it for consistency
4700  * with the commit case.
4701  */
4703 
4705 }
static void AtSubAbort_ResourceOwner(void)
Definition: xact.c:1686
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3075
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:215
#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:176
ResourceOwner curTransactionOwner
Definition: xact.c:181
bool IsInParallelMode(void)
Definition: xact.c:913
static void AtSubAbort_Memory(void)
Definition: xact.c:1661
void XLogResetInsertion(void)
Definition: xloginsert.c:193
struct TransactionStateData * parent
Definition: xact.c:191
static void ShowTransactionState(const char *str)
Definition: xact.c:4976
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5090
SubTransactionId subTransactionId
Definition: xact.c:173
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void AtSubAbort_childXids(void)
Definition: xact.c:1699
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1235
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2136
void LockErrorCleanup(void)
Definition: proc.c:677
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:674
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3518
void AtSubAbort_Notify(void)
Definition: async.c:1666
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:12940
void pgstat_progress_end_command(void)
Definition: pgstat.c:3140
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2586
TransactionId transactionId
Definition: xact.c:172
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3397
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:190
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5078
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:187
void AbortBufferIO(void)
Definition: bufmgr.c:3974
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4619
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:1814
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void AtEOSubXact_Parallel(bool isCommit, SubTransactionId mySubId)
Definition: parallel.c:886
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1530
static void AbortTransaction ( void  )
static

Definition at line 2451 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, NULL, 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().

2452 {
2454  TransactionId latestXid;
2455  bool is_parallel_worker;
2456 
2457  /* Prevent cancel/die interrupt while cleaning up */
2458  HOLD_INTERRUPTS();
2459 
2460  /* Make sure we have a valid memory context and resource owner */
2461  AtAbort_Memory();
2463 
2464  /*
2465  * Release any LW locks we might be holding as quickly as possible.
2466  * (Regular locks, however, must be held till we finish aborting.)
2467  * Releasing LW locks is critical since we might try to grab them again
2468  * while cleaning up!
2469  */
2470  LWLockReleaseAll();
2471 
2472  /* Clear wait information and command progress indicator */
2475 
2476  /* Clean up buffer I/O and buffer context locks, too */
2477  AbortBufferIO();
2478  UnlockBuffers();
2479 
2480  /* Reset WAL record construction state */
2482 
2483  /* Cancel condition variable sleep */
2485 
2486  /*
2487  * Also clean up any open wait for lock, since the lock manager will choke
2488  * if we try to wait for another lock before doing this.
2489  */
2490  LockErrorCleanup();
2491 
2492  /*
2493  * If any timeout events are still active, make sure the timeout interrupt
2494  * is scheduled. This covers possible loss of a timeout interrupt due to
2495  * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
2496  * We delay this till after LockErrorCleanup so that we don't uselessly
2497  * reschedule lock or deadlock check timeouts.
2498  */
2500 
2501  /*
2502  * Re-enable signals, in case we got here by longjmp'ing out of a signal
2503  * handler. We do this fairly early in the sequence so that the timeout
2504  * infrastructure will be functional if needed while aborting.
2505  */
2507 
2508  /*
2509  * check the current transaction state
2510  */
2511  is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
2512  if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
2513  elog(WARNING, "AbortTransaction while in %s state",
2515  Assert(s->parent == NULL);
2516 
2517  /*
2518  * set the current transaction state information appropriately during the
2519  * abort processing
2520  */
2521  s->state = TRANS_ABORT;
2522 
2523  /*
2524  * Reset user ID which might have been changed transiently. We need this
2525  * to clean up in case control escaped out of a SECURITY DEFINER function
2526  * or other local change of CurrentUserId; therefore, the prior value of
2527  * SecurityRestrictionContext also needs to be restored.
2528  *
2529  * (Note: it is not necessary to restore session authorization or role
2530  * settings here because those can only be changed via GUC, and GUC will
2531  * take care of rolling them back if need be.)
2532  */
2534 
2535  /* If in parallel mode, clean up workers and exit parallel mode. */
2536  if (IsInParallelMode())
2537  {
2538  AtEOXact_Parallel(false);
2539  s->parallelModeLevel = 0;
2540  }
2541 
2542  /*
2543  * do abort processing
2544  */
2545  AfterTriggerEndXact(false); /* 'false' means it's abort */
2546  AtAbort_Portals();
2547  AtEOXact_LargeObject(false);
2548  AtAbort_Notify();
2549  AtEOXact_RelationMap(false);
2550  AtAbort_Twophase();
2551 
2552  /*
2553  * Advertise the fact that we aborted in pg_xact (assuming that we got as
2554  * far as assigning an XID to advertise). But if we're inside a parallel
2555  * worker, skip this; the user backend must be the one to write the abort
2556  * record.
2557  */
2558  if (!is_parallel_worker)
2559  latestXid = RecordTransactionAbort(false);
2560  else
2561  {
2562  latestXid = InvalidTransactionId;
2563 
2564  /*
2565  * Since the parallel master won't get our value of XactLastRecEnd in
2566  * this case, we nudge WAL-writer ourselves in this case. See related
2567  * comments in RecordTransactionAbort for why this matters.
2568  */
2570  }
2571 
2572  TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
2573 
2574  /*
2575  * Let others know about no transaction in progress by me. Note that this
2576  * must be done _before_ releasing locks we hold and _after_
2577  * RecordTransactionAbort.
2578  */
2579  ProcArrayEndTransaction(MyProc, latestXid);
2580 
2581  /*
2582  * Post-abort cleanup. See notes in CommitTransaction() concerning
2583  * ordering. We can skip all of it if the transaction failed before
2584  * creating a resource owner.
2585  */
2587  {
2588  if (is_parallel_worker)
2590  else
2592 
2595  false, true);
2596  AtEOXact_Buffers(false);
2597  AtEOXact_RelationCache(false);
2598  AtEOXact_Inval(false);
2602  false, true);
2605  false, true);
2606  smgrDoPendingDeletes(false);
2607 
2608  AtEOXact_GUC(false, 1);
2609  AtEOXact_SPI(false);
2611  AtEOXact_Namespace(false, is_parallel_worker);
2612  AtEOXact_SMgr();
2613  AtEOXact_Files();
2615  AtEOXact_HashTables(false);
2616  AtEOXact_PgStat(false);
2617  AtEOXact_ApplyLauncher(false);
2619  }
2620 
2621  /*
2622  * State remains TRANS_ABORT until CleanupTransaction().
2623  */
2625 }
void AtEOXact_ApplyLauncher(bool isCommit)
Definition: launcher.c:823
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:306
static void AtAbort_ResourceOwner(void)
Definition: xact.c:1673
static TransactionState CurrentTransactionState
Definition: xact.c:232
void AtEOXact_Inval(bool isCommit)
Definition: inval.c:929
uint32 TransactionId
Definition: c.h:397
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:396
void pgstat_report_xact_timestamp(TimestampTz tstamp)
Definition: pgstat.c:3192
XLogRecPtr XactLastRecEnd
Definition: xlog.c:337
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:3342
void AtEOXact_on_commit_actions(bool isCommit)
Definition: tablecmds.c:12898
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:177
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
TransState state
Definition: xact.c:176
void AtEOXact_SMgr(void)
Definition: smgr.c:798
bool IsInParallelMode(void)
Definition: xact.c:913
void ConditionVariableCancelSleep(void)
void AfterTriggerEndXact(bool isCommit)
Definition: trigger.c:4499
void XLogResetInsertion(void)
Definition: xloginsert.c:193
void AtEOXact_Files(void)
Definition: fd.c:2617
struct TransactionStateData * parent
Definition: xact.c:191
void AtAbort_Twophase(void)
Definition: twophase.c:319
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5090
#define InvalidTransactionId
Definition: transam.h:31
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1235
void LockErrorCleanup(void)
Definition: proc.c:677
#define WARNING
Definition: elog.h:40
void UnlockBuffers(void)
Definition: bufmgr.c:3518
static void AtAbort_Memory(void)
Definition: xact.c:1641
void pgstat_progress_end_command(void)
Definition: pgstat.c:3140
void AtAbort_Notify(void)
Definition: async.c:1584
void AtEOXact_ComboCid(void)
Definition: combocid.c:183
void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
Definition: xlog.c:2607
int parallelModeLevel
Definition: xact.c:190
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5078
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:187
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:1814
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1830
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:633
void AtEOXact_Parallel(bool isCommit)
Definition: parallel.c:905
void AtEOXact_Namespace(bool isCommit, bool parallel)
Definition: namespace.c:3888
static TransactionId RecordTransactionAbort(bool isSubXact)
Definition: xact.c:1530
void AtEOXact_PgStat(bool isCommit)
Definition: pgstat.c:2053
LocalTransactionId lxid
Definition: proc.h:105
static void AssignTransactionId ( TransactionState  s)
static

Definition at line 489 of file xact.c.

References Assert, CurrentResourceOwner, TransactionStateData::curTransactionOwner, TransactionStateData::didLogXid, elog, ERROR, GetNewTransactionId(), GetTopTransactionId(), IsInParallelMode(), IsParallelWorker, MinSizeOfXactAssignment, TransactionStateData::nestingLevel, xl_xact_assignment::nsubxacts, NULL, nUnreportedXids, palloc(), TransactionStateData::parent, pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, 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().

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

Definition at line 1641 of file xact.c.

References MemoryContextSwitchTo(), NULL, and TopMemoryContext.

Referenced by AbortOutOfAnyTransaction(), and AbortTransaction().

1642 {
1643  /*
1644  * Switch into TransactionAbortContext, which should have some free space
1645  * even if nothing else does. We'll work in this context until we've
1646  * finished cleaning up.
1647  *
1648  * It is barely possible to get here when we've not been able to create
1649  * TransactionAbortContext yet; if so use TopMemoryContext.
1650  */
1653  else
1655 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:269
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define NULL
Definition: c.h:229
static void AtAbort_ResourceOwner ( void  )
static

Definition at line 1673 of file xact.c.

References CurrentResourceOwner, and TopTransactionResourceOwner.

Referenced by AbortTransaction().

1674 {
1675  /*
1676  * Make sure we have a valid ResourceOwner, if possible (else it will be
1677  * NULL, which is OK)
1678  */
1680 }
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static void AtCCI_LocalCache ( void  )
static

Definition at line 1365 of file xact.c.

References AtCCI_RelationMap(), and CommandEndInvalidationMessages().

Referenced by CommandCounterIncrement().

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

Definition at line 1731 of file xact.c.

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

Referenced by AbortOutOfAnyTransaction(), and CleanupTransaction().

1732 {
1734 
1735  /*
1736  * Now that we're "out" of a transaction, have the system allocate things
1737  * in the top memory context instead of per-transaction contexts.
1738  */
1740 
1741  /*
1742  * Clear the special abort context for next time.
1743  */
1746 
1747  /*
1748  * Release all transaction-local memory.
1749  */
1750  if (TopTransactionContext != NULL)
1755 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:269
struct TransactionStateData * parent
Definition: xact.c:191
MemoryContext curTransactionContext
Definition: xact.c:180
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void AtCommit_Memory ( void  )
static

Definition at line 1384 of file xact.c.

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

Referenced by CommitTransaction(), and PrepareTransaction().

1385 {
1386  /*
1387  * Now that we're "out" of a transaction, have the system allocate things
1388  * in the top memory context instead of per-transaction contexts.
1389  */
1391 
1392  /*
1393  * Release all transaction-local memory.
1394  */
1400 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:180
MemoryContext TopMemoryContext
Definition: mcxt.c:43
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void AtStart_Cache ( void  )
static

Definition at line 985 of file xact.c.

References AcceptInvalidationMessages().

Referenced by StartTransaction().

986 {
988 }
void AcceptInvalidationMessages(void)
Definition: inval.c:679
static void AtStart_Memory ( void  )
static

Definition at line 994 of file xact.c.

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

Referenced by StartTransaction().

995 {
997 
998  /*
999  * If this is the first time through, create a private context for
1000  * AbortTransaction to work in. By reserving some space now, we can
1001  * insulate AbortTransaction from out-of-memory scenarios. Like
1002  * ErrorContext, we set it up with slow growth rate and a nonzero minimum
1003  * size, so that space will be reserved immediately.
1004  */
1008  "TransactionAbortContext",
1009  32 * 1024,
1010  32 * 1024,
1011  32 * 1024);
1012 
1013  /*
1014  * We shouldn't have a transaction context already.
1015  */
1017 
1018  /*
1019  * Create a toplevel context for the transaction.
1020  */
1023  "TopTransactionContext",
1025 
1026  /*
1027  * In a top-level transaction, CurTransactionContext is the same as
1028  * TopTransactionContext.
1029  */
1032 
1033  /* Make the CurTransactionContext active. */
1035 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:269
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
MemoryContext curTransactionContext
Definition: xact.c:180
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void AtStart_ResourceOwner ( void  )
static

Definition at line 1041 of file xact.c.

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

Referenced by StartTransaction().

1042 {
1044 
1045  /*
1046  * We shouldn't have a transaction resource owner already.
1047  */
1049 
1050  /*
1051  * Create a toplevel resource owner for the transaction.
1052  */
1053  s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
1054 
1058 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:181
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
static void AtSubAbort_childXids ( void  )
static

Definition at line 1699 of file xact.c.

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

Referenced by AbortSubTransaction().

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

Definition at line 1661 of file xact.c.

References Assert, MemoryContextSwitchTo(), and NULL.

Referenced by AbortSubTransaction().

1662 {
1664 
1666 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static MemoryContext TransactionAbortContext
Definition: xact.c:269
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void AtSubAbort_ResourceOwner ( void  )
static

Definition at line 1686 of file xact.c.

References CurrentResourceOwner, CurrentTransactionState, and TransactionStateData::curTransactionOwner.

Referenced by AbortSubTransaction().

1687 {
1689 
1690  /* Make sure we have a valid ResourceOwner */
1692 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:181
static void AtSubCleanup_Memory ( void  )
static

Definition at line 1767 of file xact.c.

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

Referenced by CleanupSubTransaction().

1768 {
1770 
1771  Assert(s->parent != NULL);
1772 
1773  /* Make sure we're not in an about-to-be-deleted context */
1776 
1777  /*
1778  * Clear the special abort context for next time.
1779  */
1782 
1783  /*
1784  * Delete the subxact local memory contexts. Its CurTransactionContext can
1785  * go too (note this also kills CurTransactionContexts from any children
1786  * of the subxact).
1787  */
1788  if (s->curTransactionContext)
1791 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:269
struct TransactionStateData * parent
Definition: xact.c:191
MemoryContext curTransactionContext
Definition: xact.c:180
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void AtSubCommit_childXids ( void  )
static

Definition at line 1440 of file xact.c.

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

Referenced by CommitSubTransaction().

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

Definition at line 1411 of file xact.c.

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

Referenced by CommitSubTransaction().

1412 {
1414 
1415  Assert(s->parent != NULL);
1416 
1417  /* Return to parent transaction level's memory context. */
1420 
1421  /*
1422  * Ordinarily we cannot throw away the child's CurTransactionContext,
1423  * since the data it contains will be needed at upper commit. However, if
1424  * there isn't actually anything in it, we can throw it away. This avoids
1425  * a small memory leak in the common case of "trivial" subxacts.
1426  */
1428  {
1431  }
1432 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:415
struct TransactionStateData * parent
Definition: xact.c:191
MemoryContext curTransactionContext
Definition: xact.c:180
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void AtSubStart_Memory ( void  )
static

Definition at line 1069 of file xact.c.

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

Referenced by StartSubTransaction().

1070 {
1072 
1074 
1075  /*
1076  * Create a CurTransactionContext, which will be used to hold data that
1077  * survives subtransaction commit but disappears on subtransaction abort.
1078  * We make it a child of the immediate parent's CurTransactionContext.
1079  */
1081  "CurTransactionContext",
1084 
1085  /* Make the CurTransactionContext active. */
1087 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:180
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
static void AtSubStart_ResourceOwner ( void  )
static

Definition at line 1093 of file xact.c.

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

Referenced by StartSubTransaction().

1094 {
1096 
1097  Assert(s->parent != NULL);
1098 
1099  /*
1100  * Create a resource owner for the subtransaction. We make it a child of
1101  * the immediate parent's resource owner.
1102  */
1103  s->curTransactionOwner =
1105  "SubTransaction");
1106 
1109 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
ResourceOwner curTransactionOwner
Definition: xact.c:181
struct TransactionStateData * parent
Definition: xact.c:191
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
void BeginInternalSubTransaction ( char *  name)

Definition at line 4056 of file xact.c.

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

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

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

Definition at line 3418 of file xact.c.

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

Referenced by standard_ProcessUtility().

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

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

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

5028 {
5029  switch (blockState)
5030  {
5031  case TBLOCK_DEFAULT:
5032  return "DEFAULT";
5033  case TBLOCK_STARTED:
5034  return "STARTED";
5035  case TBLOCK_BEGIN:
5036  return "BEGIN";
5037  case TBLOCK_INPROGRESS:
5038  return "INPROGRESS";
5040  return "PARALLEL_INPROGRESS";
5041  case TBLOCK_END:
5042  return "END";
5043  case TBLOCK_ABORT:
5044  return "ABORT";
5045  case TBLOCK_ABORT_END:
5046  return "ABORT END";
5047  case TBLOCK_ABORT_PENDING:
5048  return "ABORT PEND";
5049  case TBLOCK_PREPARE:
5050  return "PREPARE";
5051  case TBLOCK_SUBBEGIN:
5052  return "SUB BEGIN";
5053  case TBLOCK_SUBINPROGRESS:
5054  return "SUB INPROGRS";
5055  case TBLOCK_SUBRELEASE:
5056  return "SUB RELEASE";
5057  case TBLOCK_SUBCOMMIT:
5058  return "SUB COMMIT";
5059  case TBLOCK_SUBABORT:
5060  return "SUB ABORT";
5061  case TBLOCK_SUBABORT_END:
5062  return "SUB ABORT END";
5064  return "SUB ABRT PEND";
5065  case TBLOCK_SUBRESTART:
5066  return "SUB RESTART";
5068  return "SUB AB RESTRT";
5069  }
5070  return "UNRECOGNIZED";
5071 }
static void CallSubXactCallbacks ( SubXactEvent  event,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)
static

Definition at line 3397 of file xact.c.

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

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

3400 {
3401  SubXactCallbackItem *item;
3402 
3403  for (item = SubXact_callbacks; item; item = item->next)
3404  (*item->callback) (event, mySubid, parentSubid, item->arg);
3405 }
static SubXactCallbackItem * SubXact_callbacks
Definition: xact.c:293
struct SubXactCallbackItem * next
Definition: xact.c:288
SubXactCallback callback
Definition: xact.c:289
static void CallXactCallbacks ( XactEvent  event)
static

Definition at line 3342 of file xact.c.

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

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

3343 {
3344  XactCallbackItem *item;
3345 
3346  for (item = Xact_callbacks; item; item = item->next)
3347  (*item->callback) (event, item->arg);
3348 }
struct XactCallbackItem * next
Definition: xact.c:276
void * arg
Definition: xact.c:278
XactCallback callback
Definition: xact.c:277
static XactCallbackItem * Xact_callbacks
Definition: xact.c:281
static void CheckTransactionChain ( bool  isTopLevel,
bool  throwError,
const char *  stmtType 
)
static

Definition at line 3234 of file xact.c.

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

Referenced by RequireTransactionChain(), and WarnNoTransactionChain().

3235 {
3236  /*
3237  * xact block already started?
3238  */
3239  if (IsTransactionBlock())
3240  return;
3241 
3242  /*
3243  * subtransaction?
3244  */
3245  if (IsSubTransaction())
3246  return;
3247 
3248  /*
3249  * inside a function call?
3250  */
3251  if (!isTopLevel)
3252  return;
3253 
3254  ereport(throwError ? ERROR : WARNING,
3255  (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
3256  /* translator: %s represents an SQL statement name */
3257  errmsg("%s can only be used in transaction blocks",
3258  stmtType)));
3259  return;
3260 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool IsTransactionBlock(void)
Definition: xact.c:4328
#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:4400
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void CleanupSubTransaction ( void  )
static

Definition at line 4714 of file xact.c.

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

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

4715 {
4717 
4718  ShowTransactionState("CleanupSubTransaction");
4719 
4720  if (s->state != TRANS_ABORT)
4721  elog(WARNING, "CleanupSubTransaction while in %s state",
4723 
4725 
4728  if (s->curTransactionOwner)
4731 
4733 
4734  s->state = TRANS_DEFAULT;
4735 
4736  PopTransaction();
4737 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
static void PopTransaction(void)
Definition: xact.c:4806
ResourceOwner CurTransactionResourceOwner
Definition: resowner.c:139
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:687
TransState state
Definition: xact.c:176
ResourceOwner curTransactionOwner
Definition: xact.c:181
struct TransactionStateData * parent
Definition: xact.c:191
static void ShowTransactionState(const char *str)
Definition: xact.c:4976
SubTransactionId subTransactionId
Definition: xact.c:173
void AtSubCleanup_Portals(SubTransactionId mySubid)
Definition: portalmem.c:1014
#define WARNING
Definition: elog.h:40
static void AtSubCleanup_Memory(void)
Definition: xact.c:1767
#define NULL
Definition: c.h:229
static const char * TransStateAsString(TransState state)
Definition: xact.c:5078
#define elog
Definition: elog.h:219
static void CleanupTransaction ( void  )
static

Definition at line 2631 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, NULL, TransactionStateData::parallelModeLevel, ResourceOwnerDelete(), TransactionStateData::state, TransactionStateData::subTransactionId, TopTransactionResourceOwner, TRANS_ABORT, TRANS_DEFAULT, TransactionStateData::transactionId, TransStateAsString(), and XactTopTransactionId.

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

2632 {
2634 
2635  /*
2636  * State should still be TRANS_ABORT from AbortTransaction().
2637  */
2638  if (s->state != TRANS_ABORT)
2639  elog(FATAL, "CleanupTransaction: unexpected state %s",
2641 
2642  /*
2643  * do abort cleanup processing
2644  */
2645  AtCleanup_Portals(); /* now safe to release portal memory */
2646  AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
2647 
2648  CurrentResourceOwner = NULL; /* and resource owner */
2654 
2655  AtCleanup_Memory(); /* and transaction memory */
2656 
2659  s->nestingLevel = 0;
2660  s->gucNestLevel = 0;
2661  s->childXids = NULL;
2662  s->nChildXids = 0;
2663  s->maxChildXids = 0;
2664  s->parallelModeLevel = 0;
2665 
2668 
2669  /*
2670  * done with abort processing, set current transaction state back to
2671  * default
2672  */
2673  s->state = TRANS_DEFAULT;
2674 }
TransactionId XactTopTransactionId
Definition: xact.c:107
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:687
static void AtCleanup_Memory(void)
Definition: xact.c:1731
int nParallelCurrentXids
Definition: xact.c:108
void AtCleanup_Portals(void)
Definition: portalmem.c:818
TransactionId * childXids
Definition: xact.c:182
TransState state
Definition: xact.c:176
ResourceOwner curTransactionOwner
Definition: xact.c:181
#define FATAL
Definition: elog.h:52
#define InvalidTransactionId
Definition: transam.h:31
SubTransactionId subTransactionId
Definition: xact.c:173
TransactionId transactionId
Definition: xact.c:172
int parallelModeLevel
Definition: xact.c:190
#define NULL
Definition: c.h:229
static const char * TransStateAsString(TransState state)
Definition: xact.c:5078
#define InvalidSubTransactionId
Definition: c.h:403
#define elog
Definition: elog.h:219
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1059
void CommandCounterIncrement ( void  )

Definition at line 922 of file xact.c.

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

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

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

Definition at line 4461 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, NULL, 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().

4462 {
4464 
4465  ShowTransactionState("CommitSubTransaction");
4466 
4467  if (s->state != TRANS_INPROGRESS)
4468  elog(WARNING, "CommitSubTransaction while in %s state",
4470 
4471  /* Pre-commit processing goes here */
4472 
4474  s->parent->subTransactionId);
4475 
4476  /* If in parallel mode, clean up workers and exit parallel mode. */
4477  if (IsInParallelMode())
4478  {
4480  s->parallelModeLevel = 0;
4481  }
4482 
4483  /* Do the actual "commit", such as it is */
4484  s->state = TRANS_COMMIT;
4485 
4486  /* Must CCI to ensure commands of subtransaction are seen as done */
4488 
4489  /*
4490  * Prior to 8.4 we marked subcommit in clog at this point. We now only
4491  * perform that step, if required, as part of the atomic update of the
4492  * whole transaction tree at top level commit or abort.
4493  */
4494 
4495  /* Post-commit cleanup */
4498  AfterTriggerEndSubXact(true);
4503  s->parent->subTransactionId);
4505 
4507  s->parent->subTransactionId);
4508 
4511  true, false);
4513  s->parent->subTransactionId);
4514  AtEOSubXact_Inval(true);
4515  AtSubCommit_smgr();
4516 
4517  /*
4518  * The only lock we actually release here is the subtransaction XID lock.
4519  */
4523 
4524  /*
4525  * Other locks should get transferred to their parent resource owner.
4526  */
4529  true, false);
4532  true, false);
4533 
4534  AtEOXact_GUC(true, s->gucNestLevel);
4537  s->parent->subTransactionId);
4539  s->parent->subTransactionId);
4541  s->parent->subTransactionId);
4543  AtEOSubXact_PgStat(true, s->nestingLevel);
4545 
4546  /*
4547  * We need to restore the upper transaction's read-only state, in case the
4548  * upper is read-write while the child is read-only; GUC will incorrectly
4549  * think it should leave the child state in place.
4550  */
4552 
4557 
4559 
4560  s->state = TRANS_DEFAULT;
4561 
4562  PopTransaction();
4563 }
void AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: relcache.c:3075
static TransactionState CurrentTransactionState
Definition: xact.c:232
static void AtSubCommit_Memory(void)
Definition: xact.c:1411
static void PopTransaction(void)
Definition: xact.c:4806
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:687
void AtSubCommit_smgr(void)
Definition: storage.c:451
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:215
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:1002
static void AtSubCommit_childXids(void)
Definition: xact.c:1440
TransState state
Definition: xact.c:176
ResourceOwner curTransactionOwner
Definition: xact.c:181
bool IsInParallelMode(void)
Definition: xact.c:913
struct TransactionStateData * parent
Definition: xact.c:191
void AtSubCommit_Notify(void)
Definition: async.c:1634
static void ShowTransactionState(const char *str)
Definition: xact.c:4976
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5090
SubTransactionId subTransactionId
Definition: xact.c:173
void AtEOSubXact_PgStat(bool isCommit, int nestDepth)
Definition: pgstat.c:2136
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:674
#define WARNING
Definition: elog.h:40
void AtEOSubXact_on_commit_actions(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: tablecmds.c:12940
void CommandCounterIncrement(void)
Definition: xact.c:922
void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: fd.c:2586
TransactionId transactionId
Definition: xact.c:172
static void CallSubXactCallbacks(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: xact.c:3397
bool XactReadOnly
Definition: xact.c:77
int parallelModeLevel
Definition: xact.c:190
#define NULL
Definition: c.h:229
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
static const char * TransStateAsString(TransState state)
Definition: xact.c:5078
bool prevXactReadOnly
Definition: xact.c:187
void AfterTriggerEndSubXact(bool isCommit)
Definition: trigger.c:4619
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:886
void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, ResourceOwner parentXactOwner)
Definition: portalmem.c:867
static void CommitTransaction ( void  )
static

Definition at line 1940 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, NULL, 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().

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

Definition at line 2750 of file xact.c.

References AbortSubTransaction(), AbortTransaction(), Assert, AssertState, TransactionStateData::blockState, BlockStateAsString(), CleanupSubTransaction(), CleanupTransaction(), CommandCounterIncrement(), CommitSubTransaction(), CommitTransaction(), CommitTransactionCommand(), CurrentTransactionState, DefineSavepoint(), elog, ERROR, FATAL, TransactionStateData::name, name, NULL, TransactionStateData::parent, PrepareTransaction(), TransactionStateData::savepointLevel, StartSubTransaction(), TBLOCK_ABORT, TBLOCK_ABORT_END, TBLOCK_ABORT_PENDING, TBLOCK_BEGIN, TBLOCK_DEFAULT, TBLOCK_END, TBLOCK_INPROGRESS, TBLOCK_PARALLEL_INPROGRESS, TBLOCK_PREPARE, TBLOCK_STARTED, TBLOCK_SUBABORT, TBLOCK_SUBABORT_END, TBLOCK_SUBABORT_PENDING, TBLOCK_SUBABORT_RESTART, TBLOCK_SUBBEGIN, TBLOCK_SUBCOMMIT, TBLOCK_SUBINPROGRESS, TBLOCK_SUBRELEASE, and TBLOCK_SUBRESTART.

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

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

Definition at line 3751 of file xact.c.

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

Referenced by CommitTransactionCommand(), and standard_ProcessUtility().

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

Definition at line 4964 of file xact.c.

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

Referenced by ParallelWorkerMain().

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

Definition at line 3528 of file xact.c.

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

Referenced by PrepareTransactionBlock(), and standard_ProcessUtility().

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

Definition at line 880 of file xact.c.

References Assert, CurrentTransactionState, and TransactionStateData::parallelModeLevel.

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

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

Definition at line 4840 of file xact.c.

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

Referenced by InitializeParallelDSM().

4841 {
4842  TransactionState s;
4843  Size nxids = 6; /* iso level, deferrable, top & current XID,
4844  * command counter, XID count */
4845 
4846  for (s = CurrentTransactionState; s != NULL; s = s->parent)
4847  {
4849  nxids = add_size(nxids, 1);
4850  nxids = add_size(nxids, s->nChildXids);
4851  }
4852 
4853  nxids = add_size(nxids, nParallelCurrentXids);
4854  return mul_size(nxids, sizeof(TransactionId));
4855 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
uint32 TransactionId
Definition: c.h:397
int nParallelCurrentXids
Definition: xact.c:108
struct TransactionStateData * parent
Definition: xact.c:191
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
TransactionId transactionId
Definition: xact.c:172
#define NULL
Definition: c.h:229
size_t Size
Definition: c.h:356
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ExitParallelMode ( void  )

Definition at line 893 of file xact.c.

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

Referenced by ExecutePlan(), and ParallelWorkerMain().

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

Definition at line 970 of file xact.c.

References forceSyncCommit.

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

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

Definition at line 687 of file xact.c.

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

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

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

Definition at line 717 of file xact.c.

References stmtStartTimestamp.

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

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

Definition at line 434 of file xact.c.

References TransactionStateData::transactionId.

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

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

Definition at line 729 of file xact.c.

References GetCurrentTimestamp(), and xactStopTimestamp.

Referenced by pgstat_report_stat().

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

Definition at line 461 of file xact.c.

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

Referenced by xid_age().

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

Definition at line 389 of file xact.c.

References AssignTransactionId(), TransactionIdIsValid, and XactTopTransactionId.

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

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

Definition at line 3273 of file xact.c.

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

Referenced by vacuum().

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

Definition at line 4328 of file xact.c.

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

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

4329 {
4331 
4333  return false;
4334 
4335  return true;
4336 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
bool IsTransactionOrTransactionBlock ( void  )

Definition at line 4346 of file xact.c.

References TransactionStateData::blockState, CurrentTransactionState, and TBLOCK_DEFAULT.

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

4347 {
4349 
4350  if (s->blockState == TBLOCK_DEFAULT)
4351  return false;
4352 
4353  return true;
4354 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TBlockState blockState
Definition: xact.c:177
bool IsTransactionState ( void  )

Definition at line 350 of file xact.c.

References CurrentTransactionState, TransactionStateData::state, and TRANS_INPROGRESS.

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

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

Definition at line 445 of file xact.c.

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

Referenced by XLogInsertRecord().

446 {
449 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
TransactionId transactionId
Definition: xact.c:172
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static void PopTransaction ( void  )
static

Definition at line 4806 of file xact.c.

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

Referenced by CleanupSubTransaction(), and CommitSubTransaction().

4807 {
4809 
4810  if (s->state != TRANS_DEFAULT)
4811  elog(WARNING, "PopTransaction while in %s state",
4813 
4814  if (s->parent == NULL)
4815  elog(FATAL, "PopTransaction with no parent");
4816 
4818 
4819  /* Let's just make sure CurTransactionContext is good */
4822 
4823  /* Ditto for ResourceOwner links */
4826 
4827  /* Free the old child structure */
4828  if (s->name)
4829  pfree(s->name);
4830  pfree(s);
4831 }
static TransactionState CurrentTransactionState
Definition: xact.c:232
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:176
ResourceOwner curTransactionOwner
Definition: xact.c:181
void pfree(void *pointer)
Definition: mcxt.c:950
#define FATAL
Definition: elog.h:52
struct TransactionStateData * parent
Definition: xact.c:191
MemoryContext curTransactionContext
Definition: xact.c:180
#define WARNING
Definition: elog.h:40
#define NULL
Definition: c.h:229
static const char * TransStateAsString(TransState state)
Definition: xact.c:5078
#define elog
Definition: elog.h:219
static void PrepareTransaction ( void  )
static

Definition at line 2176 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, NULL, 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().

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