LCOV - code coverage report
Current view: top level - src/backend/access/transam - xact.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 87.3 % 1859 1623
Test Date: 2026-07-07 10:15:45 Functions: 93.6 % 110 103
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 64.5 % 928 599

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

Generated by: LCOV version 2.0-1