LCOV - code coverage report
Current view: top level - src/backend/access/transam - xact.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 1496 1746 85.7 %
Date: 2020-06-01 00:06:26 Functions: 99 106 93.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * xact.c
       4             :  *    top level transaction system support routines
       5             :  *
       6             :  * See src/backend/access/transam/README for more information.
       7             :  *
       8             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       9             :  * Portions Copyright (c) 1994, Regents of the University of California
      10             :  *
      11             :  *
      12             :  * IDENTIFICATION
      13             :  *    src/backend/access/transam/xact.c
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : 
      18             : #include "postgres.h"
      19             : 
      20             : #include <time.h>
      21             : #include <unistd.h>
      22             : 
      23             : #include "access/commit_ts.h"
      24             : #include "access/multixact.h"
      25             : #include "access/parallel.h"
      26             : #include "access/subtrans.h"
      27             : #include "access/transam.h"
      28             : #include "access/twophase.h"
      29             : #include "access/xact.h"
      30             : #include "access/xlog.h"
      31             : #include "access/xloginsert.h"
      32             : #include "access/xlogutils.h"
      33             : #include "catalog/index.h"
      34             : #include "catalog/namespace.h"
      35             : #include "catalog/pg_enum.h"
      36             : #include "catalog/storage.h"
      37             : #include "commands/async.h"
      38             : #include "commands/tablecmds.h"
      39             : #include "commands/trigger.h"
      40             : #include "executor/spi.h"
      41             : #include "libpq/be-fsstubs.h"
      42             : #include "libpq/pqsignal.h"
      43             : #include "miscadmin.h"
      44             : #include "pg_trace.h"
      45             : #include "pgstat.h"
      46             : #include "replication/logical.h"
      47             : #include "replication/logicallauncher.h"
      48             : #include "replication/origin.h"
      49             : #include "replication/syncrep.h"
      50             : #include "replication/walsender.h"
      51             : #include "storage/condition_variable.h"
      52             : #include "storage/fd.h"
      53             : #include "storage/lmgr.h"
      54             : #include "storage/md.h"
      55             : #include "storage/predicate.h"
      56             : #include "storage/proc.h"
      57             : #include "storage/procarray.h"
      58             : #include "storage/sinvaladt.h"
      59             : #include "storage/smgr.h"
      60             : #include "utils/builtins.h"
      61             : #include "utils/catcache.h"
      62             : #include "utils/combocid.h"
      63             : #include "utils/guc.h"
      64             : #include "utils/inval.h"
      65             : #include "utils/memutils.h"
      66             : #include "utils/relmapper.h"
      67             : #include "utils/snapmgr.h"
      68             : #include "utils/timeout.h"
      69             : #include "utils/timestamp.h"
      70             : 
      71             : /*
      72             :  *  User-tweakable parameters
      73             :  */
      74             : int         DefaultXactIsoLevel = XACT_READ_COMMITTED;
      75             : int         XactIsoLevel;
      76             : 
      77             : bool        DefaultXactReadOnly = false;
      78             : bool        XactReadOnly;
      79             : 
      80             : bool        DefaultXactDeferrable = false;
      81             : bool        XactDeferrable;
      82             : 
      83             : int         synchronous_commit = SYNCHRONOUS_COMMIT_ON;
      84             : 
      85             : /*
      86             :  * When running as a parallel worker, we place only a single
      87             :  * TransactionStateData on the parallel worker's state stack, and the XID
      88             :  * reflected there will be that of the *innermost* currently-active
      89             :  * subtransaction in the backend that initiated parallelism.  However,
      90             :  * GetTopTransactionId() and TransactionIdIsCurrentTransactionId()
      91             :  * need to return the same answers in the parallel worker as they would have
      92             :  * in the user backend, so we need some additional bookkeeping.
      93             :  *
      94             :  * XactTopFullTransactionId stores the XID of our toplevel transaction, which
      95             :  * will be the same as TopTransactionStateData.fullTransactionId in an
      96             :  * ordinary backend; but in a parallel backend, which does not have the entire
      97             :  * transaction state, it will instead be copied from the backend that started
      98             :  * the parallel operation.
      99             :  *
     100             :  * nParallelCurrentXids will be 0 and ParallelCurrentXids NULL in an ordinary
     101             :  * backend, but in a parallel backend, nParallelCurrentXids will contain the
     102             :  * number of XIDs that need to be considered current, and ParallelCurrentXids
     103             :  * will contain the XIDs themselves.  This includes all XIDs that were current
     104             :  * or sub-committed in the parent at the time the parallel operation began.
     105             :  * The XIDs are stored sorted in numerical order (not logical order) to make
     106             :  * lookups as fast as possible.
     107             :  */
     108             : FullTransactionId XactTopFullTransactionId = {InvalidTransactionId};
     109             : int         nParallelCurrentXids = 0;
     110             : TransactionId *ParallelCurrentXids;
     111             : 
     112             : /*
     113             :  * Miscellaneous flag bits to record events which occur on the top level
     114             :  * transaction. These flags are only persisted in MyXactFlags and are intended
     115             :  * so we remember to do certain things later on in the transaction. This is
     116             :  * globally accessible, so can be set from anywhere in the code that requires
     117             :  * recording flags.
     118             :  */
     119             : int         MyXactFlags;
     120             : 
     121             : /*
     122             :  *  transaction states - transaction state from server perspective
     123             :  */
     124             : typedef enum TransState
     125             : {
     126             :     TRANS_DEFAULT,              /* idle */
     127             :     TRANS_START,                /* transaction starting */
     128             :     TRANS_INPROGRESS,           /* inside a valid transaction */
     129             :     TRANS_COMMIT,               /* commit in progress */
     130             :     TRANS_ABORT,                /* abort in progress */
     131             :     TRANS_PREPARE               /* prepare in progress */
     132             : } TransState;
     133             : 
     134             : /*
     135             :  *  transaction block states - transaction state of client queries
     136             :  *
     137             :  * Note: the subtransaction states are used only for non-topmost
     138             :  * transactions; the others appear only in the topmost transaction.
     139             :  */
     140             : typedef enum TBlockState
     141             : {
     142             :     /* not-in-transaction-block states */
     143             :     TBLOCK_DEFAULT,             /* idle */
     144             :     TBLOCK_STARTED,             /* running single-query transaction */
     145             : 
     146             :     /* transaction block states */
     147             :     TBLOCK_BEGIN,               /* starting transaction block */
     148             :     TBLOCK_INPROGRESS,          /* live transaction */
     149             :     TBLOCK_IMPLICIT_INPROGRESS, /* live transaction after implicit BEGIN */
     150             :     TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
     151             :     TBLOCK_END,                 /* COMMIT received */
     152             :     TBLOCK_ABORT,               /* failed xact, awaiting ROLLBACK */
     153             :     TBLOCK_ABORT_END,           /* failed xact, ROLLBACK received */
     154             :     TBLOCK_ABORT_PENDING,       /* live xact, ROLLBACK received */
     155             :     TBLOCK_PREPARE,             /* live xact, PREPARE received */
     156             : 
     157             :     /* subtransaction states */
     158             :     TBLOCK_SUBBEGIN,            /* starting a subtransaction */
     159             :     TBLOCK_SUBINPROGRESS,       /* live subtransaction */
     160             :     TBLOCK_SUBRELEASE,          /* RELEASE received */
     161             :     TBLOCK_SUBCOMMIT,           /* COMMIT received while TBLOCK_SUBINPROGRESS */
     162             :     TBLOCK_SUBABORT,            /* failed subxact, awaiting ROLLBACK */
     163             :     TBLOCK_SUBABORT_END,        /* failed subxact, ROLLBACK received */
     164             :     TBLOCK_SUBABORT_PENDING,    /* live subxact, ROLLBACK received */
     165             :     TBLOCK_SUBRESTART,          /* live subxact, ROLLBACK TO received */
     166             :     TBLOCK_SUBABORT_RESTART     /* failed subxact, ROLLBACK TO received */
     167             : } TBlockState;
     168             : 
     169             : /*
     170             :  *  transaction state structure
     171             :  */
     172             : typedef struct TransactionStateData
     173             : {
     174             :     FullTransactionId fullTransactionId;    /* my FullTransactionId */
     175             :     SubTransactionId subTransactionId;  /* my subxact ID */
     176             :     char       *name;           /* savepoint name, if any */
     177             :     int         savepointLevel; /* savepoint level */
     178             :     TransState  state;          /* low-level state */
     179             :     TBlockState blockState;     /* high-level state */
     180             :     int         nestingLevel;   /* transaction nesting depth */
     181             :     int         gucNestLevel;   /* GUC context nesting depth */
     182             :     MemoryContext curTransactionContext;    /* my xact-lifetime context */
     183             :     ResourceOwner curTransactionOwner;  /* my query resources */
     184             :     TransactionId *childXids;   /* subcommitted child XIDs, in XID order */
     185             :     int         nChildXids;     /* # of subcommitted child XIDs */
     186             :     int         maxChildXids;   /* allocated size of childXids[] */
     187             :     Oid         prevUser;       /* previous CurrentUserId setting */
     188             :     int         prevSecContext; /* previous SecurityRestrictionContext */
     189             :     bool        prevXactReadOnly;   /* entry-time xact r/o state */
     190             :     bool        startedInRecovery;  /* did we start in recovery? */
     191             :     bool        didLogXid;      /* has xid been included in WAL record? */
     192             :     int         parallelModeLevel;  /* Enter/ExitParallelMode counter */
     193             :     bool        chain;          /* start a new block after this one */
     194             :     struct TransactionStateData *parent;    /* back link to parent */
     195             : } TransactionStateData;
     196             : 
     197             : typedef TransactionStateData *TransactionState;
     198             : 
     199             : /*
     200             :  * Serialized representation used to transmit transaction state to parallel
     201             :  * workers through shared memory.
     202             :  */
     203             : typedef struct SerializedTransactionState
     204             : {
     205             :     int         xactIsoLevel;
     206             :     bool        xactDeferrable;
     207             :     FullTransactionId topFullTransactionId;
     208             :     FullTransactionId currentFullTransactionId;
     209             :     CommandId   currentCommandId;
     210             :     int         nParallelCurrentXids;
     211             :     TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER];
     212             : } SerializedTransactionState;
     213             : 
     214             : /* The size of SerializedTransactionState, not including the final array. */
     215             : #define SerializedTransactionStateHeaderSize \
     216             :     offsetof(SerializedTransactionState, parallelCurrentXids)
     217             : 
     218             : /*
     219             :  * CurrentTransactionState always points to the current transaction state
     220             :  * block.  It will point to TopTransactionStateData when not in a
     221             :  * transaction at all, or when in a top-level transaction.
     222             :  */
     223             : static TransactionStateData TopTransactionStateData = {
     224             :     .state = TRANS_DEFAULT,
     225             :     .blockState = TBLOCK_DEFAULT,
     226             : };
     227             : 
     228             : /*
     229             :  * unreportedXids holds XIDs of all subtransactions that have not yet been
     230             :  * reported in an XLOG_XACT_ASSIGNMENT record.
     231             :  */
     232             : static int  nUnreportedXids;
     233             : static TransactionId unreportedXids[PGPROC_MAX_CACHED_SUBXIDS];
     234             : 
     235             : static TransactionState CurrentTransactionState = &TopTransactionStateData;
     236             : 
     237             : /*
     238             :  * The subtransaction ID and command ID assignment counters are global
     239             :  * to a whole transaction, so we do not keep them in the state stack.
     240             :  */
     241             : static SubTransactionId currentSubTransactionId;
     242             : static CommandId currentCommandId;
     243             : static bool currentCommandIdUsed;
     244             : 
     245             : /*
     246             :  * xactStartTimestamp is the value of transaction_timestamp().
     247             :  * stmtStartTimestamp is the value of statement_timestamp().
     248             :  * xactStopTimestamp is the time at which we log a commit or abort WAL record.
     249             :  * These do not change as we enter and exit subtransactions, so we don't
     250             :  * keep them inside the TransactionState stack.
     251             :  */
     252             : static TimestampTz xactStartTimestamp;
     253             : static TimestampTz stmtStartTimestamp;
     254             : static TimestampTz xactStopTimestamp;
     255             : 
     256             : /*
     257             :  * GID to be used for preparing the current transaction.  This is also
     258             :  * global to a whole transaction, so we don't keep it in the state stack.
     259             :  */
     260             : static char *prepareGID;
     261             : 
     262             : /*
     263             :  * Some commands want to force synchronous commit.
     264             :  */
     265             : static bool forceSyncCommit = false;
     266             : 
     267             : /* Flag for logging statements in a transaction. */
     268             : bool        xact_is_sampled = false;
     269             : 
     270             : /*
     271             :  * Private context for transaction-abort work --- we reserve space for this
     272             :  * at startup to ensure that AbortTransaction and AbortSubTransaction can work
     273             :  * when we've run out of memory.
     274             :  */
     275             : static MemoryContext TransactionAbortContext = NULL;
     276             : 
     277             : /*
     278             :  * List of add-on start- and end-of-xact callbacks
     279             :  */
     280             : typedef struct XactCallbackItem
     281             : {
     282             :     struct XactCallbackItem *next;
     283             :     XactCallback callback;
     284             :     void       *arg;
     285             : } XactCallbackItem;
     286             : 
     287             : static XactCallbackItem *Xact_callbacks = NULL;
     288             : 
     289             : /*
     290             :  * List of add-on start- and end-of-subxact callbacks
     291             :  */
     292             : typedef struct SubXactCallbackItem
     293             : {
     294             :     struct SubXactCallbackItem *next;
     295             :     SubXactCallback callback;
     296             :     void       *arg;
     297             : } SubXactCallbackItem;
     298             : 
     299             : static SubXactCallbackItem *SubXact_callbacks = NULL;
     300             : 
     301             : 
     302             : /* local function prototypes */
     303             : static void AssignTransactionId(TransactionState s);
     304             : static void AbortTransaction(void);
     305             : static void AtAbort_Memory(void);
     306             : static void AtCleanup_Memory(void);
     307             : static void AtAbort_ResourceOwner(void);
     308             : static void AtCCI_LocalCache(void);
     309             : static void AtCommit_Memory(void);
     310             : static void AtStart_Cache(void);
     311             : static void AtStart_Memory(void);
     312             : static void AtStart_ResourceOwner(void);
     313             : static void CallXactCallbacks(XactEvent event);
     314             : static void CallSubXactCallbacks(SubXactEvent event,
     315             :                                  SubTransactionId mySubid,
     316             :                                  SubTransactionId parentSubid);
     317             : static void CleanupTransaction(void);
     318             : static void CheckTransactionBlock(bool isTopLevel, bool throwError,
     319             :                                   const char *stmtType);
     320             : static void CommitTransaction(void);
     321             : static TransactionId RecordTransactionAbort(bool isSubXact);
     322             : static void StartTransaction(void);
     323             : 
     324             : static void StartSubTransaction(void);
     325             : static void CommitSubTransaction(void);
     326             : static void AbortSubTransaction(void);
     327             : static void CleanupSubTransaction(void);
     328             : static void PushTransaction(void);
     329             : static void PopTransaction(void);
     330             : 
     331             : static void AtSubAbort_Memory(void);
     332             : static void AtSubCleanup_Memory(void);
     333             : static void AtSubAbort_ResourceOwner(void);
     334             : static void AtSubCommit_Memory(void);
     335             : static void AtSubStart_Memory(void);
     336             : static void AtSubStart_ResourceOwner(void);
     337             : 
     338             : static void ShowTransactionState(const char *str);
     339             : static void ShowTransactionStateRec(const char *str, TransactionState state);
     340             : static const char *BlockStateAsString(TBlockState blockState);
     341             : static const char *TransStateAsString(TransState state);
     342             : 
     343             : 
     344             : /* ----------------------------------------------------------------
     345             :  *  transaction state accessors
     346             :  * ----------------------------------------------------------------
     347             :  */
     348             : 
     349             : /*
     350             :  *  IsTransactionState
     351             :  *
     352             :  *  This returns true if we are inside a valid transaction; that is,
     353             :  *  it is safe to initiate database access, take heavyweight locks, etc.
     354             :  */
     355             : bool
     356      845388 : IsTransactionState(void)
     357             : {
     358      845388 :     TransactionState s = CurrentTransactionState;
     359             : 
     360             :     /*
     361             :      * TRANS_DEFAULT and TRANS_ABORT are obviously unsafe states.  However, we
     362             :      * also reject the startup/shutdown states TRANS_START, TRANS_COMMIT,
     363             :      * TRANS_PREPARE since it might be too soon or too late within those
     364             :      * transition states to do anything interesting.  Hence, the only "valid"
     365             :      * state is TRANS_INPROGRESS.
     366             :      */
     367      845388 :     return (s->state == TRANS_INPROGRESS);
     368             : }
     369             : 
     370             : /*
     371             :  *  IsAbortedTransactionBlockState
     372             :  *
     373             :  *  This returns true if we are within an aborted transaction block.
     374             :  */
     375             : bool
     376     1098384 : IsAbortedTransactionBlockState(void)
     377             : {
     378     1098384 :     TransactionState s = CurrentTransactionState;
     379             : 
     380     1098384 :     if (s->blockState == TBLOCK_ABORT ||
     381     1096612 :         s->blockState == TBLOCK_SUBABORT)
     382        2072 :         return true;
     383             : 
     384     1096312 :     return false;
     385             : }
     386             : 
     387             : 
     388             : /*
     389             :  *  GetTopTransactionId
     390             :  *
     391             :  * This will return the XID of the main transaction, assigning one if
     392             :  * it's not yet set.  Be careful to call this only inside a valid xact.
     393             :  */
     394             : TransactionId
     395       32596 : GetTopTransactionId(void)
     396             : {
     397       32596 :     if (!FullTransactionIdIsValid(XactTopFullTransactionId))
     398         616 :         AssignTransactionId(&TopTransactionStateData);
     399       32596 :     return XidFromFullTransactionId(XactTopFullTransactionId);
     400             : }
     401             : 
     402             : /*
     403             :  *  GetTopTransactionIdIfAny
     404             :  *
     405             :  * This will return the XID of the main transaction, if one is assigned.
     406             :  * It will return InvalidTransactionId if we are not currently inside a
     407             :  * transaction, or inside a transaction that hasn't yet been assigned an XID.
     408             :  */
     409             : TransactionId
     410    53378686 : GetTopTransactionIdIfAny(void)
     411             : {
     412    53378686 :     return XidFromFullTransactionId(XactTopFullTransactionId);
     413             : }
     414             : 
     415             : /*
     416             :  *  GetCurrentTransactionId
     417             :  *
     418             :  * This will return the XID of the current transaction (main or sub
     419             :  * transaction), assigning one if it's not yet set.  Be careful to call this
     420             :  * only inside a valid xact.
     421             :  */
     422             : TransactionId
     423    21250688 : GetCurrentTransactionId(void)
     424             : {
     425    21250688 :     TransactionState s = CurrentTransactionState;
     426             : 
     427    21250688 :     if (!FullTransactionIdIsValid(s->fullTransactionId))
     428      272298 :         AssignTransactionId(s);
     429    21250688 :     return XidFromFullTransactionId(s->fullTransactionId);
     430             : }
     431             : 
     432             : /*
     433             :  *  GetCurrentTransactionIdIfAny
     434             :  *
     435             :  * This will return the XID of the current sub xact, if one is assigned.
     436             :  * It will return InvalidTransactionId if we are not currently inside a
     437             :  * transaction, or inside a transaction that hasn't been assigned an XID yet.
     438             :  */
     439             : TransactionId
     440    29395436 : GetCurrentTransactionIdIfAny(void)
     441             : {
     442    29395436 :     return XidFromFullTransactionId(CurrentTransactionState->fullTransactionId);
     443             : }
     444             : 
     445             : /*
     446             :  *  GetTopFullTransactionId
     447             :  *
     448             :  * This will return the FullTransactionId of the main transaction, assigning
     449             :  * one if it's not yet set.  Be careful to call this only inside a valid xact.
     450             :  */
     451             : FullTransactionId
     452        5100 : GetTopFullTransactionId(void)
     453             : {
     454        5100 :     if (!FullTransactionIdIsValid(XactTopFullTransactionId))
     455        4076 :         AssignTransactionId(&TopTransactionStateData);
     456        5100 :     return XactTopFullTransactionId;
     457             : }
     458             : 
     459             : /*
     460             :  *  GetTopFullTransactionIdIfAny
     461             :  *
     462             :  * This will return the FullTransactionId of the main transaction, if one is
     463             :  * assigned.  It will return InvalidFullTransactionId if we are not currently
     464             :  * inside a transaction, or inside a transaction that hasn't yet been assigned
     465             :  * one.
     466             :  */
     467             : FullTransactionId
     468          16 : GetTopFullTransactionIdIfAny(void)
     469             : {
     470          16 :     return XactTopFullTransactionId;
     471             : }
     472             : 
     473             : /*
     474             :  *  GetCurrentFullTransactionId
     475             :  *
     476             :  * This will return the FullTransactionId of the current transaction (main or
     477             :  * sub transaction), assigning one if it's not yet set.  Be careful to call
     478             :  * this only inside a valid xact.
     479             :  */
     480             : FullTransactionId
     481           0 : GetCurrentFullTransactionId(void)
     482             : {
     483           0 :     TransactionState s = CurrentTransactionState;
     484             : 
     485           0 :     if (!FullTransactionIdIsValid(s->fullTransactionId))
     486           0 :         AssignTransactionId(s);
     487           0 :     return s->fullTransactionId;
     488             : }
     489             : 
     490             : /*
     491             :  *  GetCurrentFullTransactionIdIfAny
     492             :  *
     493             :  * This will return the FullTransactionId of the current sub xact, if one is
     494             :  * assigned.  It will return InvalidFullTransactionId if we are not currently
     495             :  * inside a transaction, or inside a transaction that hasn't been assigned one
     496             :  * yet.
     497             :  */
     498             : FullTransactionId
     499           0 : GetCurrentFullTransactionIdIfAny(void)
     500             : {
     501           0 :     return CurrentTransactionState->fullTransactionId;
     502             : }
     503             : 
     504             : /*
     505             :  *  MarkCurrentTransactionIdLoggedIfAny
     506             :  *
     507             :  * Remember that the current xid - if it is assigned - now has been wal logged.
     508             :  */
     509             : void
     510    29371146 : MarkCurrentTransactionIdLoggedIfAny(void)
     511             : {
     512    29371146 :     if (FullTransactionIdIsValid(CurrentTransactionState->fullTransactionId))
     513    28741366 :         CurrentTransactionState->didLogXid = true;
     514    29371146 : }
     515             : 
     516             : 
     517             : /*
     518             :  *  GetStableLatestTransactionId
     519             :  *
     520             :  * Get the transaction's XID if it has one, else read the next-to-be-assigned
     521             :  * XID.  Once we have a value, return that same value for the remainder of the
     522             :  * current transaction.  This is meant to provide the reference point for the
     523             :  * age(xid) function, but might be useful for other maintenance tasks as well.
     524             :  */
     525             : TransactionId
     526         216 : GetStableLatestTransactionId(void)
     527             : {
     528             :     static LocalTransactionId lxid = InvalidLocalTransactionId;
     529             :     static TransactionId stablexid = InvalidTransactionId;
     530             : 
     531         216 :     if (lxid != MyProc->lxid)
     532             :     {
     533           2 :         lxid = MyProc->lxid;
     534           2 :         stablexid = GetTopTransactionIdIfAny();
     535           2 :         if (!TransactionIdIsValid(stablexid))
     536           2 :             stablexid = ReadNewTransactionId();
     537             :     }
     538             : 
     539             :     Assert(TransactionIdIsValid(stablexid));
     540             : 
     541         216 :     return stablexid;
     542             : }
     543             : 
     544             : /*
     545             :  * AssignTransactionId
     546             :  *
     547             :  * Assigns a new permanent FullTransactionId to the given TransactionState.
     548             :  * We do not assign XIDs to transactions until/unless this is called.
     549             :  * Also, any parent TransactionStates that don't yet have XIDs are assigned
     550             :  * one; this maintains the invariant that a child transaction has an XID
     551             :  * following its parent's.
     552             :  */
     553             : static void
     554      277648 : AssignTransactionId(TransactionState s)
     555             : {
     556      277648 :     bool        isSubXact = (s->parent != NULL);
     557             :     ResourceOwner currentOwner;
     558      277648 :     bool        log_unknown_top = false;
     559             : 
     560             :     /* Assert that caller didn't screw up */
     561             :     Assert(!FullTransactionIdIsValid(s->fullTransactionId));
     562             :     Assert(s->state == TRANS_INPROGRESS);
     563             : 
     564             :     /*
     565             :      * Workers synchronize transaction state at the beginning of each parallel
     566             :      * operation, so we can't account for new XIDs at this point.
     567             :      */
     568      277648 :     if (IsInParallelMode() || IsParallelWorker())
     569           0 :         elog(ERROR, "cannot assign XIDs during a parallel operation");
     570             : 
     571             :     /*
     572             :      * Ensure parent(s) have XIDs, so that a child always has an XID later
     573             :      * than its parent.  Mustn't recurse here, or we might get a stack
     574             :      * overflow if we're at the bottom of a huge stack of subtransactions none
     575             :      * of which have XIDs yet.
     576             :      */
     577      277648 :     if (isSubXact && !FullTransactionIdIsValid(s->parent->fullTransactionId))
     578             :     {
     579         416 :         TransactionState p = s->parent;
     580             :         TransactionState *parents;
     581         416 :         size_t      parentOffset = 0;
     582             : 
     583         416 :         parents = palloc(sizeof(TransactionState) * s->nestingLevel);
     584        1074 :         while (p != NULL && !FullTransactionIdIsValid(p->fullTransactionId))
     585             :         {
     586         658 :             parents[parentOffset++] = p;
     587         658 :             p = p->parent;
     588             :         }
     589             : 
     590             :         /*
     591             :          * This is technically a recursive call, but the recursion will never
     592             :          * be more than one layer deep.
     593             :          */
     594        1074 :         while (parentOffset != 0)
     595         658 :             AssignTransactionId(parents[--parentOffset]);
     596             : 
     597         416 :         pfree(parents);
     598             :     }
     599             : 
     600             :     /*
     601             :      * When wal_level=logical, guarantee that a subtransaction's xid can only
     602             :      * be seen in the WAL stream if its toplevel xid has been logged before.
     603             :      * If necessary we log an xact_assignment record with fewer than
     604             :      * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
     605             :      * for a transaction even though it appears in a WAL record, we just might
     606             :      * superfluously log something. That can happen when an xid is included
     607             :      * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
     608             :      * xl_standby_locks.
     609             :      */
     610      277648 :     if (isSubXact && XLogLogicalInfoActive() &&
     611         492 :         !TopTransactionStateData.didLogXid)
     612          36 :         log_unknown_top = true;
     613             : 
     614             :     /*
     615             :      * Generate a new FullTransactionId and record its xid in PG_PROC and
     616             :      * pg_subtrans.
     617             :      *
     618             :      * NB: we must make the subtrans entry BEFORE the Xid appears anywhere in
     619             :      * shared storage other than PG_PROC; because if there's no room for it in
     620             :      * PG_PROC, the subtrans entry is needed to ensure that other backends see
     621             :      * the Xid as "running".  See GetNewTransactionId.
     622             :      */
     623      277648 :     s->fullTransactionId = GetNewTransactionId(isSubXact);
     624      277648 :     if (!isSubXact)
     625      273368 :         XactTopFullTransactionId = s->fullTransactionId;
     626             : 
     627      277648 :     if (isSubXact)
     628        4280 :         SubTransSetParent(XidFromFullTransactionId(s->fullTransactionId),
     629        4280 :                           XidFromFullTransactionId(s->parent->fullTransactionId));
     630             : 
     631             :     /*
     632             :      * If it's a top-level transaction, the predicate locking system needs to
     633             :      * be told about it too.
     634             :      */
     635      277648 :     if (!isSubXact)
     636      273368 :         RegisterPredicateLockingXid(XidFromFullTransactionId(s->fullTransactionId));
     637             : 
     638             :     /*
     639             :      * Acquire lock on the transaction XID.  (We assume this cannot block.) We
     640             :      * have to ensure that the lock is assigned to the transaction's own
     641             :      * ResourceOwner.
     642             :      */
     643      277648 :     currentOwner = CurrentResourceOwner;
     644      277648 :     CurrentResourceOwner = s->curTransactionOwner;
     645             : 
     646      277648 :     XactLockTableInsert(XidFromFullTransactionId(s->fullTransactionId));
     647             : 
     648      277648 :     CurrentResourceOwner = currentOwner;
     649             : 
     650             :     /*
     651             :      * Every PGPROC_MAX_CACHED_SUBXIDS assigned transaction ids within each
     652             :      * top-level transaction we issue a WAL record for the assignment. We
     653             :      * include the top-level xid and all the subxids that have not yet been
     654             :      * reported using XLOG_XACT_ASSIGNMENT records.
     655             :      *
     656             :      * This is required to limit the amount of shared memory required in a hot
     657             :      * standby server to keep track of in-progress XIDs. See notes for
     658             :      * RecordKnownAssignedTransactionIds().
     659             :      *
     660             :      * We don't keep track of the immediate parent of each subxid, only the
     661             :      * top-level transaction that each subxact belongs to. This is correct in
     662             :      * recovery only because aborted subtransactions are separately WAL
     663             :      * logged.
     664             :      *
     665             :      * This is correct even for the case where several levels above us didn't
     666             :      * have an xid assigned as we recursed up to them beforehand.
     667             :      */
     668      277648 :     if (isSubXact && XLogStandbyInfoActive())
     669             :     {
     670        4280 :         unreportedXids[nUnreportedXids] = XidFromFullTransactionId(s->fullTransactionId);
     671        4280 :         nUnreportedXids++;
     672             : 
     673             :         /*
     674             :          * ensure this test matches similar one in
     675             :          * RecoverPreparedTransactions()
     676             :          */
     677        4280 :         if (nUnreportedXids >= PGPROC_MAX_CACHED_SUBXIDS ||
     678             :             log_unknown_top)
     679             :         {
     680             :             xl_xact_assignment xlrec;
     681             : 
     682             :             /*
     683             :              * xtop is always set by now because we recurse up transaction
     684             :              * stack to the highest unassigned xid and then come back down
     685             :              */
     686          82 :             xlrec.xtop = GetTopTransactionId();
     687             :             Assert(TransactionIdIsValid(xlrec.xtop));
     688          82 :             xlrec.nsubxacts = nUnreportedXids;
     689             : 
     690          82 :             XLogBeginInsert();
     691          82 :             XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
     692          82 :             XLogRegisterData((char *) unreportedXids,
     693             :                              nUnreportedXids * sizeof(TransactionId));
     694             : 
     695          82 :             (void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
     696             : 
     697          82 :             nUnreportedXids = 0;
     698             :             /* mark top, not current xact as having been logged */
     699          82 :             TopTransactionStateData.didLogXid = true;
     700             :         }
     701             :     }
     702      277648 : }
     703             : 
     704             : /*
     705             :  *  GetCurrentSubTransactionId
     706             :  */
     707             : SubTransactionId
     708     2507112 : GetCurrentSubTransactionId(void)
     709             : {
     710     2507112 :     TransactionState s = CurrentTransactionState;
     711             : 
     712     2507112 :     return s->subTransactionId;
     713             : }
     714             : 
     715             : /*
     716             :  *  SubTransactionIsActive
     717             :  *
     718             :  * Test if the specified subxact ID is still active.  Note caller is
     719             :  * responsible for checking whether this ID is relevant to the current xact.
     720             :  */
     721             : bool
     722       49202 : SubTransactionIsActive(SubTransactionId subxid)
     723             : {
     724             :     TransactionState s;
     725             : 
     726       49210 :     for (s = CurrentTransactionState; s != NULL; s = s->parent)
     727             :     {
     728       49210 :         if (s->state == TRANS_ABORT)
     729           0 :             continue;
     730       49210 :         if (s->subTransactionId == subxid)
     731       49202 :             return true;
     732             :     }
     733           0 :     return false;
     734             : }
     735             : 
     736             : 
     737             : /*
     738             :  *  GetCurrentCommandId
     739             :  *
     740             :  * "used" must be true if the caller intends to use the command ID to mark
     741             :  * inserted/updated/deleted tuples.  false means the ID is being fetched
     742             :  * for read-only purposes (ie, as a snapshot validity cutoff).  See
     743             :  * CommandCounterIncrement() for discussion.
     744             :  */
     745             : CommandId
     746    11217468 : GetCurrentCommandId(bool used)
     747             : {
     748             :     /* this is global to a transaction, not subtransaction-local */
     749    11217468 :     if (used)
     750             :     {
     751             :         /*
     752             :          * Forbid setting currentCommandIdUsed in a parallel worker, because
     753             :          * we have no provision for communicating this back to the master.  We
     754             :          * could relax this restriction when currentCommandIdUsed was already
     755             :          * true at the start of the parallel operation.
     756             :          */
     757             :         Assert(!IsParallelWorker());
     758     9186120 :         currentCommandIdUsed = true;
     759             :     }
     760    11217468 :     return currentCommandId;
     761             : }
     762             : 
     763             : /*
     764             :  *  SetParallelStartTimestamps
     765             :  *
     766             :  * In a parallel worker, we should inherit the parent transaction's
     767             :  * timestamps rather than setting our own.  The parallel worker
     768             :  * infrastructure must call this to provide those values before
     769             :  * calling StartTransaction() or SetCurrentStatementStartTimestamp().
     770             :  */
     771             : void
     772        1568 : SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts)
     773             : {
     774             :     Assert(IsParallelWorker());
     775        1568 :     xactStartTimestamp = xact_ts;
     776        1568 :     stmtStartTimestamp = stmt_ts;
     777        1568 : }
     778             : 
     779             : /*
     780             :  *  GetCurrentTransactionStartTimestamp
     781             :  */
     782             : TimestampTz
     783        8198 : GetCurrentTransactionStartTimestamp(void)
     784             : {
     785        8198 :     return xactStartTimestamp;
     786             : }
     787             : 
     788             : /*
     789             :  *  GetCurrentStatementStartTimestamp
     790             :  */
     791             : TimestampTz
     792     1561656 : GetCurrentStatementStartTimestamp(void)
     793             : {
     794     1561656 :     return stmtStartTimestamp;
     795             : }
     796             : 
     797             : /*
     798             :  *  GetCurrentTransactionStopTimestamp
     799             :  *
     800             :  * We return current time if the transaction stop time hasn't been set
     801             :  * (which can happen if we decide we don't need to log an XLOG record).
     802             :  */
     803             : TimestampTz
     804      410732 : GetCurrentTransactionStopTimestamp(void)
     805             : {
     806      410732 :     if (xactStopTimestamp != 0)
     807      253276 :         return xactStopTimestamp;
     808      157456 :     return GetCurrentTimestamp();
     809             : }
     810             : 
     811             : /*
     812             :  *  SetCurrentStatementStartTimestamp
     813             :  *
     814             :  * In a parallel worker, this should already have been provided by a call
     815             :  * to SetParallelStartTimestamps().
     816             :  */
     817             : void
     818      554388 : SetCurrentStatementStartTimestamp(void)
     819             : {
     820      554388 :     if (!IsParallelWorker())
     821      552820 :         stmtStartTimestamp = GetCurrentTimestamp();
     822             :     else
     823             :         Assert(stmtStartTimestamp != 0);
     824      554388 : }
     825             : 
     826             : /*
     827             :  *  SetCurrentTransactionStopTimestamp
     828             :  */
     829             : static inline void
     830      273302 : SetCurrentTransactionStopTimestamp(void)
     831             : {
     832      273302 :     xactStopTimestamp = GetCurrentTimestamp();
     833      273302 : }
     834             : 
     835             : /*
     836             :  *  GetCurrentTransactionNestLevel
     837             :  *
     838             :  * Note: this will return zero when not inside any transaction, one when
     839             :  * inside a top-level transaction, etc.
     840             :  */
     841             : int
     842    24449248 : GetCurrentTransactionNestLevel(void)
     843             : {
     844    24449248 :     TransactionState s = CurrentTransactionState;
     845             : 
     846    24449248 :     return s->nestingLevel;
     847             : }
     848             : 
     849             : 
     850             : /*
     851             :  *  TransactionIdIsCurrentTransactionId
     852             :  */
     853             : bool
     854    56771498 : TransactionIdIsCurrentTransactionId(TransactionId xid)
     855             : {
     856             :     TransactionState s;
     857             : 
     858             :     /*
     859             :      * We always say that BootstrapTransactionId is "not my transaction ID"
     860             :      * even when it is (ie, during bootstrap).  Along with the fact that
     861             :      * transam.c always treats BootstrapTransactionId as already committed,
     862             :      * this causes the heapam_visibility.c routines to see all tuples as
     863             :      * committed, which is what we need during bootstrap.  (Bootstrap mode
     864             :      * only inserts tuples, it never updates or deletes them, so all tuples
     865             :      * can be presumed good immediately.)
     866             :      *
     867             :      * Likewise, InvalidTransactionId and FrozenTransactionId are certainly
     868             :      * not my transaction ID, so we can just return "false" immediately for
     869             :      * any non-normal XID.
     870             :      */
     871    56771498 :     if (!TransactionIdIsNormal(xid))
     872     4028994 :         return false;
     873             : 
     874    52742504 :     if (TransactionIdEquals(xid, GetTopTransactionIdIfAny()))
     875    37660580 :         return true;
     876             : 
     877             :     /*
     878             :      * In parallel workers, the XIDs we must consider as current are stored in
     879             :      * ParallelCurrentXids rather than the transaction-state stack.  Note that
     880             :      * the XIDs in this array are sorted numerically rather than according to
     881             :      * transactionIdPrecedes order.
     882             :      */
     883    15081924 :     if (nParallelCurrentXids > 0)
     884             :     {
     885             :         int         low,
     886             :                     high;
     887             : 
     888     2442880 :         low = 0;
     889     2442880 :         high = nParallelCurrentXids - 1;
     890     9694308 :         while (low <= high)
     891             :         {
     892             :             int         middle;
     893             :             TransactionId probe;
     894             : 
     895     9655784 :             middle = low + (high - low) / 2;
     896     9655784 :             probe = ParallelCurrentXids[middle];
     897     9655784 :             if (probe == xid)
     898     2404356 :                 return true;
     899     7251428 :             else if (probe < xid)
     900     7212914 :                 low = middle + 1;
     901             :             else
     902       38514 :                 high = middle - 1;
     903             :         }
     904       38524 :         return false;
     905             :     }
     906             : 
     907             :     /*
     908             :      * We will return true for the Xid of the current subtransaction, any of
     909             :      * its subcommitted children, any of its parents, or any of their
     910             :      * previously subcommitted children.  However, a transaction being aborted
     911             :      * is no longer "current", even though it may still have an entry on the
     912             :      * state stack.
     913             :      */
     914    25274228 :     for (s = CurrentTransactionState; s != NULL; s = s->parent)
     915             :     {
     916             :         int         low,
     917             :                     high;
     918             : 
     919    12643888 :         if (s->state == TRANS_ABORT)
     920           0 :             continue;
     921    12643888 :         if (!FullTransactionIdIsValid(s->fullTransactionId))
     922     8254662 :             continue;           /* it can't have any child XIDs either */
     923     4389226 :         if (TransactionIdEquals(xid, XidFromFullTransactionId(s->fullTransactionId)))
     924        5826 :             return true;
     925             :         /* As the childXids array is ordered, we can use binary search */
     926     4383400 :         low = 0;
     927     4383400 :         high = s->nChildXids - 1;
     928     4385084 :         while (low <= high)
     929             :         {
     930             :             int         middle;
     931             :             TransactionId probe;
     932             : 
     933        4562 :             middle = low + (high - low) / 2;
     934        4562 :             probe = s->childXids[middle];
     935        4562 :             if (TransactionIdEquals(probe, xid))
     936        2878 :                 return true;
     937        1684 :             else if (TransactionIdPrecedes(probe, xid))
     938        1600 :                 low = middle + 1;
     939             :             else
     940          84 :                 high = middle - 1;
     941             :         }
     942             :     }
     943             : 
     944    12630340 :     return false;
     945             : }
     946             : 
     947             : /*
     948             :  *  TransactionStartedDuringRecovery
     949             :  *
     950             :  * Returns true if the current transaction started while recovery was still
     951             :  * in progress. Recovery might have ended since so RecoveryInProgress() might
     952             :  * return false already.
     953             :  */
     954             : bool
     955    11003152 : TransactionStartedDuringRecovery(void)
     956             : {
     957    11003152 :     return CurrentTransactionState->startedInRecovery;
     958             : }
     959             : 
     960             : /*
     961             :  *  EnterParallelMode
     962             :  */
     963             : void
     964        3590 : EnterParallelMode(void)
     965             : {
     966        3590 :     TransactionState s = CurrentTransactionState;
     967             : 
     968             :     Assert(s->parallelModeLevel >= 0);
     969             : 
     970        3590 :     ++s->parallelModeLevel;
     971        3590 : }
     972             : 
     973             : /*
     974             :  *  ExitParallelMode
     975             :  */
     976             : void
     977        2018 : ExitParallelMode(void)
     978             : {
     979        2018 :     TransactionState s = CurrentTransactionState;
     980             : 
     981             :     Assert(s->parallelModeLevel > 0);
     982             :     Assert(s->parallelModeLevel > 1 || !ParallelContextActive());
     983             : 
     984        2018 :     --s->parallelModeLevel;
     985        2018 : }
     986             : 
     987             : /*
     988             :  *  IsInParallelMode
     989             :  *
     990             :  * Are we in a parallel operation, as either the master or a worker?  Check
     991             :  * this to prohibit operations that change backend-local state expected to
     992             :  * match across all workers.  Mere caches usually don't require such a
     993             :  * restriction.  State modified in a strict push/pop fashion, such as the
     994             :  * active snapshot stack, is often fine.
     995             :  */
     996             : bool
     997     5544622 : IsInParallelMode(void)
     998             : {
     999     5544622 :     return CurrentTransactionState->parallelModeLevel != 0;
    1000             : }
    1001             : 
    1002             : /*
    1003             :  *  CommandCounterIncrement
    1004             :  */
    1005             : void
    1006     1383164 : CommandCounterIncrement(void)
    1007             : {
    1008             :     /*
    1009             :      * If the current value of the command counter hasn't been "used" to mark
    1010             :      * tuples, we need not increment it, since there's no need to distinguish
    1011             :      * a read-only command from others.  This helps postpone command counter
    1012             :      * overflow, and keeps no-op CommandCounterIncrement operations cheap.
    1013             :      */
    1014     1383164 :     if (currentCommandIdUsed)
    1015             :     {
    1016             :         /*
    1017             :          * Workers synchronize transaction state at the beginning of each
    1018             :          * parallel operation, so we can't account for new commands after that
    1019             :          * point.
    1020             :          */
    1021      643712 :         if (IsInParallelMode() || IsParallelWorker())
    1022           0 :             elog(ERROR, "cannot start commands during a parallel operation");
    1023             : 
    1024      643712 :         currentCommandId += 1;
    1025      643712 :         if (currentCommandId == InvalidCommandId)
    1026             :         {
    1027           0 :             currentCommandId -= 1;
    1028           0 :             ereport(ERROR,
    1029             :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    1030             :                      errmsg("cannot have more than 2^32-2 commands in a transaction")));
    1031             :         }
    1032      643712 :         currentCommandIdUsed = false;
    1033             : 
    1034             :         /* Propagate new command ID into static snapshots */
    1035      643712 :         SnapshotSetCommandId(currentCommandId);
    1036             : 
    1037             :         /*
    1038             :          * Make any catalog changes done by the just-completed command visible
    1039             :          * in the local syscache.  We obviously don't need to do this after a
    1040             :          * read-only command.  (But see hacks in inval.c to make real sure we
    1041             :          * don't think a command that queued inval messages was read-only.)
    1042             :          */
    1043      643712 :         AtCCI_LocalCache();
    1044             :     }
    1045     1383160 : }
    1046             : 
    1047             : /*
    1048             :  * ForceSyncCommit
    1049             :  *
    1050             :  * Interface routine to allow commands to force a synchronous commit of the
    1051             :  * current top-level transaction
    1052             :  */
    1053             : void
    1054        1018 : ForceSyncCommit(void)
    1055             : {
    1056        1018 :     forceSyncCommit = true;
    1057        1018 : }
    1058             : 
    1059             : 
    1060             : /* ----------------------------------------------------------------
    1061             :  *                      StartTransaction stuff
    1062             :  * ----------------------------------------------------------------
    1063             :  */
    1064             : 
    1065             : /*
    1066             :  *  AtStart_Cache
    1067             :  */
    1068             : static void
    1069      494696 : AtStart_Cache(void)
    1070             : {
    1071      494696 :     AcceptInvalidationMessages();
    1072      494696 : }
    1073             : 
    1074             : /*
    1075             :  *  AtStart_Memory
    1076             :  */
    1077             : static void
    1078      494696 : AtStart_Memory(void)
    1079             : {
    1080      494696 :     TransactionState s = CurrentTransactionState;
    1081             : 
    1082             :     /*
    1083             :      * If this is the first time through, create a private context for
    1084             :      * AbortTransaction to work in.  By reserving some space now, we can
    1085             :      * insulate AbortTransaction from out-of-memory scenarios.  Like
    1086             :      * ErrorContext, we set it up with slow growth rate and a nonzero minimum
    1087             :      * size, so that space will be reserved immediately.
    1088             :      */
    1089      494696 :     if (TransactionAbortContext == NULL)
    1090       11046 :         TransactionAbortContext =
    1091       11046 :             AllocSetContextCreate(TopMemoryContext,
    1092             :                                   "TransactionAbortContext",
    1093             :                                   32 * 1024,
    1094             :                                   32 * 1024,
    1095             :                                   32 * 1024);
    1096             : 
    1097             :     /*
    1098             :      * We shouldn't have a transaction context already.
    1099             :      */
    1100             :     Assert(TopTransactionContext == NULL);
    1101             : 
    1102             :     /*
    1103             :      * Create a toplevel context for the transaction.
    1104             :      */
    1105      494696 :     TopTransactionContext =
    1106      494696 :         AllocSetContextCreate(TopMemoryContext,
    1107             :                               "TopTransactionContext",
    1108             :                               ALLOCSET_DEFAULT_SIZES);
    1109             : 
    1110             :     /*
    1111             :      * In a top-level transaction, CurTransactionContext is the same as
    1112             :      * TopTransactionContext.
    1113             :      */
    1114      494696 :     CurTransactionContext = TopTransactionContext;
    1115      494696 :     s->curTransactionContext = CurTransactionContext;
    1116             : 
    1117             :     /* Make the CurTransactionContext active. */
    1118      494696 :     MemoryContextSwitchTo(CurTransactionContext);
    1119      494696 : }
    1120             : 
    1121             : /*
    1122             :  *  AtStart_ResourceOwner
    1123             :  */
    1124             : static void
    1125      494696 : AtStart_ResourceOwner(void)
    1126             : {
    1127      494696 :     TransactionState s = CurrentTransactionState;
    1128             : 
    1129             :     /*
    1130             :      * We shouldn't have a transaction resource owner already.
    1131             :      */
    1132             :     Assert(TopTransactionResourceOwner == NULL);
    1133             : 
    1134             :     /*
    1135             :      * Create a toplevel resource owner for the transaction.
    1136             :      */
    1137      494696 :     s->curTransactionOwner = ResourceOwnerCreate(NULL, "TopTransaction");
    1138             : 
    1139      494696 :     TopTransactionResourceOwner = s->curTransactionOwner;
    1140      494696 :     CurTransactionResourceOwner = s->curTransactionOwner;
    1141      494696 :     CurrentResourceOwner = s->curTransactionOwner;
    1142      494696 : }
    1143             : 
    1144             : /* ----------------------------------------------------------------
    1145             :  *                      StartSubTransaction stuff
    1146             :  * ----------------------------------------------------------------
    1147             :  */
    1148             : 
    1149             : /*
    1150             :  * AtSubStart_Memory
    1151             :  */
    1152             : static void
    1153        7650 : AtSubStart_Memory(void)
    1154             : {
    1155        7650 :     TransactionState s = CurrentTransactionState;
    1156             : 
    1157             :     Assert(CurTransactionContext != NULL);
    1158             : 
    1159             :     /*
    1160             :      * Create a CurTransactionContext, which will be used to hold data that
    1161             :      * survives subtransaction commit but disappears on subtransaction abort.
    1162             :      * We make it a child of the immediate parent's CurTransactionContext.
    1163             :      */
    1164        7650 :     CurTransactionContext = AllocSetContextCreate(CurTransactionContext,
    1165             :                                                   "CurTransactionContext",
    1166             :                                                   ALLOCSET_DEFAULT_SIZES);
    1167        7650 :     s->curTransactionContext = CurTransactionContext;
    1168             : 
    1169             :     /* Make the CurTransactionContext active. */
    1170        7650 :     MemoryContextSwitchTo(CurTransactionContext);
    1171        7650 : }
    1172             : 
    1173             : /*
    1174             :  * AtSubStart_ResourceOwner
    1175             :  */
    1176             : static void
    1177        7650 : AtSubStart_ResourceOwner(void)
    1178             : {
    1179        7650 :     TransactionState s = CurrentTransactionState;
    1180             : 
    1181             :     Assert(s->parent != NULL);
    1182             : 
    1183             :     /*
    1184             :      * Create a resource owner for the subtransaction.  We make it a child of
    1185             :      * the immediate parent's resource owner.
    1186             :      */
    1187        7650 :     s->curTransactionOwner =
    1188        7650 :         ResourceOwnerCreate(s->parent->curTransactionOwner,
    1189             :                             "SubTransaction");
    1190             : 
    1191        7650 :     CurTransactionResourceOwner = s->curTransactionOwner;
    1192        7650 :     CurrentResourceOwner = s->curTransactionOwner;
    1193        7650 : }
    1194             : 
    1195             : /* ----------------------------------------------------------------
    1196             :  *                      CommitTransaction stuff
    1197             :  * ----------------------------------------------------------------
    1198             :  */
    1199             : 
    1200             : /*
    1201             :  *  RecordTransactionCommit
    1202             :  *
    1203             :  * Returns latest XID among xact and its children, or InvalidTransactionId
    1204             :  * if the xact has no XID.  (We compute that here just because it's easier.)
    1205             :  *
    1206             :  * If you change this function, see RecordTransactionCommitPrepared also.
    1207             :  */
    1208             : static TransactionId
    1209      472730 : RecordTransactionCommit(void)
    1210             : {
    1211      472730 :     TransactionId xid = GetTopTransactionIdIfAny();
    1212      472730 :     bool        markXidCommitted = TransactionIdIsValid(xid);
    1213      472730 :     TransactionId latestXid = InvalidTransactionId;
    1214             :     int         nrels;
    1215             :     RelFileNode *rels;
    1216             :     int         nchildren;
    1217             :     TransactionId *children;
    1218      472730 :     int         nmsgs = 0;
    1219      472730 :     SharedInvalidationMessage *invalMessages = NULL;
    1220      472730 :     bool        RelcacheInitFileInval = false;
    1221             :     bool        wrote_xlog;
    1222             : 
    1223             :     /* Get data needed for commit record */
    1224      472730 :     nrels = smgrGetPendingDeletes(true, &rels);
    1225      472730 :     nchildren = xactGetCommittedChildren(&children);
    1226      472730 :     if (XLogStandbyInfoActive())
    1227      452834 :         nmsgs = xactGetCommittedInvalidationMessages(&invalMessages,
    1228             :                                                      &RelcacheInitFileInval);
    1229      472730 :     wrote_xlog = (XactLastRecEnd != 0);
    1230             : 
    1231             :     /*
    1232             :      * If we haven't been assigned an XID yet, we neither can, nor do we want
    1233             :      * to write a COMMIT record.
    1234             :      */
    1235      472730 :     if (!markXidCommitted)
    1236             :     {
    1237             :         /*
    1238             :          * We expect that every RelationDropStorage is followed by a catalog
    1239             :          * update, and hence XID assignment, so we shouldn't get here with any
    1240             :          * pending deletes.  Use a real test not just an Assert to check this,
    1241             :          * since it's a bit fragile.
    1242             :          */
    1243      204818 :         if (nrels != 0)
    1244           0 :             elog(ERROR, "cannot commit a transaction that deleted files but has no xid");
    1245             : 
    1246             :         /* Can't have child XIDs either; AssignTransactionId enforces this */
    1247             :         Assert(nchildren == 0);
    1248             : 
    1249             :         /*
    1250             :          * Transactions without an assigned xid can contain invalidation
    1251             :          * messages (e.g. explicit relcache invalidations or catcache
    1252             :          * invalidations for inplace updates); standbys need to process those.
    1253             :          * We can't emit a commit record without an xid, and we don't want to
    1254             :          * force assigning an xid, because that'd be problematic for e.g.
    1255             :          * vacuum.  Hence we emit a bespoke record for the invalidations. We
    1256             :          * don't want to use that in case a commit record is emitted, so they
    1257             :          * happen synchronously with commits (besides not wanting to emit more
    1258             :          * WAL records).
    1259             :          */
    1260      204818 :         if (nmsgs != 0)
    1261             :         {
    1262       40082 :             LogStandbyInvalidations(nmsgs, invalMessages,
    1263             :                                     RelcacheInitFileInval);
    1264       40082 :             wrote_xlog = true;  /* not strictly necessary */
    1265             :         }
    1266             : 
    1267             :         /*
    1268             :          * If we didn't create XLOG entries, we're done here; otherwise we
    1269             :          * should trigger flushing those entries the same as a commit record
    1270             :          * would.  This will primarily happen for HOT pruning and the like; we
    1271             :          * want these to be flushed to disk in due time.
    1272             :          */
    1273      204818 :         if (!wrote_xlog)
    1274      160744 :             goto cleanup;
    1275             :     }
    1276             :     else
    1277             :     {
    1278             :         bool        replorigin;
    1279             : 
    1280             :         /*
    1281             :          * Are we using the replication origins feature?  Or, in other words,
    1282             :          * are we replaying remote actions?
    1283             :          */
    1284      268350 :         replorigin = (replorigin_session_origin != InvalidRepOriginId &&
    1285         438 :                       replorigin_session_origin != DoNotReplicateId);
    1286             : 
    1287             :         /*
    1288             :          * Begin commit critical section and insert the commit XLOG record.
    1289             :          */
    1290             :         /* Tell bufmgr and smgr to prepare for commit */
    1291      267912 :         BufmgrCommit();
    1292             : 
    1293             :         /*
    1294             :          * Mark ourselves as within our "commit critical section".  This
    1295             :          * forces any concurrent checkpoint to wait until we've updated
    1296             :          * pg_xact.  Without this, it is possible for the checkpoint to set
    1297             :          * REDO after the XLOG record but fail to flush the pg_xact update to
    1298             :          * disk, leading to loss of the transaction commit if the system
    1299             :          * crashes a little later.
    1300             :          *
    1301             :          * Note: we could, but don't bother to, set this flag in
    1302             :          * RecordTransactionAbort.  That's because loss of a transaction abort
    1303             :          * is noncritical; the presumption would be that it aborted, anyway.
    1304             :          *
    1305             :          * It's safe to change the delayChkpt flag of our own backend without
    1306             :          * holding the ProcArrayLock, since we're the only one modifying it.
    1307             :          * This makes checkpoint's determination of which xacts are delayChkpt
    1308             :          * a bit fuzzy, but it doesn't matter.
    1309             :          */
    1310      267912 :         START_CRIT_SECTION();
    1311      267912 :         MyProc->delayChkpt = true;
    1312             : 
    1313      267912 :         SetCurrentTransactionStopTimestamp();
    1314             : 
    1315      267912 :         XactLogCommitRecord(xactStopTimestamp,
    1316             :                             nchildren, children, nrels, rels,
    1317             :                             nmsgs, invalMessages,
    1318             :                             RelcacheInitFileInval, forceSyncCommit,
    1319             :                             MyXactFlags,
    1320             :                             InvalidTransactionId, NULL /* plain commit */ );
    1321             : 
    1322      267912 :         if (replorigin)
    1323             :             /* Move LSNs forward for this replication origin */
    1324         438 :             replorigin_session_advance(replorigin_session_origin_lsn,
    1325             :                                        XactLastRecEnd);
    1326             : 
    1327             :         /*
    1328             :          * Record commit timestamp.  The value comes from plain commit
    1329             :          * timestamp if there's no replication origin; otherwise, the
    1330             :          * timestamp was already set in replorigin_session_origin_timestamp by
    1331             :          * replication.
    1332             :          *
    1333             :          * We don't need to WAL-log anything here, as the commit record
    1334             :          * written above already contains the data.
    1335             :          */
    1336             : 
    1337      267912 :         if (!replorigin || replorigin_session_origin_timestamp == 0)
    1338      267508 :             replorigin_session_origin_timestamp = xactStopTimestamp;
    1339             : 
    1340      267912 :         TransactionTreeSetCommitTsData(xid, nchildren, children,
    1341             :                                        replorigin_session_origin_timestamp,
    1342             :                                        replorigin_session_origin, false);
    1343             :     }
    1344             : 
    1345             :     /*
    1346             :      * Check if we want to commit asynchronously.  We can allow the XLOG flush
    1347             :      * to happen asynchronously if synchronous_commit=off, or if the current
    1348             :      * transaction has not performed any WAL-logged operation or didn't assign
    1349             :      * an xid.  The transaction can end up not writing any WAL, even if it has
    1350             :      * an xid, if it only wrote to temporary and/or unlogged tables.  It can
    1351             :      * end up having written WAL without an xid if it did HOT pruning.  In
    1352             :      * case of a crash, the loss of such a transaction will be irrelevant;
    1353             :      * temp tables will be lost anyway, unlogged tables will be truncated and
    1354             :      * HOT pruning will be done again later. (Given the foregoing, you might
    1355             :      * think that it would be unnecessary to emit the XLOG record at all in
    1356             :      * this case, but we don't currently try to do that.  It would certainly
    1357             :      * cause problems at least in Hot Standby mode, where the
    1358             :      * KnownAssignedXids machinery requires tracking every XID assignment.  It
    1359             :      * might be OK to skip it only when wal_level < replica, but for now we
    1360             :      * don't.)
    1361             :      *
    1362             :      * However, if we're doing cleanup of any non-temp rels or committing any
    1363             :      * command that wanted to force sync commit, then we must flush XLOG
    1364             :      * immediately.  (We must not allow asynchronous commit if there are any
    1365             :      * non-temp tables to be deleted, because we might delete the files before
    1366             :      * the COMMIT record is flushed to disk.  We do allow asynchronous commit
    1367             :      * if all to-be-deleted tables are temporary though, since they are lost
    1368             :      * anyway if we crash.)
    1369             :      */
    1370      311986 :     if ((wrote_xlog && markXidCommitted &&
    1371      311986 :          synchronous_commit > SYNCHRONOUS_COMMIT_OFF) ||
    1372       55304 :         forceSyncCommit || nrels > 0)
    1373             :     {
    1374      256706 :         XLogFlush(XactLastRecEnd);
    1375             : 
    1376             :         /*
    1377             :          * Now we may update the CLOG, if we wrote a COMMIT record above
    1378             :          */
    1379      513412 :         if (markXidCommitted)
    1380      256706 :             TransactionIdCommitTree(xid, nchildren, children);
    1381             :     }
    1382             :     else
    1383             :     {
    1384             :         /*
    1385             :          * Asynchronous commit case:
    1386             :          *
    1387             :          * This enables possible committed transaction loss in the case of a
    1388             :          * postmaster crash because WAL buffers are left unwritten. Ideally we
    1389             :          * could issue the WAL write without the fsync, but some
    1390             :          * wal_sync_methods do not allow separate write/fsync.
    1391             :          *
    1392             :          * Report the latest async commit LSN, so that the WAL writer knows to
    1393             :          * flush this commit.
    1394             :          */
    1395       55280 :         XLogSetAsyncXactLSN(XactLastRecEnd);
    1396             : 
    1397             :         /*
    1398             :          * We must not immediately update the CLOG, since we didn't flush the
    1399             :          * XLOG. Instead, we store the LSN up to which the XLOG must be
    1400             :          * flushed before the CLOG may be updated.
    1401             :          */
    1402       55280 :         if (markXidCommitted)
    1403       11206 :             TransactionIdAsyncCommitTree(xid, nchildren, children, XactLastRecEnd);
    1404             :     }
    1405             : 
    1406             :     /*
    1407             :      * If we entered a commit critical section, leave it now, and let
    1408             :      * checkpoints proceed.
    1409             :      */
    1410      311986 :     if (markXidCommitted)
    1411             :     {
    1412      267912 :         MyProc->delayChkpt = false;
    1413      267912 :         END_CRIT_SECTION();
    1414             :     }
    1415             : 
    1416             :     /* Compute latestXid while we have the child XIDs handy */
    1417      311986 :     latestXid = TransactionIdLatest(xid, nchildren, children);
    1418             : 
    1419             :     /*
    1420             :      * Wait for synchronous replication, if required. Similar to the decision
    1421             :      * above about using committing asynchronously we only want to wait if
    1422             :      * this backend assigned an xid and wrote WAL.  No need to wait if an xid
    1423             :      * was assigned due to temporary/unlogged tables or due to HOT pruning.
    1424             :      *
    1425             :      * Note that at this stage we have marked clog, but still show as running
    1426             :      * in the procarray and continue to hold locks.
    1427             :      */
    1428      311986 :     if (wrote_xlog && markXidCommitted)
    1429      261790 :         SyncRepWaitForLSN(XactLastRecEnd, true);
    1430             : 
    1431             :     /* remember end of last commit record */
    1432      311986 :     XactLastCommitEnd = XactLastRecEnd;
    1433             : 
    1434             :     /* Reset XactLastRecEnd until the next transaction writes something */
    1435      311986 :     XactLastRecEnd = 0;
    1436      472730 : cleanup:
    1437             :     /* Clean up local data */
    1438      472730 :     if (rels)
    1439       10610 :         pfree(rels);
    1440             : 
    1441      472730 :     return latestXid;
    1442             : }
    1443             : 
    1444             : 
    1445             : /*
    1446             :  *  AtCCI_LocalCache
    1447             :  */
    1448             : static void
    1449      643712 : AtCCI_LocalCache(void)
    1450             : {
    1451             :     /*
    1452             :      * Make any pending relation map changes visible.  We must do this before
    1453             :      * processing local sinval messages, so that the map changes will get
    1454             :      * reflected into the relcache when relcache invals are processed.
    1455             :      */
    1456      643712 :     AtCCI_RelationMap();
    1457             : 
    1458             :     /*
    1459             :      * Make catalog changes visible to me for the next command.
    1460             :      */
    1461      643712 :     CommandEndInvalidationMessages();
    1462      643708 : }
    1463             : 
    1464             : /*
    1465             :  *  AtCommit_Memory
    1466             :  */
    1467             : static void
    1468      474360 : AtCommit_Memory(void)
    1469             : {
    1470             :     /*
    1471             :      * Now that we're "out" of a transaction, have the system allocate things
    1472             :      * in the top memory context instead of per-transaction contexts.
    1473             :      */
    1474      474360 :     MemoryContextSwitchTo(TopMemoryContext);
    1475             : 
    1476             :     /*
    1477             :      * Release all transaction-local memory.
    1478             :      */
    1479             :     Assert(TopTransactionContext != NULL);
    1480      474360 :     MemoryContextDelete(TopTransactionContext);
    1481      474360 :     TopTransactionContext = NULL;
    1482      474360 :     CurTransactionContext = NULL;
    1483      474360 :     CurrentTransactionState->curTransactionContext = NULL;
    1484      474360 : }
    1485             : 
    1486             : /* ----------------------------------------------------------------
    1487             :  *                      CommitSubTransaction stuff
    1488             :  * ----------------------------------------------------------------
    1489             :  */
    1490             : 
    1491             : /*
    1492             :  * AtSubCommit_Memory
    1493             :  */
    1494             : static void
    1495        4696 : AtSubCommit_Memory(void)
    1496             : {
    1497        4696 :     TransactionState s = CurrentTransactionState;
    1498             : 
    1499             :     Assert(s->parent != NULL);
    1500             : 
    1501             :     /* Return to parent transaction level's memory context. */
    1502        4696 :     CurTransactionContext = s->parent->curTransactionContext;
    1503        4696 :     MemoryContextSwitchTo(CurTransactionContext);
    1504             : 
    1505             :     /*
    1506             :      * Ordinarily we cannot throw away the child's CurTransactionContext,
    1507             :      * since the data it contains will be needed at upper commit.  However, if
    1508             :      * there isn't actually anything in it, we can throw it away.  This avoids
    1509             :      * a small memory leak in the common case of "trivial" subxacts.
    1510             :      */
    1511        4696 :     if (MemoryContextIsEmpty(s->curTransactionContext))
    1512             :     {
    1513        4634 :         MemoryContextDelete(s->curTransactionContext);
    1514        4634 :         s->curTransactionContext = NULL;
    1515             :     }
    1516        4696 : }
    1517             : 
    1518             : /*
    1519             :  * AtSubCommit_childXids
    1520             :  *
    1521             :  * Pass my own XID and my child XIDs up to my parent as committed children.
    1522             :  */
    1523             : static void
    1524        3918 : AtSubCommit_childXids(void)
    1525             : {
    1526        3918 :     TransactionState s = CurrentTransactionState;
    1527             :     int         new_nChildXids;
    1528             : 
    1529             :     Assert(s->parent != NULL);
    1530             : 
    1531             :     /*
    1532             :      * The parent childXids array will need to hold my XID and all my
    1533             :      * childXids, in addition to the XIDs already there.
    1534             :      */
    1535        3918 :     new_nChildXids = s->parent->nChildXids + s->nChildXids + 1;
    1536             : 
    1537             :     /* Allocate or enlarge the parent array if necessary */
    1538        3918 :     if (s->parent->maxChildXids < new_nChildXids)
    1539             :     {
    1540             :         int         new_maxChildXids;
    1541             :         TransactionId *new_childXids;
    1542             : 
    1543             :         /*
    1544             :          * Make it 2x what's needed right now, to avoid having to enlarge it
    1545             :          * repeatedly. But we can't go above MaxAllocSize.  (The latter limit
    1546             :          * is what ensures that we don't need to worry about integer overflow
    1547             :          * here or in the calculation of new_nChildXids.)
    1548             :          */
    1549        1674 :         new_maxChildXids = Min(new_nChildXids * 2,
    1550             :                                (int) (MaxAllocSize / sizeof(TransactionId)));
    1551             : 
    1552        1674 :         if (new_maxChildXids < new_nChildXids)
    1553           0 :             ereport(ERROR,
    1554             :                     (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    1555             :                      errmsg("maximum number of committed subtransactions (%d) exceeded",
    1556             :                             (int) (MaxAllocSize / sizeof(TransactionId)))));
    1557             : 
    1558             :         /*
    1559             :          * We keep the child-XID arrays in TopTransactionContext; this avoids
    1560             :          * setting up child-transaction contexts for what might be just a few
    1561             :          * bytes of grandchild XIDs.
    1562             :          */
    1563        1674 :         if (s->parent->childXids == NULL)
    1564             :             new_childXids =
    1565        1630 :                 MemoryContextAlloc(TopTransactionContext,
    1566             :                                    new_maxChildXids * sizeof(TransactionId));
    1567             :         else
    1568          44 :             new_childXids = repalloc(s->parent->childXids,
    1569             :                                      new_maxChildXids * sizeof(TransactionId));
    1570             : 
    1571        1674 :         s->parent->childXids = new_childXids;
    1572        1674 :         s->parent->maxChildXids = new_maxChildXids;
    1573             :     }
    1574             : 
    1575             :     /*
    1576             :      * Copy all my XIDs to parent's array.
    1577             :      *
    1578             :      * Note: We rely on the fact that the XID of a child always follows that
    1579             :      * of its parent.  By copying the XID of this subtransaction before the
    1580             :      * XIDs of its children, we ensure that the array stays ordered. Likewise,
    1581             :      * all XIDs already in the array belong to subtransactions started and
    1582             :      * subcommitted before us, so their XIDs must precede ours.
    1583             :      */
    1584        3918 :     s->parent->childXids[s->parent->nChildXids] = XidFromFullTransactionId(s->fullTransactionId);
    1585             : 
    1586        3918 :     if (s->nChildXids > 0)
    1587        2268 :         memcpy(&s->parent->childXids[s->parent->nChildXids + 1],
    1588        1134 :                s->childXids,
    1589        1134 :                s->nChildXids * sizeof(TransactionId));
    1590             : 
    1591        3918 :     s->parent->nChildXids = new_nChildXids;
    1592             : 
    1593             :     /* Release child's array to avoid leakage */
    1594        3918 :     if (s->childXids != NULL)
    1595        1134 :         pfree(s->childXids);
    1596             :     /* We must reset these to avoid double-free if fail later in commit */
    1597        3918 :     s->childXids = NULL;
    1598        3918 :     s->nChildXids = 0;
    1599        3918 :     s->maxChildXids = 0;
    1600        3918 : }
    1601             : 
    1602             : /* ----------------------------------------------------------------
    1603             :  *                      AbortTransaction stuff
    1604             :  * ----------------------------------------------------------------
    1605             :  */
    1606             : 
    1607             : /*
    1608             :  *  RecordTransactionAbort
    1609             :  *
    1610             :  * Returns latest XID among xact and its children, or InvalidTransactionId
    1611             :  * if the xact has no XID.  (We compute that here just because it's easier.)
    1612             :  */
    1613             : static TransactionId
    1614       23286 : RecordTransactionAbort(bool isSubXact)
    1615             : {
    1616       23286 :     TransactionId xid = GetCurrentTransactionIdIfAny();
    1617             :     TransactionId latestXid;
    1618             :     int         nrels;
    1619             :     RelFileNode *rels;
    1620             :     int         nchildren;
    1621             :     TransactionId *children;
    1622             :     TimestampTz xact_time;
    1623             : 
    1624             :     /*
    1625             :      * If we haven't been assigned an XID, nobody will care whether we aborted
    1626             :      * or not.  Hence, we're done in that case.  It does not matter if we have
    1627             :      * rels to delete (note that this routine is not responsible for actually
    1628             :      * deleting 'em).  We cannot have any child XIDs, either.
    1629             :      */
    1630       23286 :     if (!TransactionIdIsValid(xid))
    1631             :     {
    1632             :         /* Reset XactLastRecEnd until the next transaction writes something */
    1633       17534 :         if (!isSubXact)
    1634       14942 :             XactLastRecEnd = 0;
    1635       17534 :         return InvalidTransactionId;
    1636             :     }
    1637             : 
    1638             :     /*
    1639             :      * We have a valid XID, so we should write an ABORT record for it.
    1640             :      *
    1641             :      * We do not flush XLOG to disk here, since the default assumption after a
    1642             :      * crash would be that we aborted, anyway.  For the same reason, we don't
    1643             :      * need to worry about interlocking against checkpoint start.
    1644             :      */
    1645             : 
    1646             :     /*
    1647             :      * Check that we haven't aborted halfway through RecordTransactionCommit.
    1648             :      */
    1649        5752 :     if (TransactionIdDidCommit(xid))
    1650           0 :         elog(PANIC, "cannot abort transaction %u, it was already committed",
    1651             :              xid);
    1652             : 
    1653             :     /* Fetch the data we need for the abort record */
    1654        5752 :     nrels = smgrGetPendingDeletes(false, &rels);
    1655        5752 :     nchildren = xactGetCommittedChildren(&children);
    1656             : 
    1657             :     /* XXX do we really need a critical section here? */
    1658        5752 :     START_CRIT_SECTION();
    1659             : 
    1660             :     /* Write the ABORT record */
    1661        5752 :     if (isSubXact)
    1662         362 :         xact_time = GetCurrentTimestamp();
    1663             :     else
    1664             :     {
    1665        5390 :         SetCurrentTransactionStopTimestamp();
    1666        5390 :         xact_time = xactStopTimestamp;
    1667             :     }
    1668             : 
    1669        5752 :     XactLogAbortRecord(xact_time,
    1670             :                        nchildren, children,
    1671             :                        nrels, rels,
    1672             :                        MyXactFlags, InvalidTransactionId,
    1673             :                        NULL);
    1674             : 
    1675             :     /*
    1676             :      * Report the latest async abort LSN, so that the WAL writer knows to
    1677             :      * flush this abort. There's nothing to be gained by delaying this, since
    1678             :      * WALWriter may as well do this when it can. This is important with
    1679             :      * streaming replication because if we don't flush WAL regularly we will
    1680             :      * find that large aborts leave us with a long backlog for when commits
    1681             :      * occur after the abort, increasing our window of data loss should
    1682             :      * problems occur at that point.
    1683             :      */
    1684        5752 :     if (!isSubXact)
    1685        5390 :         XLogSetAsyncXactLSN(XactLastRecEnd);
    1686             : 
    1687             :     /*
    1688             :      * Mark the transaction aborted in clog.  This is not absolutely necessary
    1689             :      * but we may as well do it while we are here; also, in the subxact case
    1690             :      * it is helpful because XactLockTableWait makes use of it to avoid
    1691             :      * waiting for already-aborted subtransactions.  It is OK to do it without
    1692             :      * having flushed the ABORT record to disk, because in event of a crash
    1693             :      * we'd be assumed to have aborted anyway.
    1694             :      */
    1695        5752 :     TransactionIdAbortTree(xid, nchildren, children);
    1696             : 
    1697        5752 :     END_CRIT_SECTION();
    1698             : 
    1699             :     /* Compute latestXid while we have the child XIDs handy */
    1700        5752 :     latestXid = TransactionIdLatest(xid, nchildren, children);
    1701             : 
    1702             :     /*
    1703             :      * If we're aborting a subtransaction, we can immediately remove failed
    1704             :      * XIDs from PGPROC's cache of running child XIDs.  We do that here for
    1705             :      * subxacts, because we already have the child XID array at hand.  For
    1706             :      * main xacts, the equivalent happens just after this function returns.
    1707             :      */
    1708        5752 :     if (isSubXact)
    1709         362 :         XidCacheRemoveRunningXids(xid, nchildren, children, latestXid);
    1710             : 
    1711             :     /* Reset XactLastRecEnd until the next transaction writes something */
    1712        5752 :     if (!isSubXact)
    1713        5390 :         XactLastRecEnd = 0;
    1714             : 
    1715             :     /* And clean up local data */
    1716        5752 :     if (rels)
    1717         884 :         pfree(rels);
    1718             : 
    1719        5752 :     return latestXid;
    1720             : }
    1721             : 
    1722             : /*
    1723             :  *  AtAbort_Memory
    1724             :  */
    1725             : static void
    1726       31692 : AtAbort_Memory(void)
    1727             : {
    1728             :     /*
    1729             :      * Switch into TransactionAbortContext, which should have some free space
    1730             :      * even if nothing else does.  We'll work in this context until we've
    1731             :      * finished cleaning up.
    1732             :      *
    1733             :      * It is barely possible to get here when we've not been able to create
    1734             :      * TransactionAbortContext yet; if so use TopMemoryContext.
    1735             :      */
    1736       31692 :     if (TransactionAbortContext != NULL)
    1737       31692 :         MemoryContextSwitchTo(TransactionAbortContext);
    1738             :     else
    1739           0 :         MemoryContextSwitchTo(TopMemoryContext);
    1740       31692 : }
    1741             : 
    1742             : /*
    1743             :  * AtSubAbort_Memory
    1744             :  */
    1745             : static void
    1746        2954 : AtSubAbort_Memory(void)
    1747             : {
    1748             :     Assert(TransactionAbortContext != NULL);
    1749             : 
    1750        2954 :     MemoryContextSwitchTo(TransactionAbortContext);
    1751        2954 : }
    1752             : 
    1753             : 
    1754             : /*
    1755             :  *  AtAbort_ResourceOwner
    1756             :  */
    1757             : static void
    1758       20336 : AtAbort_ResourceOwner(void)
    1759             : {
    1760             :     /*
    1761             :      * Make sure we have a valid ResourceOwner, if possible (else it will be
    1762             :      * NULL, which is OK)
    1763             :      */
    1764       20336 :     CurrentResourceOwner = TopTransactionResourceOwner;
    1765       20336 : }
    1766             : 
    1767             : /*
    1768             :  * AtSubAbort_ResourceOwner
    1769             :  */
    1770             : static void
    1771        2954 : AtSubAbort_ResourceOwner(void)
    1772             : {
    1773        2954 :     TransactionState s = CurrentTransactionState;
    1774             : 
    1775             :     /* Make sure we have a valid ResourceOwner */
    1776        2954 :     CurrentResourceOwner = s->curTransactionOwner;
    1777        2954 : }
    1778             : 
    1779             : 
    1780             : /*
    1781             :  * AtSubAbort_childXids
    1782             :  */
    1783             : static void
    1784         362 : AtSubAbort_childXids(void)
    1785             : {
    1786         362 :     TransactionState s = CurrentTransactionState;
    1787             : 
    1788             :     /*
    1789             :      * We keep the child-XID arrays in TopTransactionContext (see
    1790             :      * AtSubCommit_childXids).  This means we'd better free the array
    1791             :      * explicitly at abort to avoid leakage.
    1792             :      */
    1793         362 :     if (s->childXids != NULL)
    1794          44 :         pfree(s->childXids);
    1795         362 :     s->childXids = NULL;
    1796         362 :     s->nChildXids = 0;
    1797         362 :     s->maxChildXids = 0;
    1798             : 
    1799             :     /*
    1800             :      * We could prune the unreportedXids array here. But we don't bother. That
    1801             :      * would potentially reduce number of XLOG_XACT_ASSIGNMENT records but it
    1802             :      * would likely introduce more CPU time into the more common paths, so we
    1803             :      * choose not to do that.
    1804             :      */
    1805         362 : }
    1806             : 
    1807             : /* ----------------------------------------------------------------
    1808             :  *                      CleanupTransaction stuff
    1809             :  * ----------------------------------------------------------------
    1810             :  */
    1811             : 
    1812             : /*
    1813             :  *  AtCleanup_Memory
    1814             :  */
    1815             : static void
    1816       31692 : AtCleanup_Memory(void)
    1817             : {
    1818             :     Assert(CurrentTransactionState->parent == NULL);
    1819             : 
    1820             :     /*
    1821             :      * Now that we're "out" of a transaction, have the system allocate things
    1822             :      * in the top memory context instead of per-transaction contexts.
    1823             :      */
    1824       31692 :     MemoryContextSwitchTo(TopMemoryContext);
    1825             : 
    1826             :     /*
    1827             :      * Clear the special abort context for next time.
    1828             :      */
    1829       31692 :     if (TransactionAbortContext != NULL)
    1830       31692 :         MemoryContextResetAndDeleteChildren(TransactionAbortContext);
    1831             : 
    1832             :     /*
    1833             :      * Release all transaction-local memory.
    1834             :      */
    1835       31692 :     if (TopTransactionContext != NULL)
    1836       20336 :         MemoryContextDelete(TopTransactionContext);
    1837       31692 :     TopTransactionContext = NULL;
    1838       31692 :     CurTransactionContext = NULL;
    1839       31692 :     CurrentTransactionState->curTransactionContext = NULL;
    1840       31692 : }
    1841             : 
    1842             : 
    1843             : /* ----------------------------------------------------------------
    1844             :  *                      CleanupSubTransaction stuff
    1845             :  * ----------------------------------------------------------------
    1846             :  */
    1847             : 
    1848             : /*
    1849             :  * AtSubCleanup_Memory
    1850             :  */
    1851             : static void
    1852        2954 : AtSubCleanup_Memory(void)
    1853             : {
    1854        2954 :     TransactionState s = CurrentTransactionState;
    1855             : 
    1856             :     Assert(s->parent != NULL);
    1857             : 
    1858             :     /* Make sure we're not in an about-to-be-deleted context */
    1859        2954 :     MemoryContextSwitchTo(s->parent->curTransactionContext);
    1860        2954 :     CurTransactionContext = s->parent->curTransactionContext;
    1861             : 
    1862             :     /*
    1863             :      * Clear the special abort context for next time.
    1864             :      */
    1865        2954 :     if (TransactionAbortContext != NULL)
    1866        2954 :         MemoryContextResetAndDeleteChildren(TransactionAbortContext);
    1867             : 
    1868             :     /*
    1869             :      * Delete the subxact local memory contexts. Its CurTransactionContext can
    1870             :      * go too (note this also kills CurTransactionContexts from any children
    1871             :      * of the subxact).
    1872             :      */
    1873        2954 :     if (s->curTransactionContext)
    1874        2954 :         MemoryContextDelete(s->curTransactionContext);
    1875        2954 :     s->curTransactionContext = NULL;
    1876        2954 : }
    1877             : 
    1878             : /* ----------------------------------------------------------------
    1879             :  *                      interface routines
    1880             :  * ----------------------------------------------------------------
    1881             :  */
    1882             : 
    1883             : /*
    1884             :  *  StartTransaction
    1885             :  */
    1886             : static void
    1887      494696 : StartTransaction(void)
    1888             : {
    1889             :     TransactionState s;
    1890             :     VirtualTransactionId vxid;
    1891             : 
    1892             :     /*
    1893             :      * Let's just make sure the state stack is empty
    1894             :      */
    1895      494696 :     s = &TopTransactionStateData;
    1896      494696 :     CurrentTransactionState = s;
    1897             : 
    1898             :     Assert(!FullTransactionIdIsValid(XactTopFullTransactionId));
    1899             : 
    1900             :     /* check the current transaction state */
    1901             :     Assert(s->state == TRANS_DEFAULT);
    1902             : 
    1903             :     /*
    1904             :      * Set the current transaction state information appropriately during
    1905             :      * start processing.  Note that once the transaction status is switched
    1906             :      * this process cannot fail until the user ID and the security context
    1907             :      * flags are fetched below.
    1908             :      */
    1909      494696 :     s->state = TRANS_START;
    1910      494696 :     s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
    1911             : 
    1912             :     /* Determine if statements are logged in this transaction */
    1913      494696 :     xact_is_sampled = log_xact_sample_rate != 0 &&
    1914           0 :         (log_xact_sample_rate == 1 ||
    1915           0 :          random() <= log_xact_sample_rate * MAX_RANDOM_VALUE);
    1916             : 
    1917             :     /*
    1918             :      * initialize current transaction state fields
    1919             :      *
    1920             :      * note: prevXactReadOnly is not used at the outermost level
    1921             :      */
    1922      494696 :     s->nestingLevel = 1;
    1923      494696 :     s->gucNestLevel = 1;
    1924      494696 :     s->childXids = NULL;
    1925      494696 :     s->nChildXids = 0;
    1926      494696 :     s->maxChildXids = 0;
    1927             : 
    1928             :     /*
    1929             :      * Once the current user ID and the security context flags are fetched,
    1930             :      * both will be properly reset even if transaction startup fails.
    1931             :      */
    1932      494696 :     GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
    1933             : 
    1934             :     /* SecurityRestrictionContext should never be set outside a transaction */
    1935             :     Assert(s->prevSecContext == 0);
    1936             : 
    1937             :     /*
    1938             :      * Make sure we've reset xact state variables
    1939             :      *
    1940             :      * If recovery is still in progress, mark this transaction as read-only.
    1941             :      * We have lower level defences in XLogInsert and elsewhere to stop us
    1942             :      * from modifying data during recovery, but this gives the normal
    1943             :      * indication to the user that the transaction is read-only.
    1944             :      */
    1945      494696 :     if (RecoveryInProgress())
    1946             :     {
    1947         472 :         s->startedInRecovery = true;
    1948         472 :         XactReadOnly = true;
    1949             :     }
    1950             :     else
    1951             :     {
    1952      494224 :         s->startedInRecovery = false;
    1953      494224 :         XactReadOnly = DefaultXactReadOnly;
    1954             :     }
    1955      494696 :     XactDeferrable = DefaultXactDeferrable;
    1956      494696 :     XactIsoLevel = DefaultXactIsoLevel;
    1957      494696 :     forceSyncCommit = false;
    1958      494696 :     MyXactFlags = 0;
    1959             : 
    1960             :     /*
    1961             :      * reinitialize within-transaction counters
    1962             :      */
    1963      494696 :     s->subTransactionId = TopSubTransactionId;
    1964      494696 :     currentSubTransactionId = TopSubTransactionId;
    1965      494696 :     currentCommandId = FirstCommandId;
    1966      494696 :     currentCommandIdUsed = false;
    1967             : 
    1968             :     /*
    1969             :      * initialize reported xid accounting
    1970             :      */
    1971      494696 :     nUnreportedXids = 0;
    1972      494696 :     s->didLogXid = false;
    1973             : 
    1974             :     /*
    1975             :      * must initialize resource-management stuff first
    1976             :      */
    1977      494696 :     AtStart_Memory();
    1978      494696 :     AtStart_ResourceOwner();
    1979             : 
    1980             :     /*
    1981             :      * Assign a new LocalTransactionId, and combine it with the backendId to
    1982             :      * form a virtual transaction id.
    1983             :      */
    1984      494696 :     vxid.backendId = MyBackendId;
    1985      494696 :     vxid.localTransactionId = GetNextLocalTransactionId();
    1986             : 
    1987             :     /*
    1988             :      * Lock the virtual transaction id before we announce it in the proc array
    1989             :      */
    1990      494696 :     VirtualXactLockTableInsert(vxid);
    1991             : 
    1992             :     /*
    1993             :      * Advertise it in the proc array.  We assume assignment of
    1994             :      * localTransactionId is atomic, and the backendId should be set already.
    1995             :      */
    1996             :     Assert(MyProc->backendId == vxid.backendId);
    1997      494696 :     MyProc->lxid = vxid.localTransactionId;
    1998             : 
    1999             :     TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
    2000             : 
    2001             :     /*
    2002             :      * set transaction_timestamp() (a/k/a now()).  Normally, we want this to
    2003             :      * be the same as the first command's statement_timestamp(), so don't do a
    2004             :      * fresh GetCurrentTimestamp() call (which'd be expensive anyway).  But
    2005             :      * for transactions started inside procedures (i.e., nonatomic SPI
    2006             :      * contexts), we do need to advance the timestamp.  Also, in a parallel
    2007             :      * worker, the timestamp should already have been provided by a call to
    2008             :      * SetParallelStartTimestamps().
    2009             :      */
    2010      494696 :     if (!IsParallelWorker())
    2011             :     {
    2012      489992 :         if (!SPI_inside_nonatomic_context())
    2013      485670 :             xactStartTimestamp = stmtStartTimestamp;
    2014             :         else
    2015        4322 :             xactStartTimestamp = GetCurrentTimestamp();
    2016             :     }
    2017             :     else
    2018             :         Assert(xactStartTimestamp != 0);
    2019      494696 :     pgstat_report_xact_timestamp(xactStartTimestamp);
    2020             :     /* Mark xactStopTimestamp as unset. */
    2021      494696 :     xactStopTimestamp = 0;
    2022             : 
    2023             :     /*
    2024             :      * initialize other subsystems for new transaction
    2025             :      */
    2026      494696 :     AtStart_GUC();
    2027      494696 :     AtStart_Cache();
    2028      494696 :     AfterTriggerBeginXact();
    2029             : 
    2030             :     /*
    2031             :      * done with start processing, set current transaction state to "in
    2032             :      * progress"
    2033             :      */
    2034      494696 :     s->state = TRANS_INPROGRESS;
    2035             : 
    2036      494696 :     ShowTransactionState("StartTransaction");
    2037      494696 : }
    2038             : 
    2039             : 
    2040             : /*
    2041             :  *  CommitTransaction
    2042             :  *
    2043             :  * NB: if you change this routine, better look at PrepareTransaction too!
    2044             :  */
    2045             : static void
    2046      474648 : CommitTransaction(void)
    2047             : {
    2048      474648 :     TransactionState s = CurrentTransactionState;
    2049             :     TransactionId latestXid;
    2050             :     bool        is_parallel_worker;
    2051             : 
    2052      474648 :     is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
    2053             : 
    2054             :     /* Enforce parallel mode restrictions during parallel worker commit. */
    2055      474648 :     if (is_parallel_worker)
    2056        1564 :         EnterParallelMode();
    2057             : 
    2058      474648 :     ShowTransactionState("CommitTransaction");
    2059             : 
    2060             :     /*
    2061             :      * check the current transaction state
    2062             :      */
    2063      474648 :     if (s->state != TRANS_INPROGRESS)
    2064           0 :         elog(WARNING, "CommitTransaction while in %s state",
    2065             :              TransStateAsString(s->state));
    2066             :     Assert(s->parent == NULL);
    2067             : 
    2068             :     /*
    2069             :      * Do pre-commit processing that involves calling user-defined code, such
    2070             :      * as triggers.  Since closing cursors could queue trigger actions,
    2071             :      * triggers could open cursors, etc, we have to keep looping until there's
    2072             :      * nothing left to do.
    2073             :      */
    2074             :     for (;;)
    2075             :     {
    2076             :         /*
    2077             :          * Fire all currently pending deferred triggers.
    2078             :          */
    2079      495762 :         AfterTriggerFireDeferred();
    2080             : 
    2081             :         /*
    2082             :          * Close open portals (converting holdable ones into static portals).
    2083             :          * If there weren't any, we are done ... otherwise loop back to check
    2084             :          * if they queued deferred triggers.  Lather, rinse, repeat.
    2085             :          */
    2086      495706 :         if (!PreCommit_Portals(false))
    2087      474592 :             break;
    2088             :     }
    2089             : 
    2090      474592 :     CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_PRE_COMMIT
    2091             :                       : XACT_EVENT_PRE_COMMIT);
    2092             : 
    2093             :     /*
    2094             :      * The remaining actions cannot call any user-defined code, so it's safe
    2095             :      * to start shutting down within-transaction services.  But note that most
    2096             :      * of this stuff could still throw an error, which would switch us into
    2097             :      * the transaction-abort path.
    2098             :      */
    2099             : 
    2100             :     /* If we might have parallel workers, clean them up now. */
    2101      474592 :     if (IsInParallelMode())
    2102        1564 :         AtEOXact_Parallel(true);
    2103             : 
    2104             :     /* Shut down the deferred-trigger manager */
    2105      474592 :     AfterTriggerEndXact(true);
    2106             : 
    2107             :     /*
    2108             :      * Let ON COMMIT management do its thing (must happen after closing
    2109             :      * cursors, to avoid dangling-reference problems)
    2110             :      */
    2111      474592 :     PreCommit_on_commit_actions();
    2112             : 
    2113             :     /*
    2114             :      * Synchronize files that are created and not WAL-logged during this
    2115             :      * transaction. This must happen before AtEOXact_RelationMap(), so that we
    2116             :      * don't see committed-but-broken files after a crash.
    2117             :      */
    2118      474588 :     smgrDoPendingSyncs(true, is_parallel_worker);
    2119             : 
    2120             :     /* close large objects before lower-level cleanup */
    2121      474588 :     AtEOXact_LargeObject(true);
    2122             : 
    2123             :     /*
    2124             :      * Insert notifications sent by NOTIFY commands into the queue.  This
    2125             :      * should be late in the pre-commit sequence to minimize time spent
    2126             :      * holding the notify-insertion lock.  However, this could result in
    2127             :      * creating a snapshot, so we must do it before serializable cleanup.
    2128             :      */
    2129      474588 :     PreCommit_Notify();
    2130             : 
    2131             :     /*
    2132             :      * Mark serializable transaction as complete for predicate locking
    2133             :      * purposes.  This should be done as late as we can put it and still allow
    2134             :      * errors to be raised for failure patterns found at commit.  This is not
    2135             :      * appropriate in a parallel worker however, because we aren't committing
    2136             :      * the leader's transaction and its serializable state will live on.
    2137             :      */
    2138      474588 :     if (!is_parallel_worker)
    2139      473024 :         PreCommit_CheckForSerializationFailure();
    2140             : 
    2141             :     /* Prevent cancel/die interrupt while cleaning up */
    2142      474294 :     HOLD_INTERRUPTS();
    2143             : 
    2144             :     /* Commit updates to the relation map --- do this as late as possible */
    2145      474294 :     AtEOXact_RelationMap(true, is_parallel_worker);
    2146             : 
    2147             :     /*
    2148             :      * set the current transaction state information appropriately during
    2149             :      * commit processing
    2150             :      */
    2151      474294 :     s->state = TRANS_COMMIT;
    2152      474294 :     s->parallelModeLevel = 0;
    2153             : 
    2154      474294 :     if (!is_parallel_worker)
    2155             :     {
    2156             :         /*
    2157             :          * We need to mark our XIDs as committed in pg_xact.  This is where we
    2158             :          * durably commit.
    2159             :          */
    2160      472730 :         latestXid = RecordTransactionCommit();
    2161             :     }
    2162             :     else
    2163             :     {
    2164             :         /*
    2165             :          * We must not mark our XID committed; the parallel master is
    2166             :          * responsible for that.
    2167             :          */
    2168        1564 :         latestXid = InvalidTransactionId;
    2169             : 
    2170             :         /*
    2171             :          * Make sure the master will know about any WAL we wrote before it
    2172             :          * commits.
    2173             :          */
    2174        1564 :         ParallelWorkerReportLastRecEnd(XactLastRecEnd);
    2175             :     }
    2176             : 
    2177             :     TRACE_POSTGRESQL_TRANSACTION_COMMIT(MyProc->lxid);
    2178             : 
    2179             :     /*
    2180             :      * Let others know about no transaction in progress by me. Note that this
    2181             :      * must be done _before_ releasing locks we hold and _after_
    2182             :      * RecordTransactionCommit.
    2183             :      */
    2184      474294 :     ProcArrayEndTransaction(MyProc, latestXid);
    2185             : 
    2186             :     /*
    2187             :      * This is all post-commit cleanup.  Note that if an error is raised here,
    2188             :      * it's too late to abort the transaction.  This should be just
    2189             :      * noncritical resource releasing.
    2190             :      *
    2191             :      * The ordering of operations is not entirely random.  The idea is:
    2192             :      * release resources visible to other backends (eg, files, buffer pins);
    2193             :      * then release locks; then release backend-local resources. We want to
    2194             :      * release locks at the point where any backend waiting for us will see
    2195             :      * our transaction as being fully cleaned up.
    2196             :      *
    2197             :      * Resources that can be associated with individual queries are handled by
    2198             :      * the ResourceOwner mechanism.  The other calls here are for backend-wide
    2199             :      * state.
    2200             :      */
    2201             : 
    2202      474294 :     CallXactCallbacks(is_parallel_worker ? XACT_EVENT_PARALLEL_COMMIT
    2203             :                       : XACT_EVENT_COMMIT);
    2204             : 
    2205      474294 :     ResourceOwnerRelease(TopTransactionResourceOwner,
    2206             :                          RESOURCE_RELEASE_BEFORE_LOCKS,
    2207             :                          true, true);
    2208             : 
    2209             :     /* Check we've released all buffer pins */
    2210      474294 :     AtEOXact_Buffers(true);
    2211             : 
    2212             :     /* Clean up the relation cache */
    2213      474294 :     AtEOXact_RelationCache(true);
    2214             : 
    2215             :     /*
    2216             :      * Make catalog changes visible to all backends.  This has to happen after
    2217             :      * relcache references are dropped (see comments for
    2218             :      * AtEOXact_RelationCache), but before locks are released (if anyone is
    2219             :      * waiting for lock on a relation we've modified, we want them to know
    2220             :      * about the catalog change before they start using the relation).
    2221             :      */
    2222      474294 :     AtEOXact_Inval(true);
    2223             : 
    2224      474294 :     AtEOXact_MultiXact();
    2225             : 
    2226      474294 :     ResourceOwnerRelease(TopTransactionResourceOwner,
    2227             :                          RESOURCE_RELEASE_LOCKS,
    2228             :                          true, true);
    2229      474294 :     ResourceOwnerRelease(TopTransactionResourceOwner,
    2230             :                          RESOURCE_RELEASE_AFTER_LOCKS,
    2231             :                          true, true);
    2232             : 
    2233             :     /*
    2234             :      * Likewise, dropping of files deleted during the transaction is best done
    2235             :      * after releasing relcache and buffer pins.  (This is not strictly
    2236             :      * necessary during commit, since such pins should have been released
    2237             :      * already, but this ordering is definitely critical during abort.)  Since
    2238             :      * this may take many seconds, also delay until after releasing locks.
    2239             :      * Other backends will observe the attendant catalog changes and not
    2240             :      * attempt to access affected files.
    2241             :      */
    2242      474294 :     smgrDoPendingDeletes(true);
    2243             : 
    2244      474294 :     AtCommit_Notify();
    2245      474294 :     AtEOXact_GUC(true, 1);
    2246      474294 :     AtEOXact_SPI(true);
    2247      474294 :     AtEOXact_Enum();
    2248      474294 :     AtEOXact_on_commit_actions(true);
    2249      474294 :     AtEOXact_Namespace(true, is_parallel_worker);
    2250      474294 :     AtEOXact_SMgr();
    2251      474294 :     AtEOXact_Files(true);
    2252      474294 :     AtEOXact_ComboCid();
    2253      474294 :     AtEOXact_HashTables(true);
    2254      474294 :     AtEOXact_PgStat(true, is_parallel_worker);
    2255      474294 :     AtEOXact_Snapshot(true, false);
    2256      474294 :     AtEOXact_ApplyLauncher(true);
    2257      474294 :     pgstat_report_xact_timestamp(0);
    2258             : 
    2259      474294 :     CurrentResourceOwner = NULL;
    2260      474294 :     ResourceOwnerDelete(TopTransactionResourceOwner);
    2261      474294 :     s->curTransactionOwner = NULL;
    2262      474294 :     CurTransactionResourceOwner = NULL;
    2263      474294 :     TopTransactionResourceOwner = NULL;
    2264             : 
    2265      474294 :     AtCommit_Memory();
    2266             : 
    2267      474294 :     s->fullTransactionId = InvalidFullTransactionId;
    2268      474294 :     s->subTransactionId = InvalidSubTransactionId;
    2269      474294 :     s->nestingLevel = 0;
    2270      474294 :     s->gucNestLevel = 0;
    2271      474294 :     s->childXids = NULL;
    2272      474294 :     s->nChildXids = 0;
    2273      474294 :     s->maxChildXids = 0;
    2274             : 
    2275      474294 :     XactTopFullTransactionId = InvalidFullTransactionId;
    2276      474294 :     nParallelCurrentXids = 0;
    2277             : 
    2278             :     /*
    2279             :      * done with commit processing, set current transaction state back to
    2280             :      * default
    2281             :      */
    2282      474294 :     s->state = TRANS_DEFAULT;
    2283             : 
    2284      474294 :     RESUME_INTERRUPTS();
    2285      474294 : }
    2286             : 
    2287             : 
    2288             : /*
    2289             :  *  PrepareTransaction
    2290             :  *
    2291             :  * NB: if you change this routine, better look at CommitTransaction too!
    2292             :  */
    2293             : static void
    2294         132 : PrepareTransaction(void)
    2295             : {
    2296         132 :     TransactionState s = CurrentTransactionState;
    2297         132 :     TransactionId xid = GetCurrentTransactionId();
    2298             :     GlobalTransaction gxact;
    2299             :     TimestampTz prepared_at;
    2300             : 
    2301             :     Assert(!IsInParallelMode());
    2302             : 
    2303         132 :     ShowTransactionState("PrepareTransaction");
    2304             : 
    2305             :     /*
    2306             :      * check the current transaction state
    2307             :      */
    2308         132 :     if (s->state != TRANS_INPROGRESS)
    2309           0 :         elog(WARNING, "PrepareTransaction while in %s state",
    2310             :              TransStateAsString(s->state));
    2311             :     Assert(s->parent == NULL);
    2312             : 
    2313             :     /*
    2314             :      * Do pre-commit processing that involves calling user-defined code, such
    2315             :      * as triggers.  Since closing cursors could queue trigger actions,
    2316             :      * triggers could open cursors, etc, we have to keep looping until there's
    2317             :      * nothing left to do.
    2318             :      */
    2319             :     for (;;)
    2320             :     {
    2321             :         /*
    2322             :          * Fire all currently pending deferred triggers.
    2323             :          */
    2324         136 :         AfterTriggerFireDeferred();
    2325             : 
    2326             :         /*
    2327             :          * Close open portals (converting holdable ones into static portals).
    2328             :          * If there weren't any, we are done ... otherwise loop back to check
    2329             :          * if they queued deferred triggers.  Lather, rinse, repeat.
    2330             :          */
    2331         136 :         if (!PreCommit_Portals(true))
    2332         132 :             break;
    2333             :     }
    2334             : 
    2335         132 :     CallXactCallbacks(XACT_EVENT_PRE_PREPARE);
    2336             : 
    2337             :     /*
    2338             :      * The remaining actions cannot call any user-defined code, so it's safe
    2339             :      * to start shutting down within-transaction services.  But note that most
    2340             :      * of this stuff could still throw an error, which would switch us into
    2341             :      * the transaction-abort path.
    2342             :      */
    2343             : 
    2344             :     /* Shut down the deferred-trigger manager */
    2345         130 :     AfterTriggerEndXact(true);
    2346             : 
    2347             :     /*
    2348             :      * Let ON COMMIT management do its thing (must happen after closing
    2349             :      * cursors, to avoid dangling-reference problems)
    2350             :      */
    2351         130 :     PreCommit_on_commit_actions();
    2352             : 
    2353             :     /*
    2354             :      * Synchronize files that are created and not WAL-logged during this
    2355             :      * transaction. This must happen before EndPrepare(), so that we don't see
    2356             :      * committed-but-broken files after a crash and COMMIT PREPARED.
    2357             :      */
    2358         130 :     smgrDoPendingSyncs(true, false);
    2359             : 
    2360             :     /* close large objects before lower-level cleanup */
    2361         130 :     AtEOXact_LargeObject(true);
    2362             : 
    2363             :     /* NOTIFY requires no work at this point */
    2364             : 
    2365             :     /*
    2366             :      * Mark serializable transaction as complete for predicate locking
    2367             :      * purposes.  This should be done as late as we can put it and still allow
    2368             :      * errors to be raised for failure patterns found at commit.
    2369             :      */
    2370         130 :     PreCommit_CheckForSerializationFailure();
    2371             : 
    2372             :     /*
    2373             :      * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
    2374             :      * this transaction.  Having the prepared xact hold locks on another
    2375             :      * backend's temp table seems a bad idea --- for instance it would prevent
    2376             :      * the backend from exiting.  There are other problems too, such as how to
    2377             :      * clean up the source backend's local buffers and ON COMMIT state if the
    2378             :      * prepared xact includes a DROP of a temp table.
    2379             :      *
    2380             :      * Other objects types, like functions, operators or extensions, share the
    2381             :      * same restriction as they should not be created, locked or dropped as
    2382             :      * this can mess up with this session or even a follow-up session trying
    2383             :      * to use the same temporary namespace.
    2384             :      *
    2385             :      * We must check this after executing any ON COMMIT actions, because they
    2386             :      * might still access a temp relation.
    2387             :      *
    2388             :      * XXX In principle this could be relaxed to allow some useful special
    2389             :      * cases, such as a temp table created and dropped all within the
    2390             :      * transaction.  That seems to require much more bookkeeping though.
    2391             :      */
    2392         130 :     if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPNAMESPACE))
    2393          46 :         ereport(ERROR,
    2394             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2395             :                  errmsg("cannot PREPARE a transaction that has operated on temporary objects")));
    2396             : 
    2397             :     /*
    2398             :      * Likewise, don't allow PREPARE after pg_export_snapshot.  This could be
    2399             :      * supported if we added cleanup logic to twophase.c, but for now it
    2400             :      * doesn't seem worth the trouble.
    2401             :      */
    2402          84 :     if (XactHasExportedSnapshots())
    2403           0 :         ereport(ERROR,
    2404             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2405             :                  errmsg("cannot PREPARE a transaction that has exported snapshots")));
    2406             : 
    2407             :     /*
    2408             :      * Don't allow PREPARE but for transaction that has/might kill logical
    2409             :      * replication workers.
    2410             :      */
    2411          84 :     if (XactManipulatesLogicalReplicationWorkers())
    2412           0 :         ereport(ERROR,
    2413             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    2414             :                  errmsg("cannot PREPARE a transaction that has manipulated logical replication workers")));
    2415             : 
    2416             :     /* Prevent cancel/die interrupt while cleaning up */
    2417          84 :     HOLD_INTERRUPTS();
    2418             : 
    2419             :     /*
    2420             :      * set the current transaction state information appropriately during
    2421             :      * prepare processing
    2422             :      */
    2423          84 :     s->state = TRANS_PREPARE;
    2424             : 
    2425          84 :     prepared_at = GetCurrentTimestamp();
    2426             : 
    2427             :     /* Tell bufmgr and smgr to prepare for commit */
    2428          84 :     BufmgrCommit();
    2429             : 
    2430             :     /*
    2431             :      * Reserve the GID for this transaction. This could fail if the requested
    2432             :      * GID is invalid or already in use.
    2433             :      */
    2434          84 :     gxact = MarkAsPreparing(xid, prepareGID, prepared_at,
    2435             :                             GetUserId(), MyDatabaseId);
    2436          66 :     prepareGID = NULL;
    2437             : 
    2438             :     /*
    2439             :      * Collect data for the 2PC state file.  Note that in general, no actual
    2440             :      * state change should happen in the called modules during this step,
    2441             :      * since it's still possible to fail before commit, and in that case we
    2442             :      * want transaction abort to be able to clean up.  (In particular, the
    2443             :      * AtPrepare routines may error out if they find cases they cannot
    2444             :      * handle.)  State cleanup should happen in the PostPrepare routines
    2445             :      * below.  However, some modules can go ahead and clear state here because
    2446             :      * they wouldn't do anything with it during abort anyway.
    2447             :      *
    2448             :      * Note: because the 2PC state file records will be replayed in the same
    2449             :      * order they are made, the order of these calls has to match the order in
    2450             :      * which we want things to happen during COMMIT PREPARED or ROLLBACK
    2451             :      * PREPARED; in particular, pay attention to whether things should happen
    2452             :      * before or after releasing the transaction's locks.
    2453             :      */
    2454          66 :     StartPrepare(gxact);
    2455             : 
    2456          66 :     AtPrepare_Notify();
    2457          66 :     AtPrepare_Locks();
    2458          66 :     AtPrepare_PredicateLocks();
    2459          66 :     AtPrepare_PgStat();
    2460          66 :     AtPrepare_MultiXact();
    2461          66 :     AtPrepare_RelationMap();
    2462             : 
    2463             :     /*
    2464             :      * Here is where we really truly prepare.
    2465             :      *
    2466             :      * We have to record transaction prepares even if we didn't make any
    2467             :      * updates, because the transaction manager might get confused if we lose
    2468             :      * a global transaction.
    2469             :      */
    2470          66 :     EndPrepare(gxact);
    2471             : 
    2472             :     /*
    2473             :      * Now we clean up backend-internal state and release internal resources.
    2474             :      */
    2475             : 
    2476             :     /* Reset XactLastRecEnd until the next transaction writes something */
    2477          66 :     XactLastRecEnd = 0;
    2478             : 
    2479             :     /*
    2480             :      * Let others know about no transaction in progress by me.  This has to be
    2481             :      * done *after* the prepared transaction has been marked valid, else
    2482             :      * someone may think it is unlocked and recyclable.
    2483             :      */
    2484          66 :     ProcArrayClearTransaction(MyProc);
    2485             : 
    2486             :     /*
    2487             :      * In normal commit-processing, this is all non-critical post-transaction
    2488             :      * cleanup.  When the transaction is prepared, however, it's important
    2489             :      * that the locks and other per-backend resources are transferred to the
    2490             :      * prepared transaction's PGPROC entry.  Note that if an error is raised
    2491             :      * here, it's too late to abort the transaction. XXX: This probably should
    2492             :      * be in a critical section, to force a PANIC if any of this fails, but
    2493             :      * that cure could be worse than the disease.
    2494             :      */
    2495             : 
    2496          66 :     CallXactCallbacks(XACT_EVENT_PREPARE);
    2497             : 
    2498          66 :     ResourceOwnerRelease(TopTransactionResourceOwner,
    2499             :                          RESOURCE_RELEASE_BEFORE_LOCKS,
    2500             :                          true, true);
    2501             : 
    2502             :     /* Check we've released all buffer pins */
    2503          66 :     AtEOXact_Buffers(true);
    2504             : 
    2505             :     /* Clean up the relation cache */
    2506          66 :     AtEOXact_RelationCache(true);
    2507             : 
    2508             :     /* notify doesn't need a postprepare call */
    2509             : 
    2510          66 :     PostPrepare_PgStat();
    2511             : 
    2512          66 :     PostPrepare_Inval();
    2513             : 
    2514          66 :     PostPrepare_smgr();
    2515             : 
    2516          66 :     PostPrepare_MultiXact(xid);
    2517             : 
    2518          66 :     PostPrepare_Locks(xid);
    2519          66 :     PostPrepare_PredicateLocks(xid);
    2520             : 
    2521          66 :     ResourceOwnerRelease(TopTransactionResourceOwner,
    2522             :                          RESOURCE_RELEASE_LOCKS,
    2523             :                          true, true);
    2524          66 :     ResourceOwnerRelease(TopTransactionResourceOwner,
    2525             :                          RESOURCE_RELEASE_AFTER_LOCKS,
    2526             :                          true, true);
    2527             : 
    2528             :     /*
    2529             :      * Allow another backend to finish the transaction.  After
    2530             :      * PostPrepare_Twophase(), the transaction is completely detached from our
    2531             :      * backend.  The rest is just non-critical cleanup of backend-local state.
    2532             :      */
    2533          66 :     PostPrepare_Twophase();
    2534             : 
    2535             :     /* PREPARE acts the same as COMMIT as far as GUC is concerned */
    2536          66 :     AtEOXact_GUC(true, 1);
    2537          66 :     AtEOXact_SPI(true);
    2538          66 :     AtEOXact_Enum();
    2539          66 :     AtEOXact_on_commit_actions(true);
    2540          66 :     AtEOXact_Namespace(true, false);
    2541          66 :     AtEOXact_SMgr();
    2542          66 :     AtEOXact_Files(true);
    2543          66 :     AtEOXact_ComboCid();
    2544          66 :     AtEOXact_HashTables(true);
    2545             :     /* don't call AtEOXact_PgStat here; we fixed pgstat state above */
    2546          66 :     AtEOXact_Snapshot(true, true);
    2547          66 :     pgstat_report_xact_timestamp(0);
    2548             : 
    2549          66 :     CurrentResourceOwner = NULL;
    2550          66 :     ResourceOwnerDelete(TopTransactionResourceOwner);
    2551          66 :     s->curTransactionOwner = NULL;
    2552          66 :     CurTransactionResourceOwner = NULL;
    2553          66 :     TopTransactionResourceOwner = NULL;
    2554             : 
    2555          66 :     AtCommit_Memory();
    2556             : 
    2557          66 :     s->fullTransactionId = InvalidFullTransactionId;
    2558          66 :     s->subTransactionId = InvalidSubTransactionId;
    2559          66 :     s->nestingLevel = 0;
    2560          66 :     s->gucNestLevel = 0;
    2561          66 :     s->childXids = NULL;
    2562          66 :     s->nChildXids = 0;
    2563          66 :     s->maxChildXids = 0;
    2564             : 
    2565          66 :     XactTopFullTransactionId = InvalidFullTransactionId;
    2566          66 :     nParallelCurrentXids = 0;
    2567             : 
    2568             :     /*
    2569             :      * done with 1st phase commit processing, set current transaction state
    2570             :      * back to default
    2571             :      */
    2572          66 :     s->state = TRANS_DEFAULT;
    2573             : 
    2574          66 :     RESUME_INTERRUPTS();
    2575          66 : }
    2576             : 
    2577             : 
    2578             : /*
    2579             :  *  AbortTransaction
    2580             :  */
    2581             : static void
    2582       20336 : AbortTransaction(void)
    2583             : {
    2584       20336 :     TransactionState s = CurrentTransactionState;
    2585             :     TransactionId latestXid;
    2586             :     bool        is_parallel_worker;
    2587             : 
    2588             :     /* Prevent cancel/die interrupt while cleaning up */
    2589       20336 :     HOLD_INTERRUPTS();
    2590             : 
    2591             :     /* Make sure we have a valid memory context and resource owner */
    2592       20336 :     AtAbort_Memory();
    2593       20336 :     AtAbort_ResourceOwner();
    2594             : 
    2595             :     /*
    2596             :      * Release any LW locks we might be holding as quickly as possible.
    2597             :      * (Regular locks, however, must be held till we finish aborting.)
    2598             :      * Releasing LW locks is critical since we might try to grab them again
    2599             :      * while cleaning up!
    2600             :      */
    2601       20336 :     LWLockReleaseAll();
    2602             : 
    2603             :     /* Clear wait information and command progress indicator */
    2604       20336 :     pgstat_report_wait_end();
    2605       20336 :     pgstat_progress_end_command();
    2606             : 
    2607             :     /* Clean up buffer I/O and buffer context locks, too */
    2608       20336 :     AbortBufferIO();
    2609       20336 :     UnlockBuffers();
    2610             : 
    2611             :     /* Reset WAL record construction state */
    2612       20336 :     XLogResetInsertion();
    2613             : 
    2614             :     /* Cancel condition variable sleep */
    2615       20336 :     ConditionVariableCancelSleep();
    2616             : 
    2617             :     /*
    2618             :      * Also clean up any open wait for lock, since the lock manager will choke
    2619             :      * if we try to wait for another lock before doing this.
    2620             :      */
    2621       20336 :     LockErrorCleanup();
    2622             : 
    2623             :     /*
    2624             :      * If any timeout events are still active, make sure the timeout interrupt
    2625             :      * is scheduled.  This covers possible loss of a timeout interrupt due to
    2626             :      * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
    2627             :      * We delay this till after LockErrorCleanup so that we don't uselessly
    2628             :      * reschedule lock or deadlock check timeouts.
    2629             :      */
    2630       20336 :     reschedule_timeouts();
    2631             : 
    2632             :     /*
    2633             :      * Re-enable signals, in case we got here by longjmp'ing out of a signal
    2634             :      * handler.  We do this fairly early in the sequence so that the timeout
    2635             :      * infrastructure will be functional if needed while aborting.
    2636             :      */
    2637       20336 :     PG_SETMASK(&UnBlockSig);
    2638             : 
    2639             :     /*
    2640             :      * check the current transaction state
    2641             :      */
    2642       20336 :     is_parallel_worker = (s->blockState == TBLOCK_PARALLEL_INPROGRESS);
    2643       20336 :     if (s->state != TRANS_INPROGRESS && s->state != TRANS_PREPARE)
    2644           0 :         elog(WARNING, "AbortTransaction while in %s state",
    2645             :              TransStateAsString(s->state));
    2646             :     Assert(s->parent == NULL);
    2647             : 
    2648             :     /*
    2649             :      * set the current transaction state information appropriately during the
    2650             :      * abort processing
    2651             :      */
    2652       20336 :     s->state = TRANS_ABORT;
    2653             : 
    2654             :     /*
    2655             :      * Reset user ID which might have been changed transiently.  We need this
    2656             :      * to clean up in case control escaped out of a SECURITY DEFINER function
    2657             :      * or other local change of CurrentUserId; therefore, the prior value of
    2658             :      * SecurityRestrictionContext also needs to be restored.
    2659             :      *
    2660             :      * (Note: it is not necessary to restore session authorization or role
    2661             :      * settings here because those can only be changed via GUC, and GUC will
    2662             :      * take care of rolling them back if need be.)
    2663             :      */
    2664       20336 :     SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
    2665             : 
    2666             :     /* Forget about any active REINDEX. */
    2667       20336 :     ResetReindexState(s->nestingLevel);
    2668             : 
    2669             :     /* If in parallel mode, clean up workers and exit parallel mode. */
    2670       20336 :     if (IsInParallelMode())
    2671             :     {
    2672           4 :         AtEOXact_Parallel(false);
    2673           4 :         s->parallelModeLevel = 0;
    2674             :     }
    2675             : 
    2676             :     /*
    2677             :      * do abort processing
    2678             :      */
    2679       20336 :     AfterTriggerEndXact(false); /* 'false' means it's abort */
    2680       20336 :     AtAbort_Portals();
    2681       20336 :     smgrDoPendingSyncs(false, is_parallel_worker);
    2682       20336 :     AtEOXact_LargeObject(false);
    2683       20336 :     AtAbort_Notify();
    2684       20336 :     AtEOXact_RelationMap(false, is_parallel_worker);
    2685       20336 :     AtAbort_Twophase();
    2686             : 
    2687             :     /*
    2688             :      * Advertise the fact that we aborted in pg_xact (assuming that we got as
    2689             :      * far as assigning an XID to advertise).  But if we're inside a parallel
    2690             :      * worker, skip this; the user backend must be the one to write the abort
    2691             :      * record.
    2692             :      */
    2693       20336 :     if (!is_parallel_worker)
    2694       20332 :         latestXid = RecordTransactionAbort(false);
    2695             :     else
    2696             :     {
    2697           4 :         latestXid = InvalidTransactionId;
    2698             : 
    2699             :         /*
    2700             :          * Since the parallel master won't get our value of XactLastRecEnd in
    2701             :          * this case, we nudge WAL-writer ourselves in this case.  See related
    2702             :          * comments in RecordTransactionAbort for why this matters.
    2703             :          */
    2704           4 :         XLogSetAsyncXactLSN(XactLastRecEnd);
    2705             :     }
    2706             : 
    2707             :     TRACE_POSTGRESQL_TRANSACTION_ABORT(MyProc->lxid);
    2708             : 
    2709             :     /*
    2710             :      * Let others know about no transaction in progress by me. Note that this
    2711             :      * must be done _before_ releasing locks we hold and _after_
    2712             :      * RecordTransactionAbort.
    2713             :      */
    2714       20336 :     ProcArrayEndTransaction(MyProc, latestXid);
    2715             : 
    2716             :     /*
    2717             :      * Post-abort cleanup.  See notes in CommitTransaction() concerning
    2718             :      * ordering.  We can skip all of it if the transaction failed before
    2719             :      * creating a resource owner.
    2720             :      */
    2721       20336 :     if (TopTransactionResourceOwner != NULL)
    2722             :     {
    2723       20336 :         if (is_parallel_worker)
    2724           4 :             CallXactCallbacks(XACT_EVENT_PARALLEL_ABORT);
    2725             :         else
    2726       20332 :             CallXactCallbacks(XACT_EVENT_ABORT);
    2727             : 
    2728       20336 :         ResourceOwnerRelease(TopTransactionResourceOwner,
    2729             :                              RESOURCE_RELEASE_BEFORE_LOCKS,
    2730             :                              false, true);
    2731       20336 :         AtEOXact_Buffers(false);
    2732       20336 :         AtEOXact_RelationCache(false);
    2733       20336 :         AtEOXact_Inval(false);
    2734       20336 :         AtEOXact_MultiXact();
    2735       20336 :         ResourceOwnerRelease(TopTransactionResourceOwner,
    2736             :                              RESOURCE_RELEASE_LOCKS,
    2737             :                              false, true);
    2738       20336 :         ResourceOwnerRelease(TopTransactionResourceOwner,
    2739             :                              RESOURCE_RELEASE_AFTER_LOCKS,
    2740             :                              false, true);
    2741       20336 :         smgrDoPendingDeletes(false);
    2742             : 
    2743       20336 :         AtEOXact_GUC(false, 1);
    2744       20336 :         AtEOXact_SPI(false);
    2745       20336 :         AtEOXact_Enum();
    2746       20336 :         AtEOXact_on_commit_actions(false);
    2747       20336 :         AtEOXact_Namespace(false, is_parallel_worker);
    2748       20336 :         AtEOXact_SMgr();
    2749       20336 :         AtEOXact_Files(false);
    2750       20336 :         AtEOXact_ComboCid();
    2751       20336 :         AtEOXact_HashTables(false);
    2752       20336 :         AtEOXact_PgStat(false, is_parallel_worker);
    2753       20336 :         AtEOXact_ApplyLauncher(false);
    2754       20336 :         pgstat_report_xact_timestamp(0);
    2755             :     }
    2756             : 
    2757             :     /*
    2758             :      * State remains TRANS_ABORT until CleanupTransaction().
    2759             :      */
    2760       20336 :     RESUME_INTERRUPTS();
    2761       20336 : }
    2762             : 
    2763             : /*
    2764             :  *  CleanupTransaction
    2765             :  */
    2766             : static void
    2767       20336 : CleanupTransaction(void)
    2768             : {
    2769       20336 :     TransactionState s = CurrentTransactionState;
    2770             : 
    2771             :     /*
    2772             :      * State should still be TRANS_ABORT from AbortTransaction().
    2773             :      */
    2774       20336 :     if (s->state != TRANS_ABORT)
    2775           0 :         elog(FATAL, "CleanupTransaction: unexpected state %s",
    2776             :              TransStateAsString(s->state));
    2777             : 
    2778             :     /*
    2779             :      * do abort cleanup processing
    2780             :      */
    2781       20336 :     AtCleanup_Portals();        /* now safe to release portal memory */
    2782       20336 :     AtEOXact_Snapshot(false, true); /* and release the transaction's snapshots */
    2783             : 
    2784       20336 :     CurrentResourceOwner = NULL;    /* and resource owner */
    2785       20336 :     if (TopTransactionResourceOwner)
    2786       20336 :         ResourceOwnerDelete(TopTransactionResourceOwner);
    2787       20336 :     s->curTransactionOwner = NULL;
    2788       20336 :     CurTransactionResourceOwner = NULL;
    2789       20336 :     TopTransactionResourceOwner = NULL;
    2790             : 
    2791       20336 :     AtCleanup_Memory();         /* and transaction memory */
    2792             : 
    2793       20336 :     s->fullTransactionId = InvalidFullTransactionId;
    2794       20336 :     s->subTransactionId = InvalidSubTransactionId;
    2795       20336 :     s->nestingLevel = 0;
    2796       20336 :     s->gucNestLevel = 0;
    2797       20336 :     s->childXids = NULL;
    2798       20336 :     s->nChildXids = 0;
    2799       20336 :     s->maxChildXids = 0;
    2800       20336 :     s->parallelModeLevel = 0;
    2801             : 
    2802       20336 :     XactTopFullTransactionId = InvalidFullTransactionId;
    2803       20336 :     nParallelCurrentXids = 0;
    2804             : 
    2805             :     /*
    2806             :      * done with abort processing, set current transaction state back to
    2807             :      * default
    2808             :      */
    2809       20336 :     s->state = TRANS_DEFAULT;
    2810       20336 : }
    2811             : 
    2812             : /*
    2813             :  *  StartTransactionCommand
    2814             :  */
    2815             : void
    2816      592920 : StartTransactionCommand(void)
    2817             : {
    2818      592920 :     TransactionState s = CurrentTransactionState;
    2819             : 
    2820      592920 :     switch (s->blockState)
    2821             :     {
    2822             :             /*
    2823             :              * if we aren't in a transaction block, we just do our usual start
    2824             :              * transaction.
    2825             :              */
    2826      493100 :         case TBLOCK_DEFAULT:
    2827      493100 :             StartTransaction();
    2828      493100 :             s->blockState = TBLOCK_STARTED;
    2829      493100 :             break;
    2830             : 
    2831             :             /*
    2832             :              * We are somewhere in a transaction block or subtransaction and
    2833             :              * about to start a new command.  For now we do nothing, but
    2834             :              * someday we may do command-local resource initialization. (Note
    2835             :              * that any needed CommandCounterIncrement was done by the
    2836             :              * previous CommitTransactionCommand.)
    2837             :              */
    2838       98790 :         case TBLOCK_INPROGRESS:
    2839             :         case TBLOCK_IMPLICIT_INPROGRESS:
    2840             :         case TBLOCK_SUBINPROGRESS:
    2841       98790 :             break;
    2842             : 
    2843             :             /*
    2844             :              * Here we are in a failed transaction block (one of the commands
    2845             :              * caused an abort) so we do nothing but remain in the abort
    2846             :              * state.  Eventually we will get a ROLLBACK command which will
    2847             :              * get us out of this state.  (It is up to other code to ensure
    2848             :              * that no commands other than ROLLBACK will be processed in these
    2849             :              * states.)
    2850             :              */
    2851        1030 :         case TBLOCK_ABORT:
    2852             :         case TBLOCK_SUBABORT:
    2853        1030 :             break;
    2854             : 
    2855             :             /* These cases are invalid. */
    2856           0 :         case TBLOCK_STARTED:
    2857             :         case TBLOCK_BEGIN:
    2858             :         case TBLOCK_PARALLEL_INPROGRESS:
    2859             :         case TBLOCK_SUBBEGIN:
    2860             :         case TBLOCK_END:
    2861             :         case TBLOCK_SUBRELEASE:
    2862             :         case TBLOCK_SUBCOMMIT:
    2863             :         case TBLOCK_ABORT_END:
    2864             :         case TBLOCK_SUBABORT_END:
    2865             :         case TBLOCK_ABORT_PENDING:
    2866             :         case TBLOCK_SUBABORT_PENDING:
    2867             :         case TBLOCK_SUBRESTART:
    2868             :         case TBLOCK_SUBABORT_RESTART:
    2869             :         case TBLOCK_PREPARE:
    2870           0 :             elog(ERROR, "StartTransactionCommand: unexpected state %s",
    2871             :                  BlockStateAsString(s->blockState));
    2872             :             break;
    2873             :     }
    2874             : 
    2875             :     /*
    2876             :      * We must switch to CurTransactionContext before returning. This is
    2877             :      * already done if we called StartTransaction, otherwise not.
    2878             :      */
    2879      592920 :     Assert(CurTransactionContext != NULL);
    2880      592920 :     MemoryContextSwitchTo(CurTransactionContext);
    2881      592920 : }
    2882             : 
    2883             : 
    2884             : /*
    2885             :  * Simple system for saving and restoring transaction characteristics
    2886             :  * (isolation level, read only, deferrable).  We need this for transaction
    2887             :  * chaining, so that we can set the characteristics of the new transaction to
    2888             :  * be the same as the previous one.  (We need something like this because the
    2889             :  * GUC system resets the characteristics at transaction end, so for example
    2890             :  * just skipping the reset in StartTransaction() won't work.)
    2891             :  */
    2892             : static int  save_XactIsoLevel;
    2893             : static bool save_XactReadOnly;
    2894             : static bool save_XactDeferrable;
    2895             : 
    2896             : void
    2897          36 : SaveTransactionCharacteristics(void)
    2898             : {
    2899          36 :     save_XactIsoLevel = XactIsoLevel;
    2900          36 :     save_XactReadOnly = XactReadOnly;
    2901          36 :     save_XactDeferrable = XactDeferrable;
    2902          36 : }
    2903             : 
    2904             : void
    2905          36 : RestoreTransactionCharacteristics(void)
    2906             : {
    2907          36 :     XactIsoLevel = save_XactIsoLevel;
    2908          36 :     XactReadOnly = save_XactReadOnly;
    2909          36 :     XactDeferrable = save_XactDeferrable;
    2910          36 : }
    2911             : 
    2912             : 
    2913             : /*
    2914             :  *  CommitTransactionCommand
    2915             :  */
    2916             : void
    2917      574184 : CommitTransactionCommand(void)
    2918             : {
    2919      574184 :     TransactionState s = CurrentTransactionState;
    2920             : 
    2921      574184 :     if (s->chain)
    2922          28 :         SaveTransactionCharacteristics();
    2923             : 
    2924      574184 :     switch (s->blockState)
    2925             :     {
    2926             :             /*
    2927             :              * These shouldn't happen.  TBLOCK_DEFAULT means the previous
    2928             :              * StartTransactionCommand didn't set the STARTED state
    2929             :              * appropriately, while TBLOCK_PARALLEL_INPROGRESS should be ended
    2930             :              * by EndParallelWorkerTransaction(), not this function.
    2931             :              */
    2932           0 :         case TBLOCK_DEFAULT:
    2933             :         case TBLOCK_PARALLEL_INPROGRESS:
    2934           0 :             elog(FATAL, "CommitTransactionCommand: unexpected state %s",
    2935             :                  BlockStateAsString(s->blockState));
    2936             :             break;
    2937             : 
    2938             :             /*
    2939             :              * If we aren't in a transaction block, just do our usual
    2940             :              * transaction commit, and return to the idle state.
    2941             :              */
    2942      465028 :         case TBLOCK_STARTED:
    2943      465028 :             CommitTransaction();
    2944      465022 :             s->blockState = TBLOCK_DEFAULT;
    2945      465022 :             break;
    2946             : 
    2947             :             /*
    2948             :              * We are completing a "BEGIN TRANSACTION" command, so we change
    2949             :              * to the "transaction block in progress" state and return.  (We
    2950             :              * assume the BEGIN did nothing to the database, so we need no
    2951             :              * CommandCounterIncrement.)
    2952             :              */
    2953       10088 :         case TBLOCK_BEGIN:
    2954       10088 :             s->blockState = TBLOCK_INPROGRESS;
    2955       10088 :             break;
    2956             : 
    2957             :             /*
    2958             :              * This is the case when we have finished executing a command
    2959             :              * someplace within a transaction block.  We increment the command
    2960             :              * counter and return.
    2961             :              */
    2962       81164 :         case TBLOCK_INPROGRESS:
    2963             :         case TBLOCK_IMPLICIT_INPROGRESS:
    2964             :         case TBLOCK_SUBINPROGRESS:
    2965       81164 :             CommandCounterIncrement();
    2966       81164 :             break;
    2967             : 
    2968             :             /*
    2969             :              * We are completing a "COMMIT" command.  Do it and return to the
    2970             :              * idle state.
    2971             :              */
    2972        7872 :         case TBLOCK_END:
    2973        7872 :             CommitTransaction();
    2974        7542 :             s->blockState = TBLOCK_DEFAULT;
    2975        7542 :             if (s->chain)
    2976             :             {
    2977           8 :                 StartTransaction();
    2978           8 :                 s->blockState = TBLOCK_INPROGRESS;
    2979           8 :                 s->chain = false;
    2980           8 :                 RestoreTransactionCharacteristics();
    2981             :             }
    2982        7542 :             break;
    2983             : 
    2984             :             /*
    2985             :              * Here we are in the middle of a transaction block but one of the
    2986             :              * commands caused an abort so we do nothing but remain in the
    2987             :              * abort state.  Eventually we will get a ROLLBACK command.
    2988             :              */
    2989           0 :         case TBLOCK_ABORT:
    2990             :         case TBLOCK_SUBABORT:
    2991           0 :             break;
    2992             : 
    2993             :             /*
    2994             :              * Here we were in an aborted transaction block and we just got
    2995             :              * the ROLLBACK command from the user, so clean up the
    2996             :              * already-aborted transaction and return to the idle state.
    2997             :              */
    2998         838 :         case TBLOCK_ABORT_END:
    2999         838 :             CleanupTransaction();
    3000         838 :             s->blockState = TBLOCK_DEFAULT;
    3001         838 :             if (s->chain)
    3002             :             {
    3003           8 :                 StartTransaction();
    3004           8 :                 s->blockState = TBLOCK_INPROGRESS;
    3005           8 :                 s->chain = false;
    3006           8 :                 RestoreTransactionCharacteristics();
    3007             :             }
    3008         838 :             break;
    3009             : 
    3010             :             /*
    3011             :              * Here we were in a perfectly good transaction block but the user
    3012             :              * told us to ROLLBACK anyway.  We have to abort the transaction
    3013             :              * and then clean up.
    3014             :              */
    3015         848 :         case TBLOCK_ABORT_PENDING:
    3016         848 :             AbortTransaction();
    3017         848 :             CleanupTransaction();
    3018         848 :             s->blockState = TBLOCK_DEFAULT;
    3019         848 :             if (s->chain)
    3020             :             {
    3021          12 :                 StartTransaction();
    3022          12 :                 s->blockState = TBLOCK_INPROGRESS;
    3023          12 :                 s->chain = false;
    3024          12 :                 RestoreTransactionCharacteristics();
    3025             :             }
    3026         848 :             break;
    3027             : 
    3028             :             /*
    3029             :              * We are completing a "PREPARE TRANSACTION" command.  Do it and
    3030             :              * return to the idle state.
    3031             :              */
    3032          90 :         case TBLOCK_PREPARE:
    3033          90 :             PrepareTransaction();
    3034          26 :             s->blockState = TBLOCK_DEFAULT;
    3035          26 :             break;
    3036             : 
    3037             :             /*
    3038             :              * We were just issued a SAVEPOINT inside a transaction block.
    3039             :              * Start a subtransaction.  (DefineSavepoint already did
    3040             :              * PushTransaction, so as to have someplace to put the SUBBEGIN
    3041             :              * state.)
    3042             :              */
    3043        7256 :         case TBLOCK_SUBBEGIN:
    3044        7256 :             StartSubTransaction();
    3045        7256 :             s->blockState = TBLOCK_SUBINPROGRESS;
    3046        7256 :             break;
    3047             : 
    3048             :             /*
    3049             :              * We were issued a RELEASE command, so we end the current
    3050             :              * subtransaction and return to the parent transaction. The parent
    3051             :              * might be ended too, so repeat till we find an INPROGRESS
    3052             :              * transaction or subtransaction.
    3053             :              */
    3054         162 :         case TBLOCK_SUBRELEASE:
    3055             :             do
    3056             :             {
    3057         302 :                 CommitSubTransaction();
    3058         302 :                 s = CurrentTransactionState;    /* changed by pop */
    3059         302 :             } while (s->blockState == TBLOCK_SUBRELEASE);
    3060             : 
    3061             :             Assert(s->blockState == TBLOCK_INPROGRESS ||
    3062             :                    s->blockState == TBLOCK_SUBINPROGRESS);
    3063         140 :             break;
    3064             : 
    3065             :             /*
    3066             :              * We were issued a COMMIT, so we end the current subtransaction
    3067             :              * hierarchy and perform final commit. We do this by rolling up
    3068             :              * any subtransactions into their parent, which leads to O(N^2)
    3069             :              * operations with respect to resource owners - this isn't that
    3070             :              * bad until we approach a thousands of savepoints but is
    3071             :              * necessary for correctness should after triggers create new
    3072             :              * resource owners.
    3073             :              */
    3074          36 :         case TBLOCK_SUBCOMMIT:
    3075             :             do
    3076             :             {
    3077         262 :                 CommitSubTransaction();
    3078         262 :                 s = CurrentTransactionState;    /* changed by pop */
    3079         262 :             } while (s->blockState == TBLOCK_SUBCOMMIT);
    3080             :             /* If we had a COMMIT command, finish off the main xact too */
    3081         226 :             if (s->blockState == TBLOCK_END)
    3082             :             {
    3083             :                 Assert(s->parent == NULL);
    3084         184 :                 CommitTransaction();
    3085         166 :                 s->blockState = TBLOCK_DEFAULT;
    3086             :             }
    3087          42 :             else if (s->blockState == TBLOCK_PREPARE)
    3088             :             {
    3089             :                 Assert(s->parent == NULL);
    3090          42 :                 PrepareTransaction();
    3091          40 :                 s->blockState = TBLOCK_DEFAULT;
    3092             :             }
    3093             :             else
    3094           0 :                 elog(ERROR, "CommitTransactionCommand: unexpected state %s",
    3095             :                      BlockStateAsString(s->blockState));
    3096         206 :             break;
    3097             : 
    3098             :             /*
    3099             :              * The current already-failed subtransaction is ending due to a
    3100             :              * ROLLBACK or ROLLBACK TO command, so pop it and recursively
    3101             :              * examine the parent (which could be in any of several states).
    3102             :              */
    3103          46 :         case TBLOCK_SUBABORT_END:
    3104          46 :             CleanupSubTransaction();
    3105          46 :             CommitTransactionCommand();
    3106          46 :             break;
    3107             : 
    3108             :             /*
    3109             :              * As above, but it's not dead yet, so abort first.
    3110             :              */
    3111         194 :         case TBLOCK_SUBABORT_PENDING:
    3112         194 :             AbortSubTransaction();
    3113         194 :             CleanupSubTransaction();
    3114         194 :             CommitTransactionCommand();
    3115         194 :             break;
    3116             : 
    3117             :             /*
    3118             :              * The current subtransaction is the target of a ROLLBACK TO
    3119             :              * command.  Abort and pop it, then start a new subtransaction
    3120             :              * with the same name.
    3121             :              */
    3122         298 :         case TBLOCK_SUBRESTART:
    3123             :             {
    3124             :                 char       *name;
    3125             :                 int         savepointLevel;
    3126             : 
    3127             :                 /* save name and keep Cleanup from freeing it */
    3128         298 :                 name = s->name;
    3129         298 :                 s->name = NULL;
    3130         298 :                 savepointLevel = s->savepointLevel;
    3131             : 
    3132         298 :                 AbortSubTransaction();
    3133         298 :                 CleanupSubTransaction();
    3134             : 
    3135         298 :                 DefineSavepoint(NULL);
    3136         298 :                 s = CurrentTransactionState;    /* changed by push */
    3137         298 :                 s->name = name;
    3138         298 :                 s->savepointLevel = savepointLevel;
    3139             : 
    3140             :                 /* This is the same as TBLOCK_SUBBEGIN case */
    3141             :                 AssertState(s->blockState == TBLOCK_SUBBEGIN);
    3142         298 :                 StartSubTransaction();
    3143         298 :                 s->blockState = TBLOCK_SUBINPROGRESS;
    3144             :             }
    3145         298 :             break;
    3146             : 
    3147             :             /*
    3148             :              * Same as above, but the subtransaction had already failed, so we
    3149             :              * don't need AbortSubTransaction.
    3150             :              */
    3151          96 :         case TBLOCK_SUBABORT_RESTART:
    3152             :             {
    3153             :                 char       *name;
    3154             :                 int         savepointLevel;
    3155             : 
    3156             :                 /* save name and keep Cleanup from freeing it */
    3157          96 :                 name = s->name;
    3158          96 :                 s->name = NULL;
    3159          96 :                 savepointLevel = s->savepointLevel;
    3160             : 
    3161          96 :                 CleanupSubTransaction();
    3162             : 
    3163          96 :                 DefineSavepoint(NULL);
    3164          96 :                 s = CurrentTransactionState;    /* changed by push */
    3165          96 :                 s->name = name;
    3166          96 :                 s->savepointLevel = savepointLevel;
    3167             : 
    3168             :                 /* This is the same as TBLOCK_SUBBEGIN case */
    3169             :                 AssertState(s->blockState == TBLOCK_SUBBEGIN);
    3170          96 :                 StartSubTransaction();
    3171          96 :                 s->blockState = TBLOCK_SUBINPROGRESS;
    3172             :             }
    3173          96 :             break;
    3174             :     }
    3175      573764 : }
    3176             : 
    3177             : /*
    3178             :  *  AbortCurrentTransaction
    3179             :  */
    3180             : void
    3181       20532 : AbortCurrentTransaction(void)
    3182             : {
    3183       20532 :     TransactionState s = CurrentTransactionState;
    3184             : 
    3185       20532 :     switch (s->blockState)
    3186             :     {
    3187          24 :         case TBLOCK_DEFAULT:
    3188          24 :             if (s->state == TRANS_DEFAULT)
    3189             :             {
    3190             :                 /* we are idle, so nothing to do */
    3191             :             }
    3192             :             else
    3193             :             {
    3194             :                 /*
    3195             :                  * We can get here after an error during transaction start
    3196             :                  * (state will be TRANS_START).  Need to clean up the
    3197             :                  * incompletely started transaction.  First, adjust the
    3198             :                  * low-level state to suppress warning message from
    3199             :                  * AbortTransaction.
    3200             :                  */
    3201           0 :                 if (s->state == TRANS_START)
    3202           0 :                     s->state = TRANS_INPROGRESS;
    3203           0 :                 AbortTransaction();
    3204           0 :                 CleanupTransaction();
    3205             :             }
    3206          24 :             break;
    3207             : 
    3208             :             /*
    3209             :              * If we aren't in a transaction block, we just do the basic abort
    3210             :              * & cleanup transaction.  For this purpose, we treat an implicit
    3211             :              * transaction block as if it were a simple statement.
    3212             :              */
    3213       17858 :         case TBLOCK_STARTED:
    3214             :         case TBLOCK_IMPLICIT_INPROGRESS:
    3215       17858 :             AbortTransaction();
    3216       17858 :             CleanupTransaction();
    3217       17858 :             s->blockState = TBLOCK_DEFAULT;
    3218       17858 :             break;
    3219             : 
    3220             :             /*
    3221             :              * If we are in TBLOCK_BEGIN it means something screwed up right
    3222             :              * after reading "BEGIN TRANSACTION".  We assume that the user
    3223             :              * will interpret the error as meaning the BEGIN failed to get him
    3224             :              * into a transaction block, so we should abort and return to idle
    3225             :              * state.
    3226             :              */
    3227           0 :         case TBLOCK_BEGIN:
    3228           0 :             AbortTransaction();
    3229           0 :             CleanupTransaction();
    3230           0 :             s->blockState = TBLOCK_DEFAULT;
    3231           0 :             break;
    3232             : 
    3233             :             /*
    3234             :              * We are somewhere in a transaction block and we've gotten a
    3235             :              * failure, so we abort the transaction and set up the persistent
    3236             :              * ABORT state.  We will stay in ABORT until we get a ROLLBACK.
    3237             :              */
    3238         850 :         case TBLOCK_INPROGRESS:
    3239             :         case TBLOCK_PARALLEL_INPROGRESS:
    3240         850 :             AbortTransaction();
    3241         850 :             s->blockState = TBLOCK_ABORT;
    3242             :             /* CleanupTransaction happens when we exit TBLOCK_ABORT_END */
    3243         850 :             break;
    3244             : 
    3245             :             /*
    3246             :              * Here, we failed while trying to COMMIT.  Clean up the
    3247             :              * transaction and return to idle state (we do not want to stay in
    3248             :              * the transaction).
    3249             :              */
    3250         348 :         case TBLOCK_END:
    3251         348 :             AbortTransaction();
    3252         348 :             CleanupTransaction();
    3253         348 :             s->blockState = TBLOCK_DEFAULT;
    3254         348 :             break;
    3255             : 
    3256             :             /*
    3257             :              * Here, we are already in an aborted transaction state and are
    3258             :              * waiting for a ROLLBACK, but for some reason we failed again! So
    3259             :              * we just remain in the abort state.
    3260             :              */
    3261          50 :         case TBLOCK_ABORT:
    3262             :         case TBLOCK_SUBABORT:
    3263          50 :             break;
    3264             : 
    3265             :             /*
    3266             :              * We are in a failed transaction and we got the ROLLBACK command.
    3267             :              * We have already aborted, we just need to cleanup and go to idle
    3268             :              * state.
    3269             :              */
    3270           0 :         case TBLOCK_ABORT_END:
    3271           0 :             CleanupTransaction();
    3272           0 :             s->blockState = TBLOCK_DEFAULT;
    3273           0 :             break;
    3274             : 
    3275             :             /*
    3276             :              * We are in a live transaction and we got a ROLLBACK command.
    3277             :              * Abort, cleanup, go to idle state.
    3278             :              */
    3279           0 :         case TBLOCK_ABORT_PENDING:
    3280           0 :             AbortTransaction();
    3281           0 :             CleanupTransaction();
    3282           0 :             s->blockState = TBLOCK_DEFAULT;
    3283           0 :             break;
    3284             : 
    3285             :             /*
    3286             :              * Here, we failed while trying to PREPARE.  Clean up the
    3287             :              * transaction and return to idle state (we do not want to stay in
    3288             :              * the transaction).
    3289             :              */
    3290          66 :         case TBLOCK_PREPARE:
    3291          66 :             AbortTransaction();
    3292          66 :             CleanupTransaction();
    3293          66 :             s->blockState = TBLOCK_DEFAULT;
    3294          66 :             break;
    3295             : 
    3296             :             /*
    3297             :              * We got an error inside a subtransaction.  Abort just the
    3298             :              * subtransaction, and go to the persistent SUBABORT state until
    3299             :              * we get ROLLBACK.
    3300             :              */
    3301        1336 :         case TBLOCK_SUBINPROGRESS:
    3302        1336 :             AbortSubTransaction();
    3303        1336 :             s->blockState = TBLOCK_SUBABORT;
    3304        1336 :             break;
    3305             : 
    3306             :             /*
    3307             :              * If we failed while trying to create a subtransaction, clean up
    3308             :              * the broken subtransaction and abort the parent.  The same
    3309             :              * applies if we get a failure while ending a subtransaction.
    3310             :              */
    3311           0 :         case TBLOCK_SUBBEGIN:
    3312             :         case TBLOCK_SUBRELEASE:
    3313             :         case TBLOCK_SUBCOMMIT:
    3314             :         case TBLOCK_SUBABORT_PENDING:
    3315             :         case TBLOCK_SUBRESTART:
    3316           0 :             AbortSubTransaction();
    3317           0 :             CleanupSubTransaction();
    3318           0 :             AbortCurrentTransaction();
    3319           0 :             break;
    3320             : 
    3321             :             /*
    3322             :              * Same as above, except the Abort() was already done.
    3323             :              */
    3324           0 :         case TBLOCK_SUBABORT_END:
    3325             :         case TBLOCK_SUBABORT_RESTART:
    3326           0 :             CleanupSubTransaction();
    3327           0 :             AbortCurrentTransaction();
    3328           0 :             break;
    3329             :     }
    3330       20532 : }
    3331             : 
    3332             : /*
    3333             :  *  PreventInTransactionBlock
    3334             :  *
    3335             :  *  This routine is to be called by statements that must not run inside
    3336             :  *  a transaction block, typically because they have non-rollback-able
    3337             :  *  side effects or do internal commits.
    3338             :  *
    3339             :  *  If we have already started a transaction block, issue an error; also issue
    3340             :  *  an error if we appear to be running inside a user-defined function (which
    3341             :  *  could issue more commands and possibly cause a failure after the statement
    3342             :  *  completes).  Subtransactions are verboten too.
    3343             :  *
    3344             :  *  isTopLevel: passed down from ProcessUtility to determine whether we are
    3345             :  *  inside a function.  (We will always fail if this is false, but it's
    3346             :  *  convenient to centralize the check here instead of making callers do it.)
    3347             :  *  stmtType: statement type name, for error messages.
    3348             :  */
    3349             : void
    3350        6178 : PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
    3351             : {
    3352             :     /*
    3353             :      * xact block already started?
    3354             :      */
    3355        6178 :     if (IsTransactionBlock())
    3356          44 :         ereport(ERROR,
    3357             :                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
    3358             :         /* translator: %s represents an SQL statement name */
    3359             :                  errmsg("%s cannot run inside a transaction block",
    3360             :                         stmtType)));
    3361             : 
    3362             :     /*
    3363             :      * subtransaction?
    3364             :      */
    3365        6134 :     if (IsSubTransaction())
    3366           0 :         ereport(ERROR,
    3367             :                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
    3368             :         /* translator: %s represents an SQL statement name */
    3369             :                  errmsg("%s cannot run inside a subtransaction",
    3370             :                         stmtType)));
    3371             : 
    3372             :     /*
    3373             :      * inside a function call?
    3374             :      */
    3375        6134 :     if (!isTopLevel)
    3376           0 :         ereport(ERROR,
    3377             :                 (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
    3378             :         /* translator: %s represents an SQL statement name */
    3379             :                  errmsg("%s cannot be executed from a function", stmtType)));
    3380             : 
    3381             :     /* If we got past IsTransactionBlock test, should be in default state */
    3382        6134 :     if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
    3383        5688 :         CurrentTransactionState->blockState != TBLOCK_STARTED)
    3384           0 :         elog(FATAL, "cannot prevent transaction chain");
    3385             :     /* all okay */
    3386        6134 : }
    3387             : 
    3388             : /*
    3389             :  *  WarnNoTransactionBlock
    3390             :  *  RequireTransactionBlock
    3391             :  *
    3392             :  *  These two functions allow for warnings or errors if a command is executed
    3393             :  *  outside of a transaction block.  This is useful for commands that have no
    3394             :  *  effects that persist past transaction end (and so calling them outside a
    3395             :  *  transaction block is presumably an error).  DECLARE CURSOR is an example.
    3396             :  *  While top-level transaction control commands (BEGIN/COMMIT/ABORT) and SET
    3397             :  *  that have no effect issue warnings, all other no-effect commands generate
    3398             :  *  errors.
    3399             :  *
    3400             :  *  If we appear to be running inside a user-defined function, we do not
    3401             :  *  issue anything, since the function could issue more commands that make
    3402             :  *  use of the current statement's results.  Likewise subtransactions.
    3403             :  *  Thus these are inverses for PreventInTransactionBlock.
    3404             :  *
    3405             :  *  isTopLevel: passed down from ProcessUtility to determine whether we are
    3406             :  *  inside a function.
    3407             :  *  stmtType: statement type name, for warning or error messages.
    3408             :  */
    3409             : void
    3410         868 : WarnNoTransactionBlock(bool isTopLevel, const char *stmtType)
    3411             : {
    3412         868 :     CheckTransactionBlock(isTopLevel, false, stmtType);
    3413         868 : }
    3414             : 
    3415             : void
    3416        7510 : RequireTransactionBlock(bool isTopLevel, const char *stmtType)
    3417             : {
    3418        7510 :     CheckTransactionBlock(isTopLevel, true, stmtType);
    3419        7496 : }
    3420             : 
    3421             : /*
    3422             :  * This is the implementation of the above two.
    3423             :  */
    3424             : static void
    3425        8378 : CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
    3426             : {
    3427             :     /*
    3428             :      * xact block already started?
    3429             :      */
    3430        8378 :     if (IsTransactionBlock())
    3431        8332 :         return;
    3432             : 
    3433             :     /*
    3434             :      * subtransaction?
    3435             :      */
    3436          46 :     if (IsSubTransaction())
    3437           0 :         return;
    3438             : 
    3439             :     /*
    3440             :      * inside a function call?
    3441             :      */
    3442          46 :     if (!isTopLevel)
    3443          24 :         return;
    3444             : 
    3445          22 :     ereport(throwError ? ERROR : WARNING,
    3446             :             (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    3447             :     /* translator: %s represents an SQL statement name */
    3448             :              errmsg("%s can only be used in transaction blocks",
    3449             :                     stmtType)));
    3450             : }
    3451             : 
    3452             : /*
    3453             :  *  IsInTransactionBlock
    3454             :  *
    3455             :  *  This routine is for statements that need to behave differently inside
    3456             :  *  a transaction block than when running as single commands.  ANALYZE is
    3457             :  *  currently the only example.
    3458             :  *
    3459             :  *  isTopLevel: passed down from ProcessUtility to determine whether we are
    3460             :  *  inside a function.
    3461             :  */
    3462             : bool
    3463        9236 : IsInTransactionBlock(bool isTopLevel)
    3464             : {
    3465             :     /*
    3466             :      * Return true on same conditions that would make
    3467             :      * PreventInTransactionBlock error out
    3468             :      */
    3469        9236 :     if (IsTransactionBlock())
    3470          44 :         return true;
    3471             : 
    3472        9192 :     if (IsSubTransaction())
    3473           0 :         return true;
    3474             : 
    3475        9192 :     if (!isTopLevel)
    3476          64 :         return true;
    3477             : 
    3478        9128 :     if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
    3479        9128 :         CurrentTransactionState->blockState != TBLOCK_STARTED)
    3480           0 :         return true;
    3481             : 
    3482        9128 :     return false;
    3483             : }
    3484             : 
    3485             : 
    3486             : /*
    3487             :  * Register or deregister callback functions for start- and end-of-xact
    3488             :  * operations.
    3489             :  *
    3490             :  * These functions are intended for use by dynamically loaded modules.
    3491             :  * For built-in modules we generally just hardwire the appropriate calls
    3492             :  * (mainly because it's easier to control the order that way, where needed).
    3493             :  *
    3494             :  * At transaction end, the callback occurs post-commit or post-abort, so the
    3495             :  * callback functions can only do noncritical cleanup.
    3496             :  */
    3497             : void
    3498        2164 : RegisterXactCallback(XactCallback callback, void *arg)
    3499             : {
    3500             :     XactCallbackItem *item;
    3501             : 
    3502             :     item = (XactCallbackItem *)
    3503        2164 :         MemoryContextAlloc(TopMemoryContext, sizeof(XactCallbackItem));
    3504        2164 :     item->callback = callback;
    3505        2164 :     item->arg = arg;
    3506        2164 :     item->next = Xact_callbacks;
    3507        2164 :     Xact_callbacks = item;
    3508        2164 : }
    3509             : 
    3510             : void
    3511           0 : UnregisterXactCallback(XactCallback callback, void *arg)
    3512             : {
    3513             :     XactCallbackItem *item;
    3514             :     XactCallbackItem *prev;
    3515             : 
    3516           0 :     prev = NULL;
    3517           0 :     for (item = Xact_callbacks; item; prev = item, item = item->next)
    3518             :     {
    3519           0 :         if (item->callback == callback && item->arg == arg)
    3520             :         {
    3521           0 :             if (prev)
    3522           0 :                 prev->next = item->next;
    3523             :             else
    3524           0 :                 Xact_callbacks = item->next;
    3525           0 :             pfree(item);
    3526           0 :             break;
    3527             :         }
    3528             :     }
    3529           0 : }
    3530             : 
    3531             : static void
    3532      969420 : CallXactCallbacks(XactEvent event)
    3533             : {
    3534             :     XactCallbackItem *item;
    3535             : 
    3536     1222022 :     for (item = Xact_callbacks; item; item = item->next)
    3537      252604 :         item->callback(event, item->arg);
    3538      969418 : }
    3539             : 
    3540             : 
    3541             : /*
    3542             :  * Register or deregister callback functions for start- and end-of-subxact
    3543             :  * operations.
    3544             :  *
    3545             :  * Pretty much same as above, but for subtransaction events.
    3546             :  *
    3547             :  * At subtransaction end, the callback occurs post-subcommit or post-subabort,
    3548             :  * so the callback functions can only do noncritical cleanup.  At
    3549             :  * subtransaction start, the callback is called when the subtransaction has
    3550             :  * finished initializing.
    3551             :  */
    3552             : void
    3553        2164 : RegisterSubXactCallback(SubXactCallback callback, void *arg)
    3554             : {
    3555             :     SubXactCallbackItem *item;
    3556             : 
    3557             :     item = (SubXactCallbackItem *)
    3558        2164 :         MemoryContextAlloc(TopMemoryContext, sizeof(SubXactCallbackItem));
    3559        2164 :     item->callback = callback;
    3560        2164 :     item->arg = arg;
    3561        2164 :     item->next = SubXact_callbacks;
    3562        2164 :     SubXact_callbacks = item;
    3563        2164 : }
    3564             : 
    3565             : void
    3566           0 : UnregisterSubXactCallback(SubXactCallback callback, void *arg)
    3567             : {
    3568             :     SubXactCallbackItem *item;
    3569             :     SubXactCallbackItem *prev;
    3570             : 
    3571           0 :     prev = NULL;
    3572           0 :     for (item = SubXact_callbacks; item; prev = item, item = item->next)
    3573             :     {
    3574           0 :         if (item->callback == callback && item->arg == arg)
    3575             :         {
    3576           0 :             if (prev)
    3577           0 :                 prev->next = item->next;
    3578             :             else
    3579           0 :                 SubXact_callbacks = item->next;
    3580           0 :             pfree(item);
    3581           0 :             break;
    3582             :         }
    3583             :     }
    3584           0 : }
    3585             : 
    3586             : static void
    3587       19996 : CallSubXactCallbacks(SubXactEvent event,
    3588             :                      SubTransactionId mySubid,
    3589             :                      SubTransactionId parentSubid)
    3590             : {
    3591             :     SubXactCallbackItem *item;
    3592             : 
    3593       32894 :     for (item = SubXact_callbacks; item; item = item->next)
    3594       12898 :         item->callback(event, mySubid, parentSubid, item->arg);
    3595       19996 : }
    3596             : 
    3597             : 
    3598             : /* ----------------------------------------------------------------
    3599             :  *                     transaction block support
    3600             :  * ----------------------------------------------------------------
    3601             :  */
    3602             : 
    3603             : /*
    3604             :  *  BeginTransactionBlock
    3605             :  *      This executes a BEGIN command.
    3606             :  */
    3607             : void
    3608       10088 : BeginTransactionBlock(void)
    3609             : {
    3610       10088 :     TransactionState s = CurrentTransactionState;
    3611             : 
    3612       10088 :     switch (s->blockState)
    3613             :     {
    3614             :             /*
    3615             :              * We are not inside a transaction block, so allow one to begin.
    3616             :              */
    3617        9212 :         case TBLOCK_STARTED:
    3618        9212 :             s->blockState = TBLOCK_BEGIN;
    3619        9212 :             break;
    3620             : 
    3621             :             /*
    3622             :              * BEGIN converts an implicit transaction block to a regular one.
    3623             :              * (Note that we allow this even if we've already done some
    3624             :              * commands, which is a bit odd but matches historical practice.)
    3625             :              */
    3626         876 :         case TBLOCK_IMPLICIT_INPROGRESS:
    3627         876 :             s->blockState = TBLOCK_BEGIN;
    3628         876 :             break;
    3629             : 
    3630             :             /*
    3631             :              * Already a transaction block in progress.
    3632             :              */
    3633           0 :         case TBLOCK_INPROGRESS:
    3634             :         case TBLOCK_PARALLEL_INPROGRESS:
    3635             :         case TBLOCK_SUBINPROGRESS:
    3636             :         case TBLOCK_ABORT:
    3637             :         case TBLOCK_SUBABORT:
    3638           0 :             ereport(WARNING,
    3639             :                     (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
    3640             :                      errmsg("there is already a transaction in progress")));
    3641           0 :             break;
    3642             : 
    3643             :             /* These cases are invalid. */
    3644           0 :         case TBLOCK_DEFAULT:
    3645             :         case TBLOCK_BEGIN:
    3646             :         case TBLOCK_SUBBEGIN:
    3647             :         case TBLOCK_END:
    3648             :         case TBLOCK_SUBRELEASE:
    3649             :         case TBLOCK_SUBCOMMIT:
    3650             :         case TBLOCK_ABORT_END:
    3651             :         case TBLOCK_SUBABORT_END:
    3652             :         case TBLOCK_ABORT_PENDING:
    3653             :         case TBLOCK_SUBABORT_PENDING:
    3654             :         case TBLOCK_SUBRESTART:
    3655             :         case TBLOCK_SUBABORT_RESTART:
    3656             :         case TBLOCK_PREPARE:
    3657           0 :             elog(FATAL, "BeginTransactionBlock: unexpected state %s",
    3658             :                  BlockStateAsString(s->blockState));
    3659             :             break;
    3660             :     }
    3661       10088 : }
    3662             : 
    3663             : /*
    3664             :  *  PrepareTransactionBlock
    3665             :  *      This executes a PREPARE command.
    3666             :  *
    3667             :  * Since PREPARE may actually do a ROLLBACK, the result indicates what
    3668             :  * happened: true for PREPARE, false for ROLLBACK.
    3669             :  *
    3670             :  * Note that we don't actually do anything here except change blockState.
    3671             :  * The real work will be done in the upcoming PrepareTransaction().
    3672             :  * We do it this way because it's not convenient to change memory context,
    3673             :  * resource owner, etc while executing inside a Portal.
    3674             :  */
    3675             : bool
    3676         134 : PrepareTransactionBlock(const char *gid)
    3677             : {
    3678             :     TransactionState s;
    3679             :     bool        result;
    3680             : 
    3681             :     /* Set up to commit the current transaction */
    3682         134 :     result = EndTransactionBlock(false);
    3683             : 
    3684             :     /* If successful, change outer tblock state to PREPARE */
    3685         134 :     if (result)
    3686             :     {
    3687         132 :         s = CurrentTransactionState;
    3688             : 
    3689         186 :         while (s->parent != NULL)
    3690          54 :             s = s->parent;
    3691             : 
    3692         132 :         if (s->blockState == TBLOCK_END)
    3693             :         {
    3694             :             /* Save GID where PrepareTransaction can find it again */
    3695         132 :             prepareGID = MemoryContextStrdup(TopTransactionContext, gid);
    3696             : 
    3697         132 :             s->blockState = TBLOCK_PREPARE;
    3698             :         }
    3699             :         else
    3700             :         {
    3701             :             /*
    3702             :              * ignore case where we are not in a transaction;
    3703             :              * EndTransactionBlock already issued a warning.
    3704             :              */
    3705             :             Assert(s->blockState == TBLOCK_STARTED ||
    3706             :                    s->blockState == TBLOCK_IMPLICIT_INPROGRESS);
    3707             :             /* Don't send back a PREPARE result tag... */
    3708           0 :             result = false;
    3709             :         }
    3710             :     }
    3711             : 
    3712         134 :     return result;
    3713             : }
    3714             : 
    3715             : /*
    3716             :  *  EndTransactionBlock
    3717             :  *      This executes a COMMIT command.
    3718             :  *
    3719             :  * Since COMMIT may actually do a ROLLBACK, the result indicates what
    3720             :  * happened: true for COMMIT, false for ROLLBACK.
    3721             :  *
    3722             :  * Note that we don't actually do anything here except change blockState.
    3723             :  * The real work will be done in the upcoming CommitTransactionCommand().
    3724             :  * We do it this way because it's not convenient to change memory context,
    3725             :  * resource owner, etc while executing inside a Portal.
    3726             :  */
    3727             : bool
    3728        8788 : EndTransactionBlock(bool chain)
    3729             : {
    3730        8788 :     TransactionState s = CurrentTransactionState;
    3731        8788 :     bool        result = false;
    3732             : 
    3733        8788 :     switch (s->blockState)
    3734             :     {
    3735             :             /*
    3736             :              * We are in a transaction block, so tell CommitTransactionCommand
    3737             :              * to COMMIT.
    3738             :              */
    3739        7946 :         case TBLOCK_INPROGRESS:
    3740        7946 :             s->blockState = TBLOCK_END;
    3741        7946 :             result = true;
    3742        7946 :             break;
    3743             : 
    3744             :             /*
    3745             :              * We are in an implicit transaction block.  If AND CHAIN was
    3746             :              * specified, error.  Otherwise commit, but issue a warning
    3747             :              * because there was no explicit BEGIN before this.
    3748             :              */
    3749          32 :         case TBLOCK_IMPLICIT_INPROGRESS:
    3750          32 :             if (chain)
    3751          16 :                 ereport(ERROR,
    3752             :                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    3753             :                 /* translator: %s represents an SQL statement name */
    3754             :                          errmsg("%s can only be used in transaction blocks",
    3755             :                                 "COMMIT AND CHAIN")));
    3756             :             else
    3757          16 :                 ereport(WARNING,
    3758             :                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    3759             :                          errmsg("there is no transaction in progress")));
    3760          16 :             s->blockState = TBLOCK_END;
    3761          16 :             result = true;
    3762          16 :             break;
    3763             : 
    3764             :             /*
    3765             :              * We are in a failed transaction block.  Tell
    3766             :              * CommitTransactionCommand it's time to exit the block.
    3767             :              */
    3768         528 :         case TBLOCK_ABORT:
    3769         528 :             s->blockState = TBLOCK_ABORT_END;
    3770         528 :             break;
    3771             : 
    3772             :             /*
    3773             :              * We are in a live subtransaction block.  Set up to subcommit all
    3774             :              * open subtransactions and then commit the main transaction.
    3775             :              */
    3776         226 :         case TBLOCK_SUBINPROGRESS:
    3777         488 :             while (s->parent != NULL)
    3778             :             {
    3779         262 :                 if (s->blockState == TBLOCK_SUBINPROGRESS)
    3780         262 :                     s->blockState = TBLOCK_SUBCOMMIT;
    3781             :                 else
    3782           0 :                     elog(FATAL, "EndTransactionBlock: unexpected state %s",
    3783             :                          BlockStateAsString(s->blockState));
    3784         262 :                 s = s->parent;
    3785             :             }
    3786         226 :             if (s->blockState == TBLOCK_INPROGRESS)
    3787         226 :                 s->blockState = TBLOCK_END;
    3788             :             else
    3789           0 :                 elog(FATAL, "EndTransactionBlock: unexpected state %s",
    3790             :                      BlockStateAsString(s->blockState));
    3791         226 :             result = true;
    3792         226 :             break;
    3793             : 
    3794             :             /*
    3795             :              * Here we are inside an aborted subtransaction.  Treat the COMMIT
    3796             :              * as ROLLBACK: set up to abort everything and exit the main
    3797             :              * transaction.
    3798             :              */
    3799          40 :         case TBLOCK_SUBABORT:
    3800          80 :             while (s->parent != NULL)
    3801             :             {
    3802          40 :                 if (s->blockState == TBLOCK_SUBINPROGRESS)
    3803           0 :                     s->blockState = TBLOCK_SUBABORT_PENDING;
    3804          40 :                 else if (s->blockState == TBLOCK_SUBABORT)
    3805          40 :                     s->blockState = TBLOCK_SUBABORT_END;
    3806             :                 else
    3807           0 :                     elog(FATAL, "EndTransactionBlock: unexpected state %s",
    3808             :                          BlockStateAsString(s->blockState));
    3809          40 :                 s = s->parent;
    3810             :             }
    3811          40 :             if (s->blockState == TBLOCK_INPROGRESS)
    3812          40 :                 s->blockState = TBLOCK_ABORT_PENDING;
    3813           0 :             else if (s->blockState == TBLOCK_ABORT)
    3814           0 :                 s->blockState = TBLOCK_ABORT_END;
    3815             :             else
    3816           0 :                 elog(FATAL, "EndTransactionBlock: unexpected state %s",
    3817             :                      BlockStateAsString(s->blockState));
    3818          40 :             break;
    3819             : 
    3820             :             /*
    3821             :              * The user issued COMMIT when not inside a transaction.  For
    3822             :              * COMMIT without CHAIN, issue a WARNING, staying in
    3823             :              * TBLOCK_STARTED state.  The upcoming call to
    3824             :              * CommitTransactionCommand() will then close the transaction and
    3825             :              * put us back into the default state.  For COMMIT AND CHAIN,
    3826             :              * error.
    3827             :              */
    3828          16 :         case TBLOCK_STARTED:
    3829          16 :             if (chain)
    3830           4 :                 ereport(ERROR,
    3831             :                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    3832             :                 /* translator: %s represents an SQL statement name */
    3833             :                          errmsg("%s can only be used in transaction blocks",
    3834             :                                 "COMMIT AND CHAIN")));
    3835             :             else
    3836          12 :                 ereport(WARNING,
    3837             :                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    3838             :                          errmsg("there is no transaction in progress")));
    3839          12 :             result = true;
    3840          12 :             break;
    3841             : 
    3842             :             /*
    3843             :              * The user issued a COMMIT that somehow ran inside a parallel
    3844             :              * worker.  We can't cope with that.
    3845             :              */
    3846           0 :         case TBLOCK_PARALLEL_INPROGRESS:
    3847           0 :             ereport(FATAL,
    3848             :                     (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
    3849             :                      errmsg("cannot commit during a parallel operation")));
    3850             :             break;
    3851             : 
    3852             :             /* These cases are invalid. */
    3853           0 :         case TBLOCK_DEFAULT:
    3854             :         case TBLOCK_BEGIN:
    3855             :         case TBLOCK_SUBBEGIN:
    3856             :         case TBLOCK_END:
    3857             :         case TBLOCK_SUBRELEASE:
    3858             :         case TBLOCK_SUBCOMMIT:
    3859             :         case TBLOCK_ABORT_END:
    3860             :         case TBLOCK_SUBABORT_END:
    3861             :         case TBLOCK_ABORT_PENDING:
    3862             :         case TBLOCK_SUBABORT_PENDING:
    3863             :         case TBLOCK_SUBRESTART:
    3864             :         case TBLOCK_SUBABORT_RESTART:
    3865             :         case TBLOCK_PREPARE:
    3866           0 :             elog(FATAL, "EndTransactionBlock: unexpected state %s",
    3867             :                  BlockStateAsString(s->blockState));
    3868             :             break;
    3869             :     }
    3870             : 
    3871        8768 :     Assert(s->blockState == TBLOCK_STARTED ||
    3872             :            s->blockState == TBLOCK_END ||
    3873             :            s->blockState == TBLOCK_ABORT_END ||
    3874             :            s->blockState == TBLOCK_ABORT_PENDING);
    3875             : 
    3876        8768 :     s->chain = chain;
    3877             : 
    3878        8768 :     return result;
    3879             : }
    3880             : 
    3881             : /*
    3882             :  *  UserAbortTransactionBlock
    3883             :  *      This executes a ROLLBACK command.
    3884             :  *
    3885             :  * As above, we don't actually do anything here except change blockState.
    3886             :  */
    3887             : void
    3888        1138 : UserAbortTransactionBlock(bool chain)
    3889             : {
    3890        1138 :     TransactionState s = CurrentTransactionState;
    3891             : 
    3892        1138 :     switch (s->blockState)
    3893             :     {
    3894             :             /*
    3895             :              * We are inside a transaction block and we got a ROLLBACK command
    3896             :              * from the user, so tell CommitTransactionCommand to abort and
    3897             :              * exit the transaction block.
    3898             :              */
    3899         720 :         case TBLOCK_INPROGRESS:
    3900         720 :             s->blockState = TBLOCK_ABORT_PENDING;
    3901         720 :             break;
    3902             : 
    3903             :             /*
    3904             :              * We are inside a failed transaction block and we got a ROLLBACK
    3905             :              * command from the user.  Abort processing is already done, so
    3906             :              * CommitTransactionCommand just has to cleanup and go back to
    3907             :              * idle state.
    3908             :              */
    3909         310 :         case TBLOCK_ABORT:
    3910         310 :             s->blockState = TBLOCK_ABORT_END;
    3911         310 :             break;
    3912             : 
    3913             :             /*
    3914             :              * We are inside a subtransaction.  Mark everything up to top
    3915             :              * level as exitable.
    3916             :              */
    3917          54 :         case TBLOCK_SUBINPROGRESS:
    3918             :         case TBLOCK_SUBABORT:
    3919         240 :             while (s->parent != NULL)
    3920             :             {
    3921         186 :                 if (s->blockState == TBLOCK_SUBINPROGRESS)
    3922         180 :                     s->blockState = TBLOCK_SUBABORT_PENDING;
    3923           6 :                 else if (s->blockState == TBLOCK_SUBABORT)
    3924           6 :                     s->blockState = TBLOCK_SUBABORT_END;
    3925             :                 else
    3926           0 :                     elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
    3927             :                          BlockStateAsString(s->blockState));
    3928         186 :                 s = s->parent;
    3929             :             }
    3930          54 :             if (s->blockState == TBLOCK_INPROGRESS)
    3931          54 :                 s->blockState = TBLOCK_ABORT_PENDING;
    3932           0 :             else if (s->blockState == TBLOCK_ABORT)
    3933           0 :                 s->blockState = TBLOCK_ABORT_END;
    3934             :             else
    3935           0 :                 elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
    3936             :                      BlockStateAsString(s->blockState));
    3937          54 :             break;
    3938             : 
    3939             :             /*
    3940             :              * The user issued ABORT when not inside a transaction.  For
    3941             :              * ROLLBACK without CHAIN, issue a WARNING and go to abort state.
    3942             :              * The upcoming call to CommitTransactionCommand() will then put
    3943             :              * us back into the default state.  For ROLLBACK AND CHAIN, error.
    3944             :              *
    3945             :              * We do the same thing with ABORT inside an implicit transaction,
    3946             :              * although in this case we might be rolling back actual database
    3947             :              * state changes.  (It's debatable whether we should issue a
    3948             :              * WARNING in this case, but we have done so historically.)
    3949             :              */
    3950          54 :         case TBLOCK_STARTED:
    3951             :         case TBLOCK_IMPLICIT_INPROGRESS:
    3952          54 :             if (chain)
    3953          20 :                 ereport(ERROR,
    3954             :                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    3955             :                 /* translator: %s represents an SQL statement name */
    3956             :                          errmsg("%s can only be used in transaction blocks",
    3957             :                                 "ROLLBACK AND CHAIN")));
    3958             :             else
    3959          34 :                 ereport(WARNING,
    3960             :                         (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    3961             :                          errmsg("there is no transaction in progress")));
    3962          34 :             s->blockState = TBLOCK_ABORT_PENDING;
    3963          34 :             break;
    3964             : 
    3965             :             /*
    3966             :              * The user issued an ABORT that somehow ran inside a parallel
    3967             :              * worker.  We can't cope with that.
    3968             :              */
    3969           0 :         case TBLOCK_PARALLEL_INPROGRESS:
    3970           0 :             ereport(FATAL,
    3971             :                     (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
    3972             :                      errmsg("cannot abort during a parallel operation")));
    3973             :             break;
    3974             : 
    3975             :             /* These cases are invalid. */
    3976           0 :         case TBLOCK_DEFAULT:
    3977             :         case TBLOCK_BEGIN:
    3978             :         case TBLOCK_SUBBEGIN:
    3979             :         case TBLOCK_END:
    3980             :         case TBLOCK_SUBRELEASE:
    3981             :         case TBLOCK_SUBCOMMIT:
    3982             :         case TBLOCK_ABORT_END:
    3983             :         case TBLOCK_SUBABORT_END:
    3984             :         case TBLOCK_ABORT_PENDING:
    3985             :         case TBLOCK_SUBABORT_PENDING:
    3986             :         case TBLOCK_SUBRESTART:
    3987             :         case TBLOCK_SUBABORT_RESTART:
    3988             :         case TBLOCK_PREPARE:
    3989           0 :             elog(FATAL, "UserAbortTransactionBlock: unexpected state %s",
    3990             :                  BlockStateAsString(s->blockState));
    3991             :             break;
    3992             :     }
    3993             : 
    3994        1118 :     Assert(s->blockState == TBLOCK_ABORT_END ||
    3995             :            s->blockState == TBLOCK_ABORT_PENDING);
    3996             : 
    3997        1118 :     s->chain = chain;
    3998        1118 : }
    3999             : 
    4000             : /*
    4001             :  * BeginImplicitTransactionBlock
    4002             :  *      Start an implicit transaction block if we're not already in one.
    4003             :  *
    4004             :  * Unlike BeginTransactionBlock, this is called directly from the main loop
    4005             :  * in postgres.c, not within a Portal.  So we can just change blockState
    4006             :  * without a lot of ceremony.  We do not expect caller to do
    4007             :  * CommitTransactionCommand/StartTransactionCommand.
    4008             :  */
    4009             : void
    4010       81732 : BeginImplicitTransactionBlock(void)
    4011             : {
    4012       81732 :     TransactionState s = CurrentTransactionState;
    4013             : 
    4014             :     /*
    4015             :      * If we are in STARTED state (that is, no transaction block is open),
    4016             :      * switch to IMPLICIT_INPROGRESS state, creating an implicit transaction
    4017             :      * block.
    4018             :      *
    4019             :      * For caller convenience, we consider all other transaction states as
    4020             :      * legal here; otherwise the caller would need its own state check, which
    4021             :      * seems rather pointless.
    4022             :      */
    4023       81732 :     if (s->blockState == TBLOCK_STARTED)
    4024       20942 :         s->blockState = TBLOCK_IMPLICIT_INPROGRESS;
    4025       81732 : }
    4026             : 
    4027             : /*
    4028             :  * EndImplicitTransactionBlock
    4029             :  *      End an implicit transaction block, if we're in one.
    4030             :  *
    4031             :  * Like EndTransactionBlock, we just make any needed blockState change here.
    4032             :  * The real work will be done in the upcoming CommitTransactionCommand().
    4033             :  */
    4034             : void
    4035       20914 : EndImplicitTransactionBlock(void)
    4036             : {
    4037       20914 :     TransactionState s = CurrentTransactionState;
    4038             : 
    4039             :     /*
    4040             :      * If we are in IMPLICIT_INPROGRESS state, switch back to STARTED state,
    4041             :      * allowing CommitTransactionCommand to commit whatever happened during
    4042             :      * the implicit transaction block as though it were a single statement.
    4043             :      *
    4044             :      * For caller convenience, we consider all other transaction states as
    4045             :      * legal here; otherwise the caller would need its own state check, which
    4046             :      * seems rather pointless.
    4047             :      */
    4048       20914 :     if (s->blockState == TBLOCK_IMPLICIT_INPROGRESS)
    4049       19968 :         s->blockState = TBLOCK_STARTED;
    4050       20914 : }
    4051             : 
    4052             : /*
    4053             :  * DefineSavepoint
    4054             :  *      This executes a SAVEPOINT command.
    4055             :  */
    4056             : void
    4057        1206 : DefineSavepoint(const char *name)
    4058             : {
    4059        1206 :     TransactionState s = CurrentTransactionState;
    4060             : 
    4061             :     /*
    4062             :      * Workers synchronize transaction state at the beginning of each parallel
    4063             :      * operation, so we can't account for new subtransactions after that
    4064             :      * point.  (Note that this check will certainly error out if s->blockState
    4065             :      * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
    4066             :      * below.)
    4067             :      */
    4068        1206 :     if (IsInParallelMode())
    4069           0 :         ereport(ERROR,
    4070             :                 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
    4071             :                  errmsg("cannot define savepoints during a parallel operation")));
    4072             : 
    4073        1206 :     switch (s->blockState)
    4074             :     {
    4075        1198 :         case TBLOCK_INPROGRESS:
    4076             :         case TBLOCK_SUBINPROGRESS:
    4077             :             /* Normal subtransaction start */
    4078        1198 :             PushTransaction();
    4079        1198 :             s = CurrentTransactionState;    /* changed by push */
    4080             : 
    4081             :             /*
    4082             :              * Savepoint names, like the TransactionState block itself, live
    4083             :              * in TopTransactionContext.
    4084             :              */
    4085        1198 :             if (name)
    4086         804 :                 s->name = MemoryContextStrdup(TopTransactionContext, name);
    4087        1198 :             break;
    4088             : 
    4089             :             /*
    4090             :              * We disallow savepoint commands in implicit transaction blocks.
    4091             :              * There would be no great difficulty in allowing them so far as
    4092             :              * this module is concerned, but a savepoint seems inconsistent
    4093             :              * with exec_simple_query's behavior of abandoning the whole query
    4094             :              * string upon error.  Also, the point of an implicit transaction
    4095             :              * block (as opposed to a regular one) is to automatically close
    4096             :              * after an error, so it's hard to see how a savepoint would fit
    4097             :              * into that.
    4098             :              *
    4099             :              * The error messages for this are phrased as if there were no
    4100             :              * active transaction block at all, which is historical but
    4101             :              * perhaps could be improved.
    4102             :              */
    4103           8 :         case TBLOCK_IMPLICIT_INPROGRESS:
    4104           8 :             ereport(ERROR,
    4105             :                     (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    4106             :             /* translator: %s represents an SQL statement name */
    4107             :                      errmsg("%s can only be used in transaction blocks",
    4108             :                             "SAVEPOINT")));
    4109             :             break;
    4110             : 
    4111             :             /* These cases are invalid. */
    4112           0 :         case TBLOCK_DEFAULT:
    4113             :         case TBLOCK_STARTED:
    4114             :         case TBLOCK_BEGIN:
    4115             :         case TBLOCK_PARALLEL_INPROGRESS:
    4116             :         case TBLOCK_SUBBEGIN:
    4117             :         case TBLOCK_END:
    4118             :         case TBLOCK_SUBRELEASE:
    4119             :         case TBLOCK_SUBCOMMIT:
    4120             :         case TBLOCK_ABORT:
    4121             :         case TBLOCK_SUBABORT:
    4122             :         case TBLOCK_ABORT_END:
    4123             :         case TBLOCK_SUBABORT_END:
    4124             :         case TBLOCK_ABORT_PENDING:
    4125             :         case TBLOCK_SUBABORT_PENDING:
    4126             :         case TBLOCK_SUBRESTART:
    4127             :         case TBLOCK_SUBABORT_RESTART:
    4128             :         case TBLOCK_PREPARE:
    4129           0 :             elog(FATAL, "DefineSavepoint: unexpected state %s",
    4130             :                  BlockStateAsString(s->blockState));
    4131             :             break;
    4132             :     }
    4133        1198 : }
    4134             : 
    4135             : /*
    4136             :  * ReleaseSavepoint
    4137             :  *      This executes a RELEASE command.
    4138             :  *
    4139             :  * As above, we don't actually do anything here except change blockState.
    4140             :  */
    4141             : void
    4142         144 : ReleaseSavepoint(const char *name)
    4143             : {
    4144         144 :     TransactionState s = CurrentTransactionState;
    4145             :     TransactionState target,
    4146             :                 xact;
    4147             : 
    4148             :     /*
    4149             :      * Workers synchronize transaction state at the beginning of each parallel
    4150             :      * operation, so we can't account for transaction state change after that
    4151             :      * point.  (Note that this check will certainly error out if s->blockState
    4152             :      * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
    4153             :      * below.)
    4154             :      */
    4155         144 :     if (IsInParallelMode())
    4156           0 :         ereport(ERROR,
    4157             :                 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
    4158             :                  errmsg("cannot release savepoints during a parallel operation")));
    4159             : 
    4160         144 :     switch (s->blockState)
    4161             :     {
    4162             :             /*
    4163             :              * We can't release a savepoint if there is no savepoint defined.
    4164             :              */
    4165           0 :         case TBLOCK_INPROGRESS:
    4166           0 :             ereport(ERROR,
    4167             :                     (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
    4168             :                      errmsg("savepoint \"%s\" does not exist", name)));
    4169             :             break;
    4170             : 
    4171           4 :         case TBLOCK_IMPLICIT_INPROGRESS:
    4172             :             /* See comment about implicit transactions in DefineSavepoint */
    4173           4 :             ereport(ERROR,
    4174             :                     (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    4175             :             /* translator: %s represents an SQL statement name */
    4176             :                      errmsg("%s can only be used in transaction blocks",
    4177             :                             "RELEASE SAVEPOINT")));
    4178             :             break;
    4179             : 
    4180             :             /*
    4181             :              * We are in a non-aborted subtransaction.  This is the only valid
    4182             :              * case.
    4183             :              */
    4184         140 :         case TBLOCK_SUBINPROGRESS:
    4185         140 :             break;
    4186             : 
    4187             :             /* These cases are invalid. */
    4188           0 :         case TBLOCK_DEFAULT:
    4189             :         case TBLOCK_STARTED:
    4190             :         case TBLOCK_BEGIN:
    4191             :         case TBLOCK_PARALLEL_INPROGRESS:
    4192             :         case TBLOCK_SUBBEGIN:
    4193             :         case TBLOCK_END:
    4194             :         case TBLOCK_SUBRELEASE:
    4195             :         case TBLOCK_SUBCOMMIT:
    4196             :         case TBLOCK_ABORT:
    4197             :         case TBLOCK_SUBABORT:
    4198             :         case TBLOCK_ABORT_END:
    4199             :         case TBLOCK_SUBABORT_END:
    4200             :         case TBLOCK_ABORT_PENDING:
    4201             :         case TBLOCK_SUBABORT_PENDING:
    4202             :         case TBLOCK_SUBRESTART:
    4203             :         case TBLOCK_SUBABORT_RESTART:
    4204             :         case TBLOCK_PREPARE:
    4205           0 :             elog(FATAL, "ReleaseSavepoint: unexpected state %s",
    4206             :                  BlockStateAsString(s->blockState));
    4207             :             break;
    4208             :     }
    4209             : 
    4210         302 :     for (target = s; PointerIsValid(target); target = target->parent)
    4211             :     {
    4212         302 :         if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
    4213         140 :             break;
    4214             :     }
    4215             : 
    4216         140 :     if (!PointerIsValid(target))
    4217           0 :         ereport(ERROR,
    4218             :                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
    4219             :                  errmsg("savepoint \"%s\" does not exist", name)));
    4220             : 
    4221             :     /* disallow crossing savepoint level boundaries */
    4222         140 :     if (target->savepointLevel != s->savepointLevel)
    4223           0 :         ereport(ERROR,
    4224             :                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
    4225             :                  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
    4226             : 
    4227             :     /*
    4228             :      * Mark "commit pending" all subtransactions up to the target
    4229             :      * subtransaction.  The actual commits will happen when control gets to
    4230             :      * CommitTransactionCommand.
    4231             :      */
    4232         140 :     xact = CurrentTransactionState;
    4233             :     for (;;)
    4234             :     {
    4235         162 :         Assert(xact->blockState == TBLOCK_SUBINPROGRESS);
    4236         302 :         xact->blockState = TBLOCK_SUBRELEASE;
    4237         302 :         if (xact == target)
    4238         140 :             break;
    4239         162 :         xact = xact->parent;
    4240             :         Assert(PointerIsValid(xact));
    4241             :     }
    4242         140 : }
    4243             : 
    4244             : /*
    4245             :  * RollbackToSavepoint
    4246             :  *      This executes a ROLLBACK TO <savepoint> command.
    4247             :  *
    4248             :  * As above, we don't actually do anything here except change blockState.
    4249             :  */
    4250             : void
    4251         402 : RollbackToSavepoint(const char *name)
    4252             : {
    4253         402 :     TransactionState s = CurrentTransactionState;
    4254             :     TransactionState target,
    4255             :                 xact;
    4256             : 
    4257             :     /*
    4258             :      * Workers synchronize transaction state at the beginning of each parallel
    4259             :      * operation, so we can't account for transaction state change after that
    4260             :      * point.  (Note that this check will certainly error out if s->blockState
    4261             :      * is TBLOCK_PARALLEL_INPROGRESS, so we can treat that as an invalid case
    4262             :      * below.)
    4263             :      */
    4264         402 :     if (IsInParallelMode())
    4265           0 :         ereport(ERROR,
    4266             :                 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
    4267             :                  errmsg("cannot rollback to savepoints during a parallel operation")));
    4268             : 
    4269         402 :     switch (s->blockState)
    4270             :     {
    4271             :             /*
    4272             :              * We can't rollback to a savepoint if there is no savepoint
    4273             :              * defined.
    4274             :              */
    4275           4 :         case TBLOCK_INPROGRESS:
    4276             :         case TBLOCK_ABORT:
    4277           4 :             ereport(ERROR,
    4278             :                     (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
    4279             :                      errmsg("savepoint \"%s\" does not exist", name)));
    4280             :             break;
    4281             : 
    4282           4 :         case TBLOCK_IMPLICIT_INPROGRESS:
    4283             :             /* See comment about implicit transactions in DefineSavepoint */
    4284           4 :             ereport(ERROR,
    4285             :                     (errcode(ERRCODE_NO_ACTIVE_SQL_TRANSACTION),
    4286             :             /* translator: %s represents an SQL statement name */
    4287             :                      errmsg("%s can only be used in transaction blocks",
    4288             :                             "ROLLBACK TO SAVEPOINT")));
    4289             :             break;
    4290             : 
    4291             :             /*
    4292             :              * There is at least one savepoint, so proceed.
    4293             :              */
    4294         394 :         case TBLOCK_SUBINPROGRESS:
    4295             :         case TBLOCK_SUBABORT:
    4296         394 :             break;
    4297             : 
    4298             :             /* These cases are invalid. */
    4299           0 :         case TBLOCK_DEFAULT:
    4300             :         case TBLOCK_STARTED:
    4301             :         case TBLOCK_BEGIN:
    4302             :         case TBLOCK_PARALLEL_INPROGRESS:
    4303             :         case TBLOCK_SUBBEGIN:
    4304             :         case TBLOCK_END:
    4305             :         case TBLOCK_SUBRELEASE:
    4306             :         case TBLOCK_SUBCOMMIT:
    4307             :         case TBLOCK_ABORT_END:
    4308             :         case TBLOCK_SUBABORT_END:
    4309             :         case TBLOCK_ABORT_PENDING:
    4310             :         case TBLOCK_SUBABORT_PENDING:
    4311             :         case TBLOCK_SUBRESTART:
    4312             :         case TBLOCK_SUBABORT_RESTART:
    4313             :         case TBLOCK_PREPARE:
    4314           0 :             elog(FATAL, "RollbackToSavepoint: unexpected state %s",
    4315             :                  BlockStateAsString(s->blockState));
    4316             :             break;
    4317             :     }
    4318             : 
    4319         408 :     for (target = s; PointerIsValid(target); target = target->parent)
    4320             :     {
    4321         408 :         if (PointerIsValid(target->name) && strcmp(target->name, name) == 0)
    4322         394 :             break;
    4323             :     }
    4324             : 
    4325         394 :     if (!PointerIsValid(target))
    4326           0 :         ereport(ERROR,
    4327             :                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
    4328             :                  errmsg("savepoint \"%s\" does not exist", name)));
    4329             : 
    4330             :     /* disallow crossing savepoint level boundaries */
    4331         394 :     if (target->savepointLevel != s->savepointLevel)
    4332           0 :         ereport(ERROR,
    4333             :                 (errcode(ERRCODE_S_E_INVALID_SPECIFICATION),
    4334             :                  errmsg("savepoint \"%s\" does not exist within current savepoint level", name)));
    4335             : 
    4336             :     /*
    4337             :      * Mark "abort pending" all subtransactions up to the target
    4338             :      * subtransaction.  The actual aborts will happen when control gets to
    4339             :      * CommitTransactionCommand.
    4340             :      */
    4341         394 :     xact = CurrentTransactionState;
    4342             :     for (;;)
    4343             :     {
    4344         408 :         if (xact == target)
    4345         394 :             break;
    4346          14 :         if (xact->blockState == TBLOCK_SUBINPROGRESS)
    4347          14 :             xact->blockState = TBLOCK_SUBABORT_PENDING;
    4348           0 :         else if (xact->blockState == TBLOCK_SUBABORT)
    4349           0 :             xact->blockState = TBLOCK_SUBABORT_END;
    4350             :         else
    4351           0 :             elog(FATAL, "RollbackToSavepoint: unexpected state %s",
    4352             :                  BlockStateAsString(xact->blockState));
    4353          14 :         xact = xact->parent;
    4354             :         Assert(PointerIsValid(xact));
    4355             :     }
    4356             : 
    4357             :     /* And mark the target as "restart pending" */
    4358         394 :     if (xact->blockState == TBLOCK_SUBINPROGRESS)
    4359         298 :         xact->blockState = TBLOCK_SUBRESTART;
    4360          96 :     else if (xact->blockState == TBLOCK_SUBABORT)
    4361          96 :         xact->blockState = TBLOCK_SUBABORT_RESTART;
    4362             :     else
    4363           0 :         elog(FATAL, "RollbackToSavepoint: unexpected state %s",
    4364             :              BlockStateAsString(xact->blockState));
    4365         394 : }
    4366             : 
    4367             : /*
    4368             :  * BeginInternalSubTransaction
    4369             :  *      This is the same as DefineSavepoint except it allows TBLOCK_STARTED,
    4370             :  *      TBLOCK_IMPLICIT_INPROGRESS, TBLOCK_END, and TBLOCK_PREPARE states,
    4371             :  *      and therefore it can safely be used in functions that might be called
    4372             :  *      when not inside a BEGIN block or when running deferred triggers at
    4373             :  *      COMMIT/PREPARE time.  Also, it automatically does
    4374             :  *      CommitTransactionCommand/StartTransactionCommand instead of expecting
    4375             :  *      the caller to do it.
    4376             :  */
    4377             : void
    4378        6452 : BeginInternalSubTransaction(const char *name)
    4379             : {
    4380        6452 :     TransactionState s = CurrentTransactionState;
    4381             : 
    4382             :     /*
    4383             :      * Workers synchronize transaction state at the beginning of each parallel
    4384             :      * operation, so we can't account for new subtransactions after that
    4385             :      * point. We might be able to make an exception for the type of
    4386             :      * subtransaction established by this function, which is typically used in
    4387             :      * contexts where we're going to release or roll back the subtransaction
    4388             :      * before proceeding further, so that no enduring change to the
    4389             :      * transaction state occurs. For now, however, we prohibit this case along
    4390             :      * with all the others.
    4391             :      */
    4392        6452 :     if (IsInParallelMode())
    4393           0 :         ereport(ERROR,
    4394             :                 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
    4395             :                  errmsg("cannot start subtransactions during a parallel operation")));
    4396             : 
    4397        6452 :     switch (s->blockState)
    4398             :     {
    4399        6452 :         case TBLOCK_STARTED:
    4400             :         case TBLOCK_INPROGRESS:
    4401             :         case TBLOCK_IMPLICIT_INPROGRESS:
    4402             :         case TBLOCK_END:
    4403             :         case TBLOCK_PREPARE:
    4404             :         case TBLOCK_SUBINPROGRESS:
    4405             :             /* Normal subtransaction start */
    4406        6452 :             PushTransaction();
    4407        6452 :             s = CurrentTransactionState;    /* changed by push */
    4408             : 
    4409             :             /*
    4410             :              * Savepoint names, like the TransactionState block itself, live
    4411             :              * in TopTransactionContext.
    4412             :              */
    4413        6452 :             if (name)
    4414        1194 :                 s->name = MemoryContextStrdup(TopTransactionContext, name);
    4415        6452 :             break;
    4416             : 
    4417             :             /* These cases are invalid. */
    4418           0 :         case TBLOCK_DEFAULT:
    4419             :         case TBLOCK_BEGIN:
    4420             :         case TBLOCK_PARALLEL_INPROGRESS:
    4421             :         case TBLOCK_SUBBEGIN:
    4422             :         case TBLOCK_SUBRELEASE:
    4423             :         case TBLOCK_SUBCOMMIT:
    4424             :         case TBLOCK_ABORT:
    4425             :         case TBLOCK_SUBABORT:
    4426             :         case TBLOCK_ABORT_END:
    4427             :         case TBLOCK_SUBABORT_END:
    4428             :         case TBLOCK_ABORT_PENDING:
    4429             :         case TBLOCK_SUBABORT_PENDING:
    4430             :         case TBLOCK_SUBRESTART:
    4431             :         case TBLOCK_SUBABORT_RESTART:
    4432           0 :             elog(FATAL, "BeginInternalSubTransaction: unexpected state %s",
    4433             :                  BlockStateAsString(s->blockState));
    4434             :             break;
    4435             :     }
    4436             : 
    4437        6452 :     CommitTransactionCommand();
    4438        6452 :     StartTransactionCommand();
    4439        6452 : }
    4440             : 
    4441             : /*
    4442             :  * ReleaseCurrentSubTransaction
    4443             :  *
    4444             :  * RELEASE (ie, commit) the innermost subtransaction, regardless of its
    4445             :  * savepoint name (if any).
    4446             :  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
    4447             :  */
    4448             : void
    4449        4132 : ReleaseCurrentSubTransaction(void)
    4450             : {
    4451        4132 :     TransactionState s = CurrentTransactionState;
    4452             : 
    4453             :     /*
    4454             :      * Workers synchronize transaction state at the beginning of each parallel
    4455             :      * operation, so we can't account for commit of subtransactions after that
    4456             :      * point.  This should not happen anyway.  Code calling this would
    4457             :      * typically have called BeginInternalSubTransaction() first, failing
    4458             :      * there.
    4459             :      */
    4460        4132 :     if (IsInParallelMode())
    4461           0 :         ereport(ERROR,
    4462             :                 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
    4463             :                  errmsg("cannot commit subtransactions during a parallel operation")));
    4464             : 
    4465        4132 :     if (s->blockState != TBLOCK_SUBINPROGRESS)
    4466           0 :         elog(ERROR, "ReleaseCurrentSubTransaction: unexpected state %s",
    4467             :              BlockStateAsString(s->blockState));
    4468             :     Assert(s->state == TRANS_INPROGRESS);
    4469        4132 :     MemoryContextSwitchTo(CurTransactionContext);
    4470        4132 :     CommitSubTransaction();
    4471        4132 :     s = CurrentTransactionState;    /* changed by pop */
    4472             :     Assert(s->state == TRANS_INPROGRESS);
    4473        4132 : }
    4474             : 
    4475             : /*
    4476             :  * RollbackAndReleaseCurrentSubTransaction
    4477             :  *
    4478             :  * ROLLBACK and RELEASE (ie, abort) the innermost subtransaction, regardless
    4479             :  * of its savepoint name (if any).
    4480             :  * NB: do NOT use CommitTransactionCommand/StartTransactionCommand with this.
    4481             :  */
    4482             : void
    4483        2320 : RollbackAndReleaseCurrentSubTransaction(void)
    4484             : {
    4485        2320 :     TransactionState s = CurrentTransactionState;
    4486             : 
    4487             :     /*
    4488             :      * Unlike ReleaseCurrentSubTransaction(), this is nominally permitted
    4489             :      * during parallel operations.  That's because we may be in the master,
    4490             :      * recovering from an error thrown while we were in parallel mode.  We
    4491             :      * won't reach here in a worker, because BeginInternalSubTransaction()
    4492             :      * will have failed.
    4493             :      */
    4494             : 
    4495        2320 :     switch (s->blockState)
    4496             :     {
    4497             :             /* Must be in a subtransaction */
    4498        2320 :         case TBLOCK_SUBINPROGRESS:
    4499             :         case TBLOCK_SUBABORT:
    4500        2320 :             break;
    4501             : 
    4502             :             /* These cases are invalid. */
    4503           0 :         case TBLOCK_DEFAULT:
    4504             :         case TBLOCK_STARTED:
    4505             :         case TBLOCK_BEGIN:
    4506             :         case TBLOCK_IMPLICIT_INPROGRESS:
    4507             :         case TBLOCK_PARALLEL_INPROGRESS:
    4508             :         case TBLOCK_SUBBEGIN:
    4509             :         case TBLOCK_INPROGRESS:
    4510             :         case TBLOCK_END:
    4511             :         case TBLOCK_SUBRELEASE:
    4512             :         case TBLOCK_SUBCOMMIT:
    4513             :         case TBLOCK_ABORT:
    4514             :         case TBLOCK_ABORT_END:
    4515             :         case TBLOCK_SUBABORT_END:
    4516             :         case TBLOCK_ABORT_PENDING:
    4517             :         case TBLOCK_SUBABORT_PENDING:
    4518             :         case TBLOCK_SUBRESTART:
    4519             :         case TBLOCK_SUBABORT_RESTART:
    4520             :         case TBLOCK_PREPARE:
    4521           0 :             elog(FATAL, "RollbackAndReleaseCurrentSubTransaction: unexpected state %s",
    4522             :                  BlockStateAsString(s->blockState));
    4523             :             break;
    4524             :     }
    4525             : 
    4526             :     /*
    4527             :      * Abort the current subtransaction, if needed.
    4528             :      */
    4529        2320 :     if (s->blockState == TBLOCK_SUBINPROGRESS)
    4530        1126 :         AbortSubTransaction();
    4531             : 
    4532             :     /* And clean it up, too */
    4533        2320 :     CleanupSubTransaction();
    4534             : 
    4535        2320 :     s = CurrentTransactionState;    /* changed by pop */
    4536             :     AssertState(s->blockState == TBLOCK_SUBINPROGRESS ||
    4537             :                 s->blockState == TBLOCK_INPROGRESS ||
    4538             :                 s->blockState == TBLOCK_IMPLICIT_INPROGRESS ||
    4539             :                 s->blockState == TBLOCK_STARTED);
    4540        2320 : }
    4541             : 
    4542             : /*
    4543             :  *  AbortOutOfAnyTransaction
    4544             :  *
    4545             :  *  This routine is provided for error recovery purposes.  It aborts any
    4546             :  *  active transaction or transaction block, leaving the system in a known
    4547             :  *  idle state.
    4548             :  */
    4549             : void
    4550       11356 : AbortOutOfAnyTransaction(void)
    4551             : {
    4552       11356 :     TransactionState s = CurrentTransactionState;
    4553             : 
    4554             :     /* Ensure we're not running in a doomed memory context */
    4555       11356 :     AtAbort_Memory();
    4556             : 
    4557             :     /*
    4558             :      * Get out of any transaction or nested transaction
    4559             :      */
    4560             :     do
    4561             :     {
    4562       11356 :         switch (s->blockState)
    4563             :         {
    4564       10978 :             case TBLOCK_DEFAULT:
    4565       10978 :                 if (s->state == TRANS_DEFAULT)
    4566             :                 {
    4567             :                     /* Not in a transaction, do nothing */
    4568             :                 }
    4569             :                 else
    4570             :                 {
    4571             :                     /*
    4572             :                      * We can get here after an error during transaction start
    4573             :                      * (state will be TRANS_START).  Need to clean up the
    4574             :                      * incompletely started transaction.  First, adjust the
    4575             :                      * low-level state to suppress warning message from
    4576             :                      * AbortTransaction.
    4577             :                      */
    4578           0 :                     if (s->state == TRANS_START)
    4579           0 :                         s->state = TRANS_INPROGRESS;
    4580           0 :                     AbortTransaction();
    4581           0 :                     CleanupTransaction();
    4582             :                 }
    4583       10978 :                 break;
    4584         366 :             case TBLOCK_STARTED:
    4585             :             case TBLOCK_BEGIN:
    4586             :             case TBLOCK_INPROGRESS:
    4587             :             case TBLOCK_IMPLICIT_INPROGRESS:
    4588             :             case TBLOCK_PARALLEL_INPROGRESS:
    4589             :             case TBLOCK_END:
    4590             :             case TBLOCK_ABORT_PENDING:
    4591             :             case TBLOCK_PREPARE:
    4592             :                 /* In a transaction, so clean up */
    4593         366 :                 AbortTransaction();
    4594         366 :                 CleanupTransaction();
    4595         366 :                 s->blockState = TBLOCK_DEFAULT;
    4596         366 :                 break;
    4597          12 :             case TBLOCK_ABORT:
    4598             :             case TBLOCK_ABORT_END:
    4599             : 
    4600             :                 /*
    4601             :                  * AbortTransaction is already done, still need Cleanup.
    4602             :                  * However, if we failed partway through running ROLLBACK,
    4603             :                  * there will be an active portal running that command, which
    4604             :                  * we need to shut down before doing CleanupTransaction.
    4605             :                  */
    4606          12 :                 AtAbort_Portals();
    4607          12 :                 CleanupTransaction();
    4608          12 :                 s->blockState = TBLOCK_DEFAULT;
    4609          12 :                 break;
    4610             : 
    4611             :                 /*
    4612             :                  * In a subtransaction, so clean it up and abort parent too
    4613             :                  */
    4614           0 :             case TBLOCK_SUBBEGIN:
    4615             :             case TBLOCK_SUBINPROGRESS:
    4616             :             case TBLOCK_SUBRELEASE:
    4617             :             case TBLOCK_SUBCOMMIT:
    4618             :             case TBLOCK_SUBABORT_PENDING:
    4619             :             case TBLOCK_SUBRESTART:
    4620           0 :                 AbortSubTransaction();
    4621           0 :                 CleanupSubTransaction();
    4622           0 :                 s = CurrentTransactionState;    /* changed by pop */
    4623           0 :                 break;
    4624             : 
    4625           0 :             case TBLOCK_SUBABORT:
    4626             :             case TBLOCK_SUBABORT_END:
    4627             :             case TBLOCK_SUBABORT_RESTART:
    4628             :                 /* As above, but AbortSubTransaction already done */
    4629           0 :                 if (s->curTransactionOwner)
    4630             :                 {
    4631             :                     /* As in TBLOCK_ABORT, might have a live portal to zap */
    4632           0 :                     AtSubAbort_Portals(s->subTransactionId,
    4633           0 :                                        s->parent->subTransactionId,
    4634             :                                        s->curTransactionOwner,
    4635           0 :                                        s->parent->curTransactionOwner);
    4636             :                 }
    4637           0 :                 CleanupSubTransaction();
    4638           0 :                 s = CurrentTransactionState;    /* changed by pop */
    4639           0 :                 break;
    4640             :         }
    4641       11356 :     } while (s->blockState != TBLOCK_DEFAULT);
    4642             : 
    4643             :     /* Should be out of all subxacts now */
    4644             :     Assert(s->parent == NULL);
    4645             : 
    4646             :     /* If we didn't actually have anything to do, revert to TopMemoryContext */
    4647       11356 :     AtCleanup_Memory();
    4648       11356 : }
    4649             : 
    4650             : /*
    4651             :  * IsTransactionBlock --- are we within a transaction block?
    4652             :  */
    4653             : bool
    4654      347672 : IsTransactionBlock(void)
    4655             : {
    4656      347672 :     TransactionState s = CurrentTransactionState;
    4657             : 
    4658      347672 :     if (s->blockState == TBLOCK_DEFAULT || s->blockState == TBLOCK_STARTED)
    4659      253630 :         return false;
    4660             : 
    4661       94042 :     return true;
    4662             : }
    4663             : 
    4664             : /*
    4665             :  * IsTransactionOrTransactionBlock --- are we within either a transaction
    4666             :  * or a transaction block?  (The backend is only really "idle" when this
    4667             :  * returns false.)
    4668             :  *
    4669             :  * This should match up with IsTransactionBlock and IsTransactionState.
    4670             :  */
    4671             : bool
    4672      496376 : IsTransactionOrTransactionBlock(void)
    4673             : {
    4674      496376 :     TransactionState s = CurrentTransactionState;
    4675             : 
    4676      496376 :     if (s->blockState == TBLOCK_DEFAULT)
    4677      402840 :         return false;
    4678             : 
    4679       93536 :     return true;
    4680             : }
    4681             : 
    4682             : /*
    4683             :  * TransactionBlockStatusCode - return status code to send in ReadyForQuery
    4684             :  */
    4685             : char
    4686      328682 : TransactionBlockStatusCode(void)
    4687             : {
    4688      328682 :     TransactionState s = CurrentTransactionState;
    4689             : 
    4690      328682 :     switch (s->blockState)
    4691             :     {
    4692      235826 :         case TBLOCK_DEFAULT:
    4693             :         case TBLOCK_STARTED:
    4694      235826 :             return 'I';         /* idle --- not in transaction */
    4695       91814 :         case TBLOCK_BEGIN:
    4696             :         case TBLOCK_SUBBEGIN:
    4697             :         case TBLOCK_INPROGRESS:
    4698             :         case TBLOCK_IMPLICIT_INPROGRESS:
    4699             :         case TBLOCK_PARALLEL_INPROGRESS:
    4700             :         case TBLOCK_SUBINPROGRESS:
    4701             :         case TBLOCK_END:
    4702             :         case TBLOCK_SUBRELEASE:
    4703             :         case TBLOCK_SUBCOMMIT:
    4704             :         case TBLOCK_PREPARE:
    4705       91814 :             return 'T';         /* in transaction */
    4706        1042 :         case TBLOCK_ABORT:
    4707             :         case TBLOCK_SUBABORT:
    4708             :         case TBLOCK_ABORT_END:
    4709             :         case TBLOCK_SUBABORT_END:
    4710             :         case TBLOCK_ABORT_PENDING:
    4711             :         case TBLOCK_SUBABORT_PENDING:
    4712             :         case TBLOCK_SUBRESTART:
    4713             :         case TBLOCK_SUBABORT_RESTART:
    4714        1042 :             return 'E';         /* in failed transaction */
    4715             :     }
    4716             : 
    4717             :     /* should never get here */
    4718           0 :     elog(FATAL, "invalid transaction block state: %s",
    4719             :          BlockStateAsString(s->blockState));
    4720             :     return 0;                   /* keep compiler quiet */
    4721             : }
    4722             : 
    4723             : /*
    4724             :  * IsSubTransaction
    4725             :  */
    4726             : bool
    4727       36014 : IsSubTransaction(void)
    4728             : {
    4729       36014 :     TransactionState s = CurrentTransactionState;
    4730             : 
    4731       36014 :     if (s->nestingLevel >= 2)
    4732          22 :         return true;
    4733             : 
    4734       35992 :     return false;
    4735             : }
    4736             : 
    4737             : /*
    4738             :  * StartSubTransaction
    4739             :  *
    4740             :  * If you're wondering why this is separate from PushTransaction: it's because
    4741             :  * we can't conveniently do this stuff right inside DefineSavepoint.  The
    4742             :  * SAVEPOINT utility command will be executed inside a Portal, and if we
    4743             :  * muck with CurrentMemoryContext or CurrentResourceOwner then exit from
    4744             :  * the Portal will undo those settings.  So we make DefineSavepoint just
    4745             :  * push a dummy transaction block, and when control returns to the main
    4746             :  * idle loop, CommitTransactionCommand will be called, and we'll come here
    4747             :  * to finish starting the subtransaction.
    4748             :  */
    4749             : static void
    4750        7650 : StartSubTransaction(void)
    4751             : {
    4752        7650 :     TransactionState s = CurrentTransactionState;
    4753             : 
    4754        7650 :     if (s->state != TRANS_DEFAULT)
    4755           0 :         elog(WARNING, "StartSubTransaction while in %s state",
    4756             :              TransStateAsString(s->state));
    4757             : 
    4758        7650 :     s->state = TRANS_START;
    4759             : 
    4760             :     /*
    4761             :      * Initialize subsystems for new subtransaction
    4762             :      *
    4763             :      * must initialize resource-management stuff first
    4764             :      */
    4765        7650 :     AtSubStart_Memory();
    4766        7650 :     AtSubStart_ResourceOwner();
    4767        7650 :     AfterTriggerBeginSubXact();
    4768             : 
    4769        7650 :     s->state = TRANS_INPROGRESS;
    4770             : 
    4771             :     /*
    4772             :      * Call start-of-subxact callbacks
    4773             :      */
    4774        7650 :     CallSubXactCallbacks(SUBXACT_EVENT_START_SUB, s->subTransactionId,
    4775        7650 :                          s->parent->subTransactionId);
    4776             : 
    4777        7650 :     ShowTransactionState("StartSubTransaction");
    4778        7650 : }
    4779             : 
    4780             : /*
    4781             :  * CommitSubTransaction
    4782             :  *
    4783             :  *  The caller has to make sure to always reassign CurrentTransactionState
    4784             :  *  if it has a local pointer to it after calling this function.
    4785             :  */
    4786             : static void
    4787        4696 : CommitSubTransaction(void)
    4788             : {
    4789        4696 :     TransactionState s = CurrentTransactionState;
    4790             : 
    4791        4696 :     ShowTransactionState("CommitSubTransaction");
    4792             : 
    4793        4696 :     if (s->state != TRANS_INPROGRESS)
    4794           0 :         elog(WARNING, "CommitSubTransaction while in %s state",
    4795             :              TransStateAsString(s->state));
    4796             : 
    4797             :     /* Pre-commit processing goes here */
    4798             : 
    4799        4696 :     CallSubXactCallbacks(SUBXACT_EVENT_PRE_COMMIT_SUB, s->subTransactionId,
    4800        4696 :                          s->parent->subTransactionId);
    4801             : 
    4802             :     /* If in parallel mode, clean up workers and exit parallel mode. */
    4803        4696 :     if (IsInParallelMode())
    4804             :     {
    4805           0 :         AtEOSubXact_Parallel(true, s->subTransactionId);
    4806           0 :         s->parallelModeLevel = 0;
    4807             :     }
    4808             : 
    4809             :     /* Do the actual "commit", such as it is */
    4810        4696 :     s->state = TRANS_COMMIT;
    4811             : 
    4812             :     /* Must CCI to ensure commands of subtransaction are seen as done */
    4813        4696 :     CommandCounterIncrement();
    4814             : 
    4815             :     /*
    4816             :      * Prior to 8.4 we marked subcommit in clog at this point.  We now only
    4817             :      * perform that step, if required, as part of the atomic update of the
    4818             :      * whole transaction tree at top level commit or abort.
    4819             :      */
    4820             : 
    4821             :     /* Post-commit cleanup */
    4822        4696 :     if (FullTransactionIdIsValid(s->fullTransactionId))
    4823        3918 :         AtSubCommit_childXids();
    4824        4696 :     AfterTriggerEndSubXact(true);
    4825        9392 :     AtSubCommit_Portals(s->subTransactionId,
    4826        4696 :                         s->parent->subTransactionId,
    4827        4696 :                         s->parent->curTransactionOwner);
    4828        4696 :     AtEOSubXact_LargeObject(true, s->subTransactionId,
    4829        4696 :                             s->parent->subTransactionId);
    4830        4696 :     AtSubCommit_Notify();
    4831             : 
    4832        4696 :     CallSubXactCallbacks(SUBXACT_EVENT_COMMIT_SUB, s->subTransactionId,
    4833        4696 :                          s->parent->subTransactionId);
    4834             : 
    4835        4696 :     ResourceOwnerRelease(s->curTransactionOwner,
    4836             :                          RESOURCE_RELEASE_BEFORE_LOCKS,
    4837             :                          true, false);
    4838        4696 :     AtEOSubXact_RelationCache(true, s->subTransactionId,
    4839        4696 :                               s->parent->subTransactionId);
    4840        4696 :     AtEOSubXact_Inval(true);
    4841        4696 :     AtSubCommit_smgr();
    4842             : 
    4843             :     /*
    4844             :      * The only lock we actually release here is the subtransaction XID lock.
    4845             :      */
    4846        4696 :     CurrentResourceOwner = s->curTransactionOwner;
    4847        4696 :     if (FullTransactionIdIsValid(s->fullTransactionId))
    4848        3918 :         XactLockTableDelete(XidFromFullTransactionId(s->fullTransactionId));
    4849             : 
    4850             :     /*
    4851             :      * Other locks should get transferred to their parent resource owner.
    4852             :      */
    4853        4696 :     ResourceOwnerRelease(s->curTransactionOwner,
    4854             :                          RESOURCE_RELEASE_LOCKS,
    4855             :                          true, false);
    4856        4696 :     ResourceOwnerRelease(s->curTransactionOwner,
    4857             :                          RESOURCE_RELEASE_AFTER_LOCKS,
    4858             :                          true, false);
    4859             : 
    4860        4696 :     AtEOXact_GUC(true, s->gucNestLevel);
    4861        4696 :     AtEOSubXact_SPI(true, s->subTransactionId);
    4862        4696 :     AtEOSubXact_on_commit_actions(true, s->subTransactionId,
    4863        4696 :                                   s->parent->subTransactionId);
    4864        4696 :     AtEOSubXact_Namespace(true, s->subTransactionId,
    4865        4696 :                           s->parent->subTransactionId);
    4866        4696 :     AtEOSubXact_Files(true, s->subTransactionId,
    4867        4696 :                       s->parent->subTransactionId);
    4868        4696 :     AtEOSubXact_HashTables(true, s->nestingLevel);
    4869        4696 :     AtEOSubXact_PgStat(true, s->nestingLevel);
    4870        4696 :     AtSubCommit_Snapshot(s->nestingLevel);
    4871        4696 :     AtEOSubXact_ApplyLauncher(true, s->nestingLevel);
    4872             : 
    4873             :     /*
    4874             :      * We need to restore the upper transaction's read-only state, in case the
    4875             :      * upper is read-write while the child is read-only; GUC will incorrectly
    4876             :      * think it should leave the child state in place.
    4877             :      */
    4878        4696 :     XactReadOnly = s->prevXactReadOnly;
    4879             : 
    4880        4696 :     CurrentResourceOwner = s->parent->curTransactionOwner;
    4881        4696 :     CurTransactionResourceOwner = s->parent->curTransactionOwner;
    4882        4696 :     ResourceOwnerDelete(s->curTransactionOwner);
    4883        4696 :     s->curTransactionOwner = NULL;
    4884             : 
    4885        4696 :     AtSubCommit_Memory();
    4886             : 
    4887        4696 :     s->state = TRANS_DEFAULT;
    4888             : 
    4889        4696 :     PopTransaction();
    4890        4696 : }
    4891             : 
    4892             : /*
    4893             :  * AbortSubTransaction
    4894             :  */
    4895             : static void
    4896        2954 : AbortSubTransaction(void)
    4897             : {
    4898        2954 :     TransactionState s = CurrentTransactionState;
    4899             : 
    4900             :     /* Prevent cancel/die interrupt while cleaning up */
    4901        2954 :     HOLD_INTERRUPTS();
    4902             : 
    4903             :     /* Make sure we have a valid memory context and resource owner */
    4904        2954 :     AtSubAbort_Memory();
    4905        2954 :     AtSubAbort_ResourceOwner();
    4906             : 
    4907             :     /*
    4908             :      * Release any LW locks we might be holding as quickly as possible.
    4909             :      * (Regular locks, however, must be held till we finish aborting.)
    4910             :      * Releasing LW locks is critical since we might try to grab them again
    4911             :      * while cleaning up!
    4912             :      *
    4913             :      * FIXME This may be incorrect --- Are there some locks we should keep?
    4914             :      * Buffer locks, for example?  I don't think so but I'm not sure.
    4915             :      */
    4916        2954 :     LWLockReleaseAll();
    4917             : 
    4918        2954 :     pgstat_report_wait_end();
    4919        2954 :     pgstat_progress_end_command();
    4920        2954 :     AbortBufferIO();
    4921        2954 :     UnlockBuffers();
    4922             : 
    4923             :     /* Reset WAL record construction state */
    4924        2954 :     XLogResetInsertion();
    4925             : 
    4926             :     /* Cancel condition variable sleep */
    4927        2954 :     ConditionVariableCancelSleep();
    4928             : 
    4929             :     /*
    4930             :      * Also clean up any open wait for lock, since the lock manager will choke
    4931             :      * if we try to wait for another lock before doing this.
    4932             :      */
    4933        2954 :     LockErrorCleanup();
    4934             : 
    4935             :     /*
    4936             :      * If any timeout events are still active, make sure the timeout interrupt
    4937             :      * is scheduled.  This covers possible loss of a timeout interrupt due to
    4938             :      * longjmp'ing out of the SIGINT handler (see notes in handle_sig_alarm).
    4939             :      * We delay this till after LockErrorCleanup so that we don't uselessly
    4940             :      * reschedule lock or deadlock check timeouts.
    4941             :      */
    4942        2954 :     reschedule_timeouts();
    4943             : 
    4944             :     /*
    4945             :      * Re-enable signals, in case we got here by longjmp'ing out of a signal
    4946             :      * handler.  We do this fairly early in the sequence so that the timeout
    4947             :      * infrastructure will be functional if needed while aborting.
    4948             :      */
    4949        2954 :     PG_SETMASK(&UnBlockSig);
    4950             : 
    4951             :     /*
    4952             :      * check the current transaction state
    4953             :      */
    4954        2954 :     ShowTransactionState("AbortSubTransaction");
    4955             : 
    4956        2954 :     if (s->state != TRANS_INPROGRESS)
    4957           0 :         elog(WARNING, "AbortSubTransaction while in %s state",
    4958             :              TransStateAsString(s->state));
    4959             : 
    4960        2954 :     s->state = TRANS_ABORT;
    4961             : 
    4962             :     /*
    4963             :      * Reset user ID which might have been changed transiently.  (See notes in
    4964             :      * AbortTransaction.)
    4965             :      */
    4966        2954 :     SetUserIdAndSecContext(s->prevUser, s->prevSecContext);
    4967             : 
    4968             :     /* Forget about any active REINDEX. */
    4969        2954 :     ResetReindexState(s->nestingLevel);
    4970             : 
    4971             :     /* Exit from parallel mode, if necessary. */
    4972        2954 :     if (IsInParallelMode())
    4973             :     {
    4974           4 :         AtEOSubXact_Parallel(false, s->subTransactionId);
    4975           4 :         s->parallelModeLevel = 0;
    4976             :     }
    4977             : 
    4978             :     /*
    4979             :      * We can skip all this stuff if the subxact failed before creating a
    4980             :      * ResourceOwner...
    4981             :      */
    4982        2954 :     if (s->curTransactionOwner)
    4983             :     {
    4984        2954 :         AfterTriggerEndSubXact(false);
    4985        5908 :         AtSubAbort_Portals(s->subTransactionId,
    4986        2954 :                            s->parent->subTransactionId,
    4987             :                            s->curTransactionOwner,
    4988        2954 :                            s->parent->curTransactionOwner);
    4989        2954 :         AtEOSubXact_LargeObject(false, s->subTransactionId,
    4990        2954 :                                 s->parent->subTransactionId);
    4991        2954 :         AtSubAbort_Notify();
    4992             : 
    4993             :         /* Advertise the fact that we aborted in pg_xact. */
    4994        2954 :         (void) RecordTransactionAbort(true);
    4995             : 
    4996             :         /* Post-abort cleanup */
    4997        2954 :         if (FullTransactionIdIsValid(s->fullTransactionId))
    4998         362 :             AtSubAbort_childXids();
    4999             : 
    5000        2954 :         CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
    5001        2954 :                              s->parent->subTransactionId);
    5002             : 
    5003        2954 :         ResourceOwnerRelease(s->curTransactionOwner,
    5004             :                              RESOURCE_RELEASE_BEFORE_LOCKS,
    5005             :                              false, false);
    5006        2954 :         AtEOSubXact_RelationCache(false, s->subTransactionId,
    5007        2954 :                                   s->parent->subTransactionId);
    5008        2954 :         AtEOSubXact_Inval(false);
    5009        2954 :         ResourceOwnerRelease(s->curTransactionOwner,
    5010             :                              RESOURCE_RELEASE_LOCKS,
    5011             :                              false, false);
    5012        2954 :         ResourceOwnerRelease(s->curTransactionOwner,
    5013             :                              RESOURCE_RELEASE_AFTER_LOCKS,
    5014             :                              false, false);
    5015        2954 :         AtSubAbort_smgr();
    5016             : 
    5017        2954 :         AtEOXact_GUC(false, s->gucNestLevel);
    5018        2954 :         AtEOSubXact_SPI(false, s->subTransactionId);
    5019        2954 :         AtEOSubXact_on_commit_actions(false, s->subTransactionId,
    5020        2954 :                                       s->parent->subTransactionId);
    5021        2954 :         AtEOSubXact_Namespace(false, s->subTransactionId,
    5022        2954 :                               s->parent->subTransactionId);
    5023        2954 :         AtEOSubXact_Files(false, s->subTransactionId,
    5024        2954 :                           s->parent->subTransactionId);
    5025        2954 :         AtEOSubXact_HashTables(false, s->nestingLevel);
    5026        2954 :         AtEOSubXact_PgStat(false, s->nestingLevel);
    5027        2954 :         AtSubAbort_Snapshot(s->nestingLevel);
    5028        2954 :         AtEOSubXact_ApplyLauncher(false, s->nestingLevel);
    5029             :     }
    5030             : 
    5031             :     /*
    5032             :      * Restore the upper transaction's read-only state, too.  This should be
    5033             :      * redundant with GUC's cleanup but we may as well do it for consistency
    5034             :      * with the commit case.
    5035             :      */
    5036        2954 :     XactReadOnly = s->prevXactReadOnly;
    5037             : 
    5038        2954 :     RESUME_INTERRUPTS();
    5039        2954 : }
    5040             : 
    5041             : /*
    5042             :  * CleanupSubTransaction
    5043             :  *
    5044             :  *  The caller has to make sure to always reassign CurrentTransactionState
    5045             :  *  if it has a local pointer to it after calling this function.
    5046             :  */
    5047             : static void
    5048        2954 : CleanupSubTransaction(void)
    5049             : {
    5050        2954 :     TransactionState s = CurrentTransactionState;
    5051             : 
    5052        2954 :     ShowTransactionState("CleanupSubTransaction");
    5053             : 
    5054        2954 :     if (s->state != TRANS_ABORT)
    5055           0 :         elog(WARNING, "CleanupSubTransaction while in %s state",
    5056             :              TransStateAsString(s->state));
    5057             : 
    5058        2954 :     AtSubCleanup_Portals(s->subTransactionId);
    5059             : 
    5060        2954 :     CurrentResourceOwner = s->parent->curTransactionOwner;
    5061        2954 :     CurTransactionResourceOwner = s->parent->curTransactionOwner;
    5062        2954 :     if (s->curTransactionOwner)
    5063        2954 :         ResourceOwnerDelete(s->curTransactionOwner);
    5064        2954 :     s->curTransactionOwner = NULL;
    5065             : 
    5066        2954 :     AtSubCleanup_Memory();
    5067             : 
    5068        2954 :     s->state = TRANS_DEFAULT;
    5069             : 
    5070        2954 :     PopTransaction();
    5071        2954 : }
    5072             : 
    5073             : /*
    5074             :  * PushTransaction
    5075             :  *      Create transaction state stack entry for a subtransaction
    5076             :  *
    5077             :  *  The caller has to make sure to always reassign CurrentTransactionState
    5078             :  *  if it has a local pointer to it after calling this function.
    5079             :  */
    5080             : static void
    5081        7650 : PushTransaction(void)
    5082             : {
    5083        7650 :     TransactionState p = CurrentTransactionState;
    5084             :     TransactionState s;
    5085             : 
    5086             :     /*
    5087             :      * We keep subtransaction state nodes in TopTransactionContext.
    5088             :      */
    5089             :     s = (TransactionState)
    5090        7650 :         MemoryContextAllocZero(TopTransactionContext,
    5091             :                                sizeof(TransactionStateData));
    5092             : 
    5093             :     /*
    5094             :      * Assign a subtransaction ID, watching out for counter wraparound.
    5095             :      */
    5096        7650 :     currentSubTransactionId += 1;
    5097        7650 :     if (currentSubTransactionId == InvalidSubTransactionId)
    5098             :     {
    5099           0 :         currentSubTransactionId -= 1;
    5100           0 :         pfree(s);
    5101           0 :         ereport(ERROR,
    5102             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
    5103             :                  errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));
    5104             :     }
    5105             : 
    5106             :     /*
    5107             :      * We can now stack a minimally valid subtransaction without fear of
    5108             :      * failure.
    5109             :      */
    5110        7650 :     s->fullTransactionId = InvalidFullTransactionId; /* until assigned */
    5111        7650 :     s->subTransactionId = currentSubTransactionId;
    5112        7650 :     s->parent = p;
    5113        7650 :     s->nestingLevel = p->nestingLevel + 1;
    5114        7650 :     s->gucNestLevel = NewGUCNestLevel();
    5115        7650 :     s->savepointLevel = p->savepointLevel;
    5116        7650 :     s->state = TRANS_DEFAULT;
    5117        7650 :     s->blockState = TBLOCK_SUBBEGIN;
    5118        7650 :     GetUserIdAndSecContext(&s->prevUser, &s->prevSecContext);
    5119        7650 :     s->prevXactReadOnly = XactReadOnly;
    5120        7650 :     s->parallelModeLevel = 0;
    5121             : 
    5122        7650 :     CurrentTransactionState = s;
    5123             : 
    5124             :     /*
    5125             :      * AbortSubTransaction and CleanupSubTransaction have to be able to cope
    5126             :      * with the subtransaction from here on out; in particular they should not
    5127             :      * assume that it necessarily has a transaction context, resource owner,
    5128             :      * or XID.
    5129             :      */
    5130        7650 : }
    5131             : 
    5132             : /*
    5133             :  * PopTransaction
    5134             :  *      Pop back to parent transaction state
    5135             :  *
    5136             :  *  The caller has to make sure to always reassign CurrentTransactionState
    5137             :  *  if it has a local pointer to it after calling this function.
    5138             :  */
    5139             : static void
    5140        7650 : PopTransaction(void)
    5141             : {
    5142        7650 :     TransactionState s = CurrentTransactionState;
    5143             : 
    5144        7650 :     if (s->state != TRANS_DEFAULT)
    5145           0 :         elog(WARNING, "PopTransaction while in %s state",
    5146             :              TransStateAsString(s->state));
    5147             : 
    5148        7650 :     if (s->parent == NULL)
    5149           0 :         elog(FATAL, "PopTransaction with no parent");
    5150             : 
    5151        7650 :     CurrentTransactionState = s->parent;
    5152             : 
    5153             :     /* Let's just make sure CurTransactionContext is good */
    5154        7650 :     CurTransactionContext = s->parent->curTransactionContext;
    5155        7650 :     MemoryContextSwitchTo(CurTransactionContext);
    5156             : 
    5157             :     /* Ditto for ResourceOwner links */
    5158        7650 :     CurTransactionResourceOwner = s->parent->curTransactionOwner;
    5159        7650 :     CurrentResourceOwner = s->parent->curTransactionOwner;
    5160             : 
    5161             :     /* Free the old child structure */
    5162        7650 :     if (s->name)
    5163        1998 :         pfree(s->name);
    5164        7650 :     pfree(s);
    5165        7650 : }
    5166             : 
    5167             : /*
    5168             :  * EstimateTransactionStateSpace
    5169             :  *      Estimate the amount of space that will be needed by
    5170             :  *      SerializeTransactionState.  It would be OK to overestimate slightly,
    5171             :  *      but it's simple for us to work out the precise value, so we do.
    5172             :  */
    5173             : Size
    5174         470 : EstimateTransactionStateSpace(void)
    5175             : {
    5176             :     TransactionState s;
    5177         470 :     Size        nxids = 0;
    5178         470 :     Size        size = SerializedTransactionStateHeaderSize;
    5179             : 
    5180        2320 :     for (s = CurrentTransactionState; s != NULL; s = s->parent)
    5181             :     {
    5182        1850 :         if (FullTransactionIdIsValid(s->fullTransactionId))
    5183        1052 :             nxids = add_size(nxids, 1);
    5184        1850 :         nxids = add_size(nxids, s->nChildXids);
    5185             :     }
    5186             : 
    5187         470 :     return add_size(size, mul_size(sizeof(TransactionId), nxids));
    5188             : }
    5189             : 
    5190             : /*
    5191             :  * SerializeTransactionState
    5192             :  *      Write out relevant details of our transaction state that will be
    5193             :  *      needed by a parallel worker.
    5194             :  *
    5195             :  * We need to save and restore XactDeferrable, XactIsoLevel, and the XIDs
    5196             :  * associated with this transaction.  These are serialized into a
    5197             :  * caller-supplied buffer big enough to hold the number of bytes reported by
    5198             :  * EstimateTransactionStateSpace().  We emit the XIDs in sorted order for the
    5199             :  * convenience of the receiving process.
    5200             :  */
    5201             : void
    5202         470 : SerializeTransactionState(Size maxsize, char *start_address)
    5203             : {
    5204             :     TransactionState s;
    5205         470 :     Size        nxids = 0;
    5206         470 :     Size        i = 0;
    5207             :     TransactionId *workspace;
    5208             :     SerializedTransactionState *result;
    5209             : 
    5210         470 :     result = (SerializedTransactionState *) start_address;
    5211             : 
    5212         470 :     result->xactIsoLevel = XactIsoLevel;
    5213         470 :     result->xactDeferrable = XactDeferrable;
    5214         470 :     result->topFullTransactionId = XactTopFullTransactionId;
    5215         470 :     result->currentFullTransactionId =
    5216         470 :         CurrentTransactionState->fullTransactionId;
    5217         470 :     result->currentCommandId = currentCommandId;
    5218             : 
    5219             :     /*
    5220             :      * If we're running in a parallel worker and launching a parallel worker
    5221             :      * of our own, we can just pass along the information that was passed to
    5222             :      * us.
    5223             :      */
    5224         470 :     if (nParallelCurrentXids > 0)
    5225             :     {
    5226           0 :         result->nParallelCurrentXids = nParallelCurrentXids;
    5227           0 :         memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
    5228             :                nParallelCurrentXids * sizeof(TransactionId));
    5229           0 :         return;
    5230             :     }
    5231             : 
    5232             :     /*
    5233             :      * OK, we need to generate a sorted list of XIDs that our workers should
    5234             :      * view as current.  First, figure out how many there are.
    5235             :      */
    5236        2320 :     for (s = CurrentTransactionState; s != NULL; s = s->parent)
    5237             :     {
    5238        1850 :         if (FullTransactionIdIsValid(s->fullTransactionId))
    5239        1052 :             nxids = add_size(nxids, 1);
    5240        1850 :         nxids = add_size(nxids, s->nChildXids);
    5241             :     }
    5242             :     Assert(SerializedTransactionStateHeaderSize + nxids * sizeof(TransactionId)
    5243             :            <= maxsize);
    5244             : 
    5245             :     /* Copy them to our scratch space. */
    5246         470 :     workspace = palloc(nxids * sizeof(TransactionId));
    5247        2320 :     for (s = CurrentTransactionState; s != NULL; s = s->parent)
    5248             :     {
    5249        1850 :         if (FullTransactionIdIsValid(s->fullTransactionId))
    5250        1052 :             workspace[i++] = XidFromFullTransactionId(s->fullTransactionId);
    5251        1850 :         memcpy(&workspace[i], s->childXids,
    5252        1850 :                s->nChildXids * sizeof(TransactionId));
    5253        1850 :         i += s->nChildXids;
    5254             :     }
    5255             :     Assert(i == nxids);
    5256             : 
    5257             :     /* Sort them. */
    5258         470 :     qsort(workspace, nxids, sizeof(TransactionId), xidComparator);
    5259             : 
    5260             :     /* Copy data into output area. */
    5261         470 :     result->nParallelCurrentXids = nxids;
    5262         470 :     memcpy(&result->parallelCurrentXids[0], workspace,
    5263             :            nxids * sizeof(TransactionId));
    5264             : }
    5265             : 
    5266             : /*
    5267             :  * StartParallelWorkerTransaction
    5268             :  *      Start a parallel worker transaction, restoring the relevant
    5269             :  *      transaction state serialized by SerializeTransactionState.
    5270             :  */
    5271             : void
    5272        1568 : StartParallelWorkerTransaction(char *tstatespace)
    5273             : {
    5274             :     SerializedTransactionState *tstate;
    5275             : 
    5276             :     Assert(CurrentTransactionState->blockState == TBLOCK_DEFAULT);
    5277        1568 :     StartTransaction();
    5278             : 
    5279        1568 :     tstate = (SerializedTransactionState *) tstatespace;
    5280        1568 :     XactIsoLevel = tstate->xactIsoLevel;
    5281        1568 :     XactDeferrable = tstate->xactDeferrable;
    5282        1568 :     XactTopFullTransactionId = tstate->topFullTransactionId;
    5283        1568 :     CurrentTransactionState->fullTransactionId =
    5284             :         tstate->currentFullTransactionId;
    5285        1568 :     currentCommandId = tstate->currentCommandId;
    5286        1568 :     nParallelCurrentXids = tstate->nParallelCurrentXids;
    5287        1568 :     ParallelCurrentXids = &tstate->parallelCurrentXids[0];
    5288             : 
    5289        1568 :     CurrentTransactionState->blockState = TBLOCK_PARALLEL_INPROGRESS;
    5290        1568 : }
    5291             : 
    5292             : /*
    5293             :  * EndParallelWorkerTransaction
    5294             :  *      End a parallel worker transaction.
    5295             :  */
    5296             : void
    5297        1564 : EndParallelWorkerTransaction(void)
    5298             : {
    5299             :     Assert(CurrentTransactionState->blockState == TBLOCK_PARALLEL_INPROGRESS);
    5300        1564 :     CommitTransaction();
    5301        1564 :     CurrentTransactionState->blockState = TBLOCK_DEFAULT;
    5302        1564 : }
    5303             : 
    5304             : /*
    5305             :  * ShowTransactionState
    5306             :  *      Debug support
    5307             :  */
    5308             : static void
    5309      987730 : ShowTransactionState(const char *str)
    5310             : {
    5311             :     /* skip work if message will definitely not be printed */
    5312      987730 :     if (log_min_messages <= DEBUG5 || client_min_messages <= DEBUG5)
    5313           0 :         ShowTransactionStateRec(str, CurrentTransactionState);
    5314      987730 : }
    5315             : 
    5316             : /*
    5317             :  * ShowTransactionStateRec
    5318             :  *      Recursive subroutine for ShowTransactionState
    5319             :  */
    5320             : static void
    5321           0 : ShowTransactionStateRec(const char *str, TransactionState s)
    5322             : {
    5323             :     StringInfoData buf;
    5324             : 
    5325           0 :     initStringInfo(&buf);
    5326             : 
    5327           0 :     if (s->nChildXids > 0)
    5328             :     {
    5329             :         int         i;
    5330             : 
    5331           0 :         appendStringInfo(&buf, ", children: %u", s->childXids[0]);
    5332           0 :         for (i = 1; i < s->nChildXids; i++)
    5333           0 :             appendStringInfo(&buf, " %u", s->childXids[i]);
    5334             :     }
    5335             : 
    5336           0 :     if (s->parent)
    5337           0 :         ShowTransactionStateRec(str, s->parent);
    5338             : 
    5339             :     /* use ereport to suppress computation if msg will not be printed */
    5340           0 :     ereport(DEBUG5,
    5341             :             (errmsg_internal("%s(%d) name: %s; blockState: %s; state: %s, xid/subid/cid: %u/%u/%u%s%s",
    5342             :                              str, s->nestingLevel,
    5343             :                              PointerIsValid(s->name) ? s->name : "unnamed",
    5344             :                              BlockStateAsString(s->blockState),
    5345             :                              TransStateAsString(s->state),
    5346             :                              (unsigned int) XidFromFullTransactionId(s->fullTransactionId),
    5347             :                              (unsigned int) s->subTransactionId,
    5348             :                              (unsigned int) currentCommandId,
    5349             :                              currentCommandIdUsed ? " (used)" : "",
    5350             :                              buf.data)));
    5351             : 
    5352           0 :     pfree(buf.data);
    5353           0 : }
    5354             : 
    5355             : /*
    5356             :  * BlockStateAsString
    5357             :  *      Debug support
    5358             :  */
    5359             : static const char *
    5360           0 : BlockStateAsString(TBlockState blockState)
    5361             : {
    5362           0 :     switch (blockState)
    5363             :     {
    5364           0 :         case TBLOCK_DEFAULT:
    5365           0 :             return "DEFAULT";
    5366           0 :         case TBLOCK_STARTED:
    5367           0 :             return "STARTED";
    5368           0 :         case TBLOCK_BEGIN:
    5369           0 :             return "BEGIN";
    5370           0 :         case TBLOCK_INPROGRESS:
    5371           0 :             return "INPROGRESS";
    5372           0 :         case TBLOCK_IMPLICIT_INPROGRESS:
    5373           0 :             return "IMPLICIT_INPROGRESS";
    5374           0 :         case TBLOCK_PARALLEL_INPROGRESS:
    5375           0 :             return "PARALLEL_INPROGRESS";
    5376           0 :         case TBLOCK_END:
    5377           0 :             return "END";
    5378           0 :         case TBLOCK_ABORT:
    5379           0 :             return "ABORT";
    5380           0 :         case TBLOCK_ABORT_END:
    5381           0 :             return "ABORT_END";
    5382           0 :         case TBLOCK_ABORT_PENDING:
    5383           0 :             return "ABORT_PENDING";
    5384           0 :         case TBLOCK_PREPARE:
    5385           0 :             return "PREPARE";
    5386           0 :         case TBLOCK_SUBBEGIN:
    5387           0 :             return "SUBBEGIN";
    5388           0 :         case TBLOCK_SUBINPROGRESS:
    5389           0 :             return "SUBINPROGRESS";
    5390           0 :         case TBLOCK_SUBRELEASE:
    5391           0 :             return "SUBRELEASE";
    5392           0 :         case TBLOCK_SUBCOMMIT:
    5393           0 :             return "SUBCOMMIT";
    5394           0 :         case TBLOCK_SUBABORT:
    5395           0 :             return "SUBABORT";
    5396           0 :         case TBLOCK_SUBABORT_END:
    5397           0 :             return "SUBABORT_END";
    5398           0 :         case TBLOCK_SUBABORT_PENDING:
    5399           0 :             return "SUBABORT_PENDING";
    5400           0 :         case TBLOCK_SUBRESTART:
    5401           0 :             return "SUBRESTART";
    5402           0 :         case TBLOCK_SUBABORT_RESTART:
    5403           0 :             return "SUBABORT_RESTART";
    5404             :     }
    5405           0 :     return "UNRECOGNIZED";
    5406             : }
    5407             : 
    5408             : /*
    5409             :  * TransStateAsString
    5410             :  *      Debug support
    5411             :  */
    5412             : static const char *
    5413           0 : TransStateAsString(TransState state)
    5414             : {
    5415           0 :     switch (state)
    5416             :     {
    5417           0 :         case TRANS_DEFAULT:
    5418           0 :             return "DEFAULT";
    5419           0 :         case TRANS_START:
    5420           0 :             return "START";
    5421           0 :         case TRANS_INPROGRESS:
    5422           0 :             return "INPROGRESS";
    5423           0 :         case TRANS_COMMIT:
    5424           0 :             return "COMMIT";
    5425           0 :         case TRANS_ABORT:
    5426           0 :             return "ABORT";
    5427           0 :         case TRANS_PREPARE:
    5428           0 :             return "PREPARE";
    5429             :     }
    5430           0 :     return "UNRECOGNIZED";
    5431             : }
    5432             : 
    5433             : /*
    5434             :  * xactGetCommittedChildren
    5435             :  *
    5436             :  * Gets the list of committed children of the current transaction.  The return
    5437             :  * value is the number of child transactions.  *ptr is set to point to an
    5438             :  * array of TransactionIds.  The array is allocated in TopTransactionContext;
    5439             :  * the caller should *not* pfree() it (this is a change from pre-8.4 code!).
    5440             :  * If there are no subxacts, *ptr is set to NULL.
    5441             :  */
    5442             : int
    5443      478556 : xactGetCommittedChildren(TransactionId **ptr)
    5444             : {
    5445      478556 :     TransactionState s = CurrentTransactionState;
    5446             : 
    5447      478556 :     if (s->nChildXids == 0)
    5448      478060 :         *ptr = NULL;
    5449             :     else
    5450         496 :         *ptr = s->childXids;
    5451             : 
    5452      478556 :     return s->nChildXids;
    5453             : }
    5454             : 
    5455             : /*
    5456             :  *  XLOG support routines
    5457             :  */
    5458             : 
    5459             : 
    5460             : /*
    5461             :  * Log the commit record for a plain or twophase transaction commit.
    5462             :  *
    5463             :  * A 2pc commit will be emitted when twophase_xid is valid, a plain one
    5464             :  * otherwise.
    5465             :  */
    5466             : XLogRecPtr
    5467      267958 : XactLogCommitRecord(TimestampTz commit_time,
    5468             :                     int nsubxacts, TransactionId *subxacts,
    5469             :                     int nrels, RelFileNode *rels,
    5470             :                     int nmsgs, SharedInvalidationMessage *msgs,
    5471             :                     bool relcacheInval, bool forceSync,
    5472             :                     int xactflags, TransactionId twophase_xid,
    5473             :                     const char *twophase_gid)
    5474             : {
    5475             :     xl_xact_commit xlrec;
    5476             :     xl_xact_xinfo xl_xinfo;
    5477             :     xl_xact_dbinfo xl_dbinfo;
    5478             :     xl_xact_subxacts xl_subxacts;
    5479             :     xl_xact_relfilenodes xl_relfilenodes;
    5480             :     xl_xact_invals xl_invals;
    5481             :     xl_xact_twophase xl_twophase;
    5482             :     xl_xact_origin xl_origin;
    5483             :     uint8       info;
    5484             : 
    5485             :     Assert(CritSectionCount > 0);
    5486             : 
    5487      267958 :     xl_xinfo.xinfo = 0;
    5488             : 
    5489             :     /* decide between a plain and 2pc commit */
    5490      267958 :     if (!TransactionIdIsValid(twophase_xid))
    5491      267912 :         info = XLOG_XACT_COMMIT;
    5492             :     else
    5493          46 :         info = XLOG_XACT_COMMIT_PREPARED;
    5494             : 
    5495             :     /* First figure out and collect all the information needed */
    5496             : 
    5497      267958 :     xlrec.xact_time = commit_time;
    5498             : 
    5499      267958 :     if (relcacheInval)
    5500        8406 :         xl_xinfo.xinfo |= XACT_COMPLETION_UPDATE_RELCACHE_FILE;
    5501      267958 :     if (forceSyncCommit)
    5502        1018 :         xl_xinfo.xinfo |= XACT_COMPLETION_FORCE_SYNC_COMMIT;
    5503      267958 :     if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
    5504       86398 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
    5505             : 
    5506             :     /*
    5507             :      * Check if the caller would like to ask standbys for immediate feedback
    5508             :      * once this commit is applied.
    5509             :      */
    5510      267958 :     if (synchronous_commit >= SYNCHRONOUS_COMMIT_REMOTE_APPLY)
    5511           0 :         xl_xinfo.xinfo |= XACT_COMPLETION_APPLY_FEEDBACK;
    5512             : 
    5513             :     /*
    5514             :      * Relcache invalidations requires information about the current database
    5515             :      * and so does logical decoding.
    5516             :      */
    5517      267958 :     if (nmsgs > 0 || XLogLogicalInfoActive())
    5518             :     {
    5519      229906 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
    5520      229906 :         xl_dbinfo.dbId = MyDatabaseId;
    5521      229906 :         xl_dbinfo.tsId = MyDatabaseTableSpace;
    5522             :     }
    5523             : 
    5524      267958 :     if (nsubxacts > 0)
    5525             :     {
    5526         328 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
    5527         328 :         xl_subxacts.nsubxacts = nsubxacts;
    5528             :     }
    5529             : 
    5530      267958 :     if (nrels > 0)
    5531             :     {
    5532       10612 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
    5533       10612 :         xl_relfilenodes.nrels = nrels;
    5534             :     }
    5535             : 
    5536      267958 :     if (nmsgs > 0)
    5537             :     {
    5538      228900 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_INVALS;
    5539      228900 :         xl_invals.nmsgs = nmsgs;
    5540             :     }
    5541             : 
    5542      267958 :     if (TransactionIdIsValid(twophase_xid))
    5543             :     {
    5544          46 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
    5545          46 :         xl_twophase.xid = twophase_xid;
    5546             :         Assert(twophase_gid != NULL);
    5547             : 
    5548          46 :         if (XLogLogicalInfoActive())
    5549           4 :             xl_xinfo.xinfo |= XACT_XINFO_HAS_GID;
    5550             :     }
    5551             : 
    5552             :     /* dump transaction origin information */
    5553      267958 :     if (replorigin_session_origin != InvalidRepOriginId)
    5554             :     {
    5555         438 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
    5556             : 
    5557         438 :         xl_origin.origin_lsn = replorigin_session_origin_lsn;
    5558         438 :         xl_origin.origin_timestamp = replorigin_session_origin_timestamp;
    5559             :     }
    5560             : 
    5561      267958 :     if (xl_xinfo.xinfo != 0)
    5562      233892 :         info |= XLOG_XACT_HAS_INFO;
    5563             : 
    5564             :     /* Then include all the collected data into the commit record. */
    5565             : 
    5566      267958 :     XLogBeginInsert();
    5567             : 
    5568      267958 :     XLogRegisterData((char *) (&xlrec), sizeof(xl_xact_commit));
    5569             : 
    5570      267958 :     if (xl_xinfo.xinfo != 0)
    5571      233892 :         XLogRegisterData((char *) (&xl_xinfo.xinfo), sizeof(xl_xinfo.xinfo));
    5572             : 
    5573      267958 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
    5574      229906 :         XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
    5575             : 
    5576      267958 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
    5577             :     {
    5578         328 :         XLogRegisterData((char *) (&xl_subxacts),
    5579             :                          MinSizeOfXactSubxacts);
    5580         328 :         XLogRegisterData((char *) subxacts,
    5581             :                          nsubxacts * sizeof(TransactionId));
    5582             :     }
    5583             : 
    5584      267958 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
    5585             :     {
    5586       10612 :         XLogRegisterData((char *) (&xl_relfilenodes),
    5587             :                          MinSizeOfXactRelfilenodes);
    5588       10612 :         XLogRegisterData((char *) rels,
    5589             :                          nrels * sizeof(RelFileNode));
    5590             :     }
    5591             : 
    5592      267958 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_INVALS)
    5593             :     {
    5594      228900 :         XLogRegisterData((char *) (&xl_invals), MinSizeOfXactInvals);
    5595      228900 :         XLogRegisterData((char *) msgs,
    5596             :                          nmsgs * sizeof(SharedInvalidationMessage));
    5597             :     }
    5598             : 
    5599      267958 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
    5600             :     {
    5601          46 :         XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
    5602          46 :         if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID)
    5603           4 :             XLogRegisterData(unconstify(char *, twophase_gid), strlen(twophase_gid) + 1);
    5604             :     }
    5605             : 
    5606      267958 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
    5607         438 :         XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
    5608             : 
    5609             :     /* we allow filtering by xacts */
    5610      267958 :     XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
    5611             : 
    5612      267958 :     return XLogInsert(RM_XACT_ID, info);
    5613             : }
    5614             : 
    5615             : /*
    5616             :  * Log the commit record for a plain or twophase transaction abort.
    5617             :  *
    5618             :  * A 2pc abort will be emitted when twophase_xid is valid, a plain one
    5619             :  * otherwise.
    5620             :  */
    5621             : XLogRecPtr
    5622        5772 : XactLogAbortRecord(TimestampTz abort_time,
    5623             :                    int nsubxacts, TransactionId *subxacts,
    5624             :                    int nrels, RelFileNode *rels,
    5625             :                    int xactflags, TransactionId twophase_xid,
    5626             :                    const char *twophase_gid)
    5627             : {
    5628             :     xl_xact_abort xlrec;
    5629             :     xl_xact_xinfo xl_xinfo;
    5630             :     xl_xact_subxacts xl_subxacts;
    5631             :     xl_xact_relfilenodes xl_relfilenodes;
    5632             :     xl_xact_twophase xl_twophase;
    5633             :     xl_xact_dbinfo xl_dbinfo;
    5634             :     xl_xact_origin xl_origin;
    5635             : 
    5636             :     uint8       info;
    5637             : 
    5638             :     Assert(CritSectionCount > 0);
    5639             : 
    5640        5772 :     xl_xinfo.xinfo = 0;
    5641             : 
    5642             :     /* decide between a plain and 2pc abort */
    5643        5772 :     if (!TransactionIdIsValid(twophase_xid))
    5644        5752 :         info = XLOG_XACT_ABORT;
    5645             :     else
    5646          20 :         info = XLOG_XACT_ABORT_PREPARED;
    5647             : 
    5648             : 
    5649             :     /* First figure out and collect all the information needed */
    5650             : 
    5651        5772 :     xlrec.xact_time = abort_time;
    5652             : 
    5653        5772 :     if ((xactflags & XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK))
    5654        2996 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_AE_LOCKS;
    5655             : 
    5656        5772 :     if (nsubxacts > 0)
    5657             :     {
    5658         168 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_SUBXACTS;
    5659         168 :         xl_subxacts.nsubxacts = nsubxacts;
    5660             :     }
    5661             : 
    5662        5772 :     if (nrels > 0)
    5663             :     {
    5664         888 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_RELFILENODES;
    5665         888 :         xl_relfilenodes.nrels = nrels;
    5666             :     }
    5667             : 
    5668        5772 :     if (TransactionIdIsValid(twophase_xid))
    5669             :     {
    5670          20 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_TWOPHASE;
    5671          20 :         xl_twophase.xid = twophase_xid;
    5672             :         Assert(twophase_gid != NULL);
    5673             : 
    5674          20 :         if (XLogLogicalInfoActive())
    5675           2 :             xl_xinfo.xinfo |= XACT_XINFO_HAS_GID;
    5676             :     }
    5677             : 
    5678        5772 :     if (TransactionIdIsValid(twophase_xid) && XLogLogicalInfoActive())
    5679             :     {
    5680           2 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_DBINFO;
    5681           2 :         xl_dbinfo.dbId = MyDatabaseId;
    5682           2 :         xl_dbinfo.tsId = MyDatabaseTableSpace;
    5683             :     }
    5684             : 
    5685             :     /* dump transaction origin information only for abort prepared */
    5686        5772 :     if ((replorigin_session_origin != InvalidRepOriginId) &&
    5687           0 :         TransactionIdIsValid(twophase_xid) &&
    5688           0 :         XLogLogicalInfoActive())
    5689             :     {
    5690           0 :         xl_xinfo.xinfo |= XACT_XINFO_HAS_ORIGIN;
    5691             : 
    5692           0 :         xl_origin.origin_lsn = replorigin_session_origin_lsn;
    5693           0 :         xl_origin.origin_timestamp = replorigin_session_origin_timestamp;
    5694             :     }
    5695             : 
    5696        5772 :     if (xl_xinfo.xinfo != 0)
    5697        3146 :         info |= XLOG_XACT_HAS_INFO;
    5698             : 
    5699             :     /* Then include all the collected data into the abort record. */
    5700             : 
    5701        5772 :     XLogBeginInsert();
    5702             : 
    5703        5772 :     XLogRegisterData((char *) (&xlrec), MinSizeOfXactAbort);
    5704             : 
    5705        5772 :     if (xl_xinfo.xinfo != 0)
    5706        3146 :         XLogRegisterData((char *) (&xl_xinfo), sizeof(xl_xinfo));
    5707             : 
    5708        5772 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_DBINFO)
    5709           2 :         XLogRegisterData((char *) (&xl_dbinfo), sizeof(xl_dbinfo));
    5710             : 
    5711        5772 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_SUBXACTS)
    5712             :     {
    5713         168 :         XLogRegisterData((char *) (&xl_subxacts),
    5714             :                          MinSizeOfXactSubxacts);
    5715         168 :         XLogRegisterData((char *) subxacts,
    5716             :                          nsubxacts * sizeof(TransactionId));
    5717             :     }
    5718             : 
    5719        5772 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_RELFILENODES)
    5720             :     {
    5721         888 :         XLogRegisterData((char *) (&xl_relfilenodes),
    5722             :                          MinSizeOfXactRelfilenodes);
    5723         888 :         XLogRegisterData((char *) rels,
    5724             :                          nrels * sizeof(RelFileNode));
    5725             :     }
    5726             : 
    5727        5772 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_TWOPHASE)
    5728             :     {
    5729          20 :         XLogRegisterData((char *) (&xl_twophase), sizeof(xl_xact_twophase));
    5730          20 :         if (xl_xinfo.xinfo & XACT_XINFO_HAS_GID)
    5731           2 :             XLogRegisterData(unconstify(char *, twophase_gid), strlen(twophase_gid) + 1);
    5732             :     }
    5733             : 
    5734        5772 :     if (xl_xinfo.xinfo & XACT_XINFO_HAS_ORIGIN)
    5735           0 :         XLogRegisterData((char *) (&xl_origin), sizeof(xl_xact_origin));
    5736             : 
    5737        5772 :     if (TransactionIdIsValid(twophase_xid))
    5738          20 :         XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
    5739             : 
    5740        5772 :     return XLogInsert(RM_XACT_ID, info);
    5741             : }
    5742             : 
    5743             : /*
    5744             :  * Before 9.0 this was a fairly short function, but now it performs many
    5745             :  * actions for which the order of execution is critical.
    5746             :  */
    5747             : static void
    5748        4390 : xact_redo_commit(xl_xact_parsed_commit *parsed,
    5749             :                  TransactionId xid,
    5750             :                  XLogRecPtr lsn,
    5751             :                  RepOriginId origin_id)
    5752             : {
    5753             :     TransactionId max_xid;
    5754             :     TimestampTz commit_time;
    5755             : 
    5756             :     Assert(TransactionIdIsValid(xid));
    5757             : 
    5758        4390 :     max_xid = TransactionIdLatest(xid, parsed->nsubxacts, parsed->subxacts);
    5759             : 
    5760             :     /* Make sure nextFullXid is beyond any XID mentioned in the record. */
    5761        4390 :     AdvanceNextFullTransactionIdPastXid(max_xid);
    5762             : 
    5763             :     Assert(((parsed->xinfo & XACT_XINFO_HAS_ORIGIN) == 0) ==
    5764             :            (origin_id == InvalidRepOriginId));
    5765             : 
    5766        4390 :     if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
    5767           0 :         commit_time = parsed->origin_timestamp;
    5768             :     else
    5769        4390 :         commit_time = parsed->xact_time;
    5770             : 
    5771             :     /* Set the transaction commit timestamp and metadata */
    5772        4390 :     TransactionTreeSetCommitTsData(xid, parsed->nsubxacts, parsed->subxacts,
    5773             :                                    commit_time, origin_id, false);
    5774             : 
    5775        4390 :     if (standbyState == STANDBY_DISABLED)
    5776             :     {
    5777             :         /*
    5778             :          * Mark the transaction committed in pg_xact.
    5779             :          */
    5780        4134 :         TransactionIdCommitTree(xid, parsed->nsubxacts, parsed->subxacts);
    5781             :     }
    5782             :     else
    5783             :     {
    5784             :         /*
    5785             :          * If a transaction completion record arrives that has as-yet
    5786             :          * unobserved subtransactions then this will not have been fully
    5787             :          * handled by the call to RecordKnownAssignedTransactionIds() in the
    5788             :          * main recovery loop in xlog.c. So we need to do bookkeeping again to
    5789             :          * cover that case. This is confusing and it is easy to think this
    5790             :          * call is irrelevant, which has happened three times in development
    5791             :          * already. Leave it in.
    5792             :          */
    5793         256 :         RecordKnownAssignedTransactionIds(max_xid);
    5794             : 
    5795             :         /*
    5796             :          * Mark the transaction committed in pg_xact. We use async commit
    5797             :          * protocol during recovery to provide information on database
    5798             :          * consistency for when users try to set hint bits. It is important
    5799             :          * that we do not set hint bits until the minRecoveryPoint is past
    5800             :          * this commit record. This ensures that if we crash we don't see hint
    5801             :          * bits set on changes made by transactions that haven't yet
    5802             :          * recovered. It's unlikely but it's good to be safe.
    5803             :          */
    5804         256 :         TransactionIdAsyncCommitTree(xid, parsed->nsubxacts, parsed->subxacts, lsn);
    5805             : 
    5806             :         /*
    5807             :          * We must mark clog before we update the ProcArray.
    5808             :          */
    5809         256 :         ExpireTreeKnownAssignedTransactionIds(xid, parsed->nsubxacts, parsed->subxacts, max_xid);
    5810             : 
    5811             :         /*
    5812             :          * Send any cache invalidations attached to the commit. We must
    5813             :          * maintain the same order of invalidation then release locks as
    5814             :          * occurs in CommitTransaction().
    5815             :          */
    5816         512 :         ProcessCommittedInvalidationMessages(parsed->msgs, parsed->nmsgs,
    5817         256 :                                              XactCompletionRelcacheInitFileInval(parsed->xinfo),
    5818             :                                              parsed->dbId, parsed->tsId);
    5819             : 
    5820             :         /*
    5821             :          * Release locks, if any. We do this for both two phase and normal one
    5822             :          * phase transactions. In effect we are ignoring the prepare phase and
    5823             :          * just going straight to lock release.
    5824             :          */
    5825         256 :         if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
    5826         166 :             StandbyReleaseLockTree(xid, parsed->nsubxacts, parsed->subxacts);
    5827             :     }
    5828             : 
    5829        4390 :     if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
    5830             :     {
    5831             :         /* recover apply progress */
    5832           0 :         replorigin_advance(origin_id, parsed->origin_lsn, lsn,
    5833             :                            false /* backward */ , false /* WAL */ );
    5834             :     }
    5835             : 
    5836             :     /* Make sure files supposed to be dropped are dropped */
    5837        4390 :     if (parsed->nrels > 0)
    5838             :     {
    5839             :         /*
    5840             :          * First update minimum recovery point to cover this WAL record. Once
    5841             :          * a relation is deleted, there's no going back. The buffer manager
    5842             :          * enforces the WAL-first rule for normal updates to relation files,
    5843             :          * so that the minimum recovery point is always updated before the
    5844             :          * corresponding change in the data file is flushed to disk, but we
    5845             :          * have to do the same here since we're bypassing the buffer manager.
    5846             :          *
    5847             :          * Doing this before deleting the files means that if a deletion fails
    5848             :          * for some reason, you cannot start up the system even after restart,
    5849             :          * until you fix the underlying situation so that the deletion will
    5850             :          * succeed. Alternatively, we could update the minimum recovery point
    5851             :          * after deletion, but that would leave a small window where the
    5852             :          * WAL-first rule would be violated.
    5853             :          */
    5854          56 :         XLogFlush(lsn);
    5855             : 
    5856             :         /* Make sure files supposed to be dropped are dropped */
    5857          56 :         DropRelationFiles(parsed->xnodes, parsed->nrels, true);
    5858             :     }
    5859             : 
    5860             :     /*
    5861             :      * We issue an XLogFlush() for the same reason we emit ForceSyncCommit()
    5862             :      * in normal operation. For example, in CREATE DATABASE, we copy all files
    5863             :      * from the template database, and then commit the transaction. If we
    5864             :      * crash after all the files have been copied but before the commit, you
    5865             :      * have files in the data directory without an entry in pg_database. To
    5866             :      * minimize the window for that, we use ForceSyncCommit() to rush the
    5867             :      * commit record to disk as quick as possible. We have the same window
    5868             :      * during recovery, and forcing an XLogFlush() (which updates
    5869             :      * minRecoveryPoint during recovery) helps to reduce that problem window,
    5870             :      * for any user that requested ForceSyncCommit().
    5871             :      */
    5872        4390 :     if (XactCompletionForceSyncCommit(parsed->xinfo))
    5873          16 :         XLogFlush(lsn);
    5874             : 
    5875             :     /*
    5876             :      * If asked by the primary (because someone is waiting for a synchronous
    5877             :      * commit = remote_apply), we will need to ask walreceiver to send a reply
    5878             :      * immediately.
    5879             :      */
    5880        4390 :     if (XactCompletionApplyFeedback(parsed->xinfo))
    5881           0 :         XLogRequestWalReceiverReply();
    5882        4390 : }
    5883             : 
    5884             : /*
    5885             :  * Be careful with the order of execution, as with xact_redo_commit().
    5886             :  * The two functions are similar but differ in key places.
    5887             :  *
    5888             :  * Note also that an abort can be for a subtransaction and its children,
    5889             :  * not just for a top level abort. That means we have to consider
    5890             :  * topxid != xid, whereas in commit we would find topxid == xid always
    5891             :  * because subtransaction commit is never WAL logged.
    5892             :  */
    5893             : static void
    5894          28 : xact_redo_abort(xl_xact_parsed_abort *parsed, TransactionId xid)
    5895             : {
    5896             :     TransactionId max_xid;
    5897             : 
    5898             :     Assert(TransactionIdIsValid(xid));
    5899             : 
    5900             :     /* Make sure nextFullXid is beyond any XID mentioned in the record. */
    5901          28 :     max_xid = TransactionIdLatest(xid,
    5902             :                                   parsed->nsubxacts,
    5903          28 :                                   parsed->subxacts);
    5904          28 :     AdvanceNextFullTransactionIdPastXid(max_xid);
    5905             : 
    5906          28 :     if (standbyState == STANDBY_DISABLED)
    5907             :     {
    5908             :         /* Mark the transaction aborted in pg_xact, no need for async stuff */
    5909          20 :         TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
    5910             :     }
    5911             :     else
    5912             :     {
    5913             :         /*
    5914             :          * If a transaction completion record arrives that has as-yet
    5915             :          * unobserved subtransactions then this will not have been fully
    5916             :          * handled by the call to RecordKnownAssignedTransactionIds() in the
    5917             :          * main recovery loop in xlog.c. So we need to do bookkeeping again to
    5918             :          * cover that case. This is confusing and it is easy to think this
    5919             :          * call is irrelevant, which has happened three times in development
    5920             :          * already. Leave it in.
    5921             :          */
    5922           8 :         RecordKnownAssignedTransactionIds(max_xid);
    5923             : 
    5924             :         /* Mark the transaction aborted in pg_xact, no need for async stuff */
    5925           8 :         TransactionIdAbortTree(xid, parsed->nsubxacts, parsed->subxacts);
    5926             : 
    5927             :         /*
    5928             :          * We must update the ProcArray after we have marked clog.
    5929             :          */
    5930           8 :         ExpireTreeKnownAssignedTransactionIds(xid, parsed->nsubxacts, parsed->subxacts, max_xid);
    5931             : 
    5932             :         /*
    5933             :          * There are no invalidation messages to send or undo.
    5934             :          */
    5935             : 
    5936             :         /*
    5937             :          * Release locks, if any. There are no invalidations to send.
    5938             :          */
    5939           8 :         if (parsed->xinfo & XACT_XINFO_HAS_AE_LOCKS)
    5940           6 :             StandbyReleaseLockTree(xid, parsed->nsubxacts, parsed->subxacts);
    5941             :     }
    5942             : 
    5943             :     /* Make sure files supposed to be dropped are dropped */
    5944          28 :     DropRelationFiles(parsed->xnodes, parsed->nrels, true);
    5945          28 : }
    5946             : 
    5947             : void
    5948        4518 : xact_redo(XLogReaderState *record)
    5949             : {
    5950        4518 :     uint8       info = XLogRecGetInfo(record) & XLOG_XACT_OPMASK;
    5951             : 
    5952             :     /* Backup blocks are not used in xact records */
    5953             :     Assert(!XLogRecHasAnyBlockRefs(record));
    5954             : 
    5955        4518 :     if (info == XLOG_XACT_COMMIT)
    5956             :     {
    5957        4348 :         xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
    5958             :         xl_xact_parsed_commit parsed;
    5959             : 
    5960        4348 :         ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
    5961        4348 :         xact_redo_commit(&parsed, XLogRecGetXid(record),
    5962        4348 :                          record->EndRecPtr, XLogRecGetOrigin(record));
    5963             :     }
    5964         170 :     else if (info == XLOG_XACT_COMMIT_PREPARED)
    5965             :     {
    5966          42 :         xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(record);
    5967             :         xl_xact_parsed_commit parsed;
    5968             : 
    5969          42 :         ParseCommitRecord(XLogRecGetInfo(record), xlrec, &parsed);
    5970          42 :         xact_redo_commit(&parsed, parsed.twophase_xid,
    5971          42 :                          record->EndRecPtr, XLogRecGetOrigin(record));
    5972             : 
    5973             :         /* Delete TwoPhaseState gxact entry and/or 2PC file. */
    5974          42 :         LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
    5975          42 :         PrepareRedoRemove(parsed.twophase_xid, false);
    5976          42 :         LWLockRelease(TwoPhaseStateLock);
    5977             :     }
    5978         128 :     else if (info == XLOG_XACT_ABORT)
    5979             :     {
    5980          22 :         xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
    5981             :         xl_xact_parsed_abort parsed;
    5982             : 
    5983          22 :         ParseAbortRecord(XLogRecGetInfo(record), xlrec, &parsed);
    5984          22 :         xact_redo_abort(&parsed, XLogRecGetXid(record));
    5985             :     }
    5986         106 :     else if (info == XLOG_XACT_ABORT_PREPARED)
    5987             :     {
    5988           6 :         xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(record);
    5989             :         xl_xact_parsed_abort parsed;
    5990             : 
    5991           6 :         ParseAbortRecord(XLogRecGetInfo(record), xlrec, &parsed);
    5992           6 :         xact_redo_abort(&parsed, parsed.twophase_xid);
    5993             : 
    5994             :         /* Delete TwoPhaseState gxact entry and/or 2PC file. */
    5995           6 :         LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
    5996           6 :         PrepareRedoRemove(parsed.twophase_xid, false);
    5997           6 :         LWLockRelease(TwoPhaseStateLock);
    5998             :     }
    5999         100 :     else if (info == XLOG_XACT_PREPARE)
    6000             :     {
    6001             :         /*
    6002             :          * Store xid and start/end pointers of the WAL record in TwoPhaseState
    6003             :          * gxact entry.
    6004             :          */
    6005          60 :         LWLockAcquire(TwoPhaseStateLock, LW_EXCLUSIVE);
    6006          60 :         PrepareRedoAdd(XLogRecGetData(record),
    6007             :                        record->ReadRecPtr,
    6008             :                        record->EndRecPtr,
    6009          60 :                        XLogRecGetOrigin(record));
    6010          60 :         LWLockRelease(TwoPhaseStateLock);
    6011             :     }
    6012          40 :     else if (info == XLOG_XACT_ASSIGNMENT)
    6013             :     {
    6014          40 :         xl_xact_assignment *xlrec = (xl_xact_assignment *) XLogRecGetData(record);
    6015             : 
    6016          40 :         if (standbyState >= STANDBY_INITIALIZED)
    6017          40 :             ProcArrayApplyXidAssignment(xlrec->xtop,
    6018          40 :                                         xlrec->nsubxacts, xlrec->xsub);
    6019             :     }
    6020             :     else
    6021           0 :         elog(PANIC, "xact_redo: unknown op code %u", info);
    6022        4518 : }

Generated by: LCOV version 1.13