LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - procarray.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 797 1052 75.8 %
Date: 2019-09-19 23:07:04 Functions: 46 56 82.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * procarray.c
       4             :  *    POSTGRES process array code.
       5             :  *
       6             :  *
       7             :  * This module maintains arrays of the PGPROC and PGXACT structures for all
       8             :  * active backends.  Although there are several uses for this, the principal
       9             :  * one is as a means of determining the set of currently running transactions.
      10             :  *
      11             :  * Because of various subtle race conditions it is critical that a backend
      12             :  * hold the correct locks while setting or clearing its MyPgXact->xid field.
      13             :  * See notes in src/backend/access/transam/README.
      14             :  *
      15             :  * The process arrays now also include structures representing prepared
      16             :  * transactions.  The xid and subxids fields of these are valid, as are the
      17             :  * myProcLocks lists.  They can be distinguished from regular backend PGPROCs
      18             :  * at need by checking for pid == 0.
      19             :  *
      20             :  * During hot standby, we also keep a list of XIDs representing transactions
      21             :  * that are known to be running in the master (or more precisely, were running
      22             :  * as of the current point in the WAL stream).  This list is kept in the
      23             :  * KnownAssignedXids array, and is updated by watching the sequence of
      24             :  * arriving XIDs.  This is necessary because if we leave those XIDs out of
      25             :  * snapshots taken for standby queries, then they will appear to be already
      26             :  * complete, leading to MVCC failures.  Note that in hot standby, the PGPROC
      27             :  * array represents standby processes, which by definition are not running
      28             :  * transactions that have XIDs.
      29             :  *
      30             :  * It is perhaps possible for a backend on the master to terminate without
      31             :  * writing an abort record for its transaction.  While that shouldn't really
      32             :  * happen, it would tie up KnownAssignedXids indefinitely, so we protect
      33             :  * ourselves by pruning the array when a valid list of running XIDs arrives.
      34             :  *
      35             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
      36             :  * Portions Copyright (c) 1994, Regents of the University of California
      37             :  *
      38             :  *
      39             :  * IDENTIFICATION
      40             :  *    src/backend/storage/ipc/procarray.c
      41             :  *
      42             :  *-------------------------------------------------------------------------
      43             :  */
      44             : #include "postgres.h"
      45             : 
      46             : #include <signal.h>
      47             : 
      48             : #include "access/clog.h"
      49             : #include "access/subtrans.h"
      50             : #include "access/transam.h"
      51             : #include "access/twophase.h"
      52             : #include "access/xact.h"
      53             : #include "access/xlog.h"
      54             : #include "catalog/catalog.h"
      55             : #include "miscadmin.h"
      56             : #include "pgstat.h"
      57             : #include "storage/proc.h"
      58             : #include "storage/procarray.h"
      59             : #include "storage/spin.h"
      60             : #include "utils/builtins.h"
      61             : #include "utils/rel.h"
      62             : #include "utils/snapmgr.h"
      63             : 
      64             : #define UINT32_ACCESS_ONCE(var)      ((uint32)(*((volatile uint32 *)&(var))))
      65             : 
      66             : /* Our shared memory area */
      67             : typedef struct ProcArrayStruct
      68             : {
      69             :     int         numProcs;       /* number of valid procs entries */
      70             :     int         maxProcs;       /* allocated size of procs array */
      71             : 
      72             :     /*
      73             :      * Known assigned XIDs handling
      74             :      */
      75             :     int         maxKnownAssignedXids;   /* allocated size of array */
      76             :     int         numKnownAssignedXids;   /* current # of valid entries */
      77             :     int         tailKnownAssignedXids;  /* index of oldest valid element */
      78             :     int         headKnownAssignedXids;  /* index of newest element, + 1 */
      79             :     slock_t     known_assigned_xids_lck;    /* protects head/tail pointers */
      80             : 
      81             :     /*
      82             :      * Highest subxid that has been removed from KnownAssignedXids array to
      83             :      * prevent overflow; or InvalidTransactionId if none.  We track this for
      84             :      * similar reasons to tracking overflowing cached subxids in PGXACT
      85             :      * entries.  Must hold exclusive ProcArrayLock to change this, and shared
      86             :      * lock to read it.
      87             :      */
      88             :     TransactionId lastOverflowedXid;
      89             : 
      90             :     /* oldest xmin of any replication slot */
      91             :     TransactionId replication_slot_xmin;
      92             :     /* oldest catalog xmin of any replication slot */
      93             :     TransactionId replication_slot_catalog_xmin;
      94             : 
      95             :     /* indexes into allPgXact[], has PROCARRAY_MAXPROCS entries */
      96             :     int         pgprocnos[FLEXIBLE_ARRAY_MEMBER];
      97             : } ProcArrayStruct;
      98             : 
      99             : static ProcArrayStruct *procArray;
     100             : 
     101             : static PGPROC *allProcs;
     102             : static PGXACT *allPgXact;
     103             : 
     104             : /*
     105             :  * Bookkeeping for tracking emulated transactions in recovery
     106             :  */
     107             : static TransactionId *KnownAssignedXids;
     108             : static bool *KnownAssignedXidsValid;
     109             : static TransactionId latestObservedXid = InvalidTransactionId;
     110             : 
     111             : /*
     112             :  * If we're in STANDBY_SNAPSHOT_PENDING state, standbySnapshotPendingXmin is
     113             :  * the highest xid that might still be running that we don't have in
     114             :  * KnownAssignedXids.
     115             :  */
     116             : static TransactionId standbySnapshotPendingXmin;
     117             : 
     118             : #ifdef XIDCACHE_DEBUG
     119             : 
     120             : /* counters for XidCache measurement */
     121             : static long xc_by_recent_xmin = 0;
     122             : static long xc_by_known_xact = 0;
     123             : static long xc_by_my_xact = 0;
     124             : static long xc_by_latest_xid = 0;
     125             : static long xc_by_main_xid = 0;
     126             : static long xc_by_child_xid = 0;
     127             : static long xc_by_known_assigned = 0;
     128             : static long xc_no_overflow = 0;
     129             : static long xc_slow_answer = 0;
     130             : 
     131             : #define xc_by_recent_xmin_inc()     (xc_by_recent_xmin++)
     132             : #define xc_by_known_xact_inc()      (xc_by_known_xact++)
     133             : #define xc_by_my_xact_inc()         (xc_by_my_xact++)
     134             : #define xc_by_latest_xid_inc()      (xc_by_latest_xid++)
     135             : #define xc_by_main_xid_inc()        (xc_by_main_xid++)
     136             : #define xc_by_child_xid_inc()       (xc_by_child_xid++)
     137             : #define xc_by_known_assigned_inc()  (xc_by_known_assigned++)
     138             : #define xc_no_overflow_inc()        (xc_no_overflow++)
     139             : #define xc_slow_answer_inc()        (xc_slow_answer++)
     140             : 
     141             : static void DisplayXidCache(void);
     142             : #else                           /* !XIDCACHE_DEBUG */
     143             : 
     144             : #define xc_by_recent_xmin_inc()     ((void) 0)
     145             : #define xc_by_known_xact_inc()      ((void) 0)
     146             : #define xc_by_my_xact_inc()         ((void) 0)
     147             : #define xc_by_latest_xid_inc()      ((void) 0)
     148             : #define xc_by_main_xid_inc()        ((void) 0)
     149             : #define xc_by_child_xid_inc()       ((void) 0)
     150             : #define xc_by_known_assigned_inc()  ((void) 0)
     151             : #define xc_no_overflow_inc()        ((void) 0)
     152             : #define xc_slow_answer_inc()        ((void) 0)
     153             : #endif                          /* XIDCACHE_DEBUG */
     154             : 
     155             : /* Primitives for KnownAssignedXids array handling for standby */
     156             : static void KnownAssignedXidsCompress(bool force);
     157             : static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
     158             :                                  bool exclusive_lock);
     159             : static bool KnownAssignedXidsSearch(TransactionId xid, bool remove);
     160             : static bool KnownAssignedXidExists(TransactionId xid);
     161             : static void KnownAssignedXidsRemove(TransactionId xid);
     162             : static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
     163             :                                         TransactionId *subxids);
     164             : static void KnownAssignedXidsRemovePreceding(TransactionId xid);
     165             : static int  KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax);
     166             : static int  KnownAssignedXidsGetAndSetXmin(TransactionId *xarray,
     167             :                                            TransactionId *xmin,
     168             :                                            TransactionId xmax);
     169             : static TransactionId KnownAssignedXidsGetOldestXmin(void);
     170             : static void KnownAssignedXidsDisplay(int trace_level);
     171             : static void KnownAssignedXidsReset(void);
     172             : static inline void ProcArrayEndTransactionInternal(PGPROC *proc,
     173             :                                                    PGXACT *pgxact, TransactionId latestXid);
     174             : static void ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid);
     175             : 
     176             : /*
     177             :  * Report shared-memory space needed by CreateSharedProcArray.
     178             :  */
     179             : Size
     180        1860 : ProcArrayShmemSize(void)
     181             : {
     182             :     Size        size;
     183             : 
     184             :     /* Size of the ProcArray structure itself */
     185             : #define PROCARRAY_MAXPROCS  (MaxBackends + max_prepared_xacts)
     186             : 
     187        1860 :     size = offsetof(ProcArrayStruct, pgprocnos);
     188        1860 :     size = add_size(size, mul_size(sizeof(int), PROCARRAY_MAXPROCS));
     189             : 
     190             :     /*
     191             :      * During Hot Standby processing we have a data structure called
     192             :      * KnownAssignedXids, created in shared memory. Local data structures are
     193             :      * also created in various backends during GetSnapshotData(),
     194             :      * TransactionIdIsInProgress() and GetRunningTransactionData(). All of the
     195             :      * main structures created in those functions must be identically sized,
     196             :      * since we may at times copy the whole of the data structures around. We
     197             :      * refer to this size as TOTAL_MAX_CACHED_SUBXIDS.
     198             :      *
     199             :      * Ideally we'd only create this structure if we were actually doing hot
     200             :      * standby in the current run, but we don't know that yet at the time
     201             :      * shared memory is being set up.
     202             :      */
     203             : #define TOTAL_MAX_CACHED_SUBXIDS \
     204             :     ((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
     205             : 
     206        1860 :     if (EnableHotStandby)
     207             :     {
     208        1860 :         size = add_size(size,
     209             :                         mul_size(sizeof(TransactionId),
     210        1860 :                                  TOTAL_MAX_CACHED_SUBXIDS));
     211        1860 :         size = add_size(size,
     212        1860 :                         mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS));
     213             :     }
     214             : 
     215        1860 :     return size;
     216             : }
     217             : 
     218             : /*
     219             :  * Initialize the shared PGPROC array during postmaster startup.
     220             :  */
     221             : void
     222        1856 : CreateSharedProcArray(void)
     223             : {
     224             :     bool        found;
     225             : 
     226             :     /* Create or attach to the ProcArray shared structure */
     227        1856 :     procArray = (ProcArrayStruct *)
     228        1856 :         ShmemInitStruct("Proc Array",
     229             :                         add_size(offsetof(ProcArrayStruct, pgprocnos),
     230             :                                  mul_size(sizeof(int),
     231        1856 :                                           PROCARRAY_MAXPROCS)),
     232             :                         &found);
     233             : 
     234        1856 :     if (!found)
     235             :     {
     236             :         /*
     237             :          * We're the first - initialize.
     238             :          */
     239        1856 :         procArray->numProcs = 0;
     240        1856 :         procArray->maxProcs = PROCARRAY_MAXPROCS;
     241        1856 :         procArray->maxKnownAssignedXids = TOTAL_MAX_CACHED_SUBXIDS;
     242        1856 :         procArray->numKnownAssignedXids = 0;
     243        1856 :         procArray->tailKnownAssignedXids = 0;
     244        1856 :         procArray->headKnownAssignedXids = 0;
     245        1856 :         SpinLockInit(&procArray->known_assigned_xids_lck);
     246        1856 :         procArray->lastOverflowedXid = InvalidTransactionId;
     247        1856 :         procArray->replication_slot_xmin = InvalidTransactionId;
     248        1856 :         procArray->replication_slot_catalog_xmin = InvalidTransactionId;
     249             :     }
     250             : 
     251        1856 :     allProcs = ProcGlobal->allProcs;
     252        1856 :     allPgXact = ProcGlobal->allPgXact;
     253             : 
     254             :     /* Create or attach to the KnownAssignedXids arrays too, if needed */
     255        1856 :     if (EnableHotStandby)
     256             :     {
     257        1856 :         KnownAssignedXids = (TransactionId *)
     258        1856 :             ShmemInitStruct("KnownAssignedXids",
     259             :                             mul_size(sizeof(TransactionId),
     260        1856 :                                      TOTAL_MAX_CACHED_SUBXIDS),
     261             :                             &found);
     262        1856 :         KnownAssignedXidsValid = (bool *)
     263        1856 :             ShmemInitStruct("KnownAssignedXidsValid",
     264        1856 :                             mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS),
     265             :                             &found);
     266             :     }
     267             : 
     268        1856 :     LWLockRegisterTranche(LWTRANCHE_PROC, "proc");
     269        1856 : }
     270             : 
     271             : /*
     272             :  * Add the specified PGPROC to the shared array.
     273             :  */
     274             : void
     275        9972 : ProcArrayAdd(PGPROC *proc)
     276             : {
     277        9972 :     ProcArrayStruct *arrayP = procArray;
     278             :     int         index;
     279             : 
     280        9972 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
     281             : 
     282        9972 :     if (arrayP->numProcs >= arrayP->maxProcs)
     283             :     {
     284             :         /*
     285             :          * Oops, no room.  (This really shouldn't happen, since there is a
     286             :          * fixed supply of PGPROC structs too, and so we should have failed
     287             :          * earlier.)
     288             :          */
     289           0 :         LWLockRelease(ProcArrayLock);
     290           0 :         ereport(FATAL,
     291             :                 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
     292             :                  errmsg("sorry, too many clients already")));
     293             :     }
     294             : 
     295             :     /*
     296             :      * Keep the procs array sorted by (PGPROC *) so that we can utilize
     297             :      * locality of references much better. This is useful while traversing the
     298             :      * ProcArray because there is an increased likelihood of finding the next
     299             :      * PGPROC structure in the cache.
     300             :      *
     301             :      * Since the occurrence of adding/removing a proc is much lower than the
     302             :      * access to the ProcArray itself, the overhead should be marginal
     303             :      */
     304       18874 :     for (index = 0; index < arrayP->numProcs; index++)
     305             :     {
     306             :         /*
     307             :          * If we are the first PGPROC or if we have found our right position
     308             :          * in the array, break
     309             :          */
     310       17032 :         if ((arrayP->pgprocnos[index] == -1) || (arrayP->pgprocnos[index] > proc->pgprocno))
     311             :             break;
     312             :     }
     313             : 
     314        9972 :     memmove(&arrayP->pgprocnos[index + 1], &arrayP->pgprocnos[index],
     315        9972 :             (arrayP->numProcs - index) * sizeof(int));
     316        9972 :     arrayP->pgprocnos[index] = proc->pgprocno;
     317        9972 :     arrayP->numProcs++;
     318             : 
     319        9972 :     LWLockRelease(ProcArrayLock);
     320        9972 : }
     321             : 
     322             : /*
     323             :  * Remove the specified PGPROC from the shared array.
     324             :  *
     325             :  * When latestXid is a valid XID, we are removing a live 2PC gxact from the
     326             :  * array, and thus causing it to appear as "not running" anymore.  In this
     327             :  * case we must advance latestCompletedXid.  (This is essentially the same
     328             :  * as ProcArrayEndTransaction followed by removal of the PGPROC, but we take
     329             :  * the ProcArrayLock only once, and don't damage the content of the PGPROC;
     330             :  * twophase.c depends on the latter.)
     331             :  */
     332             : void
     333        9942 : ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
     334             : {
     335        9942 :     ProcArrayStruct *arrayP = procArray;
     336             :     int         index;
     337             : 
     338             : #ifdef XIDCACHE_DEBUG
     339             :     /* dump stats at backend shutdown, but not prepared-xact end */
     340             :     if (proc->pid != 0)
     341             :         DisplayXidCache();
     342             : #endif
     343             : 
     344        9942 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
     345             : 
     346        9942 :     if (TransactionIdIsValid(latestXid))
     347             :     {
     348             :         Assert(TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
     349             : 
     350             :         /* Advance global latestCompletedXid while holding the lock */
     351          66 :         if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
     352             :                                   latestXid))
     353          22 :             ShmemVariableCache->latestCompletedXid = latestXid;
     354             :     }
     355             :     else
     356             :     {
     357             :         /* Shouldn't be trying to remove a live transaction here */
     358             :         Assert(!TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
     359             :     }
     360             : 
     361       19148 :     for (index = 0; index < arrayP->numProcs; index++)
     362             :     {
     363       19148 :         if (arrayP->pgprocnos[index] == proc->pgprocno)
     364             :         {
     365             :             /* Keep the PGPROC array sorted. See notes above */
     366        9942 :             memmove(&arrayP->pgprocnos[index], &arrayP->pgprocnos[index + 1],
     367        9942 :                     (arrayP->numProcs - index - 1) * sizeof(int));
     368        9942 :             arrayP->pgprocnos[arrayP->numProcs - 1] = -1; /* for debugging */
     369        9942 :             arrayP->numProcs--;
     370        9942 :             LWLockRelease(ProcArrayLock);
     371        9942 :             return;
     372             :         }
     373             :     }
     374             : 
     375             :     /* Oops */
     376           0 :     LWLockRelease(ProcArrayLock);
     377             : 
     378           0 :     elog(LOG, "failed to find proc %p in ProcArray", proc);
     379             : }
     380             : 
     381             : 
     382             : /*
     383             :  * ProcArrayEndTransaction -- mark a transaction as no longer running
     384             :  *
     385             :  * This is used interchangeably for commit and abort cases.  The transaction
     386             :  * commit/abort must already be reported to WAL and pg_xact.
     387             :  *
     388             :  * proc is currently always MyProc, but we pass it explicitly for flexibility.
     389             :  * latestXid is the latest Xid among the transaction's main XID and
     390             :  * subtransactions, or InvalidTransactionId if it has no XID.  (We must ask
     391             :  * the caller to pass latestXid, instead of computing it from the PGPROC's
     392             :  * contents, because the subxid information in the PGPROC might be
     393             :  * incomplete.)
     394             :  */
     395             : void
     396      459478 : ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
     397             : {
     398      459478 :     PGXACT     *pgxact = &allPgXact[proc->pgprocno];
     399             : 
     400      459478 :     if (TransactionIdIsValid(latestXid))
     401             :     {
     402             :         /*
     403             :          * We must lock ProcArrayLock while clearing our advertised XID, so
     404             :          * that we do not exit the set of "running" transactions while someone
     405             :          * else is taking a snapshot.  See discussion in
     406             :          * src/backend/access/transam/README.
     407             :          */
     408             :         Assert(TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
     409             : 
     410             :         /*
     411             :          * If we can immediately acquire ProcArrayLock, we clear our own XID
     412             :          * and release the lock.  If not, use group XID clearing to improve
     413             :          * efficiency.
     414             :          */
     415      251176 :         if (LWLockConditionalAcquire(ProcArrayLock, LW_EXCLUSIVE))
     416             :         {
     417      251020 :             ProcArrayEndTransactionInternal(proc, pgxact, latestXid);
     418      251020 :             LWLockRelease(ProcArrayLock);
     419             :         }
     420             :         else
     421         156 :             ProcArrayGroupClearXid(proc, latestXid);
     422             :     }
     423             :     else
     424             :     {
     425             :         /*
     426             :          * If we have no XID, we don't need to lock, since we won't affect
     427             :          * anyone else's calculation of a snapshot.  We might change their
     428             :          * estimate of global xmin, but that's OK.
     429             :          */
     430             :         Assert(!TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
     431             : 
     432      208302 :         proc->lxid = InvalidLocalTransactionId;
     433      208302 :         pgxact->xmin = InvalidTransactionId;
     434             :         /* must be cleared with xid/xmin: */
     435      208302 :         pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
     436      208302 :         pgxact->delayChkpt = false; /* be sure this is cleared in abort */
     437      208302 :         proc->recoveryConflictPending = false;
     438             : 
     439             :         Assert(pgxact->nxids == 0);
     440             :         Assert(pgxact->overflowed == false);
     441             :     }
     442      459478 : }
     443             : 
     444             : /*
     445             :  * Mark a write transaction as no longer running.
     446             :  *
     447             :  * We don't do any locking here; caller must handle that.
     448             :  */
     449             : static inline void
     450      251176 : ProcArrayEndTransactionInternal(PGPROC *proc, PGXACT *pgxact,
     451             :                                 TransactionId latestXid)
     452             : {
     453      251176 :     pgxact->xid = InvalidTransactionId;
     454      251176 :     proc->lxid = InvalidLocalTransactionId;
     455      251176 :     pgxact->xmin = InvalidTransactionId;
     456             :     /* must be cleared with xid/xmin: */
     457      251176 :     pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
     458      251176 :     pgxact->delayChkpt = false; /* be sure this is cleared in abort */
     459      251176 :     proc->recoveryConflictPending = false;
     460             : 
     461             :     /* Clear the subtransaction-XID cache too while holding the lock */
     462      251176 :     pgxact->nxids = 0;
     463      251176 :     pgxact->overflowed = false;
     464             : 
     465             :     /* Also advance global latestCompletedXid while holding the lock */
     466      251176 :     if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
     467             :                               latestXid))
     468      242932 :         ShmemVariableCache->latestCompletedXid = latestXid;
     469      251176 : }
     470             : 
     471             : /*
     472             :  * ProcArrayGroupClearXid -- group XID clearing
     473             :  *
     474             :  * When we cannot immediately acquire ProcArrayLock in exclusive mode at
     475             :  * commit time, add ourselves to a list of processes that need their XIDs
     476             :  * cleared.  The first process to add itself to the list will acquire
     477             :  * ProcArrayLock in exclusive mode and perform ProcArrayEndTransactionInternal
     478             :  * on behalf of all group members.  This avoids a great deal of contention
     479             :  * around ProcArrayLock when many processes are trying to commit at once,
     480             :  * since the lock need not be repeatedly handed off from one committing
     481             :  * process to the next.
     482             :  */
     483             : static void
     484         156 : ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
     485             : {
     486         156 :     PROC_HDR   *procglobal = ProcGlobal;
     487             :     uint32      nextidx;
     488             :     uint32      wakeidx;
     489             : 
     490             :     /* We should definitely have an XID to clear. */
     491             :     Assert(TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
     492             : 
     493             :     /* Add ourselves to the list of processes needing a group XID clear. */
     494         156 :     proc->procArrayGroupMember = true;
     495         156 :     proc->procArrayGroupMemberXid = latestXid;
     496             :     while (true)
     497             :     {
     498         156 :         nextidx = pg_atomic_read_u32(&procglobal->procArrayGroupFirst);
     499         156 :         pg_atomic_write_u32(&proc->procArrayGroupNext, nextidx);
     500             : 
     501         156 :         if (pg_atomic_compare_exchange_u32(&procglobal->procArrayGroupFirst,
     502             :                                            &nextidx,
     503         156 :                                            (uint32) proc->pgprocno))
     504         156 :             break;
     505             :     }
     506             : 
     507             :     /*
     508             :      * If the list was not empty, the leader will clear our XID.  It is
     509             :      * impossible to have followers without a leader because the first process
     510             :      * that has added itself to the list will always have nextidx as
     511             :      * INVALID_PGPROCNO.
     512             :      */
     513         156 :     if (nextidx != INVALID_PGPROCNO)
     514             :     {
     515           0 :         int         extraWaits = 0;
     516             : 
     517             :         /* Sleep until the leader clears our XID. */
     518           0 :         pgstat_report_wait_start(WAIT_EVENT_PROCARRAY_GROUP_UPDATE);
     519             :         for (;;)
     520             :         {
     521             :             /* acts as a read barrier */
     522           0 :             PGSemaphoreLock(proc->sem);
     523           0 :             if (!proc->procArrayGroupMember)
     524           0 :                 break;
     525           0 :             extraWaits++;
     526             :         }
     527           0 :         pgstat_report_wait_end();
     528             : 
     529             :         Assert(pg_atomic_read_u32(&proc->procArrayGroupNext) == INVALID_PGPROCNO);
     530             : 
     531             :         /* Fix semaphore count for any absorbed wakeups */
     532           0 :         while (extraWaits-- > 0)
     533           0 :             PGSemaphoreUnlock(proc->sem);
     534           0 :         return;
     535             :     }
     536             : 
     537             :     /* We are the leader.  Acquire the lock on behalf of everyone. */
     538         156 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
     539             : 
     540             :     /*
     541             :      * Now that we've got the lock, clear the list of processes waiting for
     542             :      * group XID clearing, saving a pointer to the head of the list.  Trying
     543             :      * to pop elements one at a time could lead to an ABA problem.
     544             :      */
     545         156 :     nextidx = pg_atomic_exchange_u32(&procglobal->procArrayGroupFirst,
     546             :                                      INVALID_PGPROCNO);
     547             : 
     548             :     /* Remember head of list so we can perform wakeups after dropping lock. */
     549         156 :     wakeidx = nextidx;
     550             : 
     551             :     /* Walk the list and clear all XIDs. */
     552         468 :     while (nextidx != INVALID_PGPROCNO)
     553             :     {
     554         156 :         PGPROC     *proc = &allProcs[nextidx];
     555         156 :         PGXACT     *pgxact = &allPgXact[nextidx];
     556             : 
     557         156 :         ProcArrayEndTransactionInternal(proc, pgxact, proc->procArrayGroupMemberXid);
     558             : 
     559             :         /* Move to next proc in list. */
     560         156 :         nextidx = pg_atomic_read_u32(&proc->procArrayGroupNext);
     561             :     }
     562             : 
     563             :     /* We're done with the lock now. */
     564         156 :     LWLockRelease(ProcArrayLock);
     565             : 
     566             :     /*
     567             :      * Now that we've released the lock, go back and wake everybody up.  We
     568             :      * don't do this under the lock so as to keep lock hold times to a
     569             :      * minimum.  The system calls we need to perform to wake other processes
     570             :      * up are probably much slower than the simple memory writes we did while
     571             :      * holding the lock.
     572             :      */
     573         468 :     while (wakeidx != INVALID_PGPROCNO)
     574             :     {
     575         156 :         PGPROC     *proc = &allProcs[wakeidx];
     576             : 
     577         156 :         wakeidx = pg_atomic_read_u32(&proc->procArrayGroupNext);
     578         156 :         pg_atomic_write_u32(&proc->procArrayGroupNext, INVALID_PGPROCNO);
     579             : 
     580             :         /* ensure all previous writes are visible before follower continues. */
     581         156 :         pg_write_barrier();
     582             : 
     583         156 :         proc->procArrayGroupMember = false;
     584             : 
     585         156 :         if (proc != MyProc)
     586           0 :             PGSemaphoreUnlock(proc->sem);
     587             :     }
     588             : }
     589             : 
     590             : /*
     591             :  * ProcArrayClearTransaction -- clear the transaction fields
     592             :  *
     593             :  * This is used after successfully preparing a 2-phase transaction.  We are
     594             :  * not actually reporting the transaction's XID as no longer running --- it
     595             :  * will still appear as running because the 2PC's gxact is in the ProcArray
     596             :  * too.  We just have to clear out our own PGXACT.
     597             :  */
     598             : void
     599          66 : ProcArrayClearTransaction(PGPROC *proc)
     600             : {
     601          66 :     PGXACT     *pgxact = &allPgXact[proc->pgprocno];
     602             : 
     603             :     /*
     604             :      * We can skip locking ProcArrayLock here, because this action does not
     605             :      * actually change anyone's view of the set of running XIDs: our entry is
     606             :      * duplicate with the gxact that has already been inserted into the
     607             :      * ProcArray.
     608             :      */
     609          66 :     pgxact->xid = InvalidTransactionId;
     610          66 :     proc->lxid = InvalidLocalTransactionId;
     611          66 :     pgxact->xmin = InvalidTransactionId;
     612          66 :     proc->recoveryConflictPending = false;
     613             : 
     614             :     /* redundant, but just in case */
     615          66 :     pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
     616          66 :     pgxact->delayChkpt = false;
     617             : 
     618             :     /* Clear the subtransaction-XID cache too */
     619          66 :     pgxact->nxids = 0;
     620          66 :     pgxact->overflowed = false;
     621          66 : }
     622             : 
     623             : /*
     624             :  * ProcArrayInitRecovery -- initialize recovery xid mgmt environment
     625             :  *
     626             :  * Remember up to where the startup process initialized the CLOG and subtrans
     627             :  * so we can ensure it's initialized gaplessly up to the point where necessary
     628             :  * while in recovery.
     629             :  */
     630             : void
     631          64 : ProcArrayInitRecovery(TransactionId initializedUptoXID)
     632             : {
     633             :     Assert(standbyState == STANDBY_INITIALIZED);
     634             :     Assert(TransactionIdIsNormal(initializedUptoXID));
     635             : 
     636             :     /*
     637             :      * we set latestObservedXid to the xid SUBTRANS has been initialized up
     638             :      * to, so we can extend it from that point onwards in
     639             :      * RecordKnownAssignedTransactionIds, and when we get consistent in
     640             :      * ProcArrayApplyRecoveryInfo().
     641             :      */
     642          64 :     latestObservedXid = initializedUptoXID;
     643          64 :     TransactionIdRetreat(latestObservedXid);
     644          64 : }
     645             : 
     646             : /*
     647             :  * ProcArrayApplyRecoveryInfo -- apply recovery info about xids
     648             :  *
     649             :  * Takes us through 3 states: Initialized, Pending and Ready.
     650             :  * Normal case is to go all the way to Ready straight away, though there
     651             :  * are atypical cases where we need to take it in steps.
     652             :  *
     653             :  * Use the data about running transactions on master to create the initial
     654             :  * state of KnownAssignedXids. We also use these records to regularly prune
     655             :  * KnownAssignedXids because we know it is possible that some transactions
     656             :  * with FATAL errors fail to write abort records, which could cause eventual
     657             :  * overflow.
     658             :  *
     659             :  * See comments for LogStandbySnapshot().
     660             :  */
     661             : void
     662         146 : ProcArrayApplyRecoveryInfo(RunningTransactions running)
     663             : {
     664             :     TransactionId *xids;
     665             :     int         nxids;
     666             :     int         i;
     667             : 
     668             :     Assert(standbyState >= STANDBY_INITIALIZED);
     669             :     Assert(TransactionIdIsValid(running->nextXid));
     670             :     Assert(TransactionIdIsValid(running->oldestRunningXid));
     671             :     Assert(TransactionIdIsNormal(running->latestCompletedXid));
     672             : 
     673             :     /*
     674             :      * Remove stale transactions, if any.
     675             :      */
     676         146 :     ExpireOldKnownAssignedTransactionIds(running->oldestRunningXid);
     677             : 
     678             :     /*
     679             :      * Remove stale locks, if any.
     680             :      */
     681         146 :     StandbyReleaseOldLocks(running->oldestRunningXid);
     682             : 
     683             :     /*
     684             :      * If our snapshot is already valid, nothing else to do...
     685             :      */
     686         146 :     if (standbyState == STANDBY_SNAPSHOT_READY)
     687          82 :         return;
     688             : 
     689             :     /*
     690             :      * If our initial RunningTransactionsData had an overflowed snapshot then
     691             :      * we knew we were missing some subxids from our snapshot. If we continue
     692             :      * to see overflowed snapshots then we might never be able to start up, so
     693             :      * we make another test to see if our snapshot is now valid. We know that
     694             :      * the missing subxids are equal to or earlier than nextXid. After we
     695             :      * initialise we continue to apply changes during recovery, so once the
     696             :      * oldestRunningXid is later than the nextXid from the initial snapshot we
     697             :      * know that we no longer have missing information and can mark the
     698             :      * snapshot as valid.
     699             :      */
     700          64 :     if (standbyState == STANDBY_SNAPSHOT_PENDING)
     701             :     {
     702             :         /*
     703             :          * If the snapshot isn't overflowed or if its empty we can reset our
     704             :          * pending state and use this snapshot instead.
     705             :          */
     706           0 :         if (!running->subxid_overflow || running->xcnt == 0)
     707             :         {
     708             :             /*
     709             :              * If we have already collected known assigned xids, we need to
     710             :              * throw them away before we apply the recovery snapshot.
     711             :              */
     712           0 :             KnownAssignedXidsReset();
     713           0 :             standbyState = STANDBY_INITIALIZED;
     714             :         }
     715             :         else
     716             :         {
     717           0 :             if (TransactionIdPrecedes(standbySnapshotPendingXmin,
     718             :                                       running->oldestRunningXid))
     719             :             {
     720           0 :                 standbyState = STANDBY_SNAPSHOT_READY;
     721           0 :                 elog(trace_recovery(DEBUG1),
     722             :                      "recovery snapshots are now enabled");
     723             :             }
     724             :             else
     725           0 :                 elog(trace_recovery(DEBUG1),
     726             :                      "recovery snapshot waiting for non-overflowed snapshot or "
     727             :                      "until oldest active xid on standby is at least %u (now %u)",
     728             :                      standbySnapshotPendingXmin,
     729             :                      running->oldestRunningXid);
     730           0 :             return;
     731             :         }
     732             :     }
     733             : 
     734             :     Assert(standbyState == STANDBY_INITIALIZED);
     735             : 
     736             :     /*
     737             :      * NB: this can be reached at least twice, so make sure new code can deal
     738             :      * with that.
     739             :      */
     740             : 
     741             :     /*
     742             :      * Nobody else is running yet, but take locks anyhow
     743             :      */
     744          64 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
     745             : 
     746             :     /*
     747             :      * KnownAssignedXids is sorted so we cannot just add the xids, we have to
     748             :      * sort them first.
     749             :      *
     750             :      * Some of the new xids are top-level xids and some are subtransactions.
     751             :      * We don't call SubTransSetParent because it doesn't matter yet. If we
     752             :      * aren't overflowed then all xids will fit in snapshot and so we don't
     753             :      * need subtrans. If we later overflow, an xid assignment record will add
     754             :      * xids to subtrans. If RunningTransactionsData is overflowed then we
     755             :      * don't have enough information to correctly update subtrans anyway.
     756             :      */
     757             : 
     758             :     /*
     759             :      * Allocate a temporary array to avoid modifying the array passed as
     760             :      * argument.
     761             :      */
     762          64 :     xids = palloc(sizeof(TransactionId) * (running->xcnt + running->subxcnt));
     763             : 
     764             :     /*
     765             :      * Add to the temp array any xids which have not already completed.
     766             :      */
     767          64 :     nxids = 0;
     768          68 :     for (i = 0; i < running->xcnt + running->subxcnt; i++)
     769             :     {
     770           4 :         TransactionId xid = running->xids[i];
     771             : 
     772             :         /*
     773             :          * The running-xacts snapshot can contain xids that were still visible
     774             :          * in the procarray when the snapshot was taken, but were already
     775             :          * WAL-logged as completed. They're not running anymore, so ignore
     776             :          * them.
     777             :          */
     778           4 :         if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
     779           0 :             continue;
     780             : 
     781           4 :         xids[nxids++] = xid;
     782             :     }
     783             : 
     784          64 :     if (nxids > 0)
     785             :     {
     786           4 :         if (procArray->numKnownAssignedXids != 0)
     787             :         {
     788           0 :             LWLockRelease(ProcArrayLock);
     789           0 :             elog(ERROR, "KnownAssignedXids is not empty");
     790             :         }
     791             : 
     792             :         /*
     793             :          * Sort the array so that we can add them safely into
     794             :          * KnownAssignedXids.
     795             :          */
     796           4 :         qsort(xids, nxids, sizeof(TransactionId), xidComparator);
     797             : 
     798             :         /*
     799             :          * Add the sorted snapshot into KnownAssignedXids.  The running-xacts
     800             :          * snapshot may include duplicated xids because of prepared
     801             :          * transactions, so ignore them.
     802             :          */
     803           8 :         for (i = 0; i < nxids; i++)
     804             :         {
     805           4 :             if (i > 0 && TransactionIdEquals(xids[i - 1], xids[i]))
     806             :             {
     807           0 :                 elog(DEBUG1,
     808             :                      "found duplicated transaction %u for KnownAssignedXids insertion",
     809             :                      xids[i]);
     810           0 :                 continue;
     811             :             }
     812           4 :             KnownAssignedXidsAdd(xids[i], xids[i], true);
     813             :         }
     814             : 
     815           4 :         KnownAssignedXidsDisplay(trace_recovery(DEBUG3));
     816             :     }
     817             : 
     818          64 :     pfree(xids);
     819             : 
     820             :     /*
     821             :      * latestObservedXid is at least set to the point where SUBTRANS was
     822             :      * started up to (cf. ProcArrayInitRecovery()) or to the biggest xid
     823             :      * RecordKnownAssignedTransactionIds() was called for.  Initialize
     824             :      * subtrans from thereon, up to nextXid - 1.
     825             :      *
     826             :      * We need to duplicate parts of RecordKnownAssignedTransactionId() here,
     827             :      * because we've just added xids to the known assigned xids machinery that
     828             :      * haven't gone through RecordKnownAssignedTransactionId().
     829             :      */
     830             :     Assert(TransactionIdIsNormal(latestObservedXid));
     831          64 :     TransactionIdAdvance(latestObservedXid);
     832         128 :     while (TransactionIdPrecedes(latestObservedXid, running->nextXid))
     833             :     {
     834           0 :         ExtendSUBTRANS(latestObservedXid);
     835           0 :         TransactionIdAdvance(latestObservedXid);
     836             :     }
     837          64 :     TransactionIdRetreat(latestObservedXid);    /* = running->nextXid - 1 */
     838             : 
     839             :     /* ----------
     840             :      * Now we've got the running xids we need to set the global values that
     841             :      * are used to track snapshots as they evolve further.
     842             :      *
     843             :      * - latestCompletedXid which will be the xmax for snapshots
     844             :      * - lastOverflowedXid which shows whether snapshots overflow
     845             :      * - nextXid
     846             :      *
     847             :      * If the snapshot overflowed, then we still initialise with what we know,
     848             :      * but the recovery snapshot isn't fully valid yet because we know there
     849             :      * are some subxids missing. We don't know the specific subxids that are
     850             :      * missing, so conservatively assume the last one is latestObservedXid.
     851             :      * ----------
     852             :      */
     853          64 :     if (running->subxid_overflow)
     854             :     {
     855           0 :         standbyState = STANDBY_SNAPSHOT_PENDING;
     856             : 
     857           0 :         standbySnapshotPendingXmin = latestObservedXid;
     858           0 :         procArray->lastOverflowedXid = latestObservedXid;
     859             :     }
     860             :     else
     861             :     {
     862          64 :         standbyState = STANDBY_SNAPSHOT_READY;
     863             : 
     864          64 :         standbySnapshotPendingXmin = InvalidTransactionId;
     865             :     }
     866             : 
     867             :     /*
     868             :      * If a transaction wrote a commit record in the gap between taking and
     869             :      * logging the snapshot then latestCompletedXid may already be higher than
     870             :      * the value from the snapshot, so check before we use the incoming value.
     871             :      */
     872          64 :     if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
     873             :                               running->latestCompletedXid))
     874          64 :         ShmemVariableCache->latestCompletedXid = running->latestCompletedXid;
     875             : 
     876             :     Assert(TransactionIdIsNormal(ShmemVariableCache->latestCompletedXid));
     877             : 
     878          64 :     LWLockRelease(ProcArrayLock);
     879             : 
     880             :     /* ShmemVariableCache->nextFullXid must be beyond any observed xid. */
     881          64 :     AdvanceNextFullTransactionIdPastXid(latestObservedXid);
     882             : 
     883             :     Assert(FullTransactionIdIsValid(ShmemVariableCache->nextFullXid));
     884             : 
     885          64 :     KnownAssignedXidsDisplay(trace_recovery(DEBUG3));
     886          64 :     if (standbyState == STANDBY_SNAPSHOT_READY)
     887          64 :         elog(trace_recovery(DEBUG1), "recovery snapshots are now enabled");
     888             :     else
     889           0 :         elog(trace_recovery(DEBUG1),
     890             :              "recovery snapshot waiting for non-overflowed snapshot or "
     891             :              "until oldest active xid on standby is at least %u (now %u)",
     892             :              standbySnapshotPendingXmin,
     893             :              running->oldestRunningXid);
     894             : }
     895             : 
     896             : /*
     897             :  * ProcArrayApplyXidAssignment
     898             :  *      Process an XLOG_XACT_ASSIGNMENT WAL record
     899             :  */
     900             : void
     901          10 : ProcArrayApplyXidAssignment(TransactionId topxid,
     902             :                             int nsubxids, TransactionId *subxids)
     903             : {
     904             :     TransactionId max_xid;
     905             :     int         i;
     906             : 
     907             :     Assert(standbyState >= STANDBY_INITIALIZED);
     908             : 
     909          10 :     max_xid = TransactionIdLatest(topxid, nsubxids, subxids);
     910             : 
     911             :     /*
     912             :      * Mark all the subtransactions as observed.
     913             :      *
     914             :      * NOTE: This will fail if the subxid contains too many previously
     915             :      * unobserved xids to fit into known-assigned-xids. That shouldn't happen
     916             :      * as the code stands, because xid-assignment records should never contain
     917             :      * more than PGPROC_MAX_CACHED_SUBXIDS entries.
     918             :      */
     919          10 :     RecordKnownAssignedTransactionIds(max_xid);
     920             : 
     921             :     /*
     922             :      * Notice that we update pg_subtrans with the top-level xid, rather than
     923             :      * the parent xid. This is a difference between normal processing and
     924             :      * recovery, yet is still correct in all cases. The reason is that
     925             :      * subtransaction commit is not marked in clog until commit processing, so
     926             :      * all aborted subtransactions have already been clearly marked in clog.
     927             :      * As a result we are able to refer directly to the top-level
     928             :      * transaction's state rather than skipping through all the intermediate
     929             :      * states in the subtransaction tree. This should be the first time we
     930             :      * have attempted to SubTransSetParent().
     931             :      */
     932         650 :     for (i = 0; i < nsubxids; i++)
     933         640 :         SubTransSetParent(subxids[i], topxid);
     934             : 
     935             :     /* KnownAssignedXids isn't maintained yet, so we're done for now */
     936          10 :     if (standbyState == STANDBY_INITIALIZED)
     937           0 :         return;
     938             : 
     939             :     /*
     940             :      * Uses same locking as transaction commit
     941             :      */
     942          10 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
     943             : 
     944             :     /*
     945             :      * Remove subxids from known-assigned-xacts.
     946             :      */
     947          10 :     KnownAssignedXidsRemoveTree(InvalidTransactionId, nsubxids, subxids);
     948             : 
     949             :     /*
     950             :      * Advance lastOverflowedXid to be at least the last of these subxids.
     951             :      */
     952          10 :     if (TransactionIdPrecedes(procArray->lastOverflowedXid, max_xid))
     953          10 :         procArray->lastOverflowedXid = max_xid;
     954             : 
     955          10 :     LWLockRelease(ProcArrayLock);
     956             : }
     957             : 
     958             : /*
     959             :  * TransactionIdIsInProgress -- is given transaction running in some backend
     960             :  *
     961             :  * Aside from some shortcuts such as checking RecentXmin and our own Xid,
     962             :  * there are four possibilities for finding a running transaction:
     963             :  *
     964             :  * 1. The given Xid is a main transaction Id.  We will find this out cheaply
     965             :  * by looking at the PGXACT struct for each backend.
     966             :  *
     967             :  * 2. The given Xid is one of the cached subxact Xids in the PGPROC array.
     968             :  * We can find this out cheaply too.
     969             :  *
     970             :  * 3. In Hot Standby mode, we must search the KnownAssignedXids list to see
     971             :  * if the Xid is running on the master.
     972             :  *
     973             :  * 4. Search the SubTrans tree to find the Xid's topmost parent, and then see
     974             :  * if that is running according to PGXACT or KnownAssignedXids.  This is the
     975             :  * slowest way, but sadly it has to be done always if the others failed,
     976             :  * unless we see that the cached subxact sets are complete (none have
     977             :  * overflowed).
     978             :  *
     979             :  * ProcArrayLock has to be held while we do 1, 2, 3.  If we save the top Xids
     980             :  * while doing 1 and 3, we can release the ProcArrayLock while we do 4.
     981             :  * This buys back some concurrency (and we can't retrieve the main Xids from
     982             :  * PGXACT again anyway; see GetNewTransactionId).
     983             :  */
     984             : bool
     985     9222826 : TransactionIdIsInProgress(TransactionId xid)
     986             : {
     987             :     static TransactionId *xids = NULL;
     988     9222826 :     int         nxids = 0;
     989     9222826 :     ProcArrayStruct *arrayP = procArray;
     990             :     TransactionId topxid;
     991             :     int         i,
     992             :                 j;
     993             : 
     994             :     /*
     995             :      * Don't bother checking a transaction older than RecentXmin; it could not
     996             :      * possibly still be running.  (Note: in particular, this guarantees that
     997             :      * we reject InvalidTransactionId, FrozenTransactionId, etc as not
     998             :      * running.)
     999             :      */
    1000     9222826 :     if (TransactionIdPrecedes(xid, RecentXmin))
    1001             :     {
    1002             :         xc_by_recent_xmin_inc();
    1003     8615514 :         return false;
    1004             :     }
    1005             : 
    1006             :     /*
    1007             :      * We may have just checked the status of this transaction, so if it is
    1008             :      * already known to be completed, we can fall out without any access to
    1009             :      * shared memory.
    1010             :      */
    1011      607312 :     if (TransactionIdIsKnownCompleted(xid))
    1012             :     {
    1013             :         xc_by_known_xact_inc();
    1014      568490 :         return false;
    1015             :     }
    1016             : 
    1017             :     /*
    1018             :      * Also, we can handle our own transaction (and subtransactions) without
    1019             :      * any access to shared memory.
    1020             :      */
    1021       38822 :     if (TransactionIdIsCurrentTransactionId(xid))
    1022             :     {
    1023             :         xc_by_my_xact_inc();
    1024       23416 :         return true;
    1025             :     }
    1026             : 
    1027             :     /*
    1028             :      * If first time through, get workspace to remember main XIDs in. We
    1029             :      * malloc it permanently to avoid repeated palloc/pfree overhead.
    1030             :      */
    1031       15406 :     if (xids == NULL)
    1032             :     {
    1033             :         /*
    1034             :          * In hot standby mode, reserve enough space to hold all xids in the
    1035             :          * known-assigned list. If we later finish recovery, we no longer need
    1036             :          * the bigger array, but we don't bother to shrink it.
    1037             :          */
    1038         696 :         int         maxxids = RecoveryInProgress() ? TOTAL_MAX_CACHED_SUBXIDS : arrayP->maxProcs;
    1039             : 
    1040         696 :         xids = (TransactionId *) malloc(maxxids * sizeof(TransactionId));
    1041         696 :         if (xids == NULL)
    1042           0 :             ereport(ERROR,
    1043             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    1044             :                      errmsg("out of memory")));
    1045             :     }
    1046             : 
    1047       15406 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    1048             : 
    1049             :     /*
    1050             :      * Now that we have the lock, we can check latestCompletedXid; if the
    1051             :      * target Xid is after that, it's surely still running.
    1052             :      */
    1053       15406 :     if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid, xid))
    1054             :     {
    1055        6250 :         LWLockRelease(ProcArrayLock);
    1056             :         xc_by_latest_xid_inc();
    1057        6250 :         return true;
    1058             :     }
    1059             : 
    1060             :     /* No shortcuts, gotta grovel through the array */
    1061       59710 :     for (i = 0; i < arrayP->numProcs; i++)
    1062             :     {
    1063       53060 :         int         pgprocno = arrayP->pgprocnos[i];
    1064       53060 :         PGPROC     *proc = &allProcs[pgprocno];
    1065       53060 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    1066             :         TransactionId pxid;
    1067             :         int         pxids;
    1068             : 
    1069             :         /* Ignore my own proc --- dealt with it above */
    1070       53060 :         if (proc == MyProc)
    1071        7482 :             continue;
    1072             : 
    1073             :         /* Fetch xid just once - see GetNewTransactionId */
    1074       45578 :         pxid = UINT32_ACCESS_ONCE(pgxact->xid);
    1075             : 
    1076       45578 :         if (!TransactionIdIsValid(pxid))
    1077       30820 :             continue;
    1078             : 
    1079             :         /*
    1080             :          * Step 1: check the main Xid
    1081             :          */
    1082       14758 :         if (TransactionIdEquals(pxid, xid))
    1083             :         {
    1084        1854 :             LWLockRelease(ProcArrayLock);
    1085             :             xc_by_main_xid_inc();
    1086        1854 :             return true;
    1087             :         }
    1088             : 
    1089             :         /*
    1090             :          * We can ignore main Xids that are younger than the target Xid, since
    1091             :          * the target could not possibly be their child.
    1092             :          */
    1093       12904 :         if (TransactionIdPrecedes(xid, pxid))
    1094        4242 :             continue;
    1095             : 
    1096             :         /*
    1097             :          * Step 2: check the cached child-Xids arrays
    1098             :          */
    1099        8662 :         pxids = pgxact->nxids;
    1100        8662 :         pg_read_barrier();      /* pairs with barrier in GetNewTransactionId() */
    1101       50232 :         for (j = pxids - 1; j >= 0; j--)
    1102             :         {
    1103             :             /* Fetch xid just once - see GetNewTransactionId */
    1104       42222 :             TransactionId cxid = UINT32_ACCESS_ONCE(proc->subxids.xids[j]);
    1105             : 
    1106       42222 :             if (TransactionIdEquals(cxid, xid))
    1107             :             {
    1108         652 :                 LWLockRelease(ProcArrayLock);
    1109             :                 xc_by_child_xid_inc();
    1110         652 :                 return true;
    1111             :             }
    1112             :         }
    1113             : 
    1114             :         /*
    1115             :          * Save the main Xid for step 4.  We only need to remember main Xids
    1116             :          * that have uncached children.  (Note: there is no race condition
    1117             :          * here because the overflowed flag cannot be cleared, only set, while
    1118             :          * we hold ProcArrayLock.  So we can't miss an Xid that we need to
    1119             :          * worry about.)
    1120             :          */
    1121        8010 :         if (pgxact->overflowed)
    1122         310 :             xids[nxids++] = pxid;
    1123             :     }
    1124             : 
    1125             :     /*
    1126             :      * Step 3: in hot standby mode, check the known-assigned-xids list.  XIDs
    1127             :      * in the list must be treated as running.
    1128             :      */
    1129        6650 :     if (RecoveryInProgress())
    1130             :     {
    1131             :         /* none of the PGXACT entries should have XIDs in hot standby mode */
    1132             :         Assert(nxids == 0);
    1133             : 
    1134           0 :         if (KnownAssignedXidExists(xid))
    1135             :         {
    1136           0 :             LWLockRelease(ProcArrayLock);
    1137             :             xc_by_known_assigned_inc();
    1138           0 :             return true;
    1139             :         }
    1140             : 
    1141             :         /*
    1142             :          * If the KnownAssignedXids overflowed, we have to check pg_subtrans
    1143             :          * too.  Fetch all xids from KnownAssignedXids that are lower than
    1144             :          * xid, since if xid is a subtransaction its parent will always have a
    1145             :          * lower value.  Note we will collect both main and subXIDs here, but
    1146             :          * there's no help for it.
    1147             :          */
    1148           0 :         if (TransactionIdPrecedesOrEquals(xid, procArray->lastOverflowedXid))
    1149           0 :             nxids = KnownAssignedXidsGet(xids, xid);
    1150             :     }
    1151             : 
    1152        6650 :     LWLockRelease(ProcArrayLock);
    1153             : 
    1154             :     /*
    1155             :      * If none of the relevant caches overflowed, we know the Xid is not
    1156             :      * running without even looking at pg_subtrans.
    1157             :      */
    1158        6650 :     if (nxids == 0)
    1159             :     {
    1160             :         xc_no_overflow_inc();
    1161        6340 :         return false;
    1162             :     }
    1163             : 
    1164             :     /*
    1165             :      * Step 4: have to check pg_subtrans.
    1166             :      *
    1167             :      * At this point, we know it's either a subtransaction of one of the Xids
    1168             :      * in xids[], or it's not running.  If it's an already-failed
    1169             :      * subtransaction, we want to say "not running" even though its parent may
    1170             :      * still be running.  So first, check pg_xact to see if it's been aborted.
    1171             :      */
    1172             :     xc_slow_answer_inc();
    1173             : 
    1174         310 :     if (TransactionIdDidAbort(xid))
    1175           0 :         return false;
    1176             : 
    1177             :     /*
    1178             :      * It isn't aborted, so check whether the transaction tree it belongs to
    1179             :      * is still running (or, more precisely, whether it was running when we
    1180             :      * held ProcArrayLock).
    1181             :      */
    1182         310 :     topxid = SubTransGetTopmostTransaction(xid);
    1183             :     Assert(TransactionIdIsValid(topxid));
    1184         310 :     if (!TransactionIdEquals(topxid, xid))
    1185             :     {
    1186         310 :         for (i = 0; i < nxids; i++)
    1187             :         {
    1188         310 :             if (TransactionIdEquals(xids[i], topxid))
    1189         310 :                 return true;
    1190             :         }
    1191             :     }
    1192             : 
    1193           0 :     return false;
    1194             : }
    1195             : 
    1196             : /*
    1197             :  * TransactionIdIsActive -- is xid the top-level XID of an active backend?
    1198             :  *
    1199             :  * This differs from TransactionIdIsInProgress in that it ignores prepared
    1200             :  * transactions, as well as transactions running on the master if we're in
    1201             :  * hot standby.  Also, we ignore subtransactions since that's not needed
    1202             :  * for current uses.
    1203             :  */
    1204             : bool
    1205           0 : TransactionIdIsActive(TransactionId xid)
    1206             : {
    1207           0 :     bool        result = false;
    1208           0 :     ProcArrayStruct *arrayP = procArray;
    1209             :     int         i;
    1210             : 
    1211             :     /*
    1212             :      * Don't bother checking a transaction older than RecentXmin; it could not
    1213             :      * possibly still be running.
    1214             :      */
    1215           0 :     if (TransactionIdPrecedes(xid, RecentXmin))
    1216           0 :         return false;
    1217             : 
    1218           0 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    1219             : 
    1220           0 :     for (i = 0; i < arrayP->numProcs; i++)
    1221             :     {
    1222           0 :         int         pgprocno = arrayP->pgprocnos[i];
    1223           0 :         PGPROC     *proc = &allProcs[pgprocno];
    1224           0 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    1225             :         TransactionId pxid;
    1226             : 
    1227             :         /* Fetch xid just once - see GetNewTransactionId */
    1228           0 :         pxid = UINT32_ACCESS_ONCE(pgxact->xid);
    1229             : 
    1230           0 :         if (!TransactionIdIsValid(pxid))
    1231           0 :             continue;
    1232             : 
    1233           0 :         if (proc->pid == 0)
    1234           0 :             continue;           /* ignore prepared transactions */
    1235             : 
    1236           0 :         if (TransactionIdEquals(pxid, xid))
    1237             :         {
    1238           0 :             result = true;
    1239           0 :             break;
    1240             :         }
    1241             :     }
    1242             : 
    1243           0 :     LWLockRelease(ProcArrayLock);
    1244             : 
    1245           0 :     return result;
    1246             : }
    1247             : 
    1248             : 
    1249             : /*
    1250             :  * GetOldestXmin -- returns oldest transaction that was running
    1251             :  *                  when any current transaction was started.
    1252             :  *
    1253             :  * If rel is NULL or a shared relation, all backends are considered, otherwise
    1254             :  * only backends running in this database are considered.
    1255             :  *
    1256             :  * The flags are used to ignore the backends in calculation when any of the
    1257             :  * corresponding flags is set. Typically, if you want to ignore ones with
    1258             :  * PROC_IN_VACUUM flag, you can use PROCARRAY_FLAGS_VACUUM.
    1259             :  *
    1260             :  * PROCARRAY_SLOTS_XMIN causes GetOldestXmin to ignore the xmin and
    1261             :  * catalog_xmin of any replication slots that exist in the system when
    1262             :  * calculating the oldest xmin.
    1263             :  *
    1264             :  * This is used by VACUUM to decide which deleted tuples must be preserved in
    1265             :  * the passed in table. For shared relations backends in all databases must be
    1266             :  * considered, but for non-shared relations that's not required, since only
    1267             :  * backends in my own database could ever see the tuples in them. Also, we can
    1268             :  * ignore concurrently running lazy VACUUMs because (a) they must be working
    1269             :  * on other tables, and (b) they don't need to do snapshot-based lookups.
    1270             :  *
    1271             :  * This is also used to determine where to truncate pg_subtrans.  For that
    1272             :  * backends in all databases have to be considered, so rel = NULL has to be
    1273             :  * passed in.
    1274             :  *
    1275             :  * Note: we include all currently running xids in the set of considered xids.
    1276             :  * This ensures that if a just-started xact has not yet set its snapshot,
    1277             :  * when it does set the snapshot it cannot set xmin less than what we compute.
    1278             :  * See notes in src/backend/access/transam/README.
    1279             :  *
    1280             :  * Note: despite the above, it's possible for the calculated value to move
    1281             :  * backwards on repeated calls. The calculated value is conservative, so that
    1282             :  * anything older is definitely not considered as running by anyone anymore,
    1283             :  * but the exact value calculated depends on a number of things. For example,
    1284             :  * if rel = NULL and there are no transactions running in the current
    1285             :  * database, GetOldestXmin() returns latestCompletedXid. If a transaction
    1286             :  * begins after that, its xmin will include in-progress transactions in other
    1287             :  * databases that started earlier, so another call will return a lower value.
    1288             :  * Nonetheless it is safe to vacuum a table in the current database with the
    1289             :  * first result.  There are also replication-related effects: a walsender
    1290             :  * process can set its xmin based on transactions that are no longer running
    1291             :  * in the master but are still being replayed on the standby, thus possibly
    1292             :  * making the GetOldestXmin reading go backwards.  In this case there is a
    1293             :  * possibility that we lose data that the standby would like to have, but
    1294             :  * unless the standby uses a replication slot to make its xmin persistent
    1295             :  * there is little we can do about that --- data is only protected if the
    1296             :  * walsender runs continuously while queries are executed on the standby.
    1297             :  * (The Hot Standby code deals with such cases by failing standby queries
    1298             :  * that needed to access already-removed data, so there's no integrity bug.)
    1299             :  * The return value is also adjusted with vacuum_defer_cleanup_age, so
    1300             :  * increasing that setting on the fly is another easy way to make
    1301             :  * GetOldestXmin() move backwards, with no consequences for data integrity.
    1302             :  */
    1303             : TransactionId
    1304      104434 : GetOldestXmin(Relation rel, int flags)
    1305             : {
    1306      104434 :     ProcArrayStruct *arrayP = procArray;
    1307             :     TransactionId result;
    1308             :     int         index;
    1309             :     bool        allDbs;
    1310             : 
    1311      104434 :     TransactionId replication_slot_xmin = InvalidTransactionId;
    1312      104434 :     TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
    1313             : 
    1314             :     /*
    1315             :      * If we're not computing a relation specific limit, or if a shared
    1316             :      * relation has been passed in, backends in all databases have to be
    1317             :      * considered.
    1318             :      */
    1319      104434 :     allDbs = rel == NULL || rel->rd_rel->relisshared;
    1320             : 
    1321             :     /* Cannot look for individual databases during recovery */
    1322             :     Assert(allDbs || !RecoveryInProgress());
    1323             : 
    1324      104434 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    1325             : 
    1326             :     /*
    1327             :      * We initialize the MIN() calculation with latestCompletedXid + 1. This
    1328             :      * is a lower bound for the XIDs that might appear in the ProcArray later,
    1329             :      * and so protects us against overestimating the result due to future
    1330             :      * additions.
    1331             :      */
    1332      104434 :     result = ShmemVariableCache->latestCompletedXid;
    1333             :     Assert(TransactionIdIsNormal(result));
    1334      104434 :     TransactionIdAdvance(result);
    1335             : 
    1336      332432 :     for (index = 0; index < arrayP->numProcs; index++)
    1337             :     {
    1338      227998 :         int         pgprocno = arrayP->pgprocnos[index];
    1339      227998 :         PGPROC     *proc = &allProcs[pgprocno];
    1340      227998 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    1341             : 
    1342      227998 :         if (pgxact->vacuumFlags & (flags & PROCARRAY_PROC_FLAGS_MASK))
    1343       41980 :             continue;
    1344             : 
    1345      348988 :         if (allDbs ||
    1346      228674 :             proc->databaseId == MyDatabaseId ||
    1347       65704 :             proc->databaseId == 0)   /* always include WalSender */
    1348             :         {
    1349             :             /* Fetch xid just once - see GetNewTransactionId */
    1350      185948 :             TransactionId xid = UINT32_ACCESS_ONCE(pgxact->xid);
    1351             : 
    1352             :             /* First consider the transaction's own Xid, if any */
    1353      223624 :             if (TransactionIdIsNormal(xid) &&
    1354       37676 :                 TransactionIdPrecedes(xid, result))
    1355        2372 :                 result = xid;
    1356             : 
    1357             :             /*
    1358             :              * Also consider the transaction's Xmin, if set.
    1359             :              *
    1360             :              * We must check both Xid and Xmin because a transaction might
    1361             :              * have an Xmin but not (yet) an Xid; conversely, if it has an
    1362             :              * Xid, that could determine some not-yet-set Xmin.
    1363             :              */
    1364      185948 :             xid = UINT32_ACCESS_ONCE(pgxact->xmin);
    1365      270418 :             if (TransactionIdIsNormal(xid) &&
    1366       84470 :                 TransactionIdPrecedes(xid, result))
    1367       13086 :                 result = xid;
    1368             :         }
    1369             :     }
    1370             : 
    1371             :     /*
    1372             :      * Fetch into local variable while ProcArrayLock is held - the
    1373             :      * LWLockRelease below is a barrier, ensuring this happens inside the
    1374             :      * lock.
    1375             :      */
    1376      104434 :     replication_slot_xmin = procArray->replication_slot_xmin;
    1377      104434 :     replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
    1378             : 
    1379      104434 :     if (RecoveryInProgress())
    1380             :     {
    1381             :         /*
    1382             :          * Check to see whether KnownAssignedXids contains an xid value older
    1383             :          * than the main procarray.
    1384             :          */
    1385          34 :         TransactionId kaxmin = KnownAssignedXidsGetOldestXmin();
    1386             : 
    1387          34 :         LWLockRelease(ProcArrayLock);
    1388             : 
    1389          40 :         if (TransactionIdIsNormal(kaxmin) &&
    1390           6 :             TransactionIdPrecedes(kaxmin, result))
    1391           2 :             result = kaxmin;
    1392             :     }
    1393             :     else
    1394             :     {
    1395             :         /*
    1396             :          * No other information needed, so release the lock immediately.
    1397             :          */
    1398      104400 :         LWLockRelease(ProcArrayLock);
    1399             : 
    1400             :         /*
    1401             :          * Compute the cutoff XID by subtracting vacuum_defer_cleanup_age,
    1402             :          * being careful not to generate a "permanent" XID.
    1403             :          *
    1404             :          * vacuum_defer_cleanup_age provides some additional "slop" for the
    1405             :          * benefit of hot standby queries on standby servers.  This is quick
    1406             :          * and dirty, and perhaps not all that useful unless the master has a
    1407             :          * predictable transaction rate, but it offers some protection when
    1408             :          * there's no walsender connection.  Note that we are assuming
    1409             :          * vacuum_defer_cleanup_age isn't large enough to cause wraparound ---
    1410             :          * so guc.c should limit it to no more than the xidStopLimit threshold
    1411             :          * in varsup.c.  Also note that we intentionally don't apply
    1412             :          * vacuum_defer_cleanup_age on standby servers.
    1413             :          */
    1414      104400 :         result -= vacuum_defer_cleanup_age;
    1415      104400 :         if (!TransactionIdIsNormal(result))
    1416           0 :             result = FirstNormalTransactionId;
    1417             :     }
    1418             : 
    1419             :     /*
    1420             :      * Check whether there are replication slots requiring an older xmin.
    1421             :      */
    1422      104434 :     if (!(flags & PROCARRAY_SLOTS_XMIN) &&
    1423         230 :         TransactionIdIsValid(replication_slot_xmin) &&
    1424         230 :         NormalTransactionIdPrecedes(replication_slot_xmin, result))
    1425         230 :         result = replication_slot_xmin;
    1426             : 
    1427             :     /*
    1428             :      * After locks have been released and vacuum_defer_cleanup_age has been
    1429             :      * applied, check whether we need to back up further to make logical
    1430             :      * decoding possible. We need to do so if we're computing the global limit
    1431             :      * (rel = NULL) or if the passed relation is a catalog relation of some
    1432             :      * kind.
    1433             :      */
    1434      104434 :     if (!(flags & PROCARRAY_SLOTS_XMIN) &&
    1435       97990 :         (rel == NULL ||
    1436       98548 :          RelationIsAccessibleInLogicalDecoding(rel)) &&
    1437         218 :         TransactionIdIsValid(replication_slot_catalog_xmin) &&
    1438         218 :         NormalTransactionIdPrecedes(replication_slot_catalog_xmin, result))
    1439         210 :         result = replication_slot_catalog_xmin;
    1440             : 
    1441      104434 :     return result;
    1442             : }
    1443             : 
    1444             : /*
    1445             :  * GetMaxSnapshotXidCount -- get max size for snapshot XID array
    1446             :  *
    1447             :  * We have to export this for use by snapmgr.c.
    1448             :  */
    1449             : int
    1450       20300 : GetMaxSnapshotXidCount(void)
    1451             : {
    1452       20300 :     return procArray->maxProcs;
    1453             : }
    1454             : 
    1455             : /*
    1456             :  * GetMaxSnapshotSubxidCount -- get max size for snapshot sub-XID array
    1457             :  *
    1458             :  * We have to export this for use by snapmgr.c.
    1459             :  */
    1460             : int
    1461       20236 : GetMaxSnapshotSubxidCount(void)
    1462             : {
    1463       20236 :     return TOTAL_MAX_CACHED_SUBXIDS;
    1464             : }
    1465             : 
    1466             : /*
    1467             :  * GetSnapshotData -- returns information about running transactions.
    1468             :  *
    1469             :  * The returned snapshot includes xmin (lowest still-running xact ID),
    1470             :  * xmax (highest completed xact ID + 1), and a list of running xact IDs
    1471             :  * in the range xmin <= xid < xmax.  It is used as follows:
    1472             :  *      All xact IDs < xmin are considered finished.
    1473             :  *      All xact IDs >= xmax are considered still running.
    1474             :  *      For an xact ID xmin <= xid < xmax, consult list to see whether
    1475             :  *      it is considered running or not.
    1476             :  * This ensures that the set of transactions seen as "running" by the
    1477             :  * current xact will not change after it takes the snapshot.
    1478             :  *
    1479             :  * All running top-level XIDs are included in the snapshot, except for lazy
    1480             :  * VACUUM processes.  We also try to include running subtransaction XIDs,
    1481             :  * but since PGPROC has only a limited cache area for subxact XIDs, full
    1482             :  * information may not be available.  If we find any overflowed subxid arrays,
    1483             :  * we have to mark the snapshot's subxid data as overflowed, and extra work
    1484             :  * *may* need to be done to determine what's running (see XidInMVCCSnapshot()
    1485             :  * in heapam_visibility.c).
    1486             :  *
    1487             :  * We also update the following backend-global variables:
    1488             :  *      TransactionXmin: the oldest xmin of any snapshot in use in the
    1489             :  *          current transaction (this is the same as MyPgXact->xmin).
    1490             :  *      RecentXmin: the xmin computed for the most recent snapshot.  XIDs
    1491             :  *          older than this are known not running any more.
    1492             :  *      RecentGlobalXmin: the global xmin (oldest TransactionXmin across all
    1493             :  *          running transactions, except those running LAZY VACUUM).  This is
    1494             :  *          the same computation done by
    1495             :  *          GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM).
    1496             :  *      RecentGlobalDataXmin: the global xmin for non-catalog tables
    1497             :  *          >= RecentGlobalXmin
    1498             :  *
    1499             :  * Note: this function should probably not be called with an argument that's
    1500             :  * not statically allocated (see xip allocation below).
    1501             :  */
    1502             : Snapshot
    1503     1919716 : GetSnapshotData(Snapshot snapshot)
    1504             : {
    1505     1919716 :     ProcArrayStruct *arrayP = procArray;
    1506             :     TransactionId xmin;
    1507             :     TransactionId xmax;
    1508             :     TransactionId globalxmin;
    1509             :     int         index;
    1510     1919716 :     int         count = 0;
    1511     1919716 :     int         subcount = 0;
    1512     1919716 :     bool        suboverflowed = false;
    1513     1919716 :     TransactionId replication_slot_xmin = InvalidTransactionId;
    1514     1919716 :     TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
    1515             : 
    1516             :     Assert(snapshot != NULL);
    1517             : 
    1518             :     /*
    1519             :      * Allocating space for maxProcs xids is usually overkill; numProcs would
    1520             :      * be sufficient.  But it seems better to do the malloc while not holding
    1521             :      * the lock, so we can't look at numProcs.  Likewise, we allocate much
    1522             :      * more subxip storage than is probably needed.
    1523             :      *
    1524             :      * This does open a possibility for avoiding repeated malloc/free: since
    1525             :      * maxProcs does not change at runtime, we can simply reuse the previous
    1526             :      * xip arrays if any.  (This relies on the fact that all callers pass
    1527             :      * static SnapshotData structs.)
    1528             :      */
    1529     1919716 :     if (snapshot->xip == NULL)
    1530             :     {
    1531             :         /*
    1532             :          * First call for this snapshot. Snapshot is same size whether or not
    1533             :          * we are in recovery, see later comments.
    1534             :          */
    1535       20212 :         snapshot->xip = (TransactionId *)
    1536       20212 :             malloc(GetMaxSnapshotXidCount() * sizeof(TransactionId));
    1537       20212 :         if (snapshot->xip == NULL)
    1538           0 :             ereport(ERROR,
    1539             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    1540             :                      errmsg("out of memory")));
    1541             :         Assert(snapshot->subxip == NULL);
    1542       20212 :         snapshot->subxip = (TransactionId *)
    1543       20212 :             malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
    1544       20212 :         if (snapshot->subxip == NULL)
    1545           0 :             ereport(ERROR,
    1546             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    1547             :                      errmsg("out of memory")));
    1548             :     }
    1549             : 
    1550             :     /*
    1551             :      * It is sufficient to get shared lock on ProcArrayLock, even if we are
    1552             :      * going to set MyPgXact->xmin.
    1553             :      */
    1554     1919716 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    1555             : 
    1556             :     /* xmax is always latestCompletedXid + 1 */
    1557     1919716 :     xmax = ShmemVariableCache->latestCompletedXid;
    1558             :     Assert(TransactionIdIsNormal(xmax));
    1559     1919716 :     TransactionIdAdvance(xmax);
    1560             : 
    1561             :     /* initialize xmin calculation with xmax */
    1562     1919716 :     globalxmin = xmin = xmax;
    1563             : 
    1564     1919716 :     snapshot->takenDuringRecovery = RecoveryInProgress();
    1565             : 
    1566     1919716 :     if (!snapshot->takenDuringRecovery)
    1567             :     {
    1568     1918536 :         int        *pgprocnos = arrayP->pgprocnos;
    1569             :         int         numProcs;
    1570             : 
    1571             :         /*
    1572             :          * Spin over procArray checking xid, xmin, and subxids.  The goal is
    1573             :          * to gather all active xids, find the lowest xmin, and try to record
    1574             :          * subxids.
    1575             :          */
    1576     1918536 :         numProcs = arrayP->numProcs;
    1577     9299632 :         for (index = 0; index < numProcs; index++)
    1578             :         {
    1579     7381096 :             int         pgprocno = pgprocnos[index];
    1580     7381096 :             PGXACT     *pgxact = &allPgXact[pgprocno];
    1581             :             TransactionId xid;
    1582             : 
    1583             :             /*
    1584             :              * Skip over backends doing logical decoding which manages xmin
    1585             :              * separately (check below) and ones running LAZY VACUUM.
    1586             :              */
    1587     7381096 :             if (pgxact->vacuumFlags &
    1588             :                 (PROC_IN_LOGICAL_DECODING | PROC_IN_VACUUM))
    1589       45452 :                 continue;
    1590             : 
    1591             :             /* Update globalxmin to be the smallest valid xmin */
    1592     7335644 :             xid = UINT32_ACCESS_ONCE(pgxact->xmin);
    1593    10171790 :             if (TransactionIdIsNormal(xid) &&
    1594     2836146 :                 NormalTransactionIdPrecedes(xid, globalxmin))
    1595      847630 :                 globalxmin = xid;
    1596             : 
    1597             :             /* Fetch xid just once - see GetNewTransactionId */
    1598     7335644 :             xid = UINT32_ACCESS_ONCE(pgxact->xid);
    1599             : 
    1600             :             /*
    1601             :              * If the transaction has no XID assigned, we can skip it; it
    1602             :              * won't have sub-XIDs either.  If the XID is >= xmax, we can also
    1603             :              * skip it; such transactions will be treated as running anyway
    1604             :              * (and any sub-XIDs will also be >= xmax).
    1605             :              */
    1606     7335644 :             if (!TransactionIdIsNormal(xid)
    1607     1591980 :                 || !NormalTransactionIdPrecedes(xid, xmax))
    1608     6708738 :                 continue;
    1609             : 
    1610             :             /*
    1611             :              * We don't include our own XIDs (if any) in the snapshot, but we
    1612             :              * must include them in xmin.
    1613             :              */
    1614      626906 :             if (NormalTransactionIdPrecedes(xid, xmin))
    1615      484592 :                 xmin = xid;
    1616      626906 :             if (pgxact == MyPgXact)
    1617       75438 :                 continue;
    1618             : 
    1619             :             /* Add XID to snapshot. */
    1620      551468 :             snapshot->xip[count++] = xid;
    1621             : 
    1622             :             /*
    1623             :              * Save subtransaction XIDs if possible (if we've already
    1624             :              * overflowed, there's no point).  Note that the subxact XIDs must
    1625             :              * be later than their parent, so no need to check them against
    1626             :              * xmin.  We could filter against xmax, but it seems better not to
    1627             :              * do that much work while holding the ProcArrayLock.
    1628             :              *
    1629             :              * The other backend can add more subxids concurrently, but cannot
    1630             :              * remove any.  Hence it's important to fetch nxids just once.
    1631             :              * Should be safe to use memcpy, though.  (We needn't worry about
    1632             :              * missing any xids added concurrently, because they must postdate
    1633             :              * xmax.)
    1634             :              *
    1635             :              * Again, our own XIDs are not included in the snapshot.
    1636             :              */
    1637      551468 :             if (!suboverflowed)
    1638             :             {
    1639      551468 :                 if (pgxact->overflowed)
    1640          58 :                     suboverflowed = true;
    1641             :                 else
    1642             :                 {
    1643      551410 :                     int         nxids = pgxact->nxids;
    1644             : 
    1645      551410 :                     if (nxids > 0)
    1646             :                     {
    1647        4034 :                         PGPROC     *proc = &allProcs[pgprocno];
    1648             : 
    1649        4034 :                         pg_read_barrier();  /* pairs with GetNewTransactionId */
    1650             : 
    1651        8068 :                         memcpy(snapshot->subxip + subcount,
    1652        4034 :                                (void *) proc->subxids.xids,
    1653             :                                nxids * sizeof(TransactionId));
    1654        4034 :                         subcount += nxids;
    1655             :                     }
    1656             :                 }
    1657             :             }
    1658             :         }
    1659             :     }
    1660             :     else
    1661             :     {
    1662             :         /*
    1663             :          * We're in hot standby, so get XIDs from KnownAssignedXids.
    1664             :          *
    1665             :          * We store all xids directly into subxip[]. Here's why:
    1666             :          *
    1667             :          * In recovery we don't know which xids are top-level and which are
    1668             :          * subxacts, a design choice that greatly simplifies xid processing.
    1669             :          *
    1670             :          * It seems like we would want to try to put xids into xip[] only, but
    1671             :          * that is fairly small. We would either need to make that bigger or
    1672             :          * to increase the rate at which we WAL-log xid assignment; neither is
    1673             :          * an appealing choice.
    1674             :          *
    1675             :          * We could try to store xids into xip[] first and then into subxip[]
    1676             :          * if there are too many xids. That only works if the snapshot doesn't
    1677             :          * overflow because we do not search subxip[] in that case. A simpler
    1678             :          * way is to just store all xids in the subxact array because this is
    1679             :          * by far the bigger array. We just leave the xip array empty.
    1680             :          *
    1681             :          * Either way we need to change the way XidInMVCCSnapshot() works
    1682             :          * depending upon when the snapshot was taken, or change normal
    1683             :          * snapshot processing so it matches.
    1684             :          *
    1685             :          * Note: It is possible for recovery to end before we finish taking
    1686             :          * the snapshot, and for newly assigned transaction ids to be added to
    1687             :          * the ProcArray.  xmax cannot change while we hold ProcArrayLock, so
    1688             :          * those newly added transaction ids would be filtered away, so we
    1689             :          * need not be concerned about them.
    1690             :          */
    1691        1180 :         subcount = KnownAssignedXidsGetAndSetXmin(snapshot->subxip, &xmin,
    1692             :                                                   xmax);
    1693             : 
    1694        1180 :         if (TransactionIdPrecedesOrEquals(xmin, procArray->lastOverflowedXid))
    1695          24 :             suboverflowed = true;
    1696             :     }
    1697             : 
    1698             : 
    1699             :     /*
    1700             :      * Fetch into local variable while ProcArrayLock is held - the
    1701             :      * LWLockRelease below is a barrier, ensuring this happens inside the
    1702             :      * lock.
    1703             :      */
    1704     1919716 :     replication_slot_xmin = procArray->replication_slot_xmin;
    1705     1919716 :     replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
    1706             : 
    1707     1919716 :     if (!TransactionIdIsValid(MyPgXact->xmin))
    1708      816146 :         MyPgXact->xmin = TransactionXmin = xmin;
    1709             : 
    1710     1919716 :     LWLockRelease(ProcArrayLock);
    1711             : 
    1712             :     /*
    1713             :      * Update globalxmin to include actual process xids.  This is a slightly
    1714             :      * different way of computing it than GetOldestXmin uses, but should give
    1715             :      * the same result.
    1716             :      */
    1717     1919716 :     if (TransactionIdPrecedes(xmin, globalxmin))
    1718        2222 :         globalxmin = xmin;
    1719             : 
    1720             :     /* Update global variables too */
    1721     1919716 :     RecentGlobalXmin = globalxmin - vacuum_defer_cleanup_age;
    1722     1919716 :     if (!TransactionIdIsNormal(RecentGlobalXmin))
    1723           0 :         RecentGlobalXmin = FirstNormalTransactionId;
    1724             : 
    1725             :     /* Check whether there's a replication slot requiring an older xmin. */
    1726     1922622 :     if (TransactionIdIsValid(replication_slot_xmin) &&
    1727        2906 :         NormalTransactionIdPrecedes(replication_slot_xmin, RecentGlobalXmin))
    1728        2706 :         RecentGlobalXmin = replication_slot_xmin;
    1729             : 
    1730             :     /* Non-catalog tables can be vacuumed if older than this xid */
    1731     1919716 :     RecentGlobalDataXmin = RecentGlobalXmin;
    1732             : 
    1733             :     /*
    1734             :      * Check whether there's a replication slot requiring an older catalog
    1735             :      * xmin.
    1736             :      */
    1737     1932658 :     if (TransactionIdIsNormal(replication_slot_catalog_xmin) &&
    1738       12942 :         NormalTransactionIdPrecedes(replication_slot_catalog_xmin, RecentGlobalXmin))
    1739        9926 :         RecentGlobalXmin = replication_slot_catalog_xmin;
    1740             : 
    1741     1919716 :     RecentXmin = xmin;
    1742             : 
    1743     1919716 :     snapshot->xmin = xmin;
    1744     1919716 :     snapshot->xmax = xmax;
    1745     1919716 :     snapshot->xcnt = count;
    1746     1919716 :     snapshot->subxcnt = subcount;
    1747     1919716 :     snapshot->suboverflowed = suboverflowed;
    1748             : 
    1749     1919716 :     snapshot->curcid = GetCurrentCommandId(false);
    1750             : 
    1751             :     /*
    1752             :      * This is a new snapshot, so set both refcounts are zero, and mark it as
    1753             :      * not copied in persistent memory.
    1754             :      */
    1755     1919716 :     snapshot->active_count = 0;
    1756     1919716 :     snapshot->regd_count = 0;
    1757     1919716 :     snapshot->copied = false;
    1758             : 
    1759     1919716 :     if (old_snapshot_threshold < 0)
    1760             :     {
    1761             :         /*
    1762             :          * If not using "snapshot too old" feature, fill related fields with
    1763             :          * dummy values that don't require any locking.
    1764             :          */
    1765     1896074 :         snapshot->lsn = InvalidXLogRecPtr;
    1766     1896074 :         snapshot->whenTaken = 0;
    1767             :     }
    1768             :     else
    1769             :     {
    1770             :         /*
    1771             :          * Capture the current time and WAL stream location in case this
    1772             :          * snapshot becomes old enough to need to fall back on the special
    1773             :          * "old snapshot" logic.
    1774             :          */
    1775       23642 :         snapshot->lsn = GetXLogInsertRecPtr();
    1776       23642 :         snapshot->whenTaken = GetSnapshotCurrentTimestamp();
    1777       23642 :         MaintainOldSnapshotTimeMapping(snapshot->whenTaken, xmin);
    1778             :     }
    1779             : 
    1780     1919716 :     return snapshot;
    1781             : }
    1782             : 
    1783             : /*
    1784             :  * ProcArrayInstallImportedXmin -- install imported xmin into MyPgXact->xmin
    1785             :  *
    1786             :  * This is called when installing a snapshot imported from another
    1787             :  * transaction.  To ensure that OldestXmin doesn't go backwards, we must
    1788             :  * check that the source transaction is still running, and we'd better do
    1789             :  * that atomically with installing the new xmin.
    1790             :  *
    1791             :  * Returns true if successful, false if source xact is no longer running.
    1792             :  */
    1793             : bool
    1794          16 : ProcArrayInstallImportedXmin(TransactionId xmin,
    1795             :                              VirtualTransactionId *sourcevxid)
    1796             : {
    1797          16 :     bool        result = false;
    1798          16 :     ProcArrayStruct *arrayP = procArray;
    1799             :     int         index;
    1800             : 
    1801             :     Assert(TransactionIdIsNormal(xmin));
    1802          16 :     if (!sourcevxid)
    1803           0 :         return false;
    1804             : 
    1805             :     /* Get lock so source xact can't end while we're doing this */
    1806          16 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    1807             : 
    1808          36 :     for (index = 0; index < arrayP->numProcs; index++)
    1809             :     {
    1810          36 :         int         pgprocno = arrayP->pgprocnos[index];
    1811          36 :         PGPROC     *proc = &allProcs[pgprocno];
    1812          36 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    1813             :         TransactionId xid;
    1814             : 
    1815             :         /* Ignore procs running LAZY VACUUM */
    1816          36 :         if (pgxact->vacuumFlags & PROC_IN_VACUUM)
    1817           0 :             continue;
    1818             : 
    1819             :         /* We are only interested in the specific virtual transaction. */
    1820          36 :         if (proc->backendId != sourcevxid->backendId)
    1821          20 :             continue;
    1822          16 :         if (proc->lxid != sourcevxid->localTransactionId)
    1823           0 :             continue;
    1824             : 
    1825             :         /*
    1826             :          * We check the transaction's database ID for paranoia's sake: if it's
    1827             :          * in another DB then its xmin does not cover us.  Caller should have
    1828             :          * detected this already, so we just treat any funny cases as
    1829             :          * "transaction not found".
    1830             :          */
    1831          16 :         if (proc->databaseId != MyDatabaseId)
    1832           0 :             continue;
    1833             : 
    1834             :         /*
    1835             :          * Likewise, let's just make real sure its xmin does cover us.
    1836             :          */
    1837          16 :         xid = UINT32_ACCESS_ONCE(pgxact->xmin);
    1838          32 :         if (!TransactionIdIsNormal(xid) ||
    1839          16 :             !TransactionIdPrecedesOrEquals(xid, xmin))
    1840           0 :             continue;
    1841             : 
    1842             :         /*
    1843             :          * We're good.  Install the new xmin.  As in GetSnapshotData, set
    1844             :          * TransactionXmin too.  (Note that because snapmgr.c called
    1845             :          * GetSnapshotData first, we'll be overwriting a valid xmin here, so
    1846             :          * we don't check that.)
    1847             :          */
    1848          16 :         MyPgXact->xmin = TransactionXmin = xmin;
    1849             : 
    1850          16 :         result = true;
    1851          16 :         break;
    1852             :     }
    1853             : 
    1854          16 :     LWLockRelease(ProcArrayLock);
    1855             : 
    1856          16 :     return result;
    1857             : }
    1858             : 
    1859             : /*
    1860             :  * ProcArrayInstallRestoredXmin -- install restored xmin into MyPgXact->xmin
    1861             :  *
    1862             :  * This is like ProcArrayInstallImportedXmin, but we have a pointer to the
    1863             :  * PGPROC of the transaction from which we imported the snapshot, rather than
    1864             :  * an XID.
    1865             :  *
    1866             :  * Returns true if successful, false if source xact is no longer running.
    1867             :  */
    1868             : bool
    1869        1688 : ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
    1870             : {
    1871        1688 :     bool        result = false;
    1872             :     TransactionId xid;
    1873             :     PGXACT     *pgxact;
    1874             : 
    1875             :     Assert(TransactionIdIsNormal(xmin));
    1876             :     Assert(proc != NULL);
    1877             : 
    1878             :     /* Get lock so source xact can't end while we're doing this */
    1879        1688 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    1880             : 
    1881        1688 :     pgxact = &allPgXact[proc->pgprocno];
    1882             : 
    1883             :     /*
    1884             :      * Be certain that the referenced PGPROC has an advertised xmin which is
    1885             :      * no later than the one we're installing, so that the system-wide xmin
    1886             :      * can't go backwards.  Also, make sure it's running in the same database,
    1887             :      * so that the per-database xmin cannot go backwards.
    1888             :      */
    1889        1688 :     xid = UINT32_ACCESS_ONCE(pgxact->xmin);
    1890        1688 :     if (proc->databaseId == MyDatabaseId &&
    1891        1688 :         TransactionIdIsNormal(xid) &&
    1892        1688 :         TransactionIdPrecedesOrEquals(xid, xmin))
    1893             :     {
    1894        1688 :         MyPgXact->xmin = TransactionXmin = xmin;
    1895        1688 :         result = true;
    1896             :     }
    1897             : 
    1898        1688 :     LWLockRelease(ProcArrayLock);
    1899             : 
    1900        1688 :     return result;
    1901             : }
    1902             : 
    1903             : /*
    1904             :  * GetRunningTransactionData -- returns information about running transactions.
    1905             :  *
    1906             :  * Similar to GetSnapshotData but returns more information. We include
    1907             :  * all PGXACTs with an assigned TransactionId, even VACUUM processes and
    1908             :  * prepared transactions.
    1909             :  *
    1910             :  * We acquire XidGenLock and ProcArrayLock, but the caller is responsible for
    1911             :  * releasing them. Acquiring XidGenLock ensures that no new XIDs enter the proc
    1912             :  * array until the caller has WAL-logged this snapshot, and releases the
    1913             :  * lock. Acquiring ProcArrayLock ensures that no transactions commit until the
    1914             :  * lock is released.
    1915             :  *
    1916             :  * The returned data structure is statically allocated; caller should not
    1917             :  * modify it, and must not assume it is valid past the next call.
    1918             :  *
    1919             :  * This is never executed during recovery so there is no need to look at
    1920             :  * KnownAssignedXids.
    1921             :  *
    1922             :  * Dummy PGXACTs from prepared transaction are included, meaning that this
    1923             :  * may return entries with duplicated TransactionId values coming from
    1924             :  * transaction finishing to prepare.  Nothing is done about duplicated
    1925             :  * entries here to not hold on ProcArrayLock more than necessary.
    1926             :  *
    1927             :  * We don't worry about updating other counters, we want to keep this as
    1928             :  * simple as possible and leave GetSnapshotData() as the primary code for
    1929             :  * that bookkeeping.
    1930             :  *
    1931             :  * Note that if any transaction has overflowed its cached subtransactions
    1932             :  * then there is no real need include any subtransactions.
    1933             :  */
    1934             : RunningTransactions
    1935        2070 : GetRunningTransactionData(void)
    1936             : {
    1937             :     /* result workspace */
    1938             :     static RunningTransactionsData CurrentRunningXactsData;
    1939             : 
    1940        2070 :     ProcArrayStruct *arrayP = procArray;
    1941        2070 :     RunningTransactions CurrentRunningXacts = &CurrentRunningXactsData;
    1942             :     TransactionId latestCompletedXid;
    1943             :     TransactionId oldestRunningXid;
    1944             :     TransactionId *xids;
    1945             :     int         index;
    1946             :     int         count;
    1947             :     int         subcount;
    1948             :     bool        suboverflowed;
    1949             : 
    1950             :     Assert(!RecoveryInProgress());
    1951             : 
    1952             :     /*
    1953             :      * Allocating space for maxProcs xids is usually overkill; numProcs would
    1954             :      * be sufficient.  But it seems better to do the malloc while not holding
    1955             :      * the lock, so we can't look at numProcs.  Likewise, we allocate much
    1956             :      * more subxip storage than is probably needed.
    1957             :      *
    1958             :      * Should only be allocated in bgwriter, since only ever executed during
    1959             :      * checkpoints.
    1960             :      */
    1961        2070 :     if (CurrentRunningXacts->xids == NULL)
    1962             :     {
    1963             :         /*
    1964             :          * First call
    1965             :          */
    1966         686 :         CurrentRunningXacts->xids = (TransactionId *)
    1967         686 :             malloc(TOTAL_MAX_CACHED_SUBXIDS * sizeof(TransactionId));
    1968         686 :         if (CurrentRunningXacts->xids == NULL)
    1969           0 :             ereport(ERROR,
    1970             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    1971             :                      errmsg("out of memory")));
    1972             :     }
    1973             : 
    1974        2070 :     xids = CurrentRunningXacts->xids;
    1975             : 
    1976        2070 :     count = subcount = 0;
    1977        2070 :     suboverflowed = false;
    1978             : 
    1979             :     /*
    1980             :      * Ensure that no xids enter or leave the procarray while we obtain
    1981             :      * snapshot.
    1982             :      */
    1983        2070 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    1984        2070 :     LWLockAcquire(XidGenLock, LW_SHARED);
    1985             : 
    1986        2070 :     latestCompletedXid = ShmemVariableCache->latestCompletedXid;
    1987             : 
    1988        2070 :     oldestRunningXid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
    1989             : 
    1990             :     /*
    1991             :      * Spin over procArray collecting all xids
    1992             :      */
    1993        6056 :     for (index = 0; index < arrayP->numProcs; index++)
    1994             :     {
    1995        3986 :         int         pgprocno = arrayP->pgprocnos[index];
    1996        3986 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    1997             :         TransactionId xid;
    1998             : 
    1999             :         /* Fetch xid just once - see GetNewTransactionId */
    2000        3986 :         xid = UINT32_ACCESS_ONCE(pgxact->xid);
    2001             : 
    2002             :         /*
    2003             :          * We don't need to store transactions that don't have a TransactionId
    2004             :          * yet because they will not show as running on a standby server.
    2005             :          */
    2006        3986 :         if (!TransactionIdIsValid(xid))
    2007        2276 :             continue;
    2008             : 
    2009             :         /*
    2010             :          * Be careful not to exclude any xids before calculating the values of
    2011             :          * oldestRunningXid and suboverflowed, since these are used to clean
    2012             :          * up transaction information held on standbys.
    2013             :          */
    2014        1710 :         if (TransactionIdPrecedes(xid, oldestRunningXid))
    2015        1702 :             oldestRunningXid = xid;
    2016             : 
    2017        1710 :         if (pgxact->overflowed)
    2018           4 :             suboverflowed = true;
    2019             : 
    2020             :         /*
    2021             :          * If we wished to exclude xids this would be the right place for it.
    2022             :          * Procs with the PROC_IN_VACUUM flag set don't usually assign xids,
    2023             :          * but they do during truncation at the end when they get the lock and
    2024             :          * truncate, so it is not much of a problem to include them if they
    2025             :          * are seen and it is cleaner to include them.
    2026             :          */
    2027             : 
    2028        1710 :         xids[count++] = xid;
    2029             :     }
    2030             : 
    2031             :     /*
    2032             :      * Spin over procArray collecting all subxids, but only if there hasn't
    2033             :      * been a suboverflow.
    2034             :      */
    2035        2070 :     if (!suboverflowed)
    2036             :     {
    2037        6044 :         for (index = 0; index < arrayP->numProcs; index++)
    2038             :         {
    2039        3978 :             int         pgprocno = arrayP->pgprocnos[index];
    2040        3978 :             PGPROC     *proc = &allProcs[pgprocno];
    2041        3978 :             PGXACT     *pgxact = &allPgXact[pgprocno];
    2042             :             int         nxids;
    2043             : 
    2044             :             /*
    2045             :              * Save subtransaction XIDs. Other backends can't add or remove
    2046             :              * entries while we're holding XidGenLock.
    2047             :              */
    2048        3978 :             nxids = pgxact->nxids;
    2049        3978 :             if (nxids > 0)
    2050             :             {
    2051             :                 /* barrier not really required, as XidGenLock is held, but ... */
    2052          10 :                 pg_read_barrier();  /* pairs with GetNewTransactionId */
    2053             : 
    2054          10 :                 memcpy(&xids[count], (void *) proc->subxids.xids,
    2055             :                        nxids * sizeof(TransactionId));
    2056          10 :                 count += nxids;
    2057          10 :                 subcount += nxids;
    2058             : 
    2059             :                 /*
    2060             :                  * Top-level XID of a transaction is always less than any of
    2061             :                  * its subxids, so we don't need to check if any of the
    2062             :                  * subxids are smaller than oldestRunningXid
    2063             :                  */
    2064             :             }
    2065             :         }
    2066             :     }
    2067             : 
    2068             :     /*
    2069             :      * It's important *not* to include the limits set by slots here because
    2070             :      * snapbuild.c uses oldestRunningXid to manage its xmin horizon. If those
    2071             :      * were to be included here the initial value could never increase because
    2072             :      * of a circular dependency where slots only increase their limits when
    2073             :      * running xacts increases oldestRunningXid and running xacts only
    2074             :      * increases if slots do.
    2075             :      */
    2076             : 
    2077        2070 :     CurrentRunningXacts->xcnt = count - subcount;
    2078        2070 :     CurrentRunningXacts->subxcnt = subcount;
    2079        2070 :     CurrentRunningXacts->subxid_overflow = suboverflowed;
    2080        2070 :     CurrentRunningXacts->nextXid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
    2081        2070 :     CurrentRunningXacts->oldestRunningXid = oldestRunningXid;
    2082        2070 :     CurrentRunningXacts->latestCompletedXid = latestCompletedXid;
    2083             : 
    2084             :     Assert(TransactionIdIsValid(CurrentRunningXacts->nextXid));
    2085             :     Assert(TransactionIdIsValid(CurrentRunningXacts->oldestRunningXid));
    2086             :     Assert(TransactionIdIsNormal(CurrentRunningXacts->latestCompletedXid));
    2087             : 
    2088             :     /* We don't release the locks here, the caller is responsible for that */
    2089             : 
    2090        2070 :     return CurrentRunningXacts;
    2091             : }
    2092             : 
    2093             : /*
    2094             :  * GetOldestActiveTransactionId()
    2095             :  *
    2096             :  * Similar to GetSnapshotData but returns just oldestActiveXid. We include
    2097             :  * all PGXACTs with an assigned TransactionId, even VACUUM processes.
    2098             :  * We look at all databases, though there is no need to include WALSender
    2099             :  * since this has no effect on hot standby conflicts.
    2100             :  *
    2101             :  * This is never executed during recovery so there is no need to look at
    2102             :  * KnownAssignedXids.
    2103             :  *
    2104             :  * We don't worry about updating other counters, we want to keep this as
    2105             :  * simple as possible and leave GetSnapshotData() as the primary code for
    2106             :  * that bookkeeping.
    2107             :  */
    2108             : TransactionId
    2109        1768 : GetOldestActiveTransactionId(void)
    2110             : {
    2111        1768 :     ProcArrayStruct *arrayP = procArray;
    2112             :     TransactionId oldestRunningXid;
    2113             :     int         index;
    2114             : 
    2115             :     Assert(!RecoveryInProgress());
    2116             : 
    2117             :     /*
    2118             :      * Read nextXid, as the upper bound of what's still active.
    2119             :      *
    2120             :      * Reading a TransactionId is atomic, but we must grab the lock to make
    2121             :      * sure that all XIDs < nextXid are already present in the proc array (or
    2122             :      * have already completed), when we spin over it.
    2123             :      */
    2124        1768 :     LWLockAcquire(XidGenLock, LW_SHARED);
    2125        1768 :     oldestRunningXid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
    2126        1768 :     LWLockRelease(XidGenLock);
    2127             : 
    2128             :     /*
    2129             :      * Spin over procArray collecting all xids and subxids.
    2130             :      */
    2131        1768 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2132        4488 :     for (index = 0; index < arrayP->numProcs; index++)
    2133             :     {
    2134        2720 :         int         pgprocno = arrayP->pgprocnos[index];
    2135        2720 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    2136             :         TransactionId xid;
    2137             : 
    2138             :         /* Fetch xid just once - see GetNewTransactionId */
    2139        2720 :         xid = UINT32_ACCESS_ONCE(pgxact->xid);
    2140             : 
    2141        2720 :         if (!TransactionIdIsNormal(xid))
    2142        1060 :             continue;
    2143             : 
    2144        1660 :         if (TransactionIdPrecedes(xid, oldestRunningXid))
    2145        1650 :             oldestRunningXid = xid;
    2146             : 
    2147             :         /*
    2148             :          * Top-level XID of a transaction is always less than any of its
    2149             :          * subxids, so we don't need to check if any of the subxids are
    2150             :          * smaller than oldestRunningXid
    2151             :          */
    2152             :     }
    2153        1768 :     LWLockRelease(ProcArrayLock);
    2154             : 
    2155        1768 :     return oldestRunningXid;
    2156             : }
    2157             : 
    2158             : /*
    2159             :  * GetOldestSafeDecodingTransactionId -- lowest xid not affected by vacuum
    2160             :  *
    2161             :  * Returns the oldest xid that we can guarantee not to have been affected by
    2162             :  * vacuum, i.e. no rows >= that xid have been vacuumed away unless the
    2163             :  * transaction aborted. Note that the value can (and most of the time will) be
    2164             :  * much more conservative than what really has been affected by vacuum, but we
    2165             :  * currently don't have better data available.
    2166             :  *
    2167             :  * This is useful to initialize the cutoff xid after which a new changeset
    2168             :  * extraction replication slot can start decoding changes.
    2169             :  *
    2170             :  * Must be called with ProcArrayLock held either shared or exclusively,
    2171             :  * although most callers will want to use exclusive mode since it is expected
    2172             :  * that the caller will immediately use the xid to peg the xmin horizon.
    2173             :  */
    2174             : TransactionId
    2175         252 : GetOldestSafeDecodingTransactionId(bool catalogOnly)
    2176             : {
    2177         252 :     ProcArrayStruct *arrayP = procArray;
    2178             :     TransactionId oldestSafeXid;
    2179             :     int         index;
    2180         252 :     bool        recovery_in_progress = RecoveryInProgress();
    2181             : 
    2182             :     Assert(LWLockHeldByMe(ProcArrayLock));
    2183             : 
    2184             :     /*
    2185             :      * Acquire XidGenLock, so no transactions can acquire an xid while we're
    2186             :      * running. If no transaction with xid were running concurrently a new xid
    2187             :      * could influence the RecentXmin et al.
    2188             :      *
    2189             :      * We initialize the computation to nextXid since that's guaranteed to be
    2190             :      * a safe, albeit pessimal, value.
    2191             :      */
    2192         252 :     LWLockAcquire(XidGenLock, LW_SHARED);
    2193         252 :     oldestSafeXid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
    2194             : 
    2195             :     /*
    2196             :      * If there's already a slot pegging the xmin horizon, we can start with
    2197             :      * that value, it's guaranteed to be safe since it's computed by this
    2198             :      * routine initially and has been enforced since.  We can always use the
    2199             :      * slot's general xmin horizon, but the catalog horizon is only usable
    2200             :      * when only catalog data is going to be looked at.
    2201             :      */
    2202         254 :     if (TransactionIdIsValid(procArray->replication_slot_xmin) &&
    2203           2 :         TransactionIdPrecedes(procArray->replication_slot_xmin,
    2204             :                               oldestSafeXid))
    2205           0 :         oldestSafeXid = procArray->replication_slot_xmin;
    2206             : 
    2207         432 :     if (catalogOnly &&
    2208         214 :         TransactionIdIsValid(procArray->replication_slot_catalog_xmin) &&
    2209          34 :         TransactionIdPrecedes(procArray->replication_slot_catalog_xmin,
    2210             :                               oldestSafeXid))
    2211           8 :         oldestSafeXid = procArray->replication_slot_catalog_xmin;
    2212             : 
    2213             :     /*
    2214             :      * If we're not in recovery, we walk over the procarray and collect the
    2215             :      * lowest xid. Since we're called with ProcArrayLock held and have
    2216             :      * acquired XidGenLock, no entries can vanish concurrently, since
    2217             :      * PGXACT->xid is only set with XidGenLock held and only cleared with
    2218             :      * ProcArrayLock held.
    2219             :      *
    2220             :      * In recovery we can't lower the safe value besides what we've computed
    2221             :      * above, so we'll have to wait a bit longer there. We unfortunately can
    2222             :      * *not* use KnownAssignedXidsGetOldestXmin() since the KnownAssignedXids
    2223             :      * machinery can miss values and return an older value than is safe.
    2224             :      */
    2225         252 :     if (!recovery_in_progress)
    2226             :     {
    2227             :         /*
    2228             :          * Spin over procArray collecting all min(PGXACT->xid)
    2229             :          */
    2230        1238 :         for (index = 0; index < arrayP->numProcs; index++)
    2231             :         {
    2232         986 :             int         pgprocno = arrayP->pgprocnos[index];
    2233         986 :             PGXACT     *pgxact = &allPgXact[pgprocno];
    2234             :             TransactionId xid;
    2235             : 
    2236             :             /* Fetch xid just once - see GetNewTransactionId */
    2237         986 :             xid = UINT32_ACCESS_ONCE(pgxact->xid);
    2238             : 
    2239         986 :             if (!TransactionIdIsNormal(xid))
    2240         982 :                 continue;
    2241             : 
    2242           4 :             if (TransactionIdPrecedes(xid, oldestSafeXid))
    2243           4 :                 oldestSafeXid = xid;
    2244             :         }
    2245             :     }
    2246             : 
    2247         252 :     LWLockRelease(XidGenLock);
    2248             : 
    2249         252 :     return oldestSafeXid;
    2250             : }
    2251             : 
    2252             : /*
    2253             :  * GetVirtualXIDsDelayingChkpt -- Get the VXIDs of transactions that are
    2254             :  * delaying checkpoint because they have critical actions in progress.
    2255             :  *
    2256             :  * Constructs an array of VXIDs of transactions that are currently in commit
    2257             :  * critical sections, as shown by having delayChkpt set in their PGXACT.
    2258             :  *
    2259             :  * Returns a palloc'd array that should be freed by the caller.
    2260             :  * *nvxids is the number of valid entries.
    2261             :  *
    2262             :  * Note that because backends set or clear delayChkpt without holding any lock,
    2263             :  * the result is somewhat indeterminate, but we don't really care.  Even in
    2264             :  * a multiprocessor with delayed writes to shared memory, it should be certain
    2265             :  * that setting of delayChkpt will propagate to shared memory when the backend
    2266             :  * takes a lock, so we cannot fail to see a virtual xact as delayChkpt if
    2267             :  * it's already inserted its commit record.  Whether it takes a little while
    2268             :  * for clearing of delayChkpt to propagate is unimportant for correctness.
    2269             :  */
    2270             : VirtualTransactionId *
    2271        2826 : GetVirtualXIDsDelayingChkpt(int *nvxids)
    2272             : {
    2273             :     VirtualTransactionId *vxids;
    2274        2826 :     ProcArrayStruct *arrayP = procArray;
    2275        2826 :     int         count = 0;
    2276             :     int         index;
    2277             : 
    2278             :     /* allocate what's certainly enough result space */
    2279        2826 :     vxids = (VirtualTransactionId *)
    2280        2826 :         palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
    2281             : 
    2282        2826 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2283             : 
    2284        6340 :     for (index = 0; index < arrayP->numProcs; index++)
    2285             :     {
    2286        3514 :         int         pgprocno = arrayP->pgprocnos[index];
    2287        3514 :         PGPROC     *proc = &allProcs[pgprocno];
    2288        3514 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    2289             : 
    2290        3514 :         if (pgxact->delayChkpt)
    2291             :         {
    2292             :             VirtualTransactionId vxid;
    2293             : 
    2294           2 :             GET_VXID_FROM_PGPROC(vxid, *proc);
    2295           2 :             if (VirtualTransactionIdIsValid(vxid))
    2296           2 :                 vxids[count++] = vxid;
    2297             :         }
    2298             :     }
    2299             : 
    2300        2826 :     LWLockRelease(ProcArrayLock);
    2301             : 
    2302        2826 :     *nvxids = count;
    2303        2826 :     return vxids;
    2304             : }
    2305             : 
    2306             : /*
    2307             :  * HaveVirtualXIDsDelayingChkpt -- Are any of the specified VXIDs delaying?
    2308             :  *
    2309             :  * This is used with the results of GetVirtualXIDsDelayingChkpt to see if any
    2310             :  * of the specified VXIDs are still in critical sections of code.
    2311             :  *
    2312             :  * Note: this is O(N^2) in the number of vxacts that are/were delaying, but
    2313             :  * those numbers should be small enough for it not to be a problem.
    2314             :  */
    2315             : bool
    2316           2 : HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids)
    2317             : {
    2318           2 :     bool        result = false;
    2319           2 :     ProcArrayStruct *arrayP = procArray;
    2320             :     int         index;
    2321             : 
    2322           2 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2323             : 
    2324          20 :     for (index = 0; index < arrayP->numProcs; index++)
    2325             :     {
    2326           8 :         int         pgprocno = arrayP->pgprocnos[index];
    2327           8 :         PGPROC     *proc = &allProcs[pgprocno];
    2328           8 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    2329             :         VirtualTransactionId vxid;
    2330             : 
    2331           8 :         GET_VXID_FROM_PGPROC(vxid, *proc);
    2332             : 
    2333           8 :         if (pgxact->delayChkpt && VirtualTransactionIdIsValid(vxid))
    2334             :         {
    2335             :             int         i;
    2336             : 
    2337           0 :             for (i = 0; i < nvxids; i++)
    2338             :             {
    2339           0 :                 if (VirtualTransactionIdEquals(vxid, vxids[i]))
    2340             :                 {
    2341           0 :                     result = true;
    2342           0 :                     break;
    2343             :                 }
    2344             :             }
    2345           0 :             if (result)
    2346           0 :                 break;
    2347             :         }
    2348             :     }
    2349             : 
    2350           2 :     LWLockRelease(ProcArrayLock);
    2351             : 
    2352           2 :     return result;
    2353             : }
    2354             : 
    2355             : /*
    2356             :  * BackendPidGetProc -- get a backend's PGPROC given its PID
    2357             :  *
    2358             :  * Returns NULL if not found.  Note that it is up to the caller to be
    2359             :  * sure that the question remains meaningful for long enough for the
    2360             :  * answer to be used ...
    2361             :  */
    2362             : PGPROC *
    2363        1598 : BackendPidGetProc(int pid)
    2364             : {
    2365             :     PGPROC     *result;
    2366             : 
    2367        1598 :     if (pid == 0)               /* never match dummy PGPROCs */
    2368           0 :         return NULL;
    2369             : 
    2370        1598 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2371             : 
    2372        1598 :     result = BackendPidGetProcWithLock(pid);
    2373             : 
    2374        1598 :     LWLockRelease(ProcArrayLock);
    2375             : 
    2376        1598 :     return result;
    2377             : }
    2378             : 
    2379             : /*
    2380             :  * BackendPidGetProcWithLock -- get a backend's PGPROC given its PID
    2381             :  *
    2382             :  * Same as above, except caller must be holding ProcArrayLock.  The found
    2383             :  * entry, if any, can be assumed to be valid as long as the lock remains held.
    2384             :  */
    2385             : PGPROC *
    2386       16886 : BackendPidGetProcWithLock(int pid)
    2387             : {
    2388       16886 :     PGPROC     *result = NULL;
    2389       16886 :     ProcArrayStruct *arrayP = procArray;
    2390             :     int         index;
    2391             : 
    2392       16886 :     if (pid == 0)               /* never match dummy PGPROCs */
    2393           0 :         return NULL;
    2394             : 
    2395       41626 :     for (index = 0; index < arrayP->numProcs; index++)
    2396             :     {
    2397       40976 :         PGPROC     *proc = &allProcs[arrayP->pgprocnos[index]];
    2398             : 
    2399       40976 :         if (proc->pid == pid)
    2400             :         {
    2401       16236 :             result = proc;
    2402       16236 :             break;
    2403             :         }
    2404             :     }
    2405             : 
    2406       16886 :     return result;
    2407             : }
    2408             : 
    2409             : /*
    2410             :  * BackendXidGetPid -- get a backend's pid given its XID
    2411             :  *
    2412             :  * Returns 0 if not found or it's a prepared transaction.  Note that
    2413             :  * it is up to the caller to be sure that the question remains
    2414             :  * meaningful for long enough for the answer to be used ...
    2415             :  *
    2416             :  * Only main transaction Ids are considered.  This function is mainly
    2417             :  * useful for determining what backend owns a lock.
    2418             :  *
    2419             :  * Beware that not every xact has an XID assigned.  However, as long as you
    2420             :  * only call this using an XID found on disk, you're safe.
    2421             :  */
    2422             : int
    2423           0 : BackendXidGetPid(TransactionId xid)
    2424             : {
    2425           0 :     int         result = 0;
    2426           0 :     ProcArrayStruct *arrayP = procArray;
    2427             :     int         index;
    2428             : 
    2429           0 :     if (xid == InvalidTransactionId)    /* never match invalid xid */
    2430           0 :         return 0;
    2431             : 
    2432           0 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2433             : 
    2434           0 :     for (index = 0; index < arrayP->numProcs; index++)
    2435             :     {
    2436           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2437           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2438           0 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    2439             : 
    2440           0 :         if (pgxact->xid == xid)
    2441             :         {
    2442           0 :             result = proc->pid;
    2443           0 :             break;
    2444             :         }
    2445             :     }
    2446             : 
    2447           0 :     LWLockRelease(ProcArrayLock);
    2448             : 
    2449           0 :     return result;
    2450             : }
    2451             : 
    2452             : /*
    2453             :  * IsBackendPid -- is a given pid a running backend
    2454             :  *
    2455             :  * This is not called by the backend, but is called by external modules.
    2456             :  */
    2457             : bool
    2458           4 : IsBackendPid(int pid)
    2459             : {
    2460           4 :     return (BackendPidGetProc(pid) != NULL);
    2461             : }
    2462             : 
    2463             : 
    2464             : /*
    2465             :  * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
    2466             :  *
    2467             :  * The array is palloc'd. The number of valid entries is returned into *nvxids.
    2468             :  *
    2469             :  * The arguments allow filtering the set of VXIDs returned.  Our own process
    2470             :  * is always skipped.  In addition:
    2471             :  *  If limitXmin is not InvalidTransactionId, skip processes with
    2472             :  *      xmin > limitXmin.
    2473             :  *  If excludeXmin0 is true, skip processes with xmin = 0.
    2474             :  *  If allDbs is false, skip processes attached to other databases.
    2475             :  *  If excludeVacuum isn't zero, skip processes for which
    2476             :  *      (vacuumFlags & excludeVacuum) is not zero.
    2477             :  *
    2478             :  * Note: the purpose of the limitXmin and excludeXmin0 parameters is to
    2479             :  * allow skipping backends whose oldest live snapshot is no older than
    2480             :  * some snapshot we have.  Since we examine the procarray with only shared
    2481             :  * lock, there are race conditions: a backend could set its xmin just after
    2482             :  * we look.  Indeed, on multiprocessors with weak memory ordering, the
    2483             :  * other backend could have set its xmin *before* we look.  We know however
    2484             :  * that such a backend must have held shared ProcArrayLock overlapping our
    2485             :  * own hold of ProcArrayLock, else we would see its xmin update.  Therefore,
    2486             :  * any snapshot the other backend is taking concurrently with our scan cannot
    2487             :  * consider any transactions as still running that we think are committed
    2488             :  * (since backends must hold ProcArrayLock exclusive to commit).
    2489             :  */
    2490             : VirtualTransactionId *
    2491         176 : GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
    2492             :                       bool allDbs, int excludeVacuum,
    2493             :                       int *nvxids)
    2494             : {
    2495             :     VirtualTransactionId *vxids;
    2496         176 :     ProcArrayStruct *arrayP = procArray;
    2497         176 :     int         count = 0;
    2498             :     int         index;
    2499             : 
    2500             :     /* allocate what's certainly enough result space */
    2501         176 :     vxids = (VirtualTransactionId *)
    2502         176 :         palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
    2503             : 
    2504         176 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2505             : 
    2506         886 :     for (index = 0; index < arrayP->numProcs; index++)
    2507             :     {
    2508         710 :         int         pgprocno = arrayP->pgprocnos[index];
    2509         710 :         PGPROC     *proc = &allProcs[pgprocno];
    2510         710 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    2511             : 
    2512         710 :         if (proc == MyProc)
    2513         176 :             continue;
    2514             : 
    2515         534 :         if (excludeVacuum & pgxact->vacuumFlags)
    2516           0 :             continue;
    2517             : 
    2518         534 :         if (allDbs || proc->databaseId == MyDatabaseId)
    2519             :         {
    2520             :             /* Fetch xmin just once - might change on us */
    2521         182 :             TransactionId pxmin = UINT32_ACCESS_ONCE(pgxact->xmin);
    2522             : 
    2523         182 :             if (excludeXmin0 && !TransactionIdIsValid(pxmin))
    2524         116 :                 continue;
    2525             : 
    2526             :             /*
    2527             :              * InvalidTransactionId precedes all other XIDs, so a proc that
    2528             :              * hasn't set xmin yet will not be rejected by this test.
    2529             :              */
    2530         132 :             if (!TransactionIdIsValid(limitXmin) ||
    2531          66 :                 TransactionIdPrecedesOrEquals(pxmin, limitXmin))
    2532             :             {
    2533             :                 VirtualTransactionId vxid;
    2534             : 
    2535          66 :                 GET_VXID_FROM_PGPROC(vxid, *proc);
    2536          66 :                 if (VirtualTransactionIdIsValid(vxid))
    2537          66 :                     vxids[count++] = vxid;
    2538             :             }
    2539             :         }
    2540             :     }
    2541             : 
    2542         176 :     LWLockRelease(ProcArrayLock);
    2543             : 
    2544         176 :     *nvxids = count;
    2545         176 :     return vxids;
    2546             : }
    2547             : 
    2548             : /*
    2549             :  * GetConflictingVirtualXIDs -- returns an array of currently active VXIDs.
    2550             :  *
    2551             :  * Usage is limited to conflict resolution during recovery on standby servers.
    2552             :  * limitXmin is supplied as either latestRemovedXid, or InvalidTransactionId
    2553             :  * in cases where we cannot accurately determine a value for latestRemovedXid.
    2554             :  *
    2555             :  * If limitXmin is InvalidTransactionId then we want to kill everybody,
    2556             :  * so we're not worried if they have a snapshot or not, nor does it really
    2557             :  * matter what type of lock we hold.
    2558             :  *
    2559             :  * All callers that are checking xmins always now supply a valid and useful
    2560             :  * value for limitXmin. The limitXmin is always lower than the lowest
    2561             :  * numbered KnownAssignedXid that is not already a FATAL error. This is
    2562             :  * because we only care about cleanup records that are cleaning up tuple
    2563             :  * versions from committed transactions. In that case they will only occur
    2564             :  * at the point where the record is less than the lowest running xid. That
    2565             :  * allows us to say that if any backend takes a snapshot concurrently with
    2566             :  * us then the conflict assessment made here would never include the snapshot
    2567             :  * that is being derived. So we take LW_SHARED on the ProcArray and allow
    2568             :  * concurrent snapshots when limitXmin is valid. We might think about adding
    2569             :  *   Assert(limitXmin < lowest(KnownAssignedXids))
    2570             :  * but that would not be true in the case of FATAL errors lagging in array,
    2571             :  * but we already know those are bogus anyway, so we skip that test.
    2572             :  *
    2573             :  * If dbOid is valid we skip backends attached to other databases.
    2574             :  *
    2575             :  * Be careful to *not* pfree the result from this function. We reuse
    2576             :  * this array sufficiently often that we use malloc for the result.
    2577             :  */
    2578             : VirtualTransactionId *
    2579        1200 : GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
    2580             : {
    2581             :     static VirtualTransactionId *vxids;
    2582        1200 :     ProcArrayStruct *arrayP = procArray;
    2583        1200 :     int         count = 0;
    2584             :     int         index;
    2585             : 
    2586             :     /*
    2587             :      * If first time through, get workspace to remember main XIDs in. We
    2588             :      * malloc it permanently to avoid repeated palloc/pfree overhead. Allow
    2589             :      * result space, remembering room for a terminator.
    2590             :      */
    2591        1200 :     if (vxids == NULL)
    2592             :     {
    2593          10 :         vxids = (VirtualTransactionId *)
    2594          10 :             malloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
    2595          10 :         if (vxids == NULL)
    2596           0 :             ereport(ERROR,
    2597             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    2598             :                      errmsg("out of memory")));
    2599             :     }
    2600             : 
    2601        1200 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2602             : 
    2603        1200 :     for (index = 0; index < arrayP->numProcs; index++)
    2604             :     {
    2605           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2606           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2607           0 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    2608             : 
    2609             :         /* Exclude prepared transactions */
    2610           0 :         if (proc->pid == 0)
    2611           0 :             continue;
    2612             : 
    2613           0 :         if (!OidIsValid(dbOid) ||
    2614           0 :             proc->databaseId == dbOid)
    2615             :         {
    2616             :             /* Fetch xmin just once - can't change on us, but good coding */
    2617           0 :             TransactionId pxmin = UINT32_ACCESS_ONCE(pgxact->xmin);
    2618             : 
    2619             :             /*
    2620             :              * We ignore an invalid pxmin because this means that backend has
    2621             :              * no snapshot currently. We hold a Share lock to avoid contention
    2622             :              * with users taking snapshots.  That is not a problem because the
    2623             :              * current xmin is always at least one higher than the latest
    2624             :              * removed xid, so any new snapshot would never conflict with the
    2625             :              * test here.
    2626             :              */
    2627           0 :             if (!TransactionIdIsValid(limitXmin) ||
    2628           0 :                 (TransactionIdIsValid(pxmin) && !TransactionIdFollows(pxmin, limitXmin)))
    2629             :             {
    2630             :                 VirtualTransactionId vxid;
    2631             : 
    2632           0 :                 GET_VXID_FROM_PGPROC(vxid, *proc);
    2633           0 :                 if (VirtualTransactionIdIsValid(vxid))
    2634           0 :                     vxids[count++] = vxid;
    2635             :             }
    2636             :         }
    2637             :     }
    2638             : 
    2639        1200 :     LWLockRelease(ProcArrayLock);
    2640             : 
    2641             :     /* add the terminator */
    2642        1200 :     vxids[count].backendId = InvalidBackendId;
    2643        1200 :     vxids[count].localTransactionId = InvalidLocalTransactionId;
    2644             : 
    2645        1200 :     return vxids;
    2646             : }
    2647             : 
    2648             : /*
    2649             :  * CancelVirtualTransaction - used in recovery conflict processing
    2650             :  *
    2651             :  * Returns pid of the process signaled, or 0 if not found.
    2652             :  */
    2653             : pid_t
    2654           0 : CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
    2655             : {
    2656           0 :     ProcArrayStruct *arrayP = procArray;
    2657             :     int         index;
    2658           0 :     pid_t       pid = 0;
    2659             : 
    2660           0 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2661             : 
    2662           0 :     for (index = 0; index < arrayP->numProcs; index++)
    2663             :     {
    2664           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2665           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2666             :         VirtualTransactionId procvxid;
    2667             : 
    2668           0 :         GET_VXID_FROM_PGPROC(procvxid, *proc);
    2669             : 
    2670           0 :         if (procvxid.backendId == vxid.backendId &&
    2671           0 :             procvxid.localTransactionId == vxid.localTransactionId)
    2672             :         {
    2673           0 :             proc->recoveryConflictPending = true;
    2674           0 :             pid = proc->pid;
    2675           0 :             if (pid != 0)
    2676             :             {
    2677             :                 /*
    2678             :                  * Kill the pid if it's still here. If not, that's what we
    2679             :                  * wanted so ignore any errors.
    2680             :                  */
    2681           0 :                 (void) SendProcSignal(pid, sigmode, vxid.backendId);
    2682             :             }
    2683           0 :             break;
    2684             :         }
    2685             :     }
    2686             : 
    2687           0 :     LWLockRelease(ProcArrayLock);
    2688             : 
    2689           0 :     return pid;
    2690             : }
    2691             : 
    2692             : /*
    2693             :  * MinimumActiveBackends --- count backends (other than myself) that are
    2694             :  *      in active transactions.  Return true if the count exceeds the
    2695             :  *      minimum threshold passed.  This is used as a heuristic to decide if
    2696             :  *      a pre-XLOG-flush delay is worthwhile during commit.
    2697             :  *
    2698             :  * Do not count backends that are blocked waiting for locks, since they are
    2699             :  * not going to get to run until someone else commits.
    2700             :  */
    2701             : bool
    2702           0 : MinimumActiveBackends(int min)
    2703             : {
    2704           0 :     ProcArrayStruct *arrayP = procArray;
    2705           0 :     int         count = 0;
    2706             :     int         index;
    2707             : 
    2708             :     /* Quick short-circuit if no minimum is specified */
    2709           0 :     if (min == 0)
    2710           0 :         return true;
    2711             : 
    2712             :     /*
    2713             :      * Note: for speed, we don't acquire ProcArrayLock.  This is a little bit
    2714             :      * bogus, but since we are only testing fields for zero or nonzero, it
    2715             :      * should be OK.  The result is only used for heuristic purposes anyway...
    2716             :      */
    2717           0 :     for (index = 0; index < arrayP->numProcs; index++)
    2718             :     {
    2719           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2720           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2721           0 :         PGXACT     *pgxact = &allPgXact[pgprocno];
    2722             : 
    2723             :         /*
    2724             :          * Since we're not holding a lock, need to be prepared to deal with
    2725             :          * garbage, as someone could have incremented numProcs but not yet
    2726             :          * filled the structure.
    2727             :          *
    2728             :          * If someone just decremented numProcs, 'proc' could also point to a
    2729             :          * PGPROC entry that's no longer in the array. It still points to a
    2730             :          * PGPROC struct, though, because freed PGPROC entries just go to the
    2731             :          * free list and are recycled. Its contents are nonsense in that case,
    2732             :          * but that's acceptable for this function.
    2733             :          */
    2734           0 :         if (pgprocno == -1)
    2735           0 :             continue;           /* do not count deleted entries */
    2736           0 :         if (proc == MyProc)
    2737           0 :             continue;           /* do not count myself */
    2738           0 :         if (pgxact->xid == InvalidTransactionId)
    2739           0 :             continue;           /* do not count if no XID assigned */
    2740           0 :         if (proc->pid == 0)
    2741           0 :             continue;           /* do not count prepared xacts */
    2742           0 :         if (proc->waitLock != NULL)
    2743           0 :             continue;           /* do not count if blocked on a lock */
    2744           0 :         count++;
    2745           0 :         if (count >= min)
    2746           0 :             break;
    2747             :     }
    2748             : 
    2749           0 :     return count >= min;
    2750             : }
    2751             : 
    2752             : /*
    2753             :  * CountDBBackends --- count backends that are using specified database
    2754             :  */
    2755             : int
    2756           2 : CountDBBackends(Oid databaseid)
    2757             : {
    2758           2 :     ProcArrayStruct *arrayP = procArray;
    2759           2 :     int         count = 0;
    2760             :     int         index;
    2761             : 
    2762           2 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2763             : 
    2764           2 :     for (index = 0; index < arrayP->numProcs; index++)
    2765             :     {
    2766           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2767           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2768             : 
    2769           0 :         if (proc->pid == 0)
    2770           0 :             continue;           /* do not count prepared xacts */
    2771           0 :         if (!OidIsValid(databaseid) ||
    2772           0 :             proc->databaseId == databaseid)
    2773           0 :             count++;
    2774             :     }
    2775             : 
    2776           2 :     LWLockRelease(ProcArrayLock);
    2777             : 
    2778           2 :     return count;
    2779             : }
    2780             : 
    2781             : /*
    2782             :  * CountDBConnections --- counts database backends ignoring any background
    2783             :  *      worker processes
    2784             :  */
    2785             : int
    2786           0 : CountDBConnections(Oid databaseid)
    2787             : {
    2788           0 :     ProcArrayStruct *arrayP = procArray;
    2789           0 :     int         count = 0;
    2790             :     int         index;
    2791             : 
    2792           0 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2793             : 
    2794           0 :     for (index = 0; index < arrayP->numProcs; index++)
    2795             :     {
    2796           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2797           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2798             : 
    2799           0 :         if (proc->pid == 0)
    2800           0 :             continue;           /* do not count prepared xacts */
    2801           0 :         if (proc->isBackgroundWorker)
    2802           0 :             continue;           /* do not count background workers */
    2803           0 :         if (!OidIsValid(databaseid) ||
    2804           0 :             proc->databaseId == databaseid)
    2805           0 :             count++;
    2806             :     }
    2807             : 
    2808           0 :     LWLockRelease(ProcArrayLock);
    2809             : 
    2810           0 :     return count;
    2811             : }
    2812             : 
    2813             : /*
    2814             :  * CancelDBBackends --- cancel backends that are using specified database
    2815             :  */
    2816             : void
    2817           0 : CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
    2818             : {
    2819           0 :     ProcArrayStruct *arrayP = procArray;
    2820             :     int         index;
    2821           0 :     pid_t       pid = 0;
    2822             : 
    2823             :     /* tell all backends to die */
    2824           0 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    2825             : 
    2826           0 :     for (index = 0; index < arrayP->numProcs; index++)
    2827             :     {
    2828           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2829           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2830             : 
    2831           0 :         if (databaseid == InvalidOid || proc->databaseId == databaseid)
    2832             :         {
    2833             :             VirtualTransactionId procvxid;
    2834             : 
    2835           0 :             GET_VXID_FROM_PGPROC(procvxid, *proc);
    2836             : 
    2837           0 :             proc->recoveryConflictPending = conflictPending;
    2838           0 :             pid = proc->pid;
    2839           0 :             if (pid != 0)
    2840             :             {
    2841             :                 /*
    2842             :                  * Kill the pid if it's still here. If not, that's what we
    2843             :                  * wanted so ignore any errors.
    2844             :                  */
    2845           0 :                 (void) SendProcSignal(pid, sigmode, procvxid.backendId);
    2846             :             }
    2847             :         }
    2848             :     }
    2849             : 
    2850           0 :     LWLockRelease(ProcArrayLock);
    2851           0 : }
    2852             : 
    2853             : /*
    2854             :  * CountUserBackends --- count backends that are used by specified user
    2855             :  */
    2856             : int
    2857           0 : CountUserBackends(Oid roleid)
    2858             : {
    2859           0 :     ProcArrayStruct *arrayP = procArray;
    2860           0 :     int         count = 0;
    2861             :     int         index;
    2862             : 
    2863           0 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    2864             : 
    2865           0 :     for (index = 0; index < arrayP->numProcs; index++)
    2866             :     {
    2867           0 :         int         pgprocno = arrayP->pgprocnos[index];
    2868           0 :         PGPROC     *proc = &allProcs[pgprocno];
    2869             : 
    2870           0 :         if (proc->pid == 0)
    2871           0 :             continue;           /* do not count prepared xacts */
    2872           0 :         if (proc->isBackgroundWorker)
    2873           0 :             continue;           /* do not count background workers */
    2874           0 :         if (proc->roleId == roleid)
    2875           0 :             count++;
    2876             :     }
    2877             : 
    2878           0 :     LWLockRelease(ProcArrayLock);
    2879             : 
    2880           0 :     return count;
    2881             : }
    2882             : 
    2883             : /*
    2884             :  * CountOtherDBBackends -- check for other backends running in the given DB
    2885             :  *
    2886             :  * If there are other backends in the DB, we will wait a maximum of 5 seconds
    2887             :  * for them to exit.  Autovacuum backends are encouraged to exit early by
    2888             :  * sending them SIGTERM, but normal user backends are just waited for.
    2889             :  *
    2890             :  * The current backend is always ignored; it is caller's responsibility to
    2891             :  * check whether the current backend uses the given DB, if it's important.
    2892             :  *
    2893             :  * Returns true if there are (still) other backends in the DB, false if not.
    2894             :  * Also, *nbackends and *nprepared are set to the number of other backends
    2895             :  * and prepared transactions in the DB, respectively.
    2896             :  *
    2897             :  * This function is used to interlock DROP DATABASE and related commands
    2898             :  * against there being any active backends in the target DB --- dropping the
    2899             :  * DB while active backends remain would be a Bad Thing.  Note that we cannot
    2900             :  * detect here the possibility of a newly-started backend that is trying to
    2901             :  * connect to the doomed database, so additional interlocking is needed during
    2902             :  * backend startup.  The caller should normally hold an exclusive lock on the
    2903             :  * target DB before calling this, which is one reason we mustn't wait
    2904             :  * indefinitely.
    2905             :  */
    2906             : bool
    2907         892 : CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
    2908             : {
    2909         892 :     ProcArrayStruct *arrayP = procArray;
    2910             : 
    2911             : #define MAXAUTOVACPIDS  10      /* max autovacs to SIGTERM per iteration */
    2912             :     int         autovac_pids[MAXAUTOVACPIDS];
    2913             :     int         tries;
    2914             : 
    2915             :     /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
    2916         892 :     for (tries = 0; tries < 50; tries++)
    2917             :     {
    2918         892 :         int         nautovacs = 0;
    2919         892 :         bool        found = false;
    2920             :         int         index;
    2921             : 
    2922         892 :         CHECK_FOR_INTERRUPTS();
    2923             : 
    2924         892 :         *nbackends = *nprepared = 0;
    2925             : 
    2926         892 :         LWLockAcquire(ProcArrayLock, LW_SHARED);
    2927             : 
    2928        2276 :         for (index = 0; index < arrayP->numProcs; index++)
    2929             :         {
    2930        1384 :             int         pgprocno = arrayP->pgprocnos[index];
    2931        1384 :             PGPROC     *proc = &allProcs[pgprocno];
    2932        1384 :             PGXACT     *pgxact = &allPgXact[pgprocno];
    2933             : 
    2934        1384 :             if (proc->databaseId != databaseId)
    2935         744 :                 continue;
    2936         640 :             if (proc == MyProc)
    2937         640 :                 continue;
    2938             : 
    2939           0 :             found = true;
    2940             : 
    2941           0 :             if (proc->pid == 0)
    2942           0 :                 (*nprepared)++;
    2943             :             else
    2944             :             {
    2945           0 :                 (*nbackends)++;
    2946           0 :                 if ((pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
    2947             :                     nautovacs < MAXAUTOVACPIDS)
    2948           0 :                     autovac_pids[nautovacs++] = proc->pid;
    2949             :             }
    2950             :         }
    2951             : 
    2952         892 :         LWLockRelease(ProcArrayLock);
    2953             : 
    2954         892 :         if (!found)
    2955         892 :             return false;       /* no conflicting backends, so done */
    2956             : 
    2957             :         /*
    2958             :          * Send SIGTERM to any conflicting autovacuums before sleeping. We
    2959             :          * postpone this step until after the loop because we don't want to
    2960             :          * hold ProcArrayLock while issuing kill(). We have no idea what might
    2961             :          * block kill() inside the kernel...
    2962             :          */
    2963           0 :         for (index = 0; index < nautovacs; index++)
    2964           0 :             (void) kill(autovac_pids[index], SIGTERM);  /* ignore any error */
    2965             : 
    2966             :         /* sleep, then try again */
    2967           0 :         pg_usleep(100 * 1000L); /* 100ms */
    2968             :     }
    2969             : 
    2970           0 :     return true;                /* timed out, still conflicts */
    2971             : }
    2972             : 
    2973             : /*
    2974             :  * ProcArraySetReplicationSlotXmin
    2975             :  *
    2976             :  * Install limits to future computations of the xmin horizon to prevent vacuum
    2977             :  * and HOT pruning from removing affected rows still needed by clients with
    2978             :  * replication slots.
    2979             :  */
    2980             : void
    2981        1848 : ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin,
    2982             :                                 bool already_locked)
    2983             : {
    2984             :     Assert(!already_locked || LWLockHeldByMe(ProcArrayLock));
    2985             : 
    2986        1848 :     if (!already_locked)
    2987        1596 :         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    2988             : 
    2989        1848 :     procArray->replication_slot_xmin = xmin;
    2990        1848 :     procArray->replication_slot_catalog_xmin = catalog_xmin;
    2991             : 
    2992        1848 :     if (!already_locked)
    2993        1596 :         LWLockRelease(ProcArrayLock);
    2994        1848 : }
    2995             : 
    2996             : /*
    2997             :  * ProcArrayGetReplicationSlotXmin
    2998             :  *
    2999             :  * Return the current slot xmin limits. That's useful to be able to remove
    3000             :  * data that's older than those limits.
    3001             :  */
    3002             : void
    3003          58 : ProcArrayGetReplicationSlotXmin(TransactionId *xmin,
    3004             :                                 TransactionId *catalog_xmin)
    3005             : {
    3006          58 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    3007             : 
    3008          58 :     if (xmin != NULL)
    3009          18 :         *xmin = procArray->replication_slot_xmin;
    3010             : 
    3011          58 :     if (catalog_xmin != NULL)
    3012          58 :         *catalog_xmin = procArray->replication_slot_catalog_xmin;
    3013             : 
    3014          58 :     LWLockRelease(ProcArrayLock);
    3015          58 : }
    3016             : 
    3017             : 
    3018             : #define XidCacheRemove(i) \
    3019             :     do { \
    3020             :         MyProc->subxids.xids[i] = MyProc->subxids.xids[MyPgXact->nxids - 1]; \
    3021             :         pg_write_barrier(); \
    3022             :         MyPgXact->nxids--; \
    3023             :     } while (0)
    3024             : 
    3025             : /*
    3026             :  * XidCacheRemoveRunningXids
    3027             :  *
    3028             :  * Remove a bunch of TransactionIds from the list of known-running
    3029             :  * subtransactions for my backend.  Both the specified xid and those in
    3030             :  * the xids[] array (of length nxids) are removed from the subxids cache.
    3031             :  * latestXid must be the latest XID among the group.
    3032             :  */
    3033             : void
    3034         358 : XidCacheRemoveRunningXids(TransactionId xid,
    3035             :                           int nxids, const TransactionId *xids,
    3036             :                           TransactionId latestXid)
    3037             : {
    3038             :     int         i,
    3039             :                 j;
    3040             : 
    3041             :     Assert(TransactionIdIsValid(xid));
    3042             : 
    3043             :     /*
    3044             :      * We must hold ProcArrayLock exclusively in order to remove transactions
    3045             :      * from the PGPROC array.  (See src/backend/access/transam/README.)  It's
    3046             :      * possible this could be relaxed since we know this routine is only used
    3047             :      * to abort subtransactions, but pending closer analysis we'd best be
    3048             :      * conservative.
    3049             :      *
    3050             :      * Note that we do not have to be careful about memory ordering of our own
    3051             :      * reads wrt. GetNewTransactionId() here - only this process can modify
    3052             :      * relevant fields of MyProc/MyPgXact.  But we do have to be careful about
    3053             :      * our own writes being well ordered.
    3054             :      */
    3055         358 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    3056             : 
    3057             :     /*
    3058             :      * Under normal circumstances xid and xids[] will be in increasing order,
    3059             :      * as will be the entries in subxids.  Scan backwards to avoid O(N^2)
    3060             :      * behavior when removing a lot of xids.
    3061             :      */
    3062         416 :     for (i = nxids - 1; i >= 0; i--)
    3063             :     {
    3064          58 :         TransactionId anxid = xids[i];
    3065             : 
    3066          58 :         for (j = MyPgXact->nxids - 1; j >= 0; j--)
    3067             :         {
    3068          58 :             if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
    3069             :             {
    3070          58 :                 XidCacheRemove(j);
    3071          58 :                 break;
    3072             :             }
    3073             :         }
    3074             : 
    3075             :         /*
    3076             :          * Ordinarily we should have found it, unless the cache has
    3077             :          * overflowed. However it's also possible for this routine to be
    3078             :          * invoked multiple times for the same subtransaction, in case of an
    3079             :          * error during AbortSubTransaction.  So instead of Assert, emit a
    3080             :          * debug warning.
    3081             :          */
    3082          58 :         if (j < 0 && !MyPgXact->overflowed)
    3083           0 :             elog(WARNING, "did not find subXID %u in MyProc", anxid);
    3084             :     }
    3085             : 
    3086         358 :     for (j = MyPgXact->nxids - 1; j >= 0; j--)
    3087             :     {
    3088         358 :         if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
    3089             :         {
    3090         358 :             XidCacheRemove(j);
    3091         358 :             break;
    3092             :         }
    3093             :     }
    3094             :     /* Ordinarily we should have found it, unless the cache has overflowed */
    3095         358 :     if (j < 0 && !MyPgXact->overflowed)
    3096           0 :         elog(WARNING, "did not find subXID %u in MyProc", xid);
    3097             : 
    3098             :     /* Also advance global latestCompletedXid while holding the lock */
    3099         358 :     if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
    3100             :                               latestXid))
    3101         234 :         ShmemVariableCache->latestCompletedXid = latestXid;
    3102             : 
    3103         358 :     LWLockRelease(ProcArrayLock);
    3104         358 : }
    3105             : 
    3106             : #ifdef XIDCACHE_DEBUG
    3107             : 
    3108             : /*
    3109             :  * Print stats about effectiveness of XID cache
    3110             :  */
    3111             : static void
    3112             : DisplayXidCache(void)
    3113             : {
    3114             :     fprintf(stderr,
    3115             :             "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, knownassigned: %ld, nooflo: %ld, slow: %ld\n",
    3116             :             xc_by_recent_xmin,
    3117             :             xc_by_known_xact,
    3118             :             xc_by_my_xact,
    3119             :             xc_by_latest_xid,
    3120             :             xc_by_main_xid,
    3121             :             xc_by_child_xid,
    3122             :             xc_by_known_assigned,
    3123             :             xc_no_overflow,
    3124             :             xc_slow_answer);
    3125             : }
    3126             : #endif                          /* XIDCACHE_DEBUG */
    3127             : 
    3128             : 
    3129             : /* ----------------------------------------------
    3130             :  *      KnownAssignedTransactionIds sub-module
    3131             :  * ----------------------------------------------
    3132             :  */
    3133             : 
    3134             : /*
    3135             :  * In Hot Standby mode, we maintain a list of transactions that are (or were)
    3136             :  * running in the master at the current point in WAL.  These XIDs must be
    3137             :  * treated as running by standby transactions, even though they are not in
    3138             :  * the standby server's PGXACT array.
    3139             :  *
    3140             :  * We record all XIDs that we know have been assigned.  That includes all the
    3141             :  * XIDs seen in WAL records, plus all unobserved XIDs that we can deduce have
    3142             :  * been assigned.  We can deduce the existence of unobserved XIDs because we
    3143             :  * know XIDs are assigned in sequence, with no gaps.  The KnownAssignedXids
    3144             :  * list expands as new XIDs are observed or inferred, and contracts when
    3145             :  * transaction completion records arrive.
    3146             :  *
    3147             :  * During hot standby we do not fret too much about the distinction between
    3148             :  * top-level XIDs and subtransaction XIDs. We store both together in the
    3149             :  * KnownAssignedXids list.  In backends, this is copied into snapshots in
    3150             :  * GetSnapshotData(), taking advantage of the fact that XidInMVCCSnapshot()
    3151             :  * doesn't care about the distinction either.  Subtransaction XIDs are
    3152             :  * effectively treated as top-level XIDs and in the typical case pg_subtrans
    3153             :  * links are *not* maintained (which does not affect visibility).
    3154             :  *
    3155             :  * We have room in KnownAssignedXids and in snapshots to hold maxProcs *
    3156             :  * (1 + PGPROC_MAX_CACHED_SUBXIDS) XIDs, so every master transaction must
    3157             :  * report its subtransaction XIDs in a WAL XLOG_XACT_ASSIGNMENT record at
    3158             :  * least every PGPROC_MAX_CACHED_SUBXIDS.  When we receive one of these
    3159             :  * records, we mark the subXIDs as children of the top XID in pg_subtrans,
    3160             :  * and then remove them from KnownAssignedXids.  This prevents overflow of
    3161             :  * KnownAssignedXids and snapshots, at the cost that status checks for these
    3162             :  * subXIDs will take a slower path through TransactionIdIsInProgress().
    3163             :  * This means that KnownAssignedXids is not necessarily complete for subXIDs,
    3164             :  * though it should be complete for top-level XIDs; this is the same situation
    3165             :  * that holds with respect to the PGPROC entries in normal running.
    3166             :  *
    3167             :  * When we throw away subXIDs from KnownAssignedXids, we need to keep track of
    3168             :  * that, similarly to tracking overflow of a PGPROC's subxids array.  We do
    3169             :  * that by remembering the lastOverflowedXid, ie the last thrown-away subXID.
    3170             :  * As long as that is within the range of interesting XIDs, we have to assume
    3171             :  * that subXIDs are missing from snapshots.  (Note that subXID overflow occurs
    3172             :  * on primary when 65th subXID arrives, whereas on standby it occurs when 64th
    3173             :  * subXID arrives - that is not an error.)
    3174             :  *
    3175             :  * Should a backend on primary somehow disappear before it can write an abort
    3176             :  * record, then we just leave those XIDs in KnownAssignedXids. They actually
    3177             :  * aborted but we think they were running; the distinction is irrelevant
    3178             :  * because either way any changes done by the transaction are not visible to
    3179             :  * backends in the standby.  We prune KnownAssignedXids when
    3180             :  * XLOG_RUNNING_XACTS arrives, to forestall possible overflow of the
    3181             :  * array due to such dead XIDs.
    3182             :  */
    3183             : 
    3184             : /*
    3185             :  * RecordKnownAssignedTransactionIds
    3186             :  *      Record the given XID in KnownAssignedXids, as well as any preceding
    3187             :  *      unobserved XIDs.
    3188             :  *
    3189             :  * RecordKnownAssignedTransactionIds() should be run for *every* WAL record
    3190             :  * associated with a transaction. Must be called for each record after we
    3191             :  * have executed StartupCLOG() et al, since we must ExtendCLOG() etc..
    3192             :  *
    3193             :  * Called during recovery in analogy with and in place of GetNewTransactionId()
    3194             :  */
    3195             : void
    3196      164974 : RecordKnownAssignedTransactionIds(TransactionId xid)
    3197             : {
    3198             :     Assert(standbyState >= STANDBY_INITIALIZED);
    3199             :     Assert(TransactionIdIsValid(xid));
    3200             :     Assert(TransactionIdIsValid(latestObservedXid));
    3201             : 
    3202      164974 :     elog(trace_recovery(DEBUG4), "record known xact %u latestObservedXid %u",
    3203             :          xid, latestObservedXid);
    3204             : 
    3205             :     /*
    3206             :      * When a newly observed xid arrives, it is frequently the case that it is
    3207             :      * *not* the next xid in sequence. When this occurs, we must treat the
    3208             :      * intervening xids as running also.
    3209             :      */
    3210      164974 :     if (TransactionIdFollows(xid, latestObservedXid))
    3211             :     {
    3212             :         TransactionId next_expected_xid;
    3213             : 
    3214             :         /*
    3215             :          * Extend subtrans like we do in GetNewTransactionId() during normal
    3216             :          * operation using individual extend steps. Note that we do not need
    3217             :          * to extend clog since its extensions are WAL logged.
    3218             :          *
    3219             :          * This part has to be done regardless of standbyState since we
    3220             :          * immediately start assigning subtransactions to their toplevel
    3221             :          * transactions.
    3222             :          */
    3223        1108 :         next_expected_xid = latestObservedXid;
    3224        3330 :         while (TransactionIdPrecedes(next_expected_xid, xid))
    3225             :         {
    3226        1114 :             TransactionIdAdvance(next_expected_xid);
    3227        1114 :             ExtendSUBTRANS(next_expected_xid);
    3228             :         }
    3229             :         Assert(next_expected_xid == xid);
    3230             : 
    3231             :         /*
    3232             :          * If the KnownAssignedXids machinery isn't up yet, there's nothing
    3233             :          * more to do since we don't track assigned xids yet.
    3234             :          */
    3235        1108 :         if (standbyState <= STANDBY_INITIALIZED)
    3236             :         {
    3237           0 :             latestObservedXid = xid;
    3238           0 :             return;
    3239             :         }
    3240             : 
    3241             :         /*
    3242             :          * Add (latestObservedXid, xid] onto the KnownAssignedXids array.
    3243             :          */
    3244        1108 :         next_expected_xid = latestObservedXid;
    3245        1108 :         TransactionIdAdvance(next_expected_xid);
    3246        1108 :         KnownAssignedXidsAdd(next_expected_xid, xid, false);
    3247             : 
    3248             :         /*
    3249             :          * Now we can advance latestObservedXid
    3250             :          */
    3251        1108 :         latestObservedXid = xid;
    3252             : 
    3253             :         /* ShmemVariableCache->nextFullXid must be beyond any observed xid */
    3254        1108 :         AdvanceNextFullTransactionIdPastXid(latestObservedXid);
    3255        1108 :         next_expected_xid = latestObservedXid;
    3256        1108 :         TransactionIdAdvance(next_expected_xid);
    3257             :     }
    3258             : }
    3259             : 
    3260             : /*
    3261             :  * ExpireTreeKnownAssignedTransactionIds
    3262             :  *      Remove the given XIDs from KnownAssignedXids.
    3263             :  *
    3264             :  * Called during recovery in analogy with and in place of ProcArrayEndTransaction()
    3265             :  */
    3266             : void
    3267         154 : ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids,
    3268             :                                       TransactionId *subxids, TransactionId max_xid)
    3269             : {
    3270             :     Assert(standbyState >= STANDBY_INITIALIZED);
    3271             : 
    3272             :     /*
    3273             :      * Uses same locking as transaction commit
    3274             :      */
    3275         154 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    3276             : 
    3277         154 :     KnownAssignedXidsRemoveTree(xid, nsubxids, subxids);
    3278             : 
    3279             :     /* As in ProcArrayEndTransaction, advance latestCompletedXid */
    3280         154 :     if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
    3281             :                               max_xid))
    3282         146 :         ShmemVariableCache->latestCompletedXid = max_xid;
    3283             : 
    3284         154 :     LWLockRelease(ProcArrayLock);
    3285         154 : }
    3286             : 
    3287             : /*
    3288             :  * ExpireAllKnownAssignedTransactionIds
    3289             :  *      Remove all entries in KnownAssignedXids
    3290             :  */
    3291             : void
    3292          42 : ExpireAllKnownAssignedTransactionIds(void)
    3293             : {
    3294          42 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    3295          42 :     KnownAssignedXidsRemovePreceding(InvalidTransactionId);
    3296          42 :     LWLockRelease(ProcArrayLock);
    3297          42 : }
    3298             : 
    3299             : /*
    3300             :  * ExpireOldKnownAssignedTransactionIds
    3301             :  *      Remove KnownAssignedXids entries preceding the given XID
    3302             :  */
    3303             : void
    3304         146 : ExpireOldKnownAssignedTransactionIds(TransactionId xid)
    3305             : {
    3306         146 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    3307         146 :     KnownAssignedXidsRemovePreceding(xid);
    3308         146 :     LWLockRelease(ProcArrayLock);
    3309         146 : }
    3310             : 
    3311             : 
    3312             : /*
    3313             :  * Private module functions to manipulate KnownAssignedXids
    3314             :  *
    3315             :  * There are 5 main uses of the KnownAssignedXids data structure:
    3316             :  *
    3317             :  *  * backends taking snapshots - all valid XIDs need to be copied out
    3318             :  *  * backends seeking to determine presence of a specific XID
    3319             :  *  * startup process adding new known-assigned XIDs
    3320             :  *  * startup process removing specific XIDs as transactions end
    3321             :  *  * startup process pruning array when special WAL records arrive
    3322             :  *
    3323             :  * This data structure is known to be a hot spot during Hot Standby, so we
    3324             :  * go to some lengths to make these operations as efficient and as concurrent
    3325             :  * as possible.
    3326             :  *
    3327             :  * The XIDs are stored in an array in sorted order --- TransactionIdPrecedes
    3328             :  * order, to be exact --- to allow binary search for specific XIDs.  Note:
    3329             :  * in general TransactionIdPrecedes would not provide a total order, but
    3330             :  * we know that the entries present at any instant should not extend across
    3331             :  * a large enough fraction of XID space to wrap around (the master would
    3332             :  * shut down for fear of XID wrap long before that happens).  So it's OK to
    3333             :  * use TransactionIdPrecedes as a binary-search comparator.
    3334             :  *
    3335             :  * It's cheap to maintain the sortedness during insertions, since new known
    3336             :  * XIDs are always reported in XID order; we just append them at the right.
    3337             :  *
    3338             :  * To keep individual deletions cheap, we need to allow gaps in the array.
    3339             :  * This is implemented by marking array elements as valid or invalid using
    3340             :  * the parallel boolean array KnownAssignedXidsValid[].  A deletion is done
    3341             :  * by setting KnownAssignedXidsValid[i] to false, *without* clearing the
    3342             :  * XID entry itself.  This preserves the property that the XID entries are
    3343             :  * sorted, so we can do binary searches easily.  Periodically we compress
    3344             :  * out the unused entries; that's much cheaper than having to compress the
    3345             :  * array immediately on every deletion.
    3346             :  *
    3347             :  * The actually valid items in KnownAssignedXids[] and KnownAssignedXidsValid[]
    3348             :  * are those with indexes tail <= i < head; items outside this subscript range
    3349             :  * have unspecified contents.  When head reaches the end of the array, we
    3350             :  * force compression of unused entries rather than wrapping around, since
    3351             :  * allowing wraparound would greatly complicate the search logic.  We maintain
    3352             :  * an explicit tail pointer so that pruning of old XIDs can be done without
    3353             :  * immediately moving the array contents.  In most cases only a small fraction
    3354             :  * of the array contains valid entries at any instant.
    3355             :  *
    3356             :  * Although only the startup process can ever change the KnownAssignedXids
    3357             :  * data structure, we still need interlocking so that standby backends will
    3358             :  * not observe invalid intermediate states.  The convention is that backends
    3359             :  * must hold shared ProcArrayLock to examine the array.  To remove XIDs from
    3360             :  * the array, the startup process must hold ProcArrayLock exclusively, for
    3361             :  * the usual transactional reasons (compare commit/abort of a transaction
    3362             :  * during normal running).  Compressing unused entries out of the array
    3363             :  * likewise requires exclusive lock.  To add XIDs to the array, we just insert
    3364             :  * them into slots to the right of the head pointer and then advance the head
    3365             :  * pointer.  This wouldn't require any lock at all, except that on machines
    3366             :  * with weak memory ordering we need to be careful that other processors
    3367             :  * see the array element changes before they see the head pointer change.
    3368             :  * We handle this by using a spinlock to protect reads and writes of the
    3369             :  * head/tail pointers.  (We could dispense with the spinlock if we were to
    3370             :  * create suitable memory access barrier primitives and use those instead.)
    3371             :  * The spinlock must be taken to read or write the head/tail pointers unless
    3372             :  * the caller holds ProcArrayLock exclusively.
    3373             :  *
    3374             :  * Algorithmic analysis:
    3375             :  *
    3376             :  * If we have a maximum of M slots, with N XIDs currently spread across
    3377             :  * S elements then we have N <= S <= M always.
    3378             :  *
    3379             :  *  * Adding a new XID is O(1) and needs little locking (unless compression
    3380             :  *      must happen)
    3381             :  *  * Compressing the array is O(S) and requires exclusive lock
    3382             :  *  * Removing an XID is O(logS) and requires exclusive lock
    3383             :  *  * Taking a snapshot is O(S) and requires shared lock
    3384             :  *  * Checking for an XID is O(logS) and requires shared lock
    3385             :  *
    3386             :  * In comparison, using a hash table for KnownAssignedXids would mean that
    3387             :  * taking snapshots would be O(M). If we can maintain S << M then the
    3388             :  * sorted array technique will deliver significantly faster snapshots.
    3389             :  * If we try to keep S too small then we will spend too much time compressing,
    3390             :  * so there is an optimal point for any workload mix. We use a heuristic to
    3391             :  * decide when to compress the array, though trimming also helps reduce
    3392             :  * frequency of compressing. The heuristic requires us to track the number of
    3393             :  * currently valid XIDs in the array.
    3394             :  */
    3395             : 
    3396             : 
    3397             : /*
    3398             :  * Compress KnownAssignedXids by shifting valid data down to the start of the
    3399             :  * array, removing any gaps.
    3400             :  *
    3401             :  * A compression step is forced if "force" is true, otherwise we do it
    3402             :  * only if a heuristic indicates it's a good time to do it.
    3403             :  *
    3404             :  * Caller must hold ProcArrayLock in exclusive mode.
    3405             :  */
    3406             : static void
    3407         310 : KnownAssignedXidsCompress(bool force)
    3408             : {
    3409         310 :     ProcArrayStruct *pArray = procArray;
    3410             :     int         head,
    3411             :                 tail;
    3412             :     int         compress_index;
    3413             :     int         i;
    3414             : 
    3415             :     /* no spinlock required since we hold ProcArrayLock exclusively */
    3416         310 :     head = pArray->headKnownAssignedXids;
    3417         310 :     tail = pArray->tailKnownAssignedXids;
    3418             : 
    3419         310 :     if (!force)
    3420             :     {
    3421             :         /*
    3422             :          * If we can choose how much to compress, use a heuristic to avoid
    3423             :          * compressing too often or not often enough.
    3424             :          *
    3425             :          * Heuristic is if we have a large enough current spread and less than
    3426             :          * 50% of the elements are currently in use, then compress. This
    3427             :          * should ensure we compress fairly infrequently. We could compress
    3428             :          * less often though the virtual array would spread out more and
    3429             :          * snapshots would become more expensive.
    3430             :          */
    3431         310 :         int         nelements = head - tail;
    3432             : 
    3433         312 :         if (nelements < 4 * PROCARRAY_MAXPROCS ||
    3434           2 :             nelements < 2 * pArray->numKnownAssignedXids)
    3435         308 :             return;
    3436             :     }
    3437             : 
    3438             :     /*
    3439             :      * We compress the array by reading the valid values from tail to head,
    3440             :      * re-aligning data to 0th element.
    3441             :      */
    3442           2 :     compress_index = 0;
    3443         388 :     for (i = tail; i < head; i++)
    3444             :     {
    3445         386 :         if (KnownAssignedXidsValid[i])
    3446             :         {
    3447           2 :             KnownAssignedXids[compress_index] = KnownAssignedXids[i];
    3448           2 :             KnownAssignedXidsValid[compress_index] = true;
    3449           2 :             compress_index++;
    3450             :         }
    3451             :     }
    3452             : 
    3453           2 :     pArray->tailKnownAssignedXids = 0;
    3454           2 :     pArray->headKnownAssignedXids = compress_index;
    3455             : }
    3456             : 
    3457             : /*
    3458             :  * Add xids into KnownAssignedXids at the head of the array.
    3459             :  *
    3460             :  * xids from from_xid to to_xid, inclusive, are added to the array.
    3461             :  *
    3462             :  * If exclusive_lock is true then caller already holds ProcArrayLock in
    3463             :  * exclusive mode, so we need no extra locking here.  Else caller holds no
    3464             :  * lock, so we need to be sure we maintain sufficient interlocks against
    3465             :  * concurrent readers.  (Only the startup process ever calls this, so no need
    3466             :  * to worry about concurrent writers.)
    3467             :  */
    3468             : static void
    3469        1112 : KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
    3470             :                      bool exclusive_lock)
    3471             : {
    3472        1112 :     ProcArrayStruct *pArray = procArray;
    3473             :     TransactionId next_xid;
    3474             :     int         head,
    3475             :                 tail;
    3476             :     int         nxids;
    3477             :     int         i;
    3478             : 
    3479             :     Assert(TransactionIdPrecedesOrEquals(from_xid, to_xid));
    3480             : 
    3481             :     /*
    3482             :      * Calculate how many array slots we'll need.  Normally this is cheap; in
    3483             :      * the unusual case where the XIDs cross the wrap point, we do it the hard
    3484             :      * way.
    3485             :      */
    3486        1112 :     if (to_xid >= from_xid)
    3487        1112 :         nxids = to_xid - from_xid + 1;
    3488             :     else
    3489             :     {
    3490           0 :         nxids = 1;
    3491           0 :         next_xid = from_xid;
    3492           0 :         while (TransactionIdPrecedes(next_xid, to_xid))
    3493             :         {
    3494           0 :             nxids++;
    3495           0 :             TransactionIdAdvance(next_xid);
    3496             :         }
    3497             :     }
    3498             : 
    3499             :     /*
    3500             :      * Since only the startup process modifies the head/tail pointers, we
    3501             :      * don't need a lock to read them here.
    3502             :      */
    3503        1112 :     head = pArray->headKnownAssignedXids;
    3504        1112 :     tail = pArray->tailKnownAssignedXids;
    3505             : 
    3506             :     Assert(head >= 0 && head <= pArray->maxKnownAssignedXids);
    3507             :     Assert(tail >= 0 && tail < pArray->maxKnownAssignedXids);
    3508             : 
    3509             :     /*
    3510             :      * Verify that insertions occur in TransactionId sequence.  Note that even
    3511             :      * if the last existing element is marked invalid, it must still have a
    3512             :      * correctly sequenced XID value.
    3513             :      */
    3514        2074 :     if (head > tail &&
    3515         962 :         TransactionIdFollowsOrEquals(KnownAssignedXids[head - 1], from_xid))
    3516             :     {
    3517           0 :         KnownAssignedXidsDisplay(LOG);
    3518           0 :         elog(ERROR, "out-of-order XID insertion in KnownAssignedXids");
    3519             :     }
    3520             : 
    3521             :     /*
    3522             :      * If our xids won't fit in the remaining space, compress out free space
    3523             :      */
    3524        1112 :     if (head + nxids > pArray->maxKnownAssignedXids)
    3525             :     {
    3526             :         /* must hold lock to compress */
    3527           0 :         if (!exclusive_lock)
    3528           0 :             LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    3529             : 
    3530           0 :         KnownAssignedXidsCompress(true);
    3531             : 
    3532           0 :         head = pArray->headKnownAssignedXids;
    3533             :         /* note: we no longer care about the tail pointer */
    3534             : 
    3535           0 :         if (!exclusive_lock)
    3536           0 :             LWLockRelease(ProcArrayLock);
    3537             : 
    3538             :         /*
    3539             :          * If it still won't fit then we're out of memory
    3540             :          */
    3541           0 :         if (head + nxids > pArray->maxKnownAssignedXids)
    3542           0 :             elog(ERROR, "too many KnownAssignedXids");
    3543             :     }
    3544             : 
    3545             :     /* Now we can insert the xids into the space starting at head */
    3546        1112 :     next_xid = from_xid;
    3547        2230 :     for (i = 0; i < nxids; i++)
    3548             :     {
    3549        1118 :         KnownAssignedXids[head] = next_xid;
    3550        1118 :         KnownAssignedXidsValid[head] = true;
    3551        1118 :         TransactionIdAdvance(next_xid);
    3552        1118 :         head++;
    3553             :     }
    3554             : 
    3555             :     /* Adjust count of number of valid entries */
    3556        1112 :     pArray->numKnownAssignedXids += nxids;
    3557             : 
    3558             :     /*
    3559             :      * Now update the head pointer.  We use a spinlock to protect this
    3560             :      * pointer, not because the update is likely to be non-atomic, but to
    3561             :      * ensure that other processors see the above array updates before they
    3562             :      * see the head pointer change.
    3563             :      *
    3564             :      * If we're holding ProcArrayLock exclusively, there's no need to take the
    3565             :      * spinlock.
    3566             :      */
    3567        1112 :     if (exclusive_lock)
    3568           4 :         pArray->headKnownAssignedXids = head;
    3569             :     else
    3570             :     {
    3571        1108 :         SpinLockAcquire(&pArray->known_assigned_xids_lck);
    3572        1108 :         pArray->headKnownAssignedXids = head;
    3573        1108 :         SpinLockRelease(&pArray->known_assigned_xids_lck);
    3574             :     }
    3575        1112 : }
    3576             : 
    3577             : /*
    3578             :  * KnownAssignedXidsSearch
    3579             :  *
    3580             :  * Searches KnownAssignedXids for a specific xid and optionally removes it.
    3581             :  * Returns true if it was found, false if not.
    3582             :  *
    3583             :  * Caller must hold ProcArrayLock in shared or exclusive mode.
    3584             :  * Exclusive lock must be held for remove = true.
    3585             :  */
    3586             : static bool
    3587        1342 : KnownAssignedXidsSearch(TransactionId xid, bool remove)
    3588             : {
    3589        1342 :     ProcArrayStruct *pArray = procArray;
    3590             :     int         first,
    3591             :                 last;
    3592             :     int         head;
    3593             :     int         tail;
    3594        1342 :     int         result_index = -1;
    3595             : 
    3596        1342 :     if (remove)
    3597             :     {
    3598             :         /* we hold ProcArrayLock exclusively, so no need for spinlock */
    3599        1342 :         tail = pArray->tailKnownAssignedXids;
    3600        1342 :         head = pArray->headKnownAssignedXids;
    3601             :     }
    3602             :     else
    3603             :     {
    3604             :         /* take spinlock to ensure we see up-to-date array contents */
    3605           0 :         SpinLockAcquire(&pArray->known_assigned_xids_lck);
    3606           0 :         tail = pArray->tailKnownAssignedXids;
    3607           0 :         head = pArray->headKnownAssignedXids;
    3608           0 :         SpinLockRelease(&pArray->known_assigned_xids_lck);
    3609             :     }
    3610             : 
    3611             :     /*
    3612             :      * Standard binary search.  Note we can ignore the KnownAssignedXidsValid
    3613             :      * array here, since even invalid entries will contain sorted XIDs.
    3614             :      */
    3615        1342 :     first = tail;
    3616        1342 :     last = head - 1;
    3617        6884 :     while (first <= last)
    3618             :     {
    3619             :         int         mid_index;
    3620             :         TransactionId mid_xid;
    3621             : 
    3622        5156 :         mid_index = (first + last) / 2;
    3623        5156 :         mid_xid = KnownAssignedXids[mid_index];
    3624             : 
    3625        5156 :         if (xid == mid_xid)
    3626             :         {
    3627         956 :             result_index = mid_index;
    3628         956 :             break;
    3629             :         }
    3630        4200 :         else if (TransactionIdPrecedes(xid, mid_xid))
    3631        2430 :             last = mid_index - 1;
    3632             :         else
    3633        1770 :             first = mid_index + 1;
    3634             :     }
    3635             : 
    3636        1342 :     if (result_index < 0)
    3637         386 :         return false;           /* not in array */
    3638             : 
    3639         956 :     if (!KnownAssignedXidsValid[result_index])
    3640           0 :         return false;           /* in array, but invalid */
    3641             : 
    3642         956 :     if (remove)
    3643             :     {
    3644         956 :         KnownAssignedXidsValid[result_index] = false;
    3645             : 
    3646         956 :         pArray->numKnownAssignedXids--;
    3647             :         Assert(pArray->numKnownAssignedXids >= 0);
    3648             : 
    3649             :         /*
    3650             :          * If we're removing the tail element then advance tail pointer over
    3651             :          * any invalid elements.  This will speed future searches.
    3652             :          */
    3653         956 :         if (result_index == tail)
    3654             :         {
    3655         306 :             tail++;
    3656         750 :             while (tail < head && !KnownAssignedXidsValid[tail])
    3657         138 :                 tail++;
    3658         306 :             if (tail >= head)
    3659             :             {
    3660             :                 /* Array is empty, so we can reset both pointers */
    3661         138 :                 pArray->headKnownAssignedXids = 0;
    3662         138 :                 pArray->tailKnownAssignedXids = 0;
    3663             :             }
    3664             :             else
    3665             :             {
    3666         168 :                 pArray->tailKnownAssignedXids = tail;
    3667             :             }
    3668             :         }
    3669             :     }
    3670             : 
    3671         956 :     return true;
    3672             : }
    3673             : 
    3674             : /*
    3675             :  * Is the specified XID present in KnownAssignedXids[]?
    3676             :  *
    3677             :  * Caller must hold ProcArrayLock in shared or exclusive mode.
    3678             :  */
    3679             : static bool
    3680           0 : KnownAssignedXidExists(TransactionId xid)
    3681             : {
    3682             :     Assert(TransactionIdIsValid(xid));
    3683             : 
    3684           0 :     return KnownAssignedXidsSearch(xid, false);
    3685             : }
    3686             : 
    3687             : /*
    3688             :  * Remove the specified XID from KnownAssignedXids[].
    3689             :  *
    3690             :  * Caller must hold ProcArrayLock in exclusive mode.
    3691             :  */
    3692             : static void
    3693        1342 : KnownAssignedXidsRemove(TransactionId xid)
    3694             : {
    3695             :     Assert(TransactionIdIsValid(xid));
    3696             : 
    3697        1342 :     elog(trace_recovery(DEBUG4), "remove KnownAssignedXid %u", xid);
    3698             : 
    3699             :     /*
    3700             :      * Note: we cannot consider it an error to remove an XID that's not
    3701             :      * present.  We intentionally remove subxact IDs while processing
    3702             :      * XLOG_XACT_ASSIGNMENT, to avoid array overflow.  Then those XIDs will be
    3703             :      * removed again when the top-level xact commits or aborts.
    3704             :      *
    3705             :      * It might be possible to track such XIDs to distinguish this case from
    3706             :      * actual errors, but it would be complicated and probably not worth it.
    3707             :      * So, just ignore the search result.
    3708             :      */
    3709        1342 :     (void) KnownAssignedXidsSearch(xid, true);
    3710        1342 : }
    3711             : 
    3712             : /*
    3713             :  * KnownAssignedXidsRemoveTree
    3714             :  *      Remove xid (if it's not InvalidTransactionId) and all the subxids.
    3715             :  *
    3716             :  * Caller must hold ProcArrayLock in exclusive mode.
    3717             :  */
    3718             : static void
    3719         164 : KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
    3720             :                             TransactionId *subxids)
    3721             : {
    3722             :     int         i;
    3723             : 
    3724         164 :     if (TransactionIdIsValid(xid))
    3725         154 :         KnownAssignedXidsRemove(xid);
    3726             : 
    3727        1352 :     for (i = 0; i < nsubxids; i++)
    3728        1188 :         KnownAssignedXidsRemove(subxids[i]);
    3729             : 
    3730             :     /* Opportunistically compress the array */
    3731         164 :     KnownAssignedXidsCompress(false);
    3732         164 : }
    3733             : 
    3734             : /*
    3735             :  * Prune KnownAssignedXids up to, but *not* including xid. If xid is invalid
    3736             :  * then clear the whole table.
    3737             :  *
    3738             :  * Caller must hold ProcArrayLock in exclusive mode.
    3739             :  */
    3740             : static void
    3741         188 : KnownAssignedXidsRemovePreceding(TransactionId removeXid)
    3742             : {
    3743         188 :     ProcArrayStruct *pArray = procArray;
    3744         188 :     int         count = 0;
    3745             :     int         head,
    3746             :                 tail,
    3747             :                 i;
    3748             : 
    3749         188 :     if (!TransactionIdIsValid(removeXid))
    3750             :     {
    3751          42 :         elog(trace_recovery(DEBUG4), "removing all KnownAssignedXids");
    3752          42 :         pArray->numKnownAssignedXids = 0;
    3753          42 :         pArray->headKnownAssignedXids = pArray->tailKnownAssignedXids = 0;
    3754          42 :         return;
    3755             :     }
    3756             : 
    3757         146 :     elog(trace_recovery(DEBUG4), "prune KnownAssignedXids to %u", removeXid);
    3758             : 
    3759             :     /*
    3760             :      * Mark entries invalid starting at the tail.  Since array is sorted, we
    3761             :      * can stop as soon as we reach an entry >= removeXid.
    3762             :      */
    3763         146 :     tail = pArray->tailKnownAssignedXids;
    3764         146 :     head = pArray->headKnownAssignedXids;
    3765             : 
    3766         146 :     for (i = tail; i < head; i++)
    3767             :     {
    3768          36 :         if (KnownAssignedXidsValid[i])
    3769             :         {
    3770          36 :             TransactionId knownXid = KnownAssignedXids[i];
    3771             : 
    3772          36 :             if (TransactionIdFollowsOrEquals(knownXid, removeXid))
    3773          36 :                 break;
    3774             : 
    3775           0 :             if (!StandbyTransactionIdIsPrepared(knownXid))
    3776             :             {
    3777           0 :                 KnownAssignedXidsValid[i] = false;
    3778           0 :                 count++;
    3779             :             }
    3780             :         }
    3781             :     }
    3782             : 
    3783         146 :     pArray->numKnownAssignedXids -= count;
    3784             :     Assert(pArray->numKnownAssignedXids >= 0);
    3785             : 
    3786             :     /*
    3787             :      * Advance the tail pointer if we've marked the tail item invalid.
    3788             :      */
    3789         146 :     for (i = tail; i < head; i++)
    3790             :     {
    3791          36 :         if (KnownAssignedXidsValid[i])
    3792          36 :             break;
    3793             :     }
    3794         146 :     if (i >= head)
    3795             :     {
    3796             :         /* Array is empty, so we can reset both pointers */
    3797         110 :         pArray->headKnownAssignedXids = 0;
    3798         110 :         pArray->tailKnownAssignedXids = 0;
    3799             :     }
    3800             :     else
    3801             :     {
    3802          36 :         pArray->tailKnownAssignedXids = i;
    3803             :     }
    3804             : 
    3805             :     /* Opportunistically compress the array */
    3806         146 :     KnownAssignedXidsCompress(false);
    3807             : }
    3808             : 
    3809             : /*
    3810             :  * KnownAssignedXidsGet - Get an array of xids by scanning KnownAssignedXids.
    3811             :  * We filter out anything >= xmax.
    3812             :  *
    3813             :  * Returns the number of XIDs stored into xarray[].  Caller is responsible
    3814             :  * that array is large enough.
    3815             :  *
    3816             :  * Caller must hold ProcArrayLock in (at least) shared mode.
    3817             :  */
    3818             : static int
    3819           0 : KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax)
    3820             : {
    3821           0 :     TransactionId xtmp = InvalidTransactionId;
    3822             : 
    3823           0 :     return KnownAssignedXidsGetAndSetXmin(xarray, &xtmp, xmax);
    3824             : }
    3825             : 
    3826             : /*
    3827             :  * KnownAssignedXidsGetAndSetXmin - as KnownAssignedXidsGet, plus
    3828             :  * we reduce *xmin to the lowest xid value seen if not already lower.
    3829             :  *
    3830             :  * Caller must hold ProcArrayLock in (at least) shared mode.
    3831             :  */
    3832             : static int
    3833        1180 : KnownAssignedXidsGetAndSetXmin(TransactionId *xarray, TransactionId *xmin,
    3834             :                                TransactionId xmax)
    3835             : {
    3836        1180 :     int         count = 0;
    3837             :     int         head,
    3838             :                 tail;
    3839             :     int         i;
    3840             : 
    3841             :     /*
    3842             :      * Fetch head just once, since it may change while we loop. We can stop
    3843             :      * once we reach the initially seen head, since we are certain that an xid
    3844             :      * cannot enter and then leave the array while we hold ProcArrayLock.  We
    3845             :      * might miss newly-added xids, but they should be >= xmax so irrelevant
    3846             :      * anyway.
    3847             :      *
    3848             :      * Must take spinlock to ensure we see up-to-date array contents.
    3849             :      */
    3850        1180 :     SpinLockAcquire(&procArray->known_assigned_xids_lck);
    3851        1180 :     tail = procArray->tailKnownAssignedXids;
    3852        1180 :     head = procArray->headKnownAssignedXids;
    3853        1180 :     SpinLockRelease(&procArray->known_assigned_xids_lck);
    3854             : 
    3855        1210 :     for (i = tail; i < head; i++)
    3856             :     {
    3857             :         /* Skip any gaps in the array */
    3858         292 :         if (KnownAssignedXidsValid[i])
    3859             :         {
    3860         268 :             TransactionId knownXid = KnownAssignedXids[i];
    3861             : 
    3862             :             /*
    3863             :              * Update xmin if required.  Only the first XID need be checked,
    3864             :              * since the array is sorted.
    3865             :              */
    3866         536 :             if (count == 0 &&
    3867         268 :                 TransactionIdPrecedes(knownXid, *xmin))
    3868           6 :                 *xmin = knownXid;
    3869             : 
    3870             :             /*
    3871             :              * Filter out anything >= xmax, again relying on sorted property
    3872             :              * of array.
    3873             :              */
    3874         536 :             if (TransactionIdIsValid(xmax) &&
    3875         268 :                 TransactionIdFollowsOrEquals(knownXid, xmax))
    3876         262 :                 break;
    3877             : 
    3878             :             /* Add knownXid into output array */
    3879           6 :             xarray[count++] = knownXid;
    3880             :         }
    3881             :     }
    3882             : 
    3883        1180 :     return count;
    3884             : }
    3885             : 
    3886             : /*
    3887             :  * Get oldest XID in the KnownAssignedXids array, or InvalidTransactionId
    3888             :  * if nothing there.
    3889             :  */
    3890             : static TransactionId
    3891          34 : KnownAssignedXidsGetOldestXmin(void)
    3892             : {
    3893             :     int         head,
    3894             :                 tail;
    3895             :     int         i;
    3896             : 
    3897             :     /*
    3898             :      * Fetch head just once, since it may change while we loop.
    3899             :      */
    3900          34 :     SpinLockAcquire(&procArray->known_assigned_xids_lck);
    3901          34 :     tail = procArray->tailKnownAssignedXids;
    3902          34 :     head = procArray->headKnownAssignedXids;
    3903          34 :     SpinLockRelease(&procArray->known_assigned_xids_lck);
    3904             : 
    3905          34 :     for (i = tail; i < head; i++)
    3906             :     {
    3907             :         /* Skip any gaps in the array */
    3908           6 :         if (KnownAssignedXidsValid[i])
    3909           6 :             return KnownAssignedXids[i];
    3910             :     }
    3911             : 
    3912          28 :     return InvalidTransactionId;
    3913             : }
    3914             : 
    3915             : /*
    3916             :  * Display KnownAssignedXids to provide debug trail
    3917             :  *
    3918             :  * Currently this is only called within startup process, so we need no
    3919             :  * special locking.
    3920             :  *
    3921             :  * Note this is pretty expensive, and much of the expense will be incurred
    3922             :  * even if the elog message will get discarded.  It's not currently called
    3923             :  * in any performance-critical places, however, so no need to be tenser.
    3924             :  */
    3925             : static void
    3926          68 : KnownAssignedXidsDisplay(int trace_level)
    3927             : {
    3928          68 :     ProcArrayStruct *pArray = procArray;
    3929             :     StringInfoData buf;
    3930             :     int         head,
    3931             :                 tail,
    3932             :                 i;
    3933          68 :     int         nxids = 0;
    3934             : 
    3935          68 :     tail = pArray->tailKnownAssignedXids;
    3936          68 :     head = pArray->headKnownAssignedXids;
    3937             : 
    3938          68 :     initStringInfo(&buf);
    3939             : 
    3940          76 :     for (i = tail; i < head; i++)
    3941             :     {
    3942           8 :         if (KnownAssignedXidsValid[i])
    3943             :         {
    3944           8 :             nxids++;
    3945           8 :             appendStringInfo(&buf, "[%d]=%u ", i, KnownAssignedXids[i]);
    3946             :         }
    3947             :     }
    3948             : 
    3949          68 :     elog(trace_level, "%d KnownAssignedXids (num=%d tail=%d head=%d) %s",
    3950             :          nxids,
    3951             :          pArray->numKnownAssignedXids,
    3952             :          pArray->tailKnownAssignedXids,
    3953             :          pArray->headKnownAssignedXids,
    3954             :          buf.data);
    3955             : 
    3956          68 :     pfree(buf.data);
    3957          68 : }
    3958             : 
    3959             : /*
    3960             :  * KnownAssignedXidsReset
    3961             :  *      Resets KnownAssignedXids to be empty
    3962             :  */
    3963             : static void
    3964           0 : KnownAssignedXidsReset(void)
    3965             : {
    3966           0 :     ProcArrayStruct *pArray = procArray;
    3967             : 
    3968           0 :     LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    3969             : 
    3970           0 :     pArray->numKnownAssignedXids = 0;
    3971           0 :     pArray->tailKnownAssignedXids = 0;
    3972           0 :     pArray->headKnownAssignedXids = 0;
    3973             : 
    3974           0 :     LWLockRelease(ProcArrayLock);
    3975           0 : }

Generated by: LCOV version 1.13