PostgreSQL Source Code  git master
procarray.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * procarray.c
4  * POSTGRES process array code.
5  *
6  *
7  * This module maintains arrays of PGPROC substructures, as well as associated
8  * arrays in ProcGlobal, for all active backends. Although there are several
9  * uses for this, the principal one is as a means of determining the set of
10  * currently running transactions.
11  *
12  * Because of various subtle race conditions it is critical that a backend
13  * hold the correct locks while setting or clearing its xid (in
14  * ProcGlobal->xids[]/MyProc->xid). See notes in
15  * src/backend/access/transam/README.
16  *
17  * The process arrays now also include structures representing prepared
18  * transactions. The xid and subxids fields of these are valid, as are the
19  * myProcLocks lists. They can be distinguished from regular backend PGPROCs
20  * at need by checking for pid == 0.
21  *
22  * During hot standby, we also keep a list of XIDs representing transactions
23  * that are known to be running on the primary (or more precisely, were running
24  * as of the current point in the WAL stream). This list is kept in the
25  * KnownAssignedXids array, and is updated by watching the sequence of
26  * arriving XIDs. This is necessary because if we leave those XIDs out of
27  * snapshots taken for standby queries, then they will appear to be already
28  * complete, leading to MVCC failures. Note that in hot standby, the PGPROC
29  * array represents standby processes, which by definition are not running
30  * transactions that have XIDs.
31  *
32  * It is perhaps possible for a backend on the primary to terminate without
33  * writing an abort record for its transaction. While that shouldn't really
34  * happen, it would tie up KnownAssignedXids indefinitely, so we protect
35  * ourselves by pruning the array when a valid list of running XIDs arrives.
36  *
37  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
38  * Portions Copyright (c) 1994, Regents of the University of California
39  *
40  *
41  * IDENTIFICATION
42  * src/backend/storage/ipc/procarray.c
43  *
44  *-------------------------------------------------------------------------
45  */
46 #include "postgres.h"
47 
48 #include <signal.h>
49 
50 #include "access/clog.h"
51 #include "access/subtrans.h"
52 #include "access/transam.h"
53 #include "access/twophase.h"
54 #include "access/xact.h"
55 #include "access/xlogutils.h"
56 #include "catalog/catalog.h"
57 #include "catalog/pg_authid.h"
58 #include "commands/dbcommands.h"
59 #include "miscadmin.h"
60 #include "pgstat.h"
61 #include "port/pg_lfind.h"
62 #include "storage/proc.h"
63 #include "storage/procarray.h"
64 #include "utils/acl.h"
65 #include "utils/builtins.h"
66 #include "utils/rel.h"
67 #include "utils/snapmgr.h"
68 
69 #define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
70 
71 /* Our shared memory area */
72 typedef struct ProcArrayStruct
73 {
74  int numProcs; /* number of valid procs entries */
75  int maxProcs; /* allocated size of procs array */
76 
77  /*
78  * Known assigned XIDs handling
79  */
80  int maxKnownAssignedXids; /* allocated size of array */
81  int numKnownAssignedXids; /* current # of valid entries */
82  int tailKnownAssignedXids; /* index of oldest valid element */
83  int headKnownAssignedXids; /* index of newest element, + 1 */
84 
85  /*
86  * Highest subxid that has been removed from KnownAssignedXids array to
87  * prevent overflow; or InvalidTransactionId if none. We track this for
88  * similar reasons to tracking overflowing cached subxids in PGPROC
89  * entries. Must hold exclusive ProcArrayLock to change this, and shared
90  * lock to read it.
91  */
93 
94  /* oldest xmin of any replication slot */
96  /* oldest catalog xmin of any replication slot */
98 
99  /* indexes into allProcs[], has PROCARRAY_MAXPROCS entries */
102 
103 /*
104  * State for the GlobalVisTest* family of functions. Those functions can
105  * e.g. be used to decide if a deleted row can be removed without violating
106  * MVCC semantics: If the deleted row's xmax is not considered to be running
107  * by anyone, the row can be removed.
108  *
109  * To avoid slowing down GetSnapshotData(), we don't calculate a precise
110  * cutoff XID while building a snapshot (looking at the frequently changing
111  * xmins scales badly). Instead we compute two boundaries while building the
112  * snapshot:
113  *
114  * 1) definitely_needed, indicating that rows deleted by XIDs >=
115  * definitely_needed are definitely still visible.
116  *
117  * 2) maybe_needed, indicating that rows deleted by XIDs < maybe_needed can
118  * definitely be removed
119  *
120  * When testing an XID that falls in between the two (i.e. XID >= maybe_needed
121  * && XID < definitely_needed), the boundaries can be recomputed (using
122  * ComputeXidHorizons()) to get a more accurate answer. This is cheaper than
123  * maintaining an accurate value all the time.
124  *
125  * As it is not cheap to compute accurate boundaries, we limit the number of
126  * times that happens in short succession. See GlobalVisTestShouldUpdate().
127  *
128  *
129  * There are three backend lifetime instances of this struct, optimized for
130  * different types of relations. As e.g. a normal user defined table in one
131  * database is inaccessible to backends connected to another database, a test
132  * specific to a relation can be more aggressive than a test for a shared
133  * relation. Currently we track four different states:
134  *
135  * 1) GlobalVisSharedRels, which only considers an XID's
136  * effects visible-to-everyone if neither snapshots in any database, nor a
137  * replication slot's xmin, nor a replication slot's catalog_xmin might
138  * still consider XID as running.
139  *
140  * 2) GlobalVisCatalogRels, which only considers an XID's
141  * effects visible-to-everyone if neither snapshots in the current
142  * database, nor a replication slot's xmin, nor a replication slot's
143  * catalog_xmin might still consider XID as running.
144  *
145  * I.e. the difference to GlobalVisSharedRels is that
146  * snapshot in other databases are ignored.
147  *
148  * 3) GlobalVisDataRels, which only considers an XID's
149  * effects visible-to-everyone if neither snapshots in the current
150  * database, nor a replication slot's xmin consider XID as running.
151  *
152  * I.e. the difference to GlobalVisCatalogRels is that
153  * replication slot's catalog_xmin is not taken into account.
154  *
155  * 4) GlobalVisTempRels, which only considers the current session, as temp
156  * tables are not visible to other sessions.
157  *
158  * GlobalVisTestFor(relation) returns the appropriate state
159  * for the relation.
160  *
161  * The boundaries are FullTransactionIds instead of TransactionIds to avoid
162  * wraparound dangers. There e.g. would otherwise exist no procarray state to
163  * prevent maybe_needed to become old enough after the GetSnapshotData()
164  * call.
165  *
166  * The typedef is in the header.
167  */
169 {
170  /* XIDs >= are considered running by some backend */
172 
173  /* XIDs < are not considered to be running by any backend */
175 };
176 
177 /*
178  * Result of ComputeXidHorizons().
179  */
181 {
182  /*
183  * The value of TransamVariables->latestCompletedXid when
184  * ComputeXidHorizons() held ProcArrayLock.
185  */
187 
188  /*
189  * The same for procArray->replication_slot_xmin and.
190  * procArray->replication_slot_catalog_xmin.
191  */
194 
195  /*
196  * Oldest xid that any backend might still consider running. This needs to
197  * include processes running VACUUM, in contrast to the normal visibility
198  * cutoffs, as vacuum needs to be able to perform pg_subtrans lookups when
199  * determining visibility, but doesn't care about rows above its xmin to
200  * be removed.
201  *
202  * This likely should only be needed to determine whether pg_subtrans can
203  * be truncated. It currently includes the effects of replication slots,
204  * for historical reasons. But that could likely be changed.
205  */
207 
208  /*
209  * Oldest xid for which deleted tuples need to be retained in shared
210  * tables.
211  *
212  * This includes the effects of replication slots. If that's not desired,
213  * look at shared_oldest_nonremovable_raw;
214  */
216 
217  /*
218  * Oldest xid that may be necessary to retain in shared tables. This is
219  * the same as shared_oldest_nonremovable, except that is not affected by
220  * replication slot's catalog_xmin.
221  *
222  * This is mainly useful to be able to send the catalog_xmin to upstream
223  * streaming replication servers via hot_standby_feedback, so they can
224  * apply the limit only when accessing catalog tables.
225  */
227 
228  /*
229  * Oldest xid for which deleted tuples need to be retained in non-shared
230  * catalog tables.
231  */
233 
234  /*
235  * Oldest xid for which deleted tuples need to be retained in normal user
236  * defined tables.
237  */
239 
240  /*
241  * Oldest xid for which deleted tuples need to be retained in this
242  * session's temporary tables.
243  */
246 
247 /*
248  * Return value for GlobalVisHorizonKindForRel().
249  */
251 {
257 
258 /*
259  * Reason codes for KnownAssignedXidsCompress().
260  */
261 typedef enum KAXCompressReason
262 {
263  KAX_NO_SPACE, /* need to free up space at array end */
264  KAX_PRUNE, /* we just pruned old entries */
265  KAX_TRANSACTION_END, /* we just committed/removed some XIDs */
266  KAX_STARTUP_PROCESS_IDLE, /* startup process is about to sleep */
268 
269 
271 
272 static PGPROC *allProcs;
273 
274 /*
275  * Cache to reduce overhead of repeated calls to TransactionIdIsInProgress()
276  */
278 
279 /*
280  * Bookkeeping for tracking emulated transactions in recovery
281  */
285 
286 /*
287  * If we're in STANDBY_SNAPSHOT_PENDING state, standbySnapshotPendingXmin is
288  * the highest xid that might still be running that we don't have in
289  * KnownAssignedXids.
290  */
292 
293 /*
294  * State for visibility checks on different types of relations. See struct
295  * GlobalVisState for details. As shared, catalog, normal and temporary
296  * relations can have different horizons, one such state exists for each.
297  */
302 
303 /*
304  * This backend's RecentXmin at the last time the accurate xmin horizon was
305  * recomputed, or InvalidTransactionId if it has not. Used to limit how many
306  * times accurate horizons are recomputed. See GlobalVisTestShouldUpdate().
307  */
309 
310 #ifdef XIDCACHE_DEBUG
311 
312 /* counters for XidCache measurement */
313 static long xc_by_recent_xmin = 0;
314 static long xc_by_known_xact = 0;
315 static long xc_by_my_xact = 0;
316 static long xc_by_latest_xid = 0;
317 static long xc_by_main_xid = 0;
318 static long xc_by_child_xid = 0;
319 static long xc_by_known_assigned = 0;
320 static long xc_no_overflow = 0;
321 static long xc_slow_answer = 0;
322 
323 #define xc_by_recent_xmin_inc() (xc_by_recent_xmin++)
324 #define xc_by_known_xact_inc() (xc_by_known_xact++)
325 #define xc_by_my_xact_inc() (xc_by_my_xact++)
326 #define xc_by_latest_xid_inc() (xc_by_latest_xid++)
327 #define xc_by_main_xid_inc() (xc_by_main_xid++)
328 #define xc_by_child_xid_inc() (xc_by_child_xid++)
329 #define xc_by_known_assigned_inc() (xc_by_known_assigned++)
330 #define xc_no_overflow_inc() (xc_no_overflow++)
331 #define xc_slow_answer_inc() (xc_slow_answer++)
332 
333 static void DisplayXidCache(void);
334 #else /* !XIDCACHE_DEBUG */
335 
336 #define xc_by_recent_xmin_inc() ((void) 0)
337 #define xc_by_known_xact_inc() ((void) 0)
338 #define xc_by_my_xact_inc() ((void) 0)
339 #define xc_by_latest_xid_inc() ((void) 0)
340 #define xc_by_main_xid_inc() ((void) 0)
341 #define xc_by_child_xid_inc() ((void) 0)
342 #define xc_by_known_assigned_inc() ((void) 0)
343 #define xc_no_overflow_inc() ((void) 0)
344 #define xc_slow_answer_inc() ((void) 0)
345 #endif /* XIDCACHE_DEBUG */
346 
347 /* Primitives for KnownAssignedXids array handling for standby */
348 static void KnownAssignedXidsCompress(KAXCompressReason reason, bool haveLock);
349 static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
350  bool exclusive_lock);
351 static bool KnownAssignedXidsSearch(TransactionId xid, bool remove);
352 static bool KnownAssignedXidExists(TransactionId xid);
353 static void KnownAssignedXidsRemove(TransactionId xid);
354 static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
355  TransactionId *subxids);
356 static void KnownAssignedXidsRemovePreceding(TransactionId removeXid);
357 static int KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax);
359  TransactionId *xmin,
360  TransactionId xmax);
362 static void KnownAssignedXidsDisplay(int trace_level);
363 static void KnownAssignedXidsReset(void);
364 static inline void ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid);
365 static void ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid);
366 static void MaintainLatestCompletedXid(TransactionId latestXid);
368 
370  TransactionId xid);
371 static void GlobalVisUpdateApply(ComputeXidHorizonsResult *horizons);
372 
373 /*
374  * Report shared-memory space needed by CreateSharedProcArray.
375  */
376 Size
378 {
379  Size size;
380 
381  /* Size of the ProcArray structure itself */
382 #define PROCARRAY_MAXPROCS (MaxBackends + max_prepared_xacts)
383 
384  size = offsetof(ProcArrayStruct, pgprocnos);
385  size = add_size(size, mul_size(sizeof(int), PROCARRAY_MAXPROCS));
386 
387  /*
388  * During Hot Standby processing we have a data structure called
389  * KnownAssignedXids, created in shared memory. Local data structures are
390  * also created in various backends during GetSnapshotData(),
391  * TransactionIdIsInProgress() and GetRunningTransactionData(). All of the
392  * main structures created in those functions must be identically sized,
393  * since we may at times copy the whole of the data structures around. We
394  * refer to this size as TOTAL_MAX_CACHED_SUBXIDS.
395  *
396  * Ideally we'd only create this structure if we were actually doing hot
397  * standby in the current run, but we don't know that yet at the time
398  * shared memory is being set up.
399  */
400 #define TOTAL_MAX_CACHED_SUBXIDS \
401  ((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
402 
403  if (EnableHotStandby)
404  {
405  size = add_size(size,
406  mul_size(sizeof(TransactionId),
408  size = add_size(size,
409  mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS));
410  }
411 
412  return size;
413 }
414 
415 /*
416  * Initialize the shared PGPROC array during postmaster startup.
417  */
418 void
420 {
421  bool found;
422 
423  /* Create or attach to the ProcArray shared structure */
425  ShmemInitStruct("Proc Array",
426  add_size(offsetof(ProcArrayStruct, pgprocnos),
427  mul_size(sizeof(int),
429  &found);
430 
431  if (!found)
432  {
433  /*
434  * We're the first - initialize.
435  */
436  procArray->numProcs = 0;
446  }
447 
449 
450  /* Create or attach to the KnownAssignedXids arrays too, if needed */
451  if (EnableHotStandby)
452  {
454  ShmemInitStruct("KnownAssignedXids",
455  mul_size(sizeof(TransactionId),
457  &found);
458  KnownAssignedXidsValid = (bool *)
459  ShmemInitStruct("KnownAssignedXidsValid",
460  mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS),
461  &found);
462  }
463 }
464 
465 /*
466  * Add the specified PGPROC to the shared array.
467  */
468 void
470 {
471  int pgprocno = GetNumberFromPGProc(proc);
472  ProcArrayStruct *arrayP = procArray;
473  int index;
474  int movecount;
475 
476  /* See ProcGlobal comment explaining why both locks are held */
477  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
478  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
479 
480  if (arrayP->numProcs >= arrayP->maxProcs)
481  {
482  /*
483  * Oops, no room. (This really shouldn't happen, since there is a
484  * fixed supply of PGPROC structs too, and so we should have failed
485  * earlier.)
486  */
487  ereport(FATAL,
488  (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
489  errmsg("sorry, too many clients already")));
490  }
491 
492  /*
493  * Keep the procs array sorted by (PGPROC *) so that we can utilize
494  * locality of references much better. This is useful while traversing the
495  * ProcArray because there is an increased likelihood of finding the next
496  * PGPROC structure in the cache.
497  *
498  * Since the occurrence of adding/removing a proc is much lower than the
499  * access to the ProcArray itself, the overhead should be marginal
500  */
501  for (index = 0; index < arrayP->numProcs; index++)
502  {
503  int this_procno = arrayP->pgprocnos[index];
504 
505  Assert(this_procno >= 0 && this_procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
506  Assert(allProcs[this_procno].pgxactoff == index);
507 
508  /* If we have found our right position in the array, break */
509  if (this_procno > pgprocno)
510  break;
511  }
512 
513  movecount = arrayP->numProcs - index;
514  memmove(&arrayP->pgprocnos[index + 1],
515  &arrayP->pgprocnos[index],
516  movecount * sizeof(*arrayP->pgprocnos));
517  memmove(&ProcGlobal->xids[index + 1],
518  &ProcGlobal->xids[index],
519  movecount * sizeof(*ProcGlobal->xids));
520  memmove(&ProcGlobal->subxidStates[index + 1],
522  movecount * sizeof(*ProcGlobal->subxidStates));
523  memmove(&ProcGlobal->statusFlags[index + 1],
525  movecount * sizeof(*ProcGlobal->statusFlags));
526 
527  arrayP->pgprocnos[index] = GetNumberFromPGProc(proc);
528  proc->pgxactoff = index;
529  ProcGlobal->xids[index] = proc->xid;
532 
533  arrayP->numProcs++;
534 
535  /* adjust pgxactoff for all following PGPROCs */
536  index++;
537  for (; index < arrayP->numProcs; index++)
538  {
539  int procno = arrayP->pgprocnos[index];
540 
541  Assert(procno >= 0 && procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
542  Assert(allProcs[procno].pgxactoff == index - 1);
543 
544  allProcs[procno].pgxactoff = index;
545  }
546 
547  /*
548  * Release in reversed acquisition order, to reduce frequency of having to
549  * wait for XidGenLock while holding ProcArrayLock.
550  */
551  LWLockRelease(XidGenLock);
552  LWLockRelease(ProcArrayLock);
553 }
554 
555 /*
556  * Remove the specified PGPROC from the shared array.
557  *
558  * When latestXid is a valid XID, we are removing a live 2PC gxact from the
559  * array, and thus causing it to appear as "not running" anymore. In this
560  * case we must advance latestCompletedXid. (This is essentially the same
561  * as ProcArrayEndTransaction followed by removal of the PGPROC, but we take
562  * the ProcArrayLock only once, and don't damage the content of the PGPROC;
563  * twophase.c depends on the latter.)
564  */
565 void
567 {
568  ProcArrayStruct *arrayP = procArray;
569  int myoff;
570  int movecount;
571 
572 #ifdef XIDCACHE_DEBUG
573  /* dump stats at backend shutdown, but not prepared-xact end */
574  if (proc->pid != 0)
575  DisplayXidCache();
576 #endif
577 
578  /* See ProcGlobal comment explaining why both locks are held */
579  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
580  LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
581 
582  myoff = proc->pgxactoff;
583 
584  Assert(myoff >= 0 && myoff < arrayP->numProcs);
585  Assert(ProcGlobal->allProcs[arrayP->pgprocnos[myoff]].pgxactoff == myoff);
586 
587  if (TransactionIdIsValid(latestXid))
588  {
590 
591  /* Advance global latestCompletedXid while holding the lock */
592  MaintainLatestCompletedXid(latestXid);
593 
594  /* Same with xactCompletionCount */
596 
598  ProcGlobal->subxidStates[myoff].overflowed = false;
599  ProcGlobal->subxidStates[myoff].count = 0;
600  }
601  else
602  {
603  /* Shouldn't be trying to remove a live transaction here */
605  }
606 
608  Assert(ProcGlobal->subxidStates[myoff].count == 0);
609  Assert(ProcGlobal->subxidStates[myoff].overflowed == false);
610 
611  ProcGlobal->statusFlags[myoff] = 0;
612 
613  /* Keep the PGPROC array sorted. See notes above */
614  movecount = arrayP->numProcs - myoff - 1;
615  memmove(&arrayP->pgprocnos[myoff],
616  &arrayP->pgprocnos[myoff + 1],
617  movecount * sizeof(*arrayP->pgprocnos));
618  memmove(&ProcGlobal->xids[myoff],
619  &ProcGlobal->xids[myoff + 1],
620  movecount * sizeof(*ProcGlobal->xids));
621  memmove(&ProcGlobal->subxidStates[myoff],
622  &ProcGlobal->subxidStates[myoff + 1],
623  movecount * sizeof(*ProcGlobal->subxidStates));
624  memmove(&ProcGlobal->statusFlags[myoff],
625  &ProcGlobal->statusFlags[myoff + 1],
626  movecount * sizeof(*ProcGlobal->statusFlags));
627 
628  arrayP->pgprocnos[arrayP->numProcs - 1] = -1; /* for debugging */
629  arrayP->numProcs--;
630 
631  /*
632  * Adjust pgxactoff of following procs for removed PGPROC (note that
633  * numProcs already has been decremented).
634  */
635  for (int index = myoff; index < arrayP->numProcs; index++)
636  {
637  int procno = arrayP->pgprocnos[index];
638 
639  Assert(procno >= 0 && procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
640  Assert(allProcs[procno].pgxactoff - 1 == index);
641 
642  allProcs[procno].pgxactoff = index;
643  }
644 
645  /*
646  * Release in reversed acquisition order, to reduce frequency of having to
647  * wait for XidGenLock while holding ProcArrayLock.
648  */
649  LWLockRelease(XidGenLock);
650  LWLockRelease(ProcArrayLock);
651 }
652 
653 
654 /*
655  * ProcArrayEndTransaction -- mark a transaction as no longer running
656  *
657  * This is used interchangeably for commit and abort cases. The transaction
658  * commit/abort must already be reported to WAL and pg_xact.
659  *
660  * proc is currently always MyProc, but we pass it explicitly for flexibility.
661  * latestXid is the latest Xid among the transaction's main XID and
662  * subtransactions, or InvalidTransactionId if it has no XID. (We must ask
663  * the caller to pass latestXid, instead of computing it from the PGPROC's
664  * contents, because the subxid information in the PGPROC might be
665  * incomplete.)
666  */
667 void
669 {
670  if (TransactionIdIsValid(latestXid))
671  {
672  /*
673  * We must lock ProcArrayLock while clearing our advertised XID, so
674  * that we do not exit the set of "running" transactions while someone
675  * else is taking a snapshot. See discussion in
676  * src/backend/access/transam/README.
677  */
679 
680  /*
681  * If we can immediately acquire ProcArrayLock, we clear our own XID
682  * and release the lock. If not, use group XID clearing to improve
683  * efficiency.
684  */
685  if (LWLockConditionalAcquire(ProcArrayLock, LW_EXCLUSIVE))
686  {
687  ProcArrayEndTransactionInternal(proc, latestXid);
688  LWLockRelease(ProcArrayLock);
689  }
690  else
691  ProcArrayGroupClearXid(proc, latestXid);
692  }
693  else
694  {
695  /*
696  * If we have no XID, we don't need to lock, since we won't affect
697  * anyone else's calculation of a snapshot. We might change their
698  * estimate of global xmin, but that's OK.
699  */
701  Assert(proc->subxidStatus.count == 0);
703 
705  proc->xmin = InvalidTransactionId;
706 
707  /* be sure this is cleared in abort */
708  proc->delayChkptFlags = 0;
709 
710  proc->recoveryConflictPending = false;
711 
712  /* must be cleared with xid/xmin: */
713  /* avoid unnecessarily dirtying shared cachelines */
715  {
716  Assert(!LWLockHeldByMe(ProcArrayLock));
717  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
721  LWLockRelease(ProcArrayLock);
722  }
723  }
724 }
725 
726 /*
727  * Mark a write transaction as no longer running.
728  *
729  * We don't do any locking here; caller must handle that.
730  */
731 static inline void
733 {
734  int pgxactoff = proc->pgxactoff;
735 
736  /*
737  * Note: we need exclusive lock here because we're going to change other
738  * processes' PGPROC entries.
739  */
740  Assert(LWLockHeldByMeInMode(ProcArrayLock, LW_EXCLUSIVE));
742  Assert(ProcGlobal->xids[pgxactoff] == proc->xid);
743 
744  ProcGlobal->xids[pgxactoff] = InvalidTransactionId;
745  proc->xid = InvalidTransactionId;
747  proc->xmin = InvalidTransactionId;
748 
749  /* be sure this is cleared in abort */
750  proc->delayChkptFlags = 0;
751 
752  proc->recoveryConflictPending = false;
753 
754  /* must be cleared with xid/xmin: */
755  /* avoid unnecessarily dirtying shared cachelines */
757  {
760  }
761 
762  /* Clear the subtransaction-XID cache too while holding the lock */
763  Assert(ProcGlobal->subxidStates[pgxactoff].count == proc->subxidStatus.count &&
765  if (proc->subxidStatus.count > 0 || proc->subxidStatus.overflowed)
766  {
767  ProcGlobal->subxidStates[pgxactoff].count = 0;
768  ProcGlobal->subxidStates[pgxactoff].overflowed = false;
769  proc->subxidStatus.count = 0;
770  proc->subxidStatus.overflowed = false;
771  }
772 
773  /* Also advance global latestCompletedXid while holding the lock */
774  MaintainLatestCompletedXid(latestXid);
775 
776  /* Same with xactCompletionCount */
778 }
779 
780 /*
781  * ProcArrayGroupClearXid -- group XID clearing
782  *
783  * When we cannot immediately acquire ProcArrayLock in exclusive mode at
784  * commit time, add ourselves to a list of processes that need their XIDs
785  * cleared. The first process to add itself to the list will acquire
786  * ProcArrayLock in exclusive mode and perform ProcArrayEndTransactionInternal
787  * on behalf of all group members. This avoids a great deal of contention
788  * around ProcArrayLock when many processes are trying to commit at once,
789  * since the lock need not be repeatedly handed off from one committing
790  * process to the next.
791  */
792 static void
794 {
795  int pgprocno = GetNumberFromPGProc(proc);
796  PROC_HDR *procglobal = ProcGlobal;
797  uint32 nextidx;
798  uint32 wakeidx;
799 
800  /* We should definitely have an XID to clear. */
802 
803  /* Add ourselves to the list of processes needing a group XID clear. */
804  proc->procArrayGroupMember = true;
805  proc->procArrayGroupMemberXid = latestXid;
806  nextidx = pg_atomic_read_u32(&procglobal->procArrayGroupFirst);
807  while (true)
808  {
809  pg_atomic_write_u32(&proc->procArrayGroupNext, nextidx);
810 
812  &nextidx,
813  (uint32) pgprocno))
814  break;
815  }
816 
817  /*
818  * If the list was not empty, the leader will clear our XID. It is
819  * impossible to have followers without a leader because the first process
820  * that has added itself to the list will always have nextidx as
821  * INVALID_PROC_NUMBER.
822  */
823  if (nextidx != INVALID_PROC_NUMBER)
824  {
825  int extraWaits = 0;
826 
827  /* Sleep until the leader clears our XID. */
828  pgstat_report_wait_start(WAIT_EVENT_PROCARRAY_GROUP_UPDATE);
829  for (;;)
830  {
831  /* acts as a read barrier */
832  PGSemaphoreLock(proc->sem);
833  if (!proc->procArrayGroupMember)
834  break;
835  extraWaits++;
836  }
838 
840 
841  /* Fix semaphore count for any absorbed wakeups */
842  while (extraWaits-- > 0)
843  PGSemaphoreUnlock(proc->sem);
844  return;
845  }
846 
847  /* We are the leader. Acquire the lock on behalf of everyone. */
848  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
849 
850  /*
851  * Now that we've got the lock, clear the list of processes waiting for
852  * group XID clearing, saving a pointer to the head of the list. Trying
853  * to pop elements one at a time could lead to an ABA problem.
854  */
855  nextidx = pg_atomic_exchange_u32(&procglobal->procArrayGroupFirst,
857 
858  /* Remember head of list so we can perform wakeups after dropping lock. */
859  wakeidx = nextidx;
860 
861  /* Walk the list and clear all XIDs. */
862  while (nextidx != INVALID_PROC_NUMBER)
863  {
864  PGPROC *nextproc = &allProcs[nextidx];
865 
867 
868  /* Move to next proc in list. */
869  nextidx = pg_atomic_read_u32(&nextproc->procArrayGroupNext);
870  }
871 
872  /* We're done with the lock now. */
873  LWLockRelease(ProcArrayLock);
874 
875  /*
876  * Now that we've released the lock, go back and wake everybody up. We
877  * don't do this under the lock so as to keep lock hold times to a
878  * minimum. The system calls we need to perform to wake other processes
879  * up are probably much slower than the simple memory writes we did while
880  * holding the lock.
881  */
882  while (wakeidx != INVALID_PROC_NUMBER)
883  {
884  PGPROC *nextproc = &allProcs[wakeidx];
885 
886  wakeidx = pg_atomic_read_u32(&nextproc->procArrayGroupNext);
888 
889  /* ensure all previous writes are visible before follower continues. */
891 
892  nextproc->procArrayGroupMember = false;
893 
894  if (nextproc != MyProc)
895  PGSemaphoreUnlock(nextproc->sem);
896  }
897 }
898 
899 /*
900  * ProcArrayClearTransaction -- clear the transaction fields
901  *
902  * This is used after successfully preparing a 2-phase transaction. We are
903  * not actually reporting the transaction's XID as no longer running --- it
904  * will still appear as running because the 2PC's gxact is in the ProcArray
905  * too. We just have to clear out our own PGPROC.
906  */
907 void
909 {
910  int pgxactoff;
911 
912  /*
913  * Currently we need to lock ProcArrayLock exclusively here, as we
914  * increment xactCompletionCount below. We also need it at least in shared
915  * mode for pgproc->pgxactoff to stay the same below.
916  *
917  * We could however, as this action does not actually change anyone's view
918  * of the set of running XIDs (our entry is duplicate with the gxact that
919  * has already been inserted into the ProcArray), lower the lock level to
920  * shared if we were to make xactCompletionCount an atomic variable. But
921  * that doesn't seem worth it currently, as a 2PC commit is heavyweight
922  * enough for this not to be the bottleneck. If it ever becomes a
923  * bottleneck it may also be worth considering to combine this with the
924  * subsequent ProcArrayRemove()
925  */
926  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
927 
928  pgxactoff = proc->pgxactoff;
929 
930  ProcGlobal->xids[pgxactoff] = InvalidTransactionId;
931  proc->xid = InvalidTransactionId;
932 
934  proc->xmin = InvalidTransactionId;
935  proc->recoveryConflictPending = false;
936 
938  Assert(!proc->delayChkptFlags);
939 
940  /*
941  * Need to increment completion count even though transaction hasn't
942  * really committed yet. The reason for that is that GetSnapshotData()
943  * omits the xid of the current transaction, thus without the increment we
944  * otherwise could end up reusing the snapshot later. Which would be bad,
945  * because it might not count the prepared transaction as running.
946  */
948 
949  /* Clear the subtransaction-XID cache too */
950  Assert(ProcGlobal->subxidStates[pgxactoff].count == proc->subxidStatus.count &&
952  if (proc->subxidStatus.count > 0 || proc->subxidStatus.overflowed)
953  {
954  ProcGlobal->subxidStates[pgxactoff].count = 0;
955  ProcGlobal->subxidStates[pgxactoff].overflowed = false;
956  proc->subxidStatus.count = 0;
957  proc->subxidStatus.overflowed = false;
958  }
959 
960  LWLockRelease(ProcArrayLock);
961 }
962 
963 /*
964  * Update TransamVariables->latestCompletedXid to point to latestXid if
965  * currently older.
966  */
967 static void
969 {
971 
972  Assert(FullTransactionIdIsValid(cur_latest));
974  Assert(LWLockHeldByMe(ProcArrayLock));
975 
976  if (TransactionIdPrecedes(XidFromFullTransactionId(cur_latest), latestXid))
977  {
979  FullXidRelativeTo(cur_latest, latestXid);
980  }
981 
984 }
985 
986 /*
987  * Same as MaintainLatestCompletedXid, except for use during WAL replay.
988  */
989 static void
991 {
993  FullTransactionId rel;
994 
996  Assert(LWLockHeldByMe(ProcArrayLock));
997 
998  /*
999  * Need a FullTransactionId to compare latestXid with. Can't rely on
1000  * latestCompletedXid to be initialized in recovery. But in recovery it's
1001  * safe to access nextXid without a lock for the startup process.
1002  */
1003  rel = TransamVariables->nextXid;
1005 
1006  if (!FullTransactionIdIsValid(cur_latest) ||
1007  TransactionIdPrecedes(XidFromFullTransactionId(cur_latest), latestXid))
1008  {
1010  FullXidRelativeTo(rel, latestXid);
1011  }
1012 
1014 }
1015 
1016 /*
1017  * ProcArrayInitRecovery -- initialize recovery xid mgmt environment
1018  *
1019  * Remember up to where the startup process initialized the CLOG and subtrans
1020  * so we can ensure it's initialized gaplessly up to the point where necessary
1021  * while in recovery.
1022  */
1023 void
1025 {
1027  Assert(TransactionIdIsNormal(initializedUptoXID));
1028 
1029  /*
1030  * we set latestObservedXid to the xid SUBTRANS has been initialized up
1031  * to, so we can extend it from that point onwards in
1032  * RecordKnownAssignedTransactionIds, and when we get consistent in
1033  * ProcArrayApplyRecoveryInfo().
1034  */
1035  latestObservedXid = initializedUptoXID;
1037 }
1038 
1039 /*
1040  * ProcArrayApplyRecoveryInfo -- apply recovery info about xids
1041  *
1042  * Takes us through 3 states: Initialized, Pending and Ready.
1043  * Normal case is to go all the way to Ready straight away, though there
1044  * are atypical cases where we need to take it in steps.
1045  *
1046  * Use the data about running transactions on the primary to create the initial
1047  * state of KnownAssignedXids. We also use these records to regularly prune
1048  * KnownAssignedXids because we know it is possible that some transactions
1049  * with FATAL errors fail to write abort records, which could cause eventual
1050  * overflow.
1051  *
1052  * See comments for LogStandbySnapshot().
1053  */
1054 void
1056 {
1057  TransactionId *xids;
1058  TransactionId advanceNextXid;
1059  int nxids;
1060  int i;
1061 
1063  Assert(TransactionIdIsValid(running->nextXid));
1066 
1067  /*
1068  * Remove stale transactions, if any.
1069  */
1071 
1072  /*
1073  * Adjust TransamVariables->nextXid before StandbyReleaseOldLocks(),
1074  * because we will need it up to date for accessing two-phase transactions
1075  * in StandbyReleaseOldLocks().
1076  */
1077  advanceNextXid = running->nextXid;
1078  TransactionIdRetreat(advanceNextXid);
1079  AdvanceNextFullTransactionIdPastXid(advanceNextXid);
1081 
1082  /*
1083  * Remove stale locks, if any.
1084  */
1086 
1087  /*
1088  * If our snapshot is already valid, nothing else to do...
1089  */
1091  return;
1092 
1093  /*
1094  * If our initial RunningTransactionsData had an overflowed snapshot then
1095  * we knew we were missing some subxids from our snapshot. If we continue
1096  * to see overflowed snapshots then we might never be able to start up, so
1097  * we make another test to see if our snapshot is now valid. We know that
1098  * the missing subxids are equal to or earlier than nextXid. After we
1099  * initialise we continue to apply changes during recovery, so once the
1100  * oldestRunningXid is later than the nextXid from the initial snapshot we
1101  * know that we no longer have missing information and can mark the
1102  * snapshot as valid.
1103  */
1105  {
1106  /*
1107  * If the snapshot isn't overflowed or if its empty we can reset our
1108  * pending state and use this snapshot instead.
1109  */
1110  if (!running->subxid_overflow || running->xcnt == 0)
1111  {
1112  /*
1113  * If we have already collected known assigned xids, we need to
1114  * throw them away before we apply the recovery snapshot.
1115  */
1118  }
1119  else
1120  {
1122  running->oldestRunningXid))
1123  {
1125  elog(DEBUG1,
1126  "recovery snapshots are now enabled");
1127  }
1128  else
1129  elog(DEBUG1,
1130  "recovery snapshot waiting for non-overflowed snapshot or "
1131  "until oldest active xid on standby is at least %u (now %u)",
1133  running->oldestRunningXid);
1134  return;
1135  }
1136  }
1137 
1139 
1140  /*
1141  * NB: this can be reached at least twice, so make sure new code can deal
1142  * with that.
1143  */
1144 
1145  /*
1146  * Nobody else is running yet, but take locks anyhow
1147  */
1148  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1149 
1150  /*
1151  * KnownAssignedXids is sorted so we cannot just add the xids, we have to
1152  * sort them first.
1153  *
1154  * Some of the new xids are top-level xids and some are subtransactions.
1155  * We don't call SubTransSetParent because it doesn't matter yet. If we
1156  * aren't overflowed then all xids will fit in snapshot and so we don't
1157  * need subtrans. If we later overflow, an xid assignment record will add
1158  * xids to subtrans. If RunningTransactionsData is overflowed then we
1159  * don't have enough information to correctly update subtrans anyway.
1160  */
1161 
1162  /*
1163  * Allocate a temporary array to avoid modifying the array passed as
1164  * argument.
1165  */
1166  xids = palloc(sizeof(TransactionId) * (running->xcnt + running->subxcnt));
1167 
1168  /*
1169  * Add to the temp array any xids which have not already completed.
1170  */
1171  nxids = 0;
1172  for (i = 0; i < running->xcnt + running->subxcnt; i++)
1173  {
1174  TransactionId xid = running->xids[i];
1175 
1176  /*
1177  * The running-xacts snapshot can contain xids that were still visible
1178  * in the procarray when the snapshot was taken, but were already
1179  * WAL-logged as completed. They're not running anymore, so ignore
1180  * them.
1181  */
1183  continue;
1184 
1185  xids[nxids++] = xid;
1186  }
1187 
1188  if (nxids > 0)
1189  {
1190  if (procArray->numKnownAssignedXids != 0)
1191  {
1192  LWLockRelease(ProcArrayLock);
1193  elog(ERROR, "KnownAssignedXids is not empty");
1194  }
1195 
1196  /*
1197  * Sort the array so that we can add them safely into
1198  * KnownAssignedXids.
1199  *
1200  * We have to sort them logically, because in KnownAssignedXidsAdd we
1201  * call TransactionIdFollowsOrEquals and so on. But we know these XIDs
1202  * come from RUNNING_XACTS, which means there are only normal XIDs
1203  * from the same epoch, so this is safe.
1204  */
1205  qsort(xids, nxids, sizeof(TransactionId), xidLogicalComparator);
1206 
1207  /*
1208  * Add the sorted snapshot into KnownAssignedXids. The running-xacts
1209  * snapshot may include duplicated xids because of prepared
1210  * transactions, so ignore them.
1211  */
1212  for (i = 0; i < nxids; i++)
1213  {
1214  if (i > 0 && TransactionIdEquals(xids[i - 1], xids[i]))
1215  {
1216  elog(DEBUG1,
1217  "found duplicated transaction %u for KnownAssignedXids insertion",
1218  xids[i]);
1219  continue;
1220  }
1221  KnownAssignedXidsAdd(xids[i], xids[i], true);
1222  }
1223 
1225  }
1226 
1227  pfree(xids);
1228 
1229  /*
1230  * latestObservedXid is at least set to the point where SUBTRANS was
1231  * started up to (cf. ProcArrayInitRecovery()) or to the biggest xid
1232  * RecordKnownAssignedTransactionIds() was called for. Initialize
1233  * subtrans from thereon, up to nextXid - 1.
1234  *
1235  * We need to duplicate parts of RecordKnownAssignedTransactionId() here,
1236  * because we've just added xids to the known assigned xids machinery that
1237  * haven't gone through RecordKnownAssignedTransactionId().
1238  */
1242  {
1245  }
1246  TransactionIdRetreat(latestObservedXid); /* = running->nextXid - 1 */
1247 
1248  /* ----------
1249  * Now we've got the running xids we need to set the global values that
1250  * are used to track snapshots as they evolve further.
1251  *
1252  * - latestCompletedXid which will be the xmax for snapshots
1253  * - lastOverflowedXid which shows whether snapshots overflow
1254  * - nextXid
1255  *
1256  * If the snapshot overflowed, then we still initialise with what we know,
1257  * but the recovery snapshot isn't fully valid yet because we know there
1258  * are some subxids missing. We don't know the specific subxids that are
1259  * missing, so conservatively assume the last one is latestObservedXid.
1260  * ----------
1261  */
1262  if (running->subxid_overflow)
1263  {
1265 
1268  }
1269  else
1270  {
1272 
1274  }
1275 
1276  /*
1277  * If a transaction wrote a commit record in the gap between taking and
1278  * logging the snapshot then latestCompletedXid may already be higher than
1279  * the value from the snapshot, so check before we use the incoming value.
1280  * It also might not yet be set at all.
1281  */
1283 
1284  /*
1285  * NB: No need to increment TransamVariables->xactCompletionCount here,
1286  * nobody can see it yet.
1287  */
1288 
1289  LWLockRelease(ProcArrayLock);
1290 
1293  elog(DEBUG1, "recovery snapshots are now enabled");
1294  else
1295  elog(DEBUG1,
1296  "recovery snapshot waiting for non-overflowed snapshot or "
1297  "until oldest active xid on standby is at least %u (now %u)",
1299  running->oldestRunningXid);
1300 }
1301 
1302 /*
1303  * ProcArrayApplyXidAssignment
1304  * Process an XLOG_XACT_ASSIGNMENT WAL record
1305  */
1306 void
1308  int nsubxids, TransactionId *subxids)
1309 {
1310  TransactionId max_xid;
1311  int i;
1312 
1314 
1315  max_xid = TransactionIdLatest(topxid, nsubxids, subxids);
1316 
1317  /*
1318  * Mark all the subtransactions as observed.
1319  *
1320  * NOTE: This will fail if the subxid contains too many previously
1321  * unobserved xids to fit into known-assigned-xids. That shouldn't happen
1322  * as the code stands, because xid-assignment records should never contain
1323  * more than PGPROC_MAX_CACHED_SUBXIDS entries.
1324  */
1326 
1327  /*
1328  * Notice that we update pg_subtrans with the top-level xid, rather than
1329  * the parent xid. This is a difference between normal processing and
1330  * recovery, yet is still correct in all cases. The reason is that
1331  * subtransaction commit is not marked in clog until commit processing, so
1332  * all aborted subtransactions have already been clearly marked in clog.
1333  * As a result we are able to refer directly to the top-level
1334  * transaction's state rather than skipping through all the intermediate
1335  * states in the subtransaction tree. This should be the first time we
1336  * have attempted to SubTransSetParent().
1337  */
1338  for (i = 0; i < nsubxids; i++)
1339  SubTransSetParent(subxids[i], topxid);
1340 
1341  /* KnownAssignedXids isn't maintained yet, so we're done for now */
1343  return;
1344 
1345  /*
1346  * Uses same locking as transaction commit
1347  */
1348  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1349 
1350  /*
1351  * Remove subxids from known-assigned-xacts.
1352  */
1354 
1355  /*
1356  * Advance lastOverflowedXid to be at least the last of these subxids.
1357  */
1359  procArray->lastOverflowedXid = max_xid;
1360 
1361  LWLockRelease(ProcArrayLock);
1362 }
1363 
1364 /*
1365  * TransactionIdIsInProgress -- is given transaction running in some backend
1366  *
1367  * Aside from some shortcuts such as checking RecentXmin and our own Xid,
1368  * there are four possibilities for finding a running transaction:
1369  *
1370  * 1. The given Xid is a main transaction Id. We will find this out cheaply
1371  * by looking at ProcGlobal->xids.
1372  *
1373  * 2. The given Xid is one of the cached subxact Xids in the PGPROC array.
1374  * We can find this out cheaply too.
1375  *
1376  * 3. In Hot Standby mode, we must search the KnownAssignedXids list to see
1377  * if the Xid is running on the primary.
1378  *
1379  * 4. Search the SubTrans tree to find the Xid's topmost parent, and then see
1380  * if that is running according to ProcGlobal->xids[] or KnownAssignedXids.
1381  * This is the slowest way, but sadly it has to be done always if the others
1382  * failed, unless we see that the cached subxact sets are complete (none have
1383  * overflowed).
1384  *
1385  * ProcArrayLock has to be held while we do 1, 2, 3. If we save the top Xids
1386  * while doing 1 and 3, we can release the ProcArrayLock while we do 4.
1387  * This buys back some concurrency (and we can't retrieve the main Xids from
1388  * ProcGlobal->xids[] again anyway; see GetNewTransactionId).
1389  */
1390 bool
1392 {
1393  static TransactionId *xids = NULL;
1394  static TransactionId *other_xids;
1395  XidCacheStatus *other_subxidstates;
1396  int nxids = 0;
1397  ProcArrayStruct *arrayP = procArray;
1398  TransactionId topxid;
1399  TransactionId latestCompletedXid;
1400  int mypgxactoff;
1401  int numProcs;
1402  int j;
1403 
1404  /*
1405  * Don't bother checking a transaction older than RecentXmin; it could not
1406  * possibly still be running. (Note: in particular, this guarantees that
1407  * we reject InvalidTransactionId, FrozenTransactionId, etc as not
1408  * running.)
1409  */
1411  {
1413  return false;
1414  }
1415 
1416  /*
1417  * We may have just checked the status of this transaction, so if it is
1418  * already known to be completed, we can fall out without any access to
1419  * shared memory.
1420  */
1422  {
1424  return false;
1425  }
1426 
1427  /*
1428  * Also, we can handle our own transaction (and subtransactions) without
1429  * any access to shared memory.
1430  */
1432  {
1434  return true;
1435  }
1436 
1437  /*
1438  * If first time through, get workspace to remember main XIDs in. We
1439  * malloc it permanently to avoid repeated palloc/pfree overhead.
1440  */
1441  if (xids == NULL)
1442  {
1443  /*
1444  * In hot standby mode, reserve enough space to hold all xids in the
1445  * known-assigned list. If we later finish recovery, we no longer need
1446  * the bigger array, but we don't bother to shrink it.
1447  */
1448  int maxxids = RecoveryInProgress() ? TOTAL_MAX_CACHED_SUBXIDS : arrayP->maxProcs;
1449 
1450  xids = (TransactionId *) malloc(maxxids * sizeof(TransactionId));
1451  if (xids == NULL)
1452  ereport(ERROR,
1453  (errcode(ERRCODE_OUT_OF_MEMORY),
1454  errmsg("out of memory")));
1455  }
1456 
1457  other_xids = ProcGlobal->xids;
1458  other_subxidstates = ProcGlobal->subxidStates;
1459 
1460  LWLockAcquire(ProcArrayLock, LW_SHARED);
1461 
1462  /*
1463  * Now that we have the lock, we can check latestCompletedXid; if the
1464  * target Xid is after that, it's surely still running.
1465  */
1466  latestCompletedXid =
1468  if (TransactionIdPrecedes(latestCompletedXid, xid))
1469  {
1470  LWLockRelease(ProcArrayLock);
1472  return true;
1473  }
1474 
1475  /* No shortcuts, gotta grovel through the array */
1476  mypgxactoff = MyProc->pgxactoff;
1477  numProcs = arrayP->numProcs;
1478  for (int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
1479  {
1480  int pgprocno;
1481  PGPROC *proc;
1482  TransactionId pxid;
1483  int pxids;
1484 
1485  /* Ignore ourselves --- dealt with it above */
1486  if (pgxactoff == mypgxactoff)
1487  continue;
1488 
1489  /* Fetch xid just once - see GetNewTransactionId */
1490  pxid = UINT32_ACCESS_ONCE(other_xids[pgxactoff]);
1491 
1492  if (!TransactionIdIsValid(pxid))
1493  continue;
1494 
1495  /*
1496  * Step 1: check the main Xid
1497  */
1498  if (TransactionIdEquals(pxid, xid))
1499  {
1500  LWLockRelease(ProcArrayLock);
1502  return true;
1503  }
1504 
1505  /*
1506  * We can ignore main Xids that are younger than the target Xid, since
1507  * the target could not possibly be their child.
1508  */
1509  if (TransactionIdPrecedes(xid, pxid))
1510  continue;
1511 
1512  /*
1513  * Step 2: check the cached child-Xids arrays
1514  */
1515  pxids = other_subxidstates[pgxactoff].count;
1516  pg_read_barrier(); /* pairs with barrier in GetNewTransactionId() */
1517  pgprocno = arrayP->pgprocnos[pgxactoff];
1518  proc = &allProcs[pgprocno];
1519  for (j = pxids - 1; j >= 0; j--)
1520  {
1521  /* Fetch xid just once - see GetNewTransactionId */
1523 
1524  if (TransactionIdEquals(cxid, xid))
1525  {
1526  LWLockRelease(ProcArrayLock);
1528  return true;
1529  }
1530  }
1531 
1532  /*
1533  * Save the main Xid for step 4. We only need to remember main Xids
1534  * that have uncached children. (Note: there is no race condition
1535  * here because the overflowed flag cannot be cleared, only set, while
1536  * we hold ProcArrayLock. So we can't miss an Xid that we need to
1537  * worry about.)
1538  */
1539  if (other_subxidstates[pgxactoff].overflowed)
1540  xids[nxids++] = pxid;
1541  }
1542 
1543  /*
1544  * Step 3: in hot standby mode, check the known-assigned-xids list. XIDs
1545  * in the list must be treated as running.
1546  */
1547  if (RecoveryInProgress())
1548  {
1549  /* none of the PGPROC entries should have XIDs in hot standby mode */
1550  Assert(nxids == 0);
1551 
1552  if (KnownAssignedXidExists(xid))
1553  {
1554  LWLockRelease(ProcArrayLock);
1556  return true;
1557  }
1558 
1559  /*
1560  * If the KnownAssignedXids overflowed, we have to check pg_subtrans
1561  * too. Fetch all xids from KnownAssignedXids that are lower than
1562  * xid, since if xid is a subtransaction its parent will always have a
1563  * lower value. Note we will collect both main and subXIDs here, but
1564  * there's no help for it.
1565  */
1567  nxids = KnownAssignedXidsGet(xids, xid);
1568  }
1569 
1570  LWLockRelease(ProcArrayLock);
1571 
1572  /*
1573  * If none of the relevant caches overflowed, we know the Xid is not
1574  * running without even looking at pg_subtrans.
1575  */
1576  if (nxids == 0)
1577  {
1580  return false;
1581  }
1582 
1583  /*
1584  * Step 4: have to check pg_subtrans.
1585  *
1586  * At this point, we know it's either a subtransaction of one of the Xids
1587  * in xids[], or it's not running. If it's an already-failed
1588  * subtransaction, we want to say "not running" even though its parent may
1589  * still be running. So first, check pg_xact to see if it's been aborted.
1590  */
1592 
1593  if (TransactionIdDidAbort(xid))
1594  {
1596  return false;
1597  }
1598 
1599  /*
1600  * It isn't aborted, so check whether the transaction tree it belongs to
1601  * is still running (or, more precisely, whether it was running when we
1602  * held ProcArrayLock).
1603  */
1604  topxid = SubTransGetTopmostTransaction(xid);
1605  Assert(TransactionIdIsValid(topxid));
1606  if (!TransactionIdEquals(topxid, xid) &&
1607  pg_lfind32(topxid, xids, nxids))
1608  return true;
1609 
1611  return false;
1612 }
1613 
1614 /*
1615  * TransactionIdIsActive -- is xid the top-level XID of an active backend?
1616  *
1617  * This differs from TransactionIdIsInProgress in that it ignores prepared
1618  * transactions, as well as transactions running on the primary if we're in
1619  * hot standby. Also, we ignore subtransactions since that's not needed
1620  * for current uses.
1621  */
1622 bool
1624 {
1625  bool result = false;
1626  ProcArrayStruct *arrayP = procArray;
1627  TransactionId *other_xids = ProcGlobal->xids;
1628  int i;
1629 
1630  /*
1631  * Don't bother checking a transaction older than RecentXmin; it could not
1632  * possibly still be running.
1633  */
1635  return false;
1636 
1637  LWLockAcquire(ProcArrayLock, LW_SHARED);
1638 
1639  for (i = 0; i < arrayP->numProcs; i++)
1640  {
1641  int pgprocno = arrayP->pgprocnos[i];
1642  PGPROC *proc = &allProcs[pgprocno];
1643  TransactionId pxid;
1644 
1645  /* Fetch xid just once - see GetNewTransactionId */
1646  pxid = UINT32_ACCESS_ONCE(other_xids[i]);
1647 
1648  if (!TransactionIdIsValid(pxid))
1649  continue;
1650 
1651  if (proc->pid == 0)
1652  continue; /* ignore prepared transactions */
1653 
1654  if (TransactionIdEquals(pxid, xid))
1655  {
1656  result = true;
1657  break;
1658  }
1659  }
1660 
1661  LWLockRelease(ProcArrayLock);
1662 
1663  return result;
1664 }
1665 
1666 
1667 /*
1668  * Determine XID horizons.
1669  *
1670  * This is used by wrapper functions like GetOldestNonRemovableTransactionId()
1671  * (for VACUUM), GetReplicationHorizons() (for hot_standby_feedback), etc as
1672  * well as "internally" by GlobalVisUpdate() (see comment above struct
1673  * GlobalVisState).
1674  *
1675  * See the definition of ComputeXidHorizonsResult for the various computed
1676  * horizons.
1677  *
1678  * For VACUUM separate horizons (used to decide which deleted tuples must
1679  * be preserved), for shared and non-shared tables are computed. For shared
1680  * relations backends in all databases must be considered, but for non-shared
1681  * relations that's not required, since only backends in my own database could
1682  * ever see the tuples in them. Also, we can ignore concurrently running lazy
1683  * VACUUMs because (a) they must be working on other tables, and (b) they
1684  * don't need to do snapshot-based lookups.
1685  *
1686  * This also computes a horizon used to truncate pg_subtrans. For that
1687  * backends in all databases have to be considered, and concurrently running
1688  * lazy VACUUMs cannot be ignored, as they still may perform pg_subtrans
1689  * accesses.
1690  *
1691  * Note: we include all currently running xids in the set of considered xids.
1692  * This ensures that if a just-started xact has not yet set its snapshot,
1693  * when it does set the snapshot it cannot set xmin less than what we compute.
1694  * See notes in src/backend/access/transam/README.
1695  *
1696  * Note: despite the above, it's possible for the calculated values to move
1697  * backwards on repeated calls. The calculated values are conservative, so
1698  * that anything older is definitely not considered as running by anyone
1699  * anymore, but the exact values calculated depend on a number of things. For
1700  * example, if there are no transactions running in the current database, the
1701  * horizon for normal tables will be latestCompletedXid. If a transaction
1702  * begins after that, its xmin will include in-progress transactions in other
1703  * databases that started earlier, so another call will return a lower value.
1704  * Nonetheless it is safe to vacuum a table in the current database with the
1705  * first result. There are also replication-related effects: a walsender
1706  * process can set its xmin based on transactions that are no longer running
1707  * on the primary but are still being replayed on the standby, thus possibly
1708  * making the values go backwards. In this case there is a possibility that
1709  * we lose data that the standby would like to have, but unless the standby
1710  * uses a replication slot to make its xmin persistent there is little we can
1711  * do about that --- data is only protected if the walsender runs continuously
1712  * while queries are executed on the standby. (The Hot Standby code deals
1713  * with such cases by failing standby queries that needed to access
1714  * already-removed data, so there's no integrity bug.)
1715  *
1716  * Note: the approximate horizons (see definition of GlobalVisState) are
1717  * updated by the computations done here. That's currently required for
1718  * correctness and a small optimization. Without doing so it's possible that
1719  * heap vacuum's call to heap_page_prune() uses a more conservative horizon
1720  * than later when deciding which tuples can be removed - which the code
1721  * doesn't expect (breaking HOT).
1722  */
1723 static void
1725 {
1726  ProcArrayStruct *arrayP = procArray;
1727  TransactionId kaxmin;
1728  bool in_recovery = RecoveryInProgress();
1729  TransactionId *other_xids = ProcGlobal->xids;
1730 
1731  /* inferred after ProcArrayLock is released */
1733 
1734  LWLockAcquire(ProcArrayLock, LW_SHARED);
1735 
1737 
1738  /*
1739  * We initialize the MIN() calculation with latestCompletedXid + 1. This
1740  * is a lower bound for the XIDs that might appear in the ProcArray later,
1741  * and so protects us against overestimating the result due to future
1742  * additions.
1743  */
1744  {
1745  TransactionId initial;
1746 
1748  Assert(TransactionIdIsValid(initial));
1749  TransactionIdAdvance(initial);
1750 
1751  h->oldest_considered_running = initial;
1752  h->shared_oldest_nonremovable = initial;
1753  h->data_oldest_nonremovable = initial;
1754 
1755  /*
1756  * Only modifications made by this backend affect the horizon for
1757  * temporary relations. Instead of a check in each iteration of the
1758  * loop over all PGPROCs it is cheaper to just initialize to the
1759  * current top-level xid any.
1760  *
1761  * Without an assigned xid we could use a horizon as aggressive as
1762  * GetNewTransactionId(), but we can get away with the much cheaper
1763  * latestCompletedXid + 1: If this backend has no xid there, by
1764  * definition, can't be any newer changes in the temp table than
1765  * latestCompletedXid.
1766  */
1769  else
1770  h->temp_oldest_nonremovable = initial;
1771  }
1772 
1773  /*
1774  * Fetch slot horizons while ProcArrayLock is held - the
1775  * LWLockAcquire/LWLockRelease are a barrier, ensuring this happens inside
1776  * the lock.
1777  */
1780 
1781  for (int index = 0; index < arrayP->numProcs; index++)
1782  {
1783  int pgprocno = arrayP->pgprocnos[index];
1784  PGPROC *proc = &allProcs[pgprocno];
1785  int8 statusFlags = ProcGlobal->statusFlags[index];
1786  TransactionId xid;
1787  TransactionId xmin;
1788 
1789  /* Fetch xid just once - see GetNewTransactionId */
1790  xid = UINT32_ACCESS_ONCE(other_xids[index]);
1791  xmin = UINT32_ACCESS_ONCE(proc->xmin);
1792 
1793  /*
1794  * Consider both the transaction's Xmin, and its Xid.
1795  *
1796  * We must check both because a transaction might have an Xmin but not
1797  * (yet) an Xid; conversely, if it has an Xid, that could determine
1798  * some not-yet-set Xmin.
1799  */
1800  xmin = TransactionIdOlder(xmin, xid);
1801 
1802  /* if neither is set, this proc doesn't influence the horizon */
1803  if (!TransactionIdIsValid(xmin))
1804  continue;
1805 
1806  /*
1807  * Don't ignore any procs when determining which transactions might be
1808  * considered running. While slots should ensure logical decoding
1809  * backends are protected even without this check, it can't hurt to
1810  * include them here as well..
1811  */
1814 
1815  /*
1816  * Skip over backends either vacuuming (which is ok with rows being
1817  * removed, as long as pg_subtrans is not truncated) or doing logical
1818  * decoding (which manages xmin separately, check below).
1819  */
1820  if (statusFlags & (PROC_IN_VACUUM | PROC_IN_LOGICAL_DECODING))
1821  continue;
1822 
1823  /* shared tables need to take backends in all databases into account */
1826 
1827  /*
1828  * Normally sessions in other databases are ignored for anything but
1829  * the shared horizon.
1830  *
1831  * However, include them when MyDatabaseId is not (yet) set. A
1832  * backend in the process of starting up must not compute a "too
1833  * aggressive" horizon, otherwise we could end up using it to prune
1834  * still-needed data away. If the current backend never connects to a
1835  * database this is harmless, because data_oldest_nonremovable will
1836  * never be utilized.
1837  *
1838  * Also, sessions marked with PROC_AFFECTS_ALL_HORIZONS should always
1839  * be included. (This flag is used for hot standby feedback, which
1840  * can't be tied to a specific database.)
1841  *
1842  * Also, while in recovery we cannot compute an accurate per-database
1843  * horizon, as all xids are managed via the KnownAssignedXids
1844  * machinery.
1845  */
1846  if (proc->databaseId == MyDatabaseId ||
1847  MyDatabaseId == InvalidOid ||
1848  (statusFlags & PROC_AFFECTS_ALL_HORIZONS) ||
1849  in_recovery)
1850  {
1853  }
1854  }
1855 
1856  /*
1857  * If in recovery fetch oldest xid in KnownAssignedXids, will be applied
1858  * after lock is released.
1859  */
1860  if (in_recovery)
1861  kaxmin = KnownAssignedXidsGetOldestXmin();
1862 
1863  /*
1864  * No other information from shared state is needed, release the lock
1865  * immediately. The rest of the computations can be done without a lock.
1866  */
1867  LWLockRelease(ProcArrayLock);
1868 
1869  if (in_recovery)
1870  {
1877  /* temp relations cannot be accessed in recovery */
1878  }
1879 
1884 
1885  /*
1886  * Check whether there are replication slots requiring an older xmin.
1887  */
1892 
1893  /*
1894  * The only difference between catalog / data horizons is that the slot's
1895  * catalog xmin is applied to the catalog one (so catalogs can be accessed
1896  * for logical decoding). Initialize with data horizon, and then back up
1897  * further if necessary. Have to back up the shared horizon as well, since
1898  * that also can contain catalogs.
1899  */
1903  h->slot_catalog_xmin);
1907  h->slot_catalog_xmin);
1908 
1909  /*
1910  * It's possible that slots backed up the horizons further than
1911  * oldest_considered_running. Fix.
1912  */
1922 
1923  /*
1924  * shared horizons have to be at least as old as the oldest visible in
1925  * current db
1926  */
1931 
1932  /*
1933  * Horizons need to ensure that pg_subtrans access is still possible for
1934  * the relevant backends.
1935  */
1946  h->slot_xmin));
1949  h->slot_catalog_xmin));
1950 
1951  /* update approximate horizons with the computed horizons */
1953 }
1954 
1955 /*
1956  * Determine what kind of visibility horizon needs to be used for a
1957  * relation. If rel is NULL, the most conservative horizon is used.
1958  */
1959 static inline GlobalVisHorizonKind
1961 {
1962  /*
1963  * Other relkinds currently don't contain xids, nor always the necessary
1964  * logical decoding markers.
1965  */
1966  Assert(!rel ||
1967  rel->rd_rel->relkind == RELKIND_RELATION ||
1968  rel->rd_rel->relkind == RELKIND_MATVIEW ||
1969  rel->rd_rel->relkind == RELKIND_TOASTVALUE);
1970 
1971  if (rel == NULL || rel->rd_rel->relisshared || RecoveryInProgress())
1972  return VISHORIZON_SHARED;
1973  else if (IsCatalogRelation(rel) ||
1975  return VISHORIZON_CATALOG;
1976  else if (!RELATION_IS_LOCAL(rel))
1977  return VISHORIZON_DATA;
1978  else
1979  return VISHORIZON_TEMP;
1980 }
1981 
1982 /*
1983  * Return the oldest XID for which deleted tuples must be preserved in the
1984  * passed table.
1985  *
1986  * If rel is not NULL the horizon may be considerably more recent than
1987  * otherwise (i.e. fewer tuples will be removable). In the NULL case a horizon
1988  * that is correct (but not optimal) for all relations will be returned.
1989  *
1990  * This is used by VACUUM to decide which deleted tuples must be preserved in
1991  * the passed in table.
1992  */
1995 {
1996  ComputeXidHorizonsResult horizons;
1997 
1998  ComputeXidHorizons(&horizons);
1999 
2000  switch (GlobalVisHorizonKindForRel(rel))
2001  {
2002  case VISHORIZON_SHARED:
2003  return horizons.shared_oldest_nonremovable;
2004  case VISHORIZON_CATALOG:
2005  return horizons.catalog_oldest_nonremovable;
2006  case VISHORIZON_DATA:
2007  return horizons.data_oldest_nonremovable;
2008  case VISHORIZON_TEMP:
2009  return horizons.temp_oldest_nonremovable;
2010  }
2011 
2012  /* just to prevent compiler warnings */
2013  return InvalidTransactionId;
2014 }
2015 
2016 /*
2017  * Return the oldest transaction id any currently running backend might still
2018  * consider running. This should not be used for visibility / pruning
2019  * determinations (see GetOldestNonRemovableTransactionId()), but for
2020  * decisions like up to where pg_subtrans can be truncated.
2021  */
2024 {
2025  ComputeXidHorizonsResult horizons;
2026 
2027  ComputeXidHorizons(&horizons);
2028 
2029  return horizons.oldest_considered_running;
2030 }
2031 
2032 /*
2033  * Return the visibility horizons for a hot standby feedback message.
2034  */
2035 void
2037 {
2038  ComputeXidHorizonsResult horizons;
2039 
2040  ComputeXidHorizons(&horizons);
2041 
2042  /*
2043  * Don't want to use shared_oldest_nonremovable here, as that contains the
2044  * effect of replication slot's catalog_xmin. We want to send a separate
2045  * feedback for the catalog horizon, so the primary can remove data table
2046  * contents more aggressively.
2047  */
2048  *xmin = horizons.shared_oldest_nonremovable_raw;
2049  *catalog_xmin = horizons.slot_catalog_xmin;
2050 }
2051 
2052 /*
2053  * GetMaxSnapshotXidCount -- get max size for snapshot XID array
2054  *
2055  * We have to export this for use by snapmgr.c.
2056  */
2057 int
2059 {
2060  return procArray->maxProcs;
2061 }
2062 
2063 /*
2064  * GetMaxSnapshotSubxidCount -- get max size for snapshot sub-XID array
2065  *
2066  * We have to export this for use by snapmgr.c.
2067  */
2068 int
2070 {
2071  return TOTAL_MAX_CACHED_SUBXIDS;
2072 }
2073 
2074 /*
2075  * Helper function for GetSnapshotData() that checks if the bulk of the
2076  * visibility information in the snapshot is still valid. If so, it updates
2077  * the fields that need to change and returns true. Otherwise it returns
2078  * false.
2079  *
2080  * This very likely can be evolved to not need ProcArrayLock held (at very
2081  * least in the case we already hold a snapshot), but that's for another day.
2082  */
2083 static bool
2085 {
2086  uint64 curXactCompletionCount;
2087 
2088  Assert(LWLockHeldByMe(ProcArrayLock));
2089 
2090  if (unlikely(snapshot->snapXactCompletionCount == 0))
2091  return false;
2092 
2093  curXactCompletionCount = TransamVariables->xactCompletionCount;
2094  if (curXactCompletionCount != snapshot->snapXactCompletionCount)
2095  return false;
2096 
2097  /*
2098  * If the current xactCompletionCount is still the same as it was at the
2099  * time the snapshot was built, we can be sure that rebuilding the
2100  * contents of the snapshot the hard way would result in the same snapshot
2101  * contents:
2102  *
2103  * As explained in transam/README, the set of xids considered running by
2104  * GetSnapshotData() cannot change while ProcArrayLock is held. Snapshot
2105  * contents only depend on transactions with xids and xactCompletionCount
2106  * is incremented whenever a transaction with an xid finishes (while
2107  * holding ProcArrayLock exclusively). Thus the xactCompletionCount check
2108  * ensures we would detect if the snapshot would have changed.
2109  *
2110  * As the snapshot contents are the same as it was before, it is safe to
2111  * re-enter the snapshot's xmin into the PGPROC array. None of the rows
2112  * visible under the snapshot could already have been removed (that'd
2113  * require the set of running transactions to change) and it fulfills the
2114  * requirement that concurrent GetSnapshotData() calls yield the same
2115  * xmin.
2116  */
2118  MyProc->xmin = TransactionXmin = snapshot->xmin;
2119 
2120  RecentXmin = snapshot->xmin;
2122 
2123  snapshot->curcid = GetCurrentCommandId(false);
2124  snapshot->active_count = 0;
2125  snapshot->regd_count = 0;
2126  snapshot->copied = false;
2127  snapshot->lsn = InvalidXLogRecPtr;
2128  snapshot->whenTaken = 0;
2129 
2130  return true;
2131 }
2132 
2133 /*
2134  * GetSnapshotData -- returns information about running transactions.
2135  *
2136  * The returned snapshot includes xmin (lowest still-running xact ID),
2137  * xmax (highest completed xact ID + 1), and a list of running xact IDs
2138  * in the range xmin <= xid < xmax. It is used as follows:
2139  * All xact IDs < xmin are considered finished.
2140  * All xact IDs >= xmax are considered still running.
2141  * For an xact ID xmin <= xid < xmax, consult list to see whether
2142  * it is considered running or not.
2143  * This ensures that the set of transactions seen as "running" by the
2144  * current xact will not change after it takes the snapshot.
2145  *
2146  * All running top-level XIDs are included in the snapshot, except for lazy
2147  * VACUUM processes. We also try to include running subtransaction XIDs,
2148  * but since PGPROC has only a limited cache area for subxact XIDs, full
2149  * information may not be available. If we find any overflowed subxid arrays,
2150  * we have to mark the snapshot's subxid data as overflowed, and extra work
2151  * *may* need to be done to determine what's running (see XidInMVCCSnapshot()).
2152  *
2153  * We also update the following backend-global variables:
2154  * TransactionXmin: the oldest xmin of any snapshot in use in the
2155  * current transaction (this is the same as MyProc->xmin).
2156  * RecentXmin: the xmin computed for the most recent snapshot. XIDs
2157  * older than this are known not running any more.
2158  *
2159  * And try to advance the bounds of GlobalVis{Shared,Catalog,Data,Temp}Rels
2160  * for the benefit of the GlobalVisTest* family of functions.
2161  *
2162  * Note: this function should probably not be called with an argument that's
2163  * not statically allocated (see xip allocation below).
2164  */
2165 Snapshot
2167 {
2168  ProcArrayStruct *arrayP = procArray;
2169  TransactionId *other_xids = ProcGlobal->xids;
2170  TransactionId xmin;
2171  TransactionId xmax;
2172  int count = 0;
2173  int subcount = 0;
2174  bool suboverflowed = false;
2175  FullTransactionId latest_completed;
2176  TransactionId oldestxid;
2177  int mypgxactoff;
2178  TransactionId myxid;
2179  uint64 curXactCompletionCount;
2180 
2181  TransactionId replication_slot_xmin = InvalidTransactionId;
2182  TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
2183 
2184  Assert(snapshot != NULL);
2185 
2186  /*
2187  * Allocating space for maxProcs xids is usually overkill; numProcs would
2188  * be sufficient. But it seems better to do the malloc while not holding
2189  * the lock, so we can't look at numProcs. Likewise, we allocate much
2190  * more subxip storage than is probably needed.
2191  *
2192  * This does open a possibility for avoiding repeated malloc/free: since
2193  * maxProcs does not change at runtime, we can simply reuse the previous
2194  * xip arrays if any. (This relies on the fact that all callers pass
2195  * static SnapshotData structs.)
2196  */
2197  if (snapshot->xip == NULL)
2198  {
2199  /*
2200  * First call for this snapshot. Snapshot is same size whether or not
2201  * we are in recovery, see later comments.
2202  */
2203  snapshot->xip = (TransactionId *)
2205  if (snapshot->xip == NULL)
2206  ereport(ERROR,
2207  (errcode(ERRCODE_OUT_OF_MEMORY),
2208  errmsg("out of memory")));
2209  Assert(snapshot->subxip == NULL);
2210  snapshot->subxip = (TransactionId *)
2212  if (snapshot->subxip == NULL)
2213  ereport(ERROR,
2214  (errcode(ERRCODE_OUT_OF_MEMORY),
2215  errmsg("out of memory")));
2216  }
2217 
2218  /*
2219  * It is sufficient to get shared lock on ProcArrayLock, even if we are
2220  * going to set MyProc->xmin.
2221  */
2222  LWLockAcquire(ProcArrayLock, LW_SHARED);
2223 
2224  if (GetSnapshotDataReuse(snapshot))
2225  {
2226  LWLockRelease(ProcArrayLock);
2227  return snapshot;
2228  }
2229 
2230  latest_completed = TransamVariables->latestCompletedXid;
2231  mypgxactoff = MyProc->pgxactoff;
2232  myxid = other_xids[mypgxactoff];
2233  Assert(myxid == MyProc->xid);
2234 
2235  oldestxid = TransamVariables->oldestXid;
2236  curXactCompletionCount = TransamVariables->xactCompletionCount;
2237 
2238  /* xmax is always latestCompletedXid + 1 */
2239  xmax = XidFromFullTransactionId(latest_completed);
2240  TransactionIdAdvance(xmax);
2242 
2243  /* initialize xmin calculation with xmax */
2244  xmin = xmax;
2245 
2246  /* take own xid into account, saves a check inside the loop */
2247  if (TransactionIdIsNormal(myxid) && NormalTransactionIdPrecedes(myxid, xmin))
2248  xmin = myxid;
2249 
2251 
2252  if (!snapshot->takenDuringRecovery)
2253  {
2254  int numProcs = arrayP->numProcs;
2255  TransactionId *xip = snapshot->xip;
2256  int *pgprocnos = arrayP->pgprocnos;
2257  XidCacheStatus *subxidStates = ProcGlobal->subxidStates;
2258  uint8 *allStatusFlags = ProcGlobal->statusFlags;
2259 
2260  /*
2261  * First collect set of pgxactoff/xids that need to be included in the
2262  * snapshot.
2263  */
2264  for (int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
2265  {
2266  /* Fetch xid just once - see GetNewTransactionId */
2267  TransactionId xid = UINT32_ACCESS_ONCE(other_xids[pgxactoff]);
2268  uint8 statusFlags;
2269 
2270  Assert(allProcs[arrayP->pgprocnos[pgxactoff]].pgxactoff == pgxactoff);
2271 
2272  /*
2273  * If the transaction has no XID assigned, we can skip it; it
2274  * won't have sub-XIDs either.
2275  */
2276  if (likely(xid == InvalidTransactionId))
2277  continue;
2278 
2279  /*
2280  * We don't include our own XIDs (if any) in the snapshot. It
2281  * needs to be included in the xmin computation, but we did so
2282  * outside the loop.
2283  */
2284  if (pgxactoff == mypgxactoff)
2285  continue;
2286 
2287  /*
2288  * The only way we are able to get here with a non-normal xid is
2289  * during bootstrap - with this backend using
2290  * BootstrapTransactionId. But the above test should filter that
2291  * out.
2292  */
2294 
2295  /*
2296  * If the XID is >= xmax, we can skip it; such transactions will
2297  * be treated as running anyway (and any sub-XIDs will also be >=
2298  * xmax).
2299  */
2300  if (!NormalTransactionIdPrecedes(xid, xmax))
2301  continue;
2302 
2303  /*
2304  * Skip over backends doing logical decoding which manages xmin
2305  * separately (check below) and ones running LAZY VACUUM.
2306  */
2307  statusFlags = allStatusFlags[pgxactoff];
2308  if (statusFlags & (PROC_IN_LOGICAL_DECODING | PROC_IN_VACUUM))
2309  continue;
2310 
2311  if (NormalTransactionIdPrecedes(xid, xmin))
2312  xmin = xid;
2313 
2314  /* Add XID to snapshot. */
2315  xip[count++] = xid;
2316 
2317  /*
2318  * Save subtransaction XIDs if possible (if we've already
2319  * overflowed, there's no point). Note that the subxact XIDs must
2320  * be later than their parent, so no need to check them against
2321  * xmin. We could filter against xmax, but it seems better not to
2322  * do that much work while holding the ProcArrayLock.
2323  *
2324  * The other backend can add more subxids concurrently, but cannot
2325  * remove any. Hence it's important to fetch nxids just once.
2326  * Should be safe to use memcpy, though. (We needn't worry about
2327  * missing any xids added concurrently, because they must postdate
2328  * xmax.)
2329  *
2330  * Again, our own XIDs are not included in the snapshot.
2331  */
2332  if (!suboverflowed)
2333  {
2334 
2335  if (subxidStates[pgxactoff].overflowed)
2336  suboverflowed = true;
2337  else
2338  {
2339  int nsubxids = subxidStates[pgxactoff].count;
2340 
2341  if (nsubxids > 0)
2342  {
2343  int pgprocno = pgprocnos[pgxactoff];
2344  PGPROC *proc = &allProcs[pgprocno];
2345 
2346  pg_read_barrier(); /* pairs with GetNewTransactionId */
2347 
2348  memcpy(snapshot->subxip + subcount,
2349  proc->subxids.xids,
2350  nsubxids * sizeof(TransactionId));
2351  subcount += nsubxids;
2352  }
2353  }
2354  }
2355  }
2356  }
2357  else
2358  {
2359  /*
2360  * We're in hot standby, so get XIDs from KnownAssignedXids.
2361  *
2362  * We store all xids directly into subxip[]. Here's why:
2363  *
2364  * In recovery we don't know which xids are top-level and which are
2365  * subxacts, a design choice that greatly simplifies xid processing.
2366  *
2367  * It seems like we would want to try to put xids into xip[] only, but
2368  * that is fairly small. We would either need to make that bigger or
2369  * to increase the rate at which we WAL-log xid assignment; neither is
2370  * an appealing choice.
2371  *
2372  * We could try to store xids into xip[] first and then into subxip[]
2373  * if there are too many xids. That only works if the snapshot doesn't
2374  * overflow because we do not search subxip[] in that case. A simpler
2375  * way is to just store all xids in the subxip array because this is
2376  * by far the bigger array. We just leave the xip array empty.
2377  *
2378  * Either way we need to change the way XidInMVCCSnapshot() works
2379  * depending upon when the snapshot was taken, or change normal
2380  * snapshot processing so it matches.
2381  *
2382  * Note: It is possible for recovery to end before we finish taking
2383  * the snapshot, and for newly assigned transaction ids to be added to
2384  * the ProcArray. xmax cannot change while we hold ProcArrayLock, so
2385  * those newly added transaction ids would be filtered away, so we
2386  * need not be concerned about them.
2387  */
2388  subcount = KnownAssignedXidsGetAndSetXmin(snapshot->subxip, &xmin,
2389  xmax);
2390 
2392  suboverflowed = true;
2393  }
2394 
2395 
2396  /*
2397  * Fetch into local variable while ProcArrayLock is held - the
2398  * LWLockRelease below is a barrier, ensuring this happens inside the
2399  * lock.
2400  */
2401  replication_slot_xmin = procArray->replication_slot_xmin;
2402  replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
2403 
2405  MyProc->xmin = TransactionXmin = xmin;
2406 
2407  LWLockRelease(ProcArrayLock);
2408 
2409  /* maintain state for GlobalVis* */
2410  {
2411  TransactionId def_vis_xid;
2412  TransactionId def_vis_xid_data;
2413  FullTransactionId def_vis_fxid;
2414  FullTransactionId def_vis_fxid_data;
2415  FullTransactionId oldestfxid;
2416 
2417  /*
2418  * Converting oldestXid is only safe when xid horizon cannot advance,
2419  * i.e. holding locks. While we don't hold the lock anymore, all the
2420  * necessary data has been gathered with lock held.
2421  */
2422  oldestfxid = FullXidRelativeTo(latest_completed, oldestxid);
2423 
2424  /* Check whether there's a replication slot requiring an older xmin. */
2425  def_vis_xid_data =
2426  TransactionIdOlder(xmin, replication_slot_xmin);
2427 
2428  /*
2429  * Rows in non-shared, non-catalog tables possibly could be vacuumed
2430  * if older than this xid.
2431  */
2432  def_vis_xid = def_vis_xid_data;
2433 
2434  /*
2435  * Check whether there's a replication slot requiring an older catalog
2436  * xmin.
2437  */
2438  def_vis_xid =
2439  TransactionIdOlder(replication_slot_catalog_xmin, def_vis_xid);
2440 
2441  def_vis_fxid = FullXidRelativeTo(latest_completed, def_vis_xid);
2442  def_vis_fxid_data = FullXidRelativeTo(latest_completed, def_vis_xid_data);
2443 
2444  /*
2445  * Check if we can increase upper bound. As a previous
2446  * GlobalVisUpdate() might have computed more aggressive values, don't
2447  * overwrite them if so.
2448  */
2450  FullTransactionIdNewer(def_vis_fxid,
2453  FullTransactionIdNewer(def_vis_fxid,
2456  FullTransactionIdNewer(def_vis_fxid_data,
2458  /* See temp_oldest_nonremovable computation in ComputeXidHorizons() */
2459  if (TransactionIdIsNormal(myxid))
2461  FullXidRelativeTo(latest_completed, myxid);
2462  else
2463  {
2464  GlobalVisTempRels.definitely_needed = latest_completed;
2466  }
2467 
2468  /*
2469  * Check if we know that we can initialize or increase the lower
2470  * bound. Currently the only cheap way to do so is to use
2471  * TransamVariables->oldestXid as input.
2472  *
2473  * We should definitely be able to do better. We could e.g. put a
2474  * global lower bound value into TransamVariables.
2475  */
2478  oldestfxid);
2481  oldestfxid);
2484  oldestfxid);
2485  /* accurate value known */
2487  }
2488 
2489  RecentXmin = xmin;
2491 
2492  snapshot->xmin = xmin;
2493  snapshot->xmax = xmax;
2494  snapshot->xcnt = count;
2495  snapshot->subxcnt = subcount;
2496  snapshot->suboverflowed = suboverflowed;
2497  snapshot->snapXactCompletionCount = curXactCompletionCount;
2498 
2499  snapshot->curcid = GetCurrentCommandId(false);
2500 
2501  /*
2502  * This is a new snapshot, so set both refcounts are zero, and mark it as
2503  * not copied in persistent memory.
2504  */
2505  snapshot->active_count = 0;
2506  snapshot->regd_count = 0;
2507  snapshot->copied = false;
2508  snapshot->lsn = InvalidXLogRecPtr;
2509  snapshot->whenTaken = 0;
2510 
2511  return snapshot;
2512 }
2513 
2514 /*
2515  * ProcArrayInstallImportedXmin -- install imported xmin into MyProc->xmin
2516  *
2517  * This is called when installing a snapshot imported from another
2518  * transaction. To ensure that OldestXmin doesn't go backwards, we must
2519  * check that the source transaction is still running, and we'd better do
2520  * that atomically with installing the new xmin.
2521  *
2522  * Returns true if successful, false if source xact is no longer running.
2523  */
2524 bool
2526  VirtualTransactionId *sourcevxid)
2527 {
2528  bool result = false;
2529  ProcArrayStruct *arrayP = procArray;
2530  int index;
2531 
2533  if (!sourcevxid)
2534  return false;
2535 
2536  /* Get lock so source xact can't end while we're doing this */
2537  LWLockAcquire(ProcArrayLock, LW_SHARED);
2538 
2539  /*
2540  * Find the PGPROC entry of the source transaction. (This could use
2541  * GetPGProcByNumber(), unless it's a prepared xact. But this isn't
2542  * performance critical.)
2543  */
2544  for (index = 0; index < arrayP->numProcs; index++)
2545  {
2546  int pgprocno = arrayP->pgprocnos[index];
2547  PGPROC *proc = &allProcs[pgprocno];
2548  int statusFlags = ProcGlobal->statusFlags[index];
2549  TransactionId xid;
2550 
2551  /* Ignore procs running LAZY VACUUM */
2552  if (statusFlags & PROC_IN_VACUUM)
2553  continue;
2554 
2555  /* We are only interested in the specific virtual transaction. */
2556  if (proc->vxid.procNumber != sourcevxid->procNumber)
2557  continue;
2558  if (proc->vxid.lxid != sourcevxid->localTransactionId)
2559  continue;
2560 
2561  /*
2562  * We check the transaction's database ID for paranoia's sake: if it's
2563  * in another DB then its xmin does not cover us. Caller should have
2564  * detected this already, so we just treat any funny cases as
2565  * "transaction not found".
2566  */
2567  if (proc->databaseId != MyDatabaseId)
2568  continue;
2569 
2570  /*
2571  * Likewise, let's just make real sure its xmin does cover us.
2572  */
2573  xid = UINT32_ACCESS_ONCE(proc->xmin);
2574  if (!TransactionIdIsNormal(xid) ||
2575  !TransactionIdPrecedesOrEquals(xid, xmin))
2576  continue;
2577 
2578  /*
2579  * We're good. Install the new xmin. As in GetSnapshotData, set
2580  * TransactionXmin too. (Note that because snapmgr.c called
2581  * GetSnapshotData first, we'll be overwriting a valid xmin here, so
2582  * we don't check that.)
2583  */
2584  MyProc->xmin = TransactionXmin = xmin;
2585 
2586  result = true;
2587  break;
2588  }
2589 
2590  LWLockRelease(ProcArrayLock);
2591 
2592  return result;
2593 }
2594 
2595 /*
2596  * ProcArrayInstallRestoredXmin -- install restored xmin into MyProc->xmin
2597  *
2598  * This is like ProcArrayInstallImportedXmin, but we have a pointer to the
2599  * PGPROC of the transaction from which we imported the snapshot, rather than
2600  * an XID.
2601  *
2602  * Note that this function also copies statusFlags from the source `proc` in
2603  * order to avoid the case where MyProc's xmin needs to be skipped for
2604  * computing xid horizon.
2605  *
2606  * Returns true if successful, false if source xact is no longer running.
2607  */
2608 bool
2610 {
2611  bool result = false;
2612  TransactionId xid;
2613 
2615  Assert(proc != NULL);
2616 
2617  /*
2618  * Get an exclusive lock so that we can copy statusFlags from source proc.
2619  */
2620  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2621 
2622  /*
2623  * Be certain that the referenced PGPROC has an advertised xmin which is
2624  * no later than the one we're installing, so that the system-wide xmin
2625  * can't go backwards. Also, make sure it's running in the same database,
2626  * so that the per-database xmin cannot go backwards.
2627  */
2628  xid = UINT32_ACCESS_ONCE(proc->xmin);
2629  if (proc->databaseId == MyDatabaseId &&
2630  TransactionIdIsNormal(xid) &&
2631  TransactionIdPrecedesOrEquals(xid, xmin))
2632  {
2633  /*
2634  * Install xmin and propagate the statusFlags that affect how the
2635  * value is interpreted by vacuum.
2636  */
2637  MyProc->xmin = TransactionXmin = xmin;
2639  (proc->statusFlags & PROC_XMIN_FLAGS);
2641 
2642  result = true;
2643  }
2644 
2645  LWLockRelease(ProcArrayLock);
2646 
2647  return result;
2648 }
2649 
2650 /*
2651  * GetRunningTransactionData -- returns information about running transactions.
2652  *
2653  * Similar to GetSnapshotData but returns more information. We include
2654  * all PGPROCs with an assigned TransactionId, even VACUUM processes and
2655  * prepared transactions.
2656  *
2657  * We acquire XidGenLock and ProcArrayLock, but the caller is responsible for
2658  * releasing them. Acquiring XidGenLock ensures that no new XIDs enter the proc
2659  * array until the caller has WAL-logged this snapshot, and releases the
2660  * lock. Acquiring ProcArrayLock ensures that no transactions commit until the
2661  * lock is released.
2662  *
2663  * The returned data structure is statically allocated; caller should not
2664  * modify it, and must not assume it is valid past the next call.
2665  *
2666  * This is never executed during recovery so there is no need to look at
2667  * KnownAssignedXids.
2668  *
2669  * Dummy PGPROCs from prepared transaction are included, meaning that this
2670  * may return entries with duplicated TransactionId values coming from
2671  * transaction finishing to prepare. Nothing is done about duplicated
2672  * entries here to not hold on ProcArrayLock more than necessary.
2673  *
2674  * We don't worry about updating other counters, we want to keep this as
2675  * simple as possible and leave GetSnapshotData() as the primary code for
2676  * that bookkeeping.
2677  *
2678  * Note that if any transaction has overflowed its cached subtransactions
2679  * then there is no real need include any subtransactions.
2680  */
2683 {
2684  /* result workspace */
2685  static RunningTransactionsData CurrentRunningXactsData;
2686 
2687  ProcArrayStruct *arrayP = procArray;
2688  TransactionId *other_xids = ProcGlobal->xids;
2689  RunningTransactions CurrentRunningXacts = &CurrentRunningXactsData;
2690  TransactionId latestCompletedXid;
2691  TransactionId oldestRunningXid;
2692  TransactionId *xids;
2693  int index;
2694  int count;
2695  int subcount;
2696  bool suboverflowed;
2697 
2699 
2700  /*
2701  * Allocating space for maxProcs xids is usually overkill; numProcs would
2702  * be sufficient. But it seems better to do the malloc while not holding
2703  * the lock, so we can't look at numProcs. Likewise, we allocate much
2704  * more subxip storage than is probably needed.
2705  *
2706  * Should only be allocated in bgwriter, since only ever executed during
2707  * checkpoints.
2708  */
2709  if (CurrentRunningXacts->xids == NULL)
2710  {
2711  /*
2712  * First call
2713  */
2714  CurrentRunningXacts->xids = (TransactionId *)
2716  if (CurrentRunningXacts->xids == NULL)
2717  ereport(ERROR,
2718  (errcode(ERRCODE_OUT_OF_MEMORY),
2719  errmsg("out of memory")));
2720  }
2721 
2722  xids = CurrentRunningXacts->xids;
2723 
2724  count = subcount = 0;
2725  suboverflowed = false;
2726 
2727  /*
2728  * Ensure that no xids enter or leave the procarray while we obtain
2729  * snapshot.
2730  */
2731  LWLockAcquire(ProcArrayLock, LW_SHARED);
2732  LWLockAcquire(XidGenLock, LW_SHARED);
2733 
2734  latestCompletedXid =
2736  oldestRunningXid =
2738 
2739  /*
2740  * Spin over procArray collecting all xids
2741  */
2742  for (index = 0; index < arrayP->numProcs; index++)
2743  {
2744  TransactionId xid;
2745 
2746  /* Fetch xid just once - see GetNewTransactionId */
2747  xid = UINT32_ACCESS_ONCE(other_xids[index]);
2748 
2749  /*
2750  * We don't need to store transactions that don't have a TransactionId
2751  * yet because they will not show as running on a standby server.
2752  */
2753  if (!TransactionIdIsValid(xid))
2754  continue;
2755 
2756  /*
2757  * Be careful not to exclude any xids before calculating the values of
2758  * oldestRunningXid and suboverflowed, since these are used to clean
2759  * up transaction information held on standbys.
2760  */
2761  if (TransactionIdPrecedes(xid, oldestRunningXid))
2762  oldestRunningXid = xid;
2763 
2765  suboverflowed = true;
2766 
2767  /*
2768  * If we wished to exclude xids this would be the right place for it.
2769  * Procs with the PROC_IN_VACUUM flag set don't usually assign xids,
2770  * but they do during truncation at the end when they get the lock and
2771  * truncate, so it is not much of a problem to include them if they
2772  * are seen and it is cleaner to include them.
2773  */
2774 
2775  xids[count++] = xid;
2776  }
2777 
2778  /*
2779  * Spin over procArray collecting all subxids, but only if there hasn't
2780  * been a suboverflow.
2781  */
2782  if (!suboverflowed)
2783  {
2784  XidCacheStatus *other_subxidstates = ProcGlobal->subxidStates;
2785 
2786  for (index = 0; index < arrayP->numProcs; index++)
2787  {
2788  int pgprocno = arrayP->pgprocnos[index];
2789  PGPROC *proc = &allProcs[pgprocno];
2790  int nsubxids;
2791 
2792  /*
2793  * Save subtransaction XIDs. Other backends can't add or remove
2794  * entries while we're holding XidGenLock.
2795  */
2796  nsubxids = other_subxidstates[index].count;
2797  if (nsubxids > 0)
2798  {
2799  /* barrier not really required, as XidGenLock is held, but ... */
2800  pg_read_barrier(); /* pairs with GetNewTransactionId */
2801 
2802  memcpy(&xids[count], proc->subxids.xids,
2803  nsubxids * sizeof(TransactionId));
2804  count += nsubxids;
2805  subcount += nsubxids;
2806 
2807  /*
2808  * Top-level XID of a transaction is always less than any of
2809  * its subxids, so we don't need to check if any of the
2810  * subxids are smaller than oldestRunningXid
2811  */
2812  }
2813  }
2814  }
2815 
2816  /*
2817  * It's important *not* to include the limits set by slots here because
2818  * snapbuild.c uses oldestRunningXid to manage its xmin horizon. If those
2819  * were to be included here the initial value could never increase because
2820  * of a circular dependency where slots only increase their limits when
2821  * running xacts increases oldestRunningXid and running xacts only
2822  * increases if slots do.
2823  */
2824 
2825  CurrentRunningXacts->xcnt = count - subcount;
2826  CurrentRunningXacts->subxcnt = subcount;
2827  CurrentRunningXacts->subxid_overflow = suboverflowed;
2828  CurrentRunningXacts->nextXid = XidFromFullTransactionId(TransamVariables->nextXid);
2829  CurrentRunningXacts->oldestRunningXid = oldestRunningXid;
2830  CurrentRunningXacts->latestCompletedXid = latestCompletedXid;
2831 
2832  Assert(TransactionIdIsValid(CurrentRunningXacts->nextXid));
2833  Assert(TransactionIdIsValid(CurrentRunningXacts->oldestRunningXid));
2834  Assert(TransactionIdIsNormal(CurrentRunningXacts->latestCompletedXid));
2835 
2836  /* We don't release the locks here, the caller is responsible for that */
2837 
2838  return CurrentRunningXacts;
2839 }
2840 
2841 /*
2842  * GetOldestActiveTransactionId()
2843  *
2844  * Similar to GetSnapshotData but returns just oldestActiveXid. We include
2845  * all PGPROCs with an assigned TransactionId, even VACUUM processes.
2846  * We look at all databases, though there is no need to include WALSender
2847  * since this has no effect on hot standby conflicts.
2848  *
2849  * This is never executed during recovery so there is no need to look at
2850  * KnownAssignedXids.
2851  *
2852  * We don't worry about updating other counters, we want to keep this as
2853  * simple as possible and leave GetSnapshotData() as the primary code for
2854  * that bookkeeping.
2855  */
2858 {
2859  ProcArrayStruct *arrayP = procArray;
2860  TransactionId *other_xids = ProcGlobal->xids;
2861  TransactionId oldestRunningXid;
2862  int index;
2863 
2865 
2866  /*
2867  * Read nextXid, as the upper bound of what's still active.
2868  *
2869  * Reading a TransactionId is atomic, but we must grab the lock to make
2870  * sure that all XIDs < nextXid are already present in the proc array (or
2871  * have already completed), when we spin over it.
2872  */
2873  LWLockAcquire(XidGenLock, LW_SHARED);
2874  oldestRunningXid = XidFromFullTransactionId(TransamVariables->nextXid);
2875  LWLockRelease(XidGenLock);
2876 
2877  /*
2878  * Spin over procArray collecting all xids and subxids.
2879  */
2880  LWLockAcquire(ProcArrayLock, LW_SHARED);
2881  for (index = 0; index < arrayP->numProcs; index++)
2882  {
2883  TransactionId xid;
2884 
2885  /* Fetch xid just once - see GetNewTransactionId */
2886  xid = UINT32_ACCESS_ONCE(other_xids[index]);
2887 
2888  if (!TransactionIdIsNormal(xid))
2889  continue;
2890 
2891  if (TransactionIdPrecedes(xid, oldestRunningXid))
2892  oldestRunningXid = xid;
2893 
2894  /*
2895  * Top-level XID of a transaction is always less than any of its
2896  * subxids, so we don't need to check if any of the subxids are
2897  * smaller than oldestRunningXid
2898  */
2899  }
2900  LWLockRelease(ProcArrayLock);
2901 
2902  return oldestRunningXid;
2903 }
2904 
2905 /*
2906  * GetOldestSafeDecodingTransactionId -- lowest xid not affected by vacuum
2907  *
2908  * Returns the oldest xid that we can guarantee not to have been affected by
2909  * vacuum, i.e. no rows >= that xid have been vacuumed away unless the
2910  * transaction aborted. Note that the value can (and most of the time will) be
2911  * much more conservative than what really has been affected by vacuum, but we
2912  * currently don't have better data available.
2913  *
2914  * This is useful to initialize the cutoff xid after which a new changeset
2915  * extraction replication slot can start decoding changes.
2916  *
2917  * Must be called with ProcArrayLock held either shared or exclusively,
2918  * although most callers will want to use exclusive mode since it is expected
2919  * that the caller will immediately use the xid to peg the xmin horizon.
2920  */
2923 {
2924  ProcArrayStruct *arrayP = procArray;
2925  TransactionId oldestSafeXid;
2926  int index;
2927  bool recovery_in_progress = RecoveryInProgress();
2928 
2929  Assert(LWLockHeldByMe(ProcArrayLock));
2930 
2931  /*
2932  * Acquire XidGenLock, so no transactions can acquire an xid while we're
2933  * running. If no transaction with xid were running concurrently a new xid
2934  * could influence the RecentXmin et al.
2935  *
2936  * We initialize the computation to nextXid since that's guaranteed to be
2937  * a safe, albeit pessimal, value.
2938  */
2939  LWLockAcquire(XidGenLock, LW_SHARED);
2941 
2942  /*
2943  * If there's already a slot pegging the xmin horizon, we can start with
2944  * that value, it's guaranteed to be safe since it's computed by this
2945  * routine initially and has been enforced since. We can always use the
2946  * slot's general xmin horizon, but the catalog horizon is only usable
2947  * when only catalog data is going to be looked at.
2948  */
2951  oldestSafeXid))
2952  oldestSafeXid = procArray->replication_slot_xmin;
2953 
2954  if (catalogOnly &&
2957  oldestSafeXid))
2958  oldestSafeXid = procArray->replication_slot_catalog_xmin;
2959 
2960  /*
2961  * If we're not in recovery, we walk over the procarray and collect the
2962  * lowest xid. Since we're called with ProcArrayLock held and have
2963  * acquired XidGenLock, no entries can vanish concurrently, since
2964  * ProcGlobal->xids[i] is only set with XidGenLock held and only cleared
2965  * with ProcArrayLock held.
2966  *
2967  * In recovery we can't lower the safe value besides what we've computed
2968  * above, so we'll have to wait a bit longer there. We unfortunately can
2969  * *not* use KnownAssignedXidsGetOldestXmin() since the KnownAssignedXids
2970  * machinery can miss values and return an older value than is safe.
2971  */
2972  if (!recovery_in_progress)
2973  {
2974  TransactionId *other_xids = ProcGlobal->xids;
2975 
2976  /*
2977  * Spin over procArray collecting min(ProcGlobal->xids[i])
2978  */
2979  for (index = 0; index < arrayP->numProcs; index++)
2980  {
2981  TransactionId xid;
2982 
2983  /* Fetch xid just once - see GetNewTransactionId */
2984  xid = UINT32_ACCESS_ONCE(other_xids[index]);
2985 
2986  if (!TransactionIdIsNormal(xid))
2987  continue;
2988 
2989  if (TransactionIdPrecedes(xid, oldestSafeXid))
2990  oldestSafeXid = xid;
2991  }
2992  }
2993 
2994  LWLockRelease(XidGenLock);
2995 
2996  return oldestSafeXid;
2997 }
2998 
2999 /*
3000  * GetVirtualXIDsDelayingChkpt -- Get the VXIDs of transactions that are
3001  * delaying checkpoint because they have critical actions in progress.
3002  *
3003  * Constructs an array of VXIDs of transactions that are currently in commit
3004  * critical sections, as shown by having specified delayChkptFlags bits set
3005  * in their PGPROC.
3006  *
3007  * Returns a palloc'd array that should be freed by the caller.
3008  * *nvxids is the number of valid entries.
3009  *
3010  * Note that because backends set or clear delayChkptFlags without holding any
3011  * lock, the result is somewhat indeterminate, but we don't really care. Even
3012  * in a multiprocessor with delayed writes to shared memory, it should be
3013  * certain that setting of delayChkptFlags will propagate to shared memory
3014  * when the backend takes a lock, so we cannot fail to see a virtual xact as
3015  * delayChkptFlags if it's already inserted its commit record. Whether it
3016  * takes a little while for clearing of delayChkptFlags to propagate is
3017  * unimportant for correctness.
3018  */
3021 {
3022  VirtualTransactionId *vxids;
3023  ProcArrayStruct *arrayP = procArray;
3024  int count = 0;
3025  int index;
3026 
3027  Assert(type != 0);
3028 
3029  /* allocate what's certainly enough result space */
3030  vxids = (VirtualTransactionId *)
3031  palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
3032 
3033  LWLockAcquire(ProcArrayLock, LW_SHARED);
3034 
3035  for (index = 0; index < arrayP->numProcs; index++)
3036  {
3037  int pgprocno = arrayP->pgprocnos[index];
3038  PGPROC *proc = &allProcs[pgprocno];
3039 
3040  if ((proc->delayChkptFlags & type) != 0)
3041  {
3042  VirtualTransactionId vxid;
3043 
3044  GET_VXID_FROM_PGPROC(vxid, *proc);
3045  if (VirtualTransactionIdIsValid(vxid))
3046  vxids[count++] = vxid;
3047  }
3048  }
3049 
3050  LWLockRelease(ProcArrayLock);
3051 
3052  *nvxids = count;
3053  return vxids;
3054 }
3055 
3056 /*
3057  * HaveVirtualXIDsDelayingChkpt -- Are any of the specified VXIDs delaying?
3058  *
3059  * This is used with the results of GetVirtualXIDsDelayingChkpt to see if any
3060  * of the specified VXIDs are still in critical sections of code.
3061  *
3062  * Note: this is O(N^2) in the number of vxacts that are/were delaying, but
3063  * those numbers should be small enough for it not to be a problem.
3064  */
3065 bool
3067 {
3068  bool result = false;
3069  ProcArrayStruct *arrayP = procArray;
3070  int index;
3071 
3072  Assert(type != 0);
3073 
3074  LWLockAcquire(ProcArrayLock, LW_SHARED);
3075 
3076  for (index = 0; index < arrayP->numProcs; index++)
3077  {
3078  int pgprocno = arrayP->pgprocnos[index];
3079  PGPROC *proc = &allProcs[pgprocno];
3080  VirtualTransactionId vxid;
3081 
3082  GET_VXID_FROM_PGPROC(vxid, *proc);
3083 
3084  if ((proc->delayChkptFlags & type) != 0 &&
3086  {
3087  int i;
3088 
3089  for (i = 0; i < nvxids; i++)
3090  {
3091  if (VirtualTransactionIdEquals(vxid, vxids[i]))
3092  {
3093  result = true;
3094  break;
3095  }
3096  }
3097  if (result)
3098  break;
3099  }
3100  }
3101 
3102  LWLockRelease(ProcArrayLock);
3103 
3104  return result;
3105 }
3106 
3107 /*
3108  * ProcNumberGetProc -- get a backend's PGPROC given its proc number
3109  *
3110  * The result may be out of date arbitrarily quickly, so the caller
3111  * must be careful about how this information is used. NULL is
3112  * returned if the backend is not active.
3113  */
3114 PGPROC *
3116 {
3117  PGPROC *result;
3118 
3119  if (procNumber < 0 || procNumber >= ProcGlobal->allProcCount)
3120  return NULL;
3121  result = GetPGProcByNumber(procNumber);
3122 
3123  if (result->pid == 0)
3124  return NULL;
3125 
3126  return result;
3127 }
3128 
3129 /*
3130  * ProcNumberGetTransactionIds -- get a backend's transaction status
3131  *
3132  * Get the xid, xmin, nsubxid and overflow status of the backend. The
3133  * result may be out of date arbitrarily quickly, so the caller must be
3134  * careful about how this information is used.
3135  */
3136 void
3138  TransactionId *xmin, int *nsubxid, bool *overflowed)
3139 {
3140  PGPROC *proc;
3141 
3142  *xid = InvalidTransactionId;
3143  *xmin = InvalidTransactionId;
3144  *nsubxid = 0;
3145  *overflowed = false;
3146 
3147  if (procNumber < 0 || procNumber >= ProcGlobal->allProcCount)
3148  return;
3149  proc = GetPGProcByNumber(procNumber);
3150 
3151  /* Need to lock out additions/removals of backends */
3152  LWLockAcquire(ProcArrayLock, LW_SHARED);
3153 
3154  if (proc->pid != 0)
3155  {
3156  *xid = proc->xid;
3157  *xmin = proc->xmin;
3158  *nsubxid = proc->subxidStatus.count;
3159  *overflowed = proc->subxidStatus.overflowed;
3160  }
3161 
3162  LWLockRelease(ProcArrayLock);
3163 }
3164 
3165 /*
3166  * BackendPidGetProc -- get a backend's PGPROC given its PID
3167  *
3168  * Returns NULL if not found. Note that it is up to the caller to be
3169  * sure that the question remains meaningful for long enough for the
3170  * answer to be used ...
3171  */
3172 PGPROC *
3174 {
3175  PGPROC *result;
3176 
3177  if (pid == 0) /* never match dummy PGPROCs */
3178  return NULL;
3179 
3180  LWLockAcquire(ProcArrayLock, LW_SHARED);
3181 
3182  result = BackendPidGetProcWithLock(pid);
3183 
3184  LWLockRelease(ProcArrayLock);
3185 
3186  return result;
3187 }
3188 
3189 /*
3190  * BackendPidGetProcWithLock -- get a backend's PGPROC given its PID
3191  *
3192  * Same as above, except caller must be holding ProcArrayLock. The found
3193  * entry, if any, can be assumed to be valid as long as the lock remains held.
3194  */
3195 PGPROC *
3197 {
3198  PGPROC *result = NULL;
3199  ProcArrayStruct *arrayP = procArray;
3200  int index;
3201 
3202  if (pid == 0) /* never match dummy PGPROCs */
3203  return NULL;
3204 
3205  for (index = 0; index < arrayP->numProcs; index++)
3206  {
3207  PGPROC *proc = &allProcs[arrayP->pgprocnos[index]];
3208 
3209  if (proc->pid == pid)
3210  {
3211  result = proc;
3212  break;
3213  }
3214  }
3215 
3216  return result;
3217 }
3218 
3219 /*
3220  * BackendXidGetPid -- get a backend's pid given its XID
3221  *
3222  * Returns 0 if not found or it's a prepared transaction. Note that
3223  * it is up to the caller to be sure that the question remains
3224  * meaningful for long enough for the answer to be used ...
3225  *
3226  * Only main transaction Ids are considered. This function is mainly
3227  * useful for determining what backend owns a lock.
3228  *
3229  * Beware that not every xact has an XID assigned. However, as long as you
3230  * only call this using an XID found on disk, you're safe.
3231  */
3232 int
3234 {
3235  int result = 0;
3236  ProcArrayStruct *arrayP = procArray;
3237  TransactionId *other_xids = ProcGlobal->xids;
3238  int index;
3239 
3240  if (xid == InvalidTransactionId) /* never match invalid xid */
3241  return 0;
3242 
3243  LWLockAcquire(ProcArrayLock, LW_SHARED);
3244 
3245  for (index = 0; index < arrayP->numProcs; index++)
3246  {
3247  if (other_xids[index] == xid)
3248  {
3249  int pgprocno = arrayP->pgprocnos[index];
3250  PGPROC *proc = &allProcs[pgprocno];
3251 
3252  result = proc->pid;
3253  break;
3254  }
3255  }
3256 
3257  LWLockRelease(ProcArrayLock);
3258 
3259  return result;
3260 }
3261 
3262 /*
3263  * IsBackendPid -- is a given pid a running backend
3264  *
3265  * This is not called by the backend, but is called by external modules.
3266  */
3267 bool
3269 {
3270  return (BackendPidGetProc(pid) != NULL);
3271 }
3272 
3273 
3274 /*
3275  * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
3276  *
3277  * The array is palloc'd. The number of valid entries is returned into *nvxids.
3278  *
3279  * The arguments allow filtering the set of VXIDs returned. Our own process
3280  * is always skipped. In addition:
3281  * If limitXmin is not InvalidTransactionId, skip processes with
3282  * xmin > limitXmin.
3283  * If excludeXmin0 is true, skip processes with xmin = 0.
3284  * If allDbs is false, skip processes attached to other databases.
3285  * If excludeVacuum isn't zero, skip processes for which
3286  * (statusFlags & excludeVacuum) is not zero.
3287  *
3288  * Note: the purpose of the limitXmin and excludeXmin0 parameters is to
3289  * allow skipping backends whose oldest live snapshot is no older than
3290  * some snapshot we have. Since we examine the procarray with only shared
3291  * lock, there are race conditions: a backend could set its xmin just after
3292  * we look. Indeed, on multiprocessors with weak memory ordering, the
3293  * other backend could have set its xmin *before* we look. We know however
3294  * that such a backend must have held shared ProcArrayLock overlapping our
3295  * own hold of ProcArrayLock, else we would see its xmin update. Therefore,
3296  * any snapshot the other backend is taking concurrently with our scan cannot
3297  * consider any transactions as still running that we think are committed
3298  * (since backends must hold ProcArrayLock exclusive to commit).
3299  */
3301 GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
3302  bool allDbs, int excludeVacuum,
3303  int *nvxids)
3304 {
3305  VirtualTransactionId *vxids;
3306  ProcArrayStruct *arrayP = procArray;
3307  int count = 0;
3308  int index;
3309 
3310  /* allocate what's certainly enough result space */
3311  vxids = (VirtualTransactionId *)
3312  palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
3313 
3314  LWLockAcquire(ProcArrayLock, LW_SHARED);
3315 
3316  for (index = 0; index < arrayP->numProcs; index++)
3317  {
3318  int pgprocno = arrayP->pgprocnos[index];
3319  PGPROC *proc = &allProcs[pgprocno];
3320  uint8 statusFlags = ProcGlobal->statusFlags[index];
3321 
3322  if (proc == MyProc)
3323  continue;
3324 
3325  if (excludeVacuum & statusFlags)
3326  continue;
3327 
3328  if (allDbs || proc->databaseId == MyDatabaseId)
3329  {
3330  /* Fetch xmin just once - might change on us */
3331  TransactionId pxmin = UINT32_ACCESS_ONCE(proc->xmin);
3332 
3333  if (excludeXmin0 && !TransactionIdIsValid(pxmin))
3334  continue;
3335 
3336  /*
3337  * InvalidTransactionId precedes all other XIDs, so a proc that
3338  * hasn't set xmin yet will not be rejected by this test.
3339  */
3340  if (!TransactionIdIsValid(limitXmin) ||
3341  TransactionIdPrecedesOrEquals(pxmin, limitXmin))
3342  {
3343  VirtualTransactionId vxid;
3344 
3345  GET_VXID_FROM_PGPROC(vxid, *proc);
3346  if (VirtualTransactionIdIsValid(vxid))
3347  vxids[count++] = vxid;
3348  }
3349  }
3350  }
3351 
3352  LWLockRelease(ProcArrayLock);
3353 
3354  *nvxids = count;
3355  return vxids;
3356 }
3357 
3358 /*
3359  * GetConflictingVirtualXIDs -- returns an array of currently active VXIDs.
3360  *
3361  * Usage is limited to conflict resolution during recovery on standby servers.
3362  * limitXmin is supplied as either a cutoff with snapshotConflictHorizon
3363  * semantics, or InvalidTransactionId in cases where caller cannot accurately
3364  * determine a safe snapshotConflictHorizon value.
3365  *
3366  * If limitXmin is InvalidTransactionId then we want to kill everybody,
3367  * so we're not worried if they have a snapshot or not, nor does it really
3368  * matter what type of lock we hold. Caller must avoid calling here with
3369  * snapshotConflictHorizon style cutoffs that were set to InvalidTransactionId
3370  * during original execution, since that actually indicates that there is
3371  * definitely no need for a recovery conflict (the snapshotConflictHorizon
3372  * convention for InvalidTransactionId values is the opposite of our own!).
3373  *
3374  * All callers that are checking xmins always now supply a valid and useful
3375  * value for limitXmin. The limitXmin is always lower than the lowest
3376  * numbered KnownAssignedXid that is not already a FATAL error. This is
3377  * because we only care about cleanup records that are cleaning up tuple
3378  * versions from committed transactions. In that case they will only occur
3379  * at the point where the record is less than the lowest running xid. That
3380  * allows us to say that if any backend takes a snapshot concurrently with
3381  * us then the conflict assessment made here would never include the snapshot
3382  * that is being derived. So we take LW_SHARED on the ProcArray and allow
3383  * concurrent snapshots when limitXmin is valid. We might think about adding
3384  * Assert(limitXmin < lowest(KnownAssignedXids))
3385  * but that would not be true in the case of FATAL errors lagging in array,
3386  * but we already know those are bogus anyway, so we skip that test.
3387  *
3388  * If dbOid is valid we skip backends attached to other databases.
3389  *
3390  * Be careful to *not* pfree the result from this function. We reuse
3391  * this array sufficiently often that we use malloc for the result.
3392  */
3395 {
3396  static VirtualTransactionId *vxids;
3397  ProcArrayStruct *arrayP = procArray;
3398  int count = 0;
3399  int index;
3400 
3401  /*
3402  * If first time through, get workspace to remember main XIDs in. We
3403  * malloc it permanently to avoid repeated palloc/pfree overhead. Allow
3404  * result space, remembering room for a terminator.
3405  */
3406  if (vxids == NULL)
3407  {
3408  vxids = (VirtualTransactionId *)
3409  malloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
3410  if (vxids == NULL)
3411  ereport(ERROR,
3412  (errcode(ERRCODE_OUT_OF_MEMORY),
3413  errmsg("out of memory")));
3414  }
3415 
3416  LWLockAcquire(ProcArrayLock, LW_SHARED);
3417 
3418  for (index = 0; index < arrayP->numProcs; index++)
3419  {
3420  int pgprocno = arrayP->pgprocnos[index];
3421  PGPROC *proc = &allProcs[pgprocno];
3422 
3423  /* Exclude prepared transactions */
3424  if (proc->pid == 0)
3425  continue;
3426 
3427  if (!OidIsValid(dbOid) ||
3428  proc->databaseId == dbOid)
3429  {
3430  /* Fetch xmin just once - can't change on us, but good coding */
3431  TransactionId pxmin = UINT32_ACCESS_ONCE(proc->xmin);
3432 
3433  /*
3434  * We ignore an invalid pxmin because this means that backend has
3435  * no snapshot currently. We hold a Share lock to avoid contention
3436  * with users taking snapshots. That is not a problem because the
3437  * current xmin is always at least one higher than the latest
3438  * removed xid, so any new snapshot would never conflict with the
3439  * test here.
3440  */
3441  if (!TransactionIdIsValid(limitXmin) ||
3442  (TransactionIdIsValid(pxmin) && !TransactionIdFollows(pxmin, limitXmin)))
3443  {
3444  VirtualTransactionId vxid;
3445 
3446  GET_VXID_FROM_PGPROC(vxid, *proc);
3447  if (VirtualTransactionIdIsValid(vxid))
3448  vxids[count++] = vxid;
3449  }
3450  }
3451  }
3452 
3453  LWLockRelease(ProcArrayLock);
3454 
3455  /* add the terminator */
3456  vxids[count].procNumber = INVALID_PROC_NUMBER;
3458 
3459  return vxids;
3460 }
3461 
3462 /*
3463  * CancelVirtualTransaction - used in recovery conflict processing
3464  *
3465  * Returns pid of the process signaled, or 0 if not found.
3466  */
3467 pid_t
3469 {
3470  return SignalVirtualTransaction(vxid, sigmode, true);
3471 }
3472 
3473 pid_t
3475  bool conflictPending)
3476 {
3477  ProcArrayStruct *arrayP = procArray;
3478  int index;
3479  pid_t pid = 0;
3480 
3481  LWLockAcquire(ProcArrayLock, LW_SHARED);
3482 
3483  for (index = 0; index < arrayP->numProcs; index++)
3484  {
3485  int pgprocno = arrayP->pgprocnos[index];
3486  PGPROC *proc = &allProcs[pgprocno];
3487  VirtualTransactionId procvxid;
3488 
3489  GET_VXID_FROM_PGPROC(procvxid, *proc);
3490 
3491  if (procvxid.procNumber == vxid.procNumber &&
3492  procvxid.localTransactionId == vxid.localTransactionId)
3493  {
3494  proc->recoveryConflictPending = conflictPending;
3495  pid = proc->pid;
3496  if (pid != 0)
3497  {
3498  /*
3499  * Kill the pid if it's still here. If not, that's what we
3500  * wanted so ignore any errors.
3501  */
3502  (void) SendProcSignal(pid, sigmode, vxid.procNumber);
3503  }
3504  break;
3505  }
3506  }
3507 
3508  LWLockRelease(ProcArrayLock);
3509 
3510  return pid;
3511 }
3512 
3513 /*
3514  * MinimumActiveBackends --- count backends (other than myself) that are
3515  * in active transactions. Return true if the count exceeds the
3516  * minimum threshold passed. This is used as a heuristic to decide if
3517  * a pre-XLOG-flush delay is worthwhile during commit.
3518  *
3519  * Do not count backends that are blocked waiting for locks, since they are
3520  * not going to get to run until someone else commits.
3521  */
3522 bool
3524 {
3525  ProcArrayStruct *arrayP = procArray;
3526  int count = 0;
3527  int index;
3528 
3529  /* Quick short-circuit if no minimum is specified */
3530  if (min == 0)
3531  return true;
3532 
3533  /*
3534  * Note: for speed, we don't acquire ProcArrayLock. This is a little bit
3535  * bogus, but since we are only testing fields for zero or nonzero, it
3536  * should be OK. The result is only used for heuristic purposes anyway...
3537  */
3538  for (index = 0; index < arrayP->numProcs; index++)
3539  {
3540  int pgprocno = arrayP->pgprocnos[index];
3541  PGPROC *proc = &allProcs[pgprocno];
3542 
3543  /*
3544  * Since we're not holding a lock, need to be prepared to deal with
3545  * garbage, as someone could have incremented numProcs but not yet
3546  * filled the structure.
3547  *
3548  * If someone just decremented numProcs, 'proc' could also point to a
3549  * PGPROC entry that's no longer in the array. It still points to a
3550  * PGPROC struct, though, because freed PGPROC entries just go to the
3551  * free list and are recycled. Its contents are nonsense in that case,
3552  * but that's acceptable for this function.
3553  */
3554  if (pgprocno == -1)
3555  continue; /* do not count deleted entries */
3556  if (proc == MyProc)
3557  continue; /* do not count myself */
3558  if (proc->xid == InvalidTransactionId)
3559  continue; /* do not count if no XID assigned */
3560  if (proc->pid == 0)
3561  continue; /* do not count prepared xacts */
3562  if (proc->waitLock != NULL)
3563  continue; /* do not count if blocked on a lock */
3564  count++;
3565  if (count >= min)
3566  break;
3567  }
3568 
3569  return count >= min;
3570 }
3571 
3572 /*
3573  * CountDBBackends --- count backends that are using specified database
3574  */
3575 int
3577 {
3578  ProcArrayStruct *arrayP = procArray;
3579  int count = 0;
3580  int index;
3581 
3582  LWLockAcquire(ProcArrayLock, LW_SHARED);
3583 
3584  for (index = 0; index < arrayP->numProcs; index++)
3585  {
3586  int pgprocno = arrayP->pgprocnos[index];
3587  PGPROC *proc = &allProcs[pgprocno];
3588 
3589  if (proc->pid == 0)
3590  continue; /* do not count prepared xacts */
3591  if (!OidIsValid(databaseid) ||
3592  proc->databaseId == databaseid)
3593  count++;
3594  }
3595 
3596  LWLockRelease(ProcArrayLock);
3597 
3598  return count;
3599 }
3600 
3601 /*
3602  * CountDBConnections --- counts database backends ignoring any background
3603  * worker processes
3604  */
3605 int
3607 {
3608  ProcArrayStruct *arrayP = procArray;
3609  int count = 0;
3610  int index;
3611 
3612  LWLockAcquire(ProcArrayLock, LW_SHARED);
3613 
3614  for (index = 0; index < arrayP->numProcs; index++)
3615  {
3616  int pgprocno = arrayP->pgprocnos[index];
3617  PGPROC *proc = &allProcs[pgprocno];
3618 
3619  if (proc->pid == 0)
3620  continue; /* do not count prepared xacts */
3621  if (proc->isBackgroundWorker)
3622  continue; /* do not count background workers */
3623  if (!OidIsValid(databaseid) ||
3624  proc->databaseId == databaseid)
3625  count++;
3626  }
3627 
3628  LWLockRelease(ProcArrayLock);
3629 
3630  return count;
3631 }
3632 
3633 /*
3634  * CancelDBBackends --- cancel backends that are using specified database
3635  */
3636 void
3637 CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
3638 {
3639  ProcArrayStruct *arrayP = procArray;
3640  int index;
3641 
3642  /* tell all backends to die */
3643  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3644 
3645  for (index = 0; index < arrayP->numProcs; index++)
3646  {
3647  int pgprocno = arrayP->pgprocnos[index];
3648  PGPROC *proc = &allProcs[pgprocno];
3649 
3650  if (databaseid == InvalidOid || proc->databaseId == databaseid)
3651  {
3652  VirtualTransactionId procvxid;
3653  pid_t pid;
3654 
3655  GET_VXID_FROM_PGPROC(procvxid, *proc);
3656 
3657  proc->recoveryConflictPending = conflictPending;
3658  pid = proc->pid;
3659  if (pid != 0)
3660  {
3661  /*
3662  * Kill the pid if it's still here. If not, that's what we
3663  * wanted so ignore any errors.
3664  */
3665  (void) SendProcSignal(pid, sigmode, procvxid.procNumber);
3666  }
3667  }
3668  }
3669 
3670  LWLockRelease(ProcArrayLock);
3671 }
3672 
3673 /*
3674  * CountUserBackends --- count backends that are used by specified user
3675  */
3676 int
3678 {
3679  ProcArrayStruct *arrayP = procArray;
3680  int count = 0;
3681  int index;
3682 
3683  LWLockAcquire(ProcArrayLock, LW_SHARED);
3684 
3685  for (index = 0; index < arrayP->numProcs; index++)
3686  {
3687  int pgprocno = arrayP->pgprocnos[index];
3688  PGPROC *proc = &allProcs[pgprocno];
3689 
3690  if (proc->pid == 0)
3691  continue; /* do not count prepared xacts */
3692  if (proc->isBackgroundWorker)
3693  continue; /* do not count background workers */
3694  if (proc->roleId == roleid)
3695  count++;
3696  }
3697 
3698  LWLockRelease(ProcArrayLock);
3699 
3700  return count;
3701 }
3702 
3703 /*
3704  * CountOtherDBBackends -- check for other backends running in the given DB
3705  *
3706  * If there are other backends in the DB, we will wait a maximum of 5 seconds
3707  * for them to exit. Autovacuum backends are encouraged to exit early by
3708  * sending them SIGTERM, but normal user backends are just waited for.
3709  *
3710  * The current backend is always ignored; it is caller's responsibility to
3711  * check whether the current backend uses the given DB, if it's important.
3712  *
3713  * Returns true if there are (still) other backends in the DB, false if not.
3714  * Also, *nbackends and *nprepared are set to the number of other backends
3715  * and prepared transactions in the DB, respectively.
3716  *
3717  * This function is used to interlock DROP DATABASE and related commands
3718  * against there being any active backends in the target DB --- dropping the
3719  * DB while active backends remain would be a Bad Thing. Note that we cannot
3720  * detect here the possibility of a newly-started backend that is trying to
3721  * connect to the doomed database, so additional interlocking is needed during
3722  * backend startup. The caller should normally hold an exclusive lock on the
3723  * target DB before calling this, which is one reason we mustn't wait
3724  * indefinitely.
3725  */
3726 bool
3727 CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
3728 {
3729  ProcArrayStruct *arrayP = procArray;
3730 
3731 #define MAXAUTOVACPIDS 10 /* max autovacs to SIGTERM per iteration */
3732  int autovac_pids[MAXAUTOVACPIDS];
3733  int tries;
3734 
3735  /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
3736  for (tries = 0; tries < 50; tries++)
3737  {
3738  int nautovacs = 0;
3739  bool found = false;
3740  int index;
3741 
3743 
3744  *nbackends = *nprepared = 0;
3745 
3746  LWLockAcquire(ProcArrayLock, LW_SHARED);
3747 
3748  for (index = 0; index < arrayP->numProcs; index++)
3749  {
3750  int pgprocno = arrayP->pgprocnos[index];
3751  PGPROC *proc = &allProcs[pgprocno];
3752  uint8 statusFlags = ProcGlobal->statusFlags[index];
3753 
3754  if (proc->databaseId != databaseId)
3755  continue;
3756  if (proc == MyProc)
3757  continue;
3758 
3759  found = true;
3760 
3761  if (proc->pid == 0)
3762  (*nprepared)++;
3763  else
3764  {
3765  (*nbackends)++;
3766  if ((statusFlags & PROC_IS_AUTOVACUUM) &&
3767  nautovacs < MAXAUTOVACPIDS)
3768  autovac_pids[nautovacs++] = proc->pid;
3769  }
3770  }
3771 
3772  LWLockRelease(ProcArrayLock);
3773 
3774  if (!found)
3775  return false; /* no conflicting backends, so done */
3776 
3777  /*
3778  * Send SIGTERM to any conflicting autovacuums before sleeping. We
3779  * postpone this step until after the loop because we don't want to
3780  * hold ProcArrayLock while issuing kill(). We have no idea what might
3781  * block kill() inside the kernel...
3782  */
3783  for (index = 0; index < nautovacs; index++)
3784  (void) kill(autovac_pids[index], SIGTERM); /* ignore any error */
3785 
3786  /* sleep, then try again */
3787  pg_usleep(100 * 1000L); /* 100ms */
3788  }
3789 
3790  return true; /* timed out, still conflicts */
3791 }
3792 
3793 /*
3794  * Terminate existing connections to the specified database. This routine
3795  * is used by the DROP DATABASE command when user has asked to forcefully
3796  * drop the database.
3797  *
3798  * The current backend is always ignored; it is caller's responsibility to
3799  * check whether the current backend uses the given DB, if it's important.
3800  *
3801  * It doesn't allow to terminate the connections even if there is a one
3802  * backend with the prepared transaction in the target database.
3803  */
3804 void
3806 {
3807  ProcArrayStruct *arrayP = procArray;
3808  List *pids = NIL;
3809  int nprepared = 0;
3810  int i;
3811 
3812  LWLockAcquire(ProcArrayLock, LW_SHARED);
3813 
3814  for (i = 0; i < procArray->numProcs; i++)
3815  {
3816  int pgprocno = arrayP->pgprocnos[i];
3817  PGPROC *proc = &allProcs[pgprocno];
3818 
3819  if (proc->databaseId != databaseId)
3820  continue;
3821  if (proc == MyProc)
3822  continue;
3823 
3824  if (proc->pid != 0)
3825  pids = lappend_int(pids, proc->pid);
3826  else
3827  nprepared++;
3828  }
3829 
3830  LWLockRelease(ProcArrayLock);
3831 
3832  if (nprepared > 0)
3833  ereport(ERROR,
3834  (errcode(ERRCODE_OBJECT_IN_USE),
3835  errmsg("database \"%s\" is being used by prepared transactions",
3836  get_database_name(databaseId)),
3837  errdetail_plural("There is %d prepared transaction using the database.",
3838  "There are %d prepared transactions using the database.",
3839  nprepared,
3840  nprepared)));
3841 
3842  if (pids)
3843  {
3844  ListCell *lc;
3845 
3846  /*
3847  * Check whether we have the necessary rights to terminate other
3848  * sessions. We don't terminate any session until we ensure that we
3849  * have rights on all the sessions to be terminated. These checks are
3850  * the same as we do in pg_terminate_backend.
3851  *
3852  * In this case we don't raise some warnings - like "PID %d is not a
3853  * PostgreSQL server process", because for us already finished session
3854  * is not a problem.
3855  */
3856  foreach(lc, pids)
3857  {
3858  int pid = lfirst_int(lc);
3859  PGPROC *proc = BackendPidGetProc(pid);
3860 
3861  if (proc != NULL)
3862  {
3863  /* Only allow superusers to signal superuser-owned backends. */
3864  if (superuser_arg(proc->roleId) && !superuser())
3865  ereport(ERROR,
3866  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3867  errmsg("permission denied to terminate process"),
3868  errdetail("Only roles with the %s attribute may terminate processes of roles with the %s attribute.",
3869  "SUPERUSER", "SUPERUSER")));
3870 
3871  /* Users can signal backends they have role membership in. */
3872  if (!has_privs_of_role(GetUserId(), proc->roleId) &&
3873  !has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_BACKEND))
3874  ereport(ERROR,
3875  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3876  errmsg("permission denied to terminate process"),
3877  errdetail("Only roles with privileges of the role whose process is being terminated or with privileges of the \"%s\" role may terminate this process.",
3878  "pg_signal_backend")));
3879  }
3880  }
3881 
3882  /*
3883  * There's a race condition here: once we release the ProcArrayLock,
3884  * it's possible for the session to exit before we issue kill. That
3885  * race condition possibility seems too unlikely to worry about. See
3886  * pg_signal_backend.
3887  */
3888  foreach(lc, pids)
3889  {
3890  int pid = lfirst_int(lc);
3891  PGPROC *proc = BackendPidGetProc(pid);
3892 
3893  if (proc != NULL)
3894  {
3895  /*
3896  * If we have setsid(), signal the backend's whole process
3897  * group
3898  */
3899 #ifdef HAVE_SETSID
3900  (void) kill(-pid, SIGTERM);
3901 #else
3902  (void) kill(pid, SIGTERM);
3903 #endif
3904  }
3905  }
3906  }
3907 }
3908 
3909 /*
3910  * ProcArraySetReplicationSlotXmin
3911  *
3912  * Install limits to future computations of the xmin horizon to prevent vacuum
3913  * and HOT pruning from removing affected rows still needed by clients with
3914  * replication slots.
3915  */
3916 void
3918  bool already_locked)
3919 {
3920  Assert(!already_locked || LWLockHeldByMe(ProcArrayLock));
3921 
3922  if (!already_locked)
3923  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3924 
3926  procArray->replication_slot_catalog_xmin = catalog_xmin;
3927 
3928  if (!already_locked)
3929  LWLockRelease(ProcArrayLock);
3930 
3931  elog(DEBUG1, "xmin required by slots: data %u, catalog %u",
3932  xmin, catalog_xmin);
3933 }
3934 
3935 /*
3936  * ProcArrayGetReplicationSlotXmin
3937  *
3938  * Return the current slot xmin limits. That's useful to be able to remove
3939  * data that's older than those limits.
3940  */
3941 void
3943  TransactionId *catalog_xmin)
3944 {
3945  LWLockAcquire(ProcArrayLock, LW_SHARED);
3946 
3947  if (xmin != NULL)
3949 
3950  if (catalog_xmin != NULL)
3951  *catalog_xmin = procArray->replication_slot_catalog_xmin;
3952 
3953  LWLockRelease(ProcArrayLock);
3954 }
3955 
3956 /*
3957  * XidCacheRemoveRunningXids
3958  *
3959  * Remove a bunch of TransactionIds from the list of known-running
3960  * subtransactions for my backend. Both the specified xid and those in
3961  * the xids[] array (of length nxids) are removed from the subxids cache.
3962  * latestXid must be the latest XID among the group.
3963  */
3964 void
3966  int nxids, const TransactionId *xids,
3967  TransactionId latestXid)
3968 {
3969  int i,
3970  j;
3971  XidCacheStatus *mysubxidstat;
3972 
3974 
3975  /*
3976  * We must hold ProcArrayLock exclusively in order to remove transactions
3977  * from the PGPROC array. (See src/backend/access/transam/README.) It's
3978  * possible this could be relaxed since we know this routine is only used
3979  * to abort subtransactions, but pending closer analysis we'd best be
3980  * conservative.
3981  *
3982  * Note that we do not have to be careful about memory ordering of our own
3983  * reads wrt. GetNewTransactionId() here - only this process can modify
3984  * relevant fields of MyProc/ProcGlobal->xids[]. But we do have to be
3985  * careful about our own writes being well ordered.
3986  */
3987  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3988 
3989  mysubxidstat = &ProcGlobal->subxidStates[MyProc->pgxactoff];
3990 
3991  /*
3992  * Under normal circumstances xid and xids[] will be in increasing order,
3993  * as will be the entries in subxids. Scan backwards to avoid O(N^2)
3994  * behavior when removing a lot of xids.
3995  */
3996  for (i = nxids - 1; i >= 0; i--)
3997  {
3998  TransactionId anxid = xids[i];
3999 
4000  for (j = MyProc->subxidStatus.count - 1; j >= 0; j--)
4001  {
4002  if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
4003  {
4005  pg_write_barrier();
4006  mysubxidstat->count--;
4008  break;
4009  }
4010  }
4011 
4012  /*
4013  * Ordinarily we should have found it, unless the cache has
4014  * overflowed. However it's also possible for this routine to be
4015  * invoked multiple times for the same subtransaction, in case of an
4016  * error during AbortSubTransaction. So instead of Assert, emit a
4017  * debug warning.
4018  */
4019  if (j < 0 && !MyProc->subxidStatus.overflowed)
4020  elog(WARNING, "did not find subXID %u in MyProc", anxid);
4021  }
4022 
4023  for (j = MyProc->subxidStatus.count - 1; j >= 0; j--)
4024  {
4025  if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
4026  {
4028  pg_write_barrier();
4029  mysubxidstat->count--;
4031  break;
4032  }
4033  }
4034  /* Ordinarily we should have found it, unless the cache has overflowed */
4035  if (j < 0 && !MyProc->subxidStatus.overflowed)
4036  elog(WARNING, "did not find subXID %u in MyProc", xid);
4037 
4038  /* Also advance global latestCompletedXid while holding the lock */
4039  MaintainLatestCompletedXid(latestXid);
4040 
4041  /* ... and xactCompletionCount */
4043 
4044  LWLockRelease(ProcArrayLock);
4045 }
4046 
4047 #ifdef XIDCACHE_DEBUG
4048 
4049 /*
4050  * Print stats about effectiveness of XID cache
4051  */
4052 static void
4053 DisplayXidCache(void)
4054 {
4055  fprintf(stderr,
4056  "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, knownassigned: %ld, nooflo: %ld, slow: %ld\n",
4057  xc_by_recent_xmin,
4058  xc_by_known_xact,
4059  xc_by_my_xact,
4060  xc_by_latest_xid,
4061  xc_by_main_xid,
4062  xc_by_child_xid,
4063  xc_by_known_assigned,
4064  xc_no_overflow,
4065  xc_slow_answer);
4066 }
4067 #endif /* XIDCACHE_DEBUG */
4068 
4069 /*
4070  * If rel != NULL, return test state appropriate for relation, otherwise
4071  * return state usable for all relations. The latter may consider XIDs as
4072  * not-yet-visible-to-everyone that a state for a specific relation would
4073  * already consider visible-to-everyone.
4074  *
4075  * This needs to be called while a snapshot is active or registered, otherwise
4076  * there are wraparound and other dangers.
4077  *
4078  * See comment for GlobalVisState for details.
4079  */
4082 {
4083  GlobalVisState *state = NULL;
4084 
4085  /* XXX: we should assert that a snapshot is pushed or registered */
4086  Assert(RecentXmin);
4087 
4088  switch (GlobalVisHorizonKindForRel(rel))
4089  {
4090  case VISHORIZON_SHARED:
4092  break;
4093  case VISHORIZON_CATALOG:
4095  break;
4096  case VISHORIZON_DATA:
4098  break;
4099  case VISHORIZON_TEMP:
4101  break;
4102  }
4103 
4104  Assert(FullTransactionIdIsValid(state->definitely_needed) &&
4105  FullTransactionIdIsValid(state->maybe_needed));
4106 
4107  return state;
4108 }
4109 
4110 /*
4111  * Return true if it's worth updating the accurate maybe_needed boundary.
4112  *
4113  * As it is somewhat expensive to determine xmin horizons, we don't want to
4114  * repeatedly do so when there is a low likelihood of it being beneficial.
4115  *
4116  * The current heuristic is that we update only if RecentXmin has changed
4117  * since the last update. If the oldest currently running transaction has not
4118  * finished, it is unlikely that recomputing the horizon would be useful.
4119  */
4120 static bool
4122 {
4123  /* hasn't been updated yet */
4125  return true;
4126 
4127  /*
4128  * If the maybe_needed/definitely_needed boundaries are the same, it's
4129  * unlikely to be beneficial to refresh boundaries.
4130  */
4131  if (FullTransactionIdFollowsOrEquals(state->maybe_needed,
4132  state->definitely_needed))
4133  return false;
4134 
4135  /* does the last snapshot built have a different xmin? */
4137 }
4138 
4139 static void
4141 {
4144  horizons->shared_oldest_nonremovable);
4147  horizons->catalog_oldest_nonremovable);
4150  horizons->data_oldest_nonremovable);
4153  horizons->temp_oldest_nonremovable);
4154 
4155  /*
4156  * In longer running transactions it's possible that transactions we
4157  * previously needed to treat as running aren't around anymore. So update
4158  * definitely_needed to not be earlier than maybe_needed.
4159  */
4170 
4172 }
4173 
4174 /*
4175  * Update boundaries in GlobalVis{Shared,Catalog, Data}Rels
4176  * using ComputeXidHorizons().
4177  */
4178 static void
4180 {
4181  ComputeXidHorizonsResult horizons;
4182 
4183  /* updates the horizons as a side-effect */
4184  ComputeXidHorizons(&horizons);
4185 }
4186 
4187 /*
4188  * Return true if no snapshot still considers fxid to be running.
4189  *
4190  * The state passed needs to have been initialized for the relation fxid is
4191  * from (NULL is also OK), otherwise the result may not be correct.
4192  *
4193  * See comment for GlobalVisState for details.
4194  */
4195 bool
4197  FullTransactionId fxid)
4198 {
4199  /*
4200  * If fxid is older than maybe_needed bound, it definitely is visible to
4201  * everyone.
4202  */
4203  if (FullTransactionIdPrecedes(fxid, state->maybe_needed))
4204  return true;
4205 
4206  /*
4207  * If fxid is >= definitely_needed bound, it is very likely to still be
4208  * considered running.
4209  */
4210  if (FullTransactionIdFollowsOrEquals(fxid, state->definitely_needed))
4211  return false;
4212 
4213  /*
4214  * fxid is between maybe_needed and definitely_needed, i.e. there might or
4215  * might not exist a snapshot considering fxid running. If it makes sense,
4216  * update boundaries and recheck.
4217  */
4219  {
4220  GlobalVisUpdate();
4221 
4222  Assert(FullTransactionIdPrecedes(fxid, state->definitely_needed));
4223 
4224  return FullTransactionIdPrecedes(fxid, state->maybe_needed);
4225  }
4226  else
4227  return false;
4228 }
4229 
4230 /*
4231  * Wrapper around GlobalVisTestIsRemovableFullXid() for 32bit xids.
4232  *
4233  * It is crucial that this only gets called for xids from a source that
4234  * protects against xid wraparounds (e.g. from a table and thus protected by
4235  * relfrozenxid).
4236  */
4237 bool
4239 {
4240  FullTransactionId fxid;
4241 
4242  /*
4243  * Convert 32 bit argument to FullTransactionId. We can do so safely
4244  * because we know the xid has to, at the very least, be between
4245  * [oldestXid, nextXid), i.e. within 2 billion of xid. To avoid taking a
4246  * lock to determine either, we can just compare with
4247  * state->definitely_needed, which was based on those value at the time
4248  * the current snapshot was built.
4249  */
4250  fxid = FullXidRelativeTo(state->definitely_needed, xid);
4251 
4252  return GlobalVisTestIsRemovableFullXid(state, fxid);
4253 }
4254 
4255 /*
4256  * Return FullTransactionId below which all transactions are not considered
4257  * running anymore.
4258  *
4259  * Note: This is less efficient than testing with
4260  * GlobalVisTestIsRemovableFullXid as it likely requires building an accurate
4261  * cutoff, even in the case all the XIDs compared with the cutoff are outside
4262  * [maybe_needed, definitely_needed).
4263  */
4266 {
4267  /* acquire accurate horizon if not already done */
4269  GlobalVisUpdate();
4270 
4271  return state->maybe_needed;
4272 }
4273 
4274 /* Convenience wrapper around GlobalVisTestNonRemovableFullHorizon */
4277 {
4278  FullTransactionId cutoff;
4279 
4281 
4282  return XidFromFullTransactionId(cutoff);
4283 }
4284 
4285 /*
4286  * Convenience wrapper around GlobalVisTestFor() and
4287  * GlobalVisTestIsRemovableFullXid(), see their comments.
4288  */
4289 bool
4291 {
4293 
4294  state = GlobalVisTestFor(rel);
4295 
4296  return GlobalVisTestIsRemovableFullXid(state, fxid);
4297 }
4298 
4299 /*
4300  * Convenience wrapper around GlobalVisTestFor() and
4301  * GlobalVisTestIsRemovableXid(), see their comments.
4302  */
4303 bool
4305 {
4307 
4308  state = GlobalVisTestFor(rel);
4309 
4310  return GlobalVisTestIsRemovableXid(state, xid);
4311 }
4312 
4313 /*
4314  * Convert a 32 bit transaction id into 64 bit transaction id, by assuming it
4315  * is within MaxTransactionId / 2 of XidFromFullTransactionId(rel).
4316  *
4317  * Be very careful about when to use this function. It can only safely be used
4318  * when there is a guarantee that xid is within MaxTransactionId / 2 xids of
4319  * rel. That e.g. can be guaranteed if the caller assures a snapshot is
4320  * held by the backend and xid is from a table (where vacuum/freezing ensures
4321  * the xid has to be within that range), or if xid is from the procarray and
4322  * prevents xid wraparound that way.
4323  */
4324 static inline FullTransactionId
4326 {
4327  TransactionId rel_xid = XidFromFullTransactionId(rel);
4328 
4330  Assert(TransactionIdIsValid(rel_xid));
4331 
4332  /* not guaranteed to find issues, but likely to catch mistakes */
4334 
4336  + (int32) (xid - rel_xid));
4337 }
4338 
4339 
4340 /* ----------------------------------------------
4341  * KnownAssignedTransactionIds sub-module
4342  * ----------------------------------------------
4343  */
4344 
4345 /*
4346  * In Hot Standby mode, we maintain a list of transactions that are (or were)
4347  * running on the primary at the current point in WAL. These XIDs must be
4348  * treated as running by standby transactions, even though they are not in
4349  * the standby server's PGPROC array.
4350  *
4351  * We record all XIDs that we know have been assigned. That includes all the
4352  * XIDs seen in WAL records, plus all unobserved XIDs that we can deduce have
4353  * been assigned. We can deduce the existence of unobserved XIDs because we
4354  * know XIDs are assigned in sequence, with no gaps. The KnownAssignedXids
4355  * list expands as new XIDs are observed or inferred, and contracts when
4356  * transaction completion records arrive.
4357  *
4358  * During hot standby we do not fret too much about the distinction between
4359  * top-level XIDs and subtransaction XIDs. We store both together in the
4360  * KnownAssignedXids list. In backends, this is copied into snapshots in
4361  * GetSnapshotData(), taking advantage of the fact that XidInMVCCSnapshot()
4362  * doesn't care about the distinction either. Subtransaction XIDs are
4363  * effectively treated as top-level XIDs and in the typical case pg_subtrans
4364  * links are *not* maintained (which does not affect visibility).
4365  *
4366  * We have room in KnownAssignedXids and in snapshots to hold maxProcs *
4367  * (1 + PGPROC_MAX_CACHED_SUBXIDS) XIDs, so every primary transaction must
4368  * report its subtransaction XIDs in a WAL XLOG_XACT_ASSIGNMENT record at
4369  * least every PGPROC_MAX_CACHED_SUBXIDS. When we receive one of these
4370  * records, we mark the subXIDs as children of the top XID in pg_subtrans,
4371  * and then remove them from KnownAssignedXids. This prevents overflow of
4372  * KnownAssignedXids and snapshots, at the cost that status checks for these
4373  * subXIDs will take a slower path through TransactionIdIsInProgress().
4374  * This means that KnownAssignedXids is not necessarily complete for subXIDs,
4375  * though it should be complete for top-level XIDs; this is the same situation
4376  * that holds with respect to the PGPROC entries in normal running.
4377  *
4378  * When we throw away subXIDs from KnownAssignedXids, we need to keep track of
4379  * that, similarly to tracking overflow of a PGPROC's subxids array. We do
4380  * that by remembering the lastOverflowedXid, ie the last thrown-away subXID.
4381  * As long as that is within the range of interesting XIDs, we have to assume
4382  * that subXIDs are missing from snapshots. (Note that subXID overflow occurs
4383  * on primary when 65th subXID arrives, whereas on standby it occurs when 64th
4384  * subXID arrives - that is not an error.)
4385  *
4386  * Should a backend on primary somehow disappear before it can write an abort
4387  * record, then we just leave those XIDs in KnownAssignedXids. They actually
4388  * aborted but we think they were running; the distinction is irrelevant
4389  * because either way any changes done by the transaction are not visible to
4390  * backends in the standby. We prune KnownAssignedXids when
4391  * XLOG_RUNNING_XACTS arrives, to forestall possible overflow of the
4392  * array due to such dead XIDs.
4393  */
4394 
4395 /*
4396  * RecordKnownAssignedTransactionIds
4397  * Record the given XID in KnownAssignedXids, as well as any preceding
4398  * unobserved XIDs.
4399  *
4400  * RecordKnownAssignedTransactionIds() should be run for *every* WAL record
4401  * associated with a transaction. Must be called for each record after we
4402  * have executed StartupCLOG() et al, since we must ExtendCLOG() etc..
4403  *
4404  * Called during recovery in analogy with and in place of GetNewTransactionId()
4405  */
4406 void
4408 {
4412 
4413  elog(DEBUG4, "record known xact %u latestObservedXid %u",
4414  xid, latestObservedXid);
4415 
4416  /*
4417  * When a newly observed xid arrives, it is frequently the case that it is
4418  * *not* the next xid in sequence. When this occurs, we must treat the
4419  * intervening xids as running also.
4420  */
4422  {
4423  TransactionId next_expected_xid;
4424 
4425  /*
4426  * Extend subtrans like we do in GetNewTransactionId() during normal
4427  * operation using individual extend steps. Note that we do not need
4428  * to extend clog since its extensions are WAL logged.
4429  *
4430  * This part has to be done regardless of standbyState since we
4431  * immediately start assigning subtransactions to their toplevel
4432  * transactions.
4433  */
4434  next_expected_xid = latestObservedXid;
4435  while (TransactionIdPrecedes(next_expected_xid, xid))
4436  {
4437  TransactionIdAdvance(next_expected_xid);
4438  ExtendSUBTRANS(next_expected_xid);
4439  }
4440  Assert(next_expected_xid == xid);
4441 
4442  /*
4443  * If the KnownAssignedXids machinery isn't up yet, there's nothing
4444  * more to do since we don't track assigned xids yet.
4445  */
4447  {
4448  latestObservedXid = xid;
4449  return;
4450  }
4451 
4452  /*
4453  * Add (latestObservedXid, xid] onto the KnownAssignedXids array.
4454  */
4455  next_expected_xid = latestObservedXid;
4456  TransactionIdAdvance(next_expected_xid);
4457  KnownAssignedXidsAdd(next_expected_xid, xid, false);
4458 
4459  /*
4460  * Now we can advance latestObservedXid
4461  */
4462  latestObservedXid = xid;
4463 
4464  /* TransamVariables->nextXid must be beyond any observed xid */
4466  }
4467 }
4468 
4469 /*
4470  * ExpireTreeKnownAssignedTransactionIds
4471  * Remove the given XIDs from KnownAssignedXids.
4472  *
4473  * Called during recovery in analogy with and in place of ProcArrayEndTransaction()
4474  */
4475 void
4477  TransactionId *subxids, TransactionId max_xid)
4478 {
4480 
4481  /*
4482  * Uses same locking as transaction commit
4483  */
4484  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4485 
4486  KnownAssignedXidsRemoveTree(xid, nsubxids, subxids);
4487 
4488  /* As in ProcArrayEndTransaction, advance latestCompletedXid */
4490 
4491  /* ... and xactCompletionCount */
4493 
4494  LWLockRelease(ProcArrayLock);
4495 }
4496 
4497 /*
4498  * ExpireAllKnownAssignedTransactionIds
4499  * Remove all entries in KnownAssignedXids and reset lastOverflowedXid.
4500  */
4501 void
4503 {
4504  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4506 
4507  /*
4508  * Reset lastOverflowedXid. Currently, lastOverflowedXid has no use after
4509  * the call of this function. But do this for unification with what
4510  * ExpireOldKnownAssignedTransactionIds() do.
4511  */
4513  LWLockRelease(ProcArrayLock);
4514 }
4515 
4516 /*
4517  * ExpireOldKnownAssignedTransactionIds
4518  * Remove KnownAssignedXids entries preceding the given XID and
4519  * potentially reset lastOverflowedXid.
4520  */
4521 void
4523 {
4524  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4525 
4526  /*
4527  * Reset lastOverflowedXid if we know all transactions that have been
4528  * possibly running are being gone. Not doing so could cause an incorrect
4529  * lastOverflowedXid value, which makes extra snapshots be marked as
4530  * suboverflowed.
4531  */
4535  LWLockRelease(ProcArrayLock);
4536 }
4537 
4538 /*
4539  * KnownAssignedTransactionIdsIdleMaintenance
4540  * Opportunistically do maintenance work when the startup process
4541  * is about to go idle.
4542  */
4543 void
4545 {
4547 }
4548 
4549 
4550 /*
4551  * Private module functions to manipulate KnownAssignedXids
4552  *
4553  * There are 5 main uses of the KnownAssignedXids data structure:
4554  *
4555  * * backends taking snapshots - all valid XIDs need to be copied out
4556  * * backends seeking to determine presence of a specific XID
4557  * * startup process adding new known-assigned XIDs
4558  * * startup process removing specific XIDs as transactions end
4559  * * startup process pruning array when special WAL records arrive
4560  *
4561  * This data structure is known to be a hot spot during Hot Standby, so we
4562  * go to some lengths to make these operations as efficient and as concurrent
4563  * as possible.
4564  *
4565  * The XIDs are stored in an array in sorted order --- TransactionIdPrecedes
4566  * order, to be exact --- to allow binary search for specific XIDs. Note:
4567  * in general TransactionIdPrecedes would not provide a total order, but
4568  * we know that the entries present at any instant should not extend across
4569  * a large enough fraction of XID space to wrap around (the primary would
4570  * shut down for fear of XID wrap long before that happens). So it's OK to
4571  * use TransactionIdPrecedes as a binary-search comparator.
4572  *
4573  * It's cheap to maintain the sortedness during insertions, since new known
4574  * XIDs are always reported in XID order; we just append them at the right.
4575  *
4576  * To keep individual deletions cheap, we need to allow gaps in the array.
4577  * This is implemented by marking array elements as valid or invalid using
4578  * the parallel boolean array KnownAssignedXidsValid[]. A deletion is done
4579  * by setting KnownAssignedXidsValid[i] to false, *without* clearing the
4580  * XID entry itself. This preserves the property that the XID entries are
4581  * sorted, so we can do binary searches easily. Periodically we compress
4582  * out the unused entries; that's much cheaper than having to compress the
4583  * array immediately on every deletion.
4584  *
4585  * The actually valid items in KnownAssignedXids[] and KnownAssignedXidsValid[]
4586  * are those with indexes tail <= i < head; items outside this subscript range
4587  * have unspecified contents. When head reaches the end of the array, we
4588  * force compression of unused entries rather than wrapping around, since
4589  * allowing wraparound would greatly complicate the search logic. We maintain
4590  * an explicit tail pointer so that pruning of old XIDs can be done without
4591  * immediately moving the array contents. In most cases only a small fraction
4592  * of the array contains valid entries at any instant.
4593  *
4594  * Although only the startup process can ever change the KnownAssignedXids
4595  * data structure, we still need interlocking so that standby backends will
4596  * not observe invalid intermediate states. The convention is that backends
4597  * must hold shared ProcArrayLock to examine the array. To remove XIDs from
4598  * the array, the startup process must hold ProcArrayLock exclusively, for
4599  * the usual transactional reasons (compare commit/abort of a transaction
4600  * during normal running). Compressing unused entries out of the array
4601  * likewise requires exclusive lock. To add XIDs to the array, we just insert
4602  * them into slots to the right of the head pointer and then advance the head
4603  * pointer. This doesn't require any lock at all, but on machines with weak
4604  * memory ordering, we need to be careful that other processors see the array
4605  * element changes before they see the head pointer change. We handle this by
4606  * using memory barriers when reading or writing the head/tail pointers (unless
4607  * the caller holds ProcArrayLock exclusively).
4608  *
4609  * Algorithmic analysis:
4610  *
4611  * If we have a maximum of M slots, with N XIDs currently spread across
4612  * S elements then we have N <= S <= M always.
4613  *
4614  * * Adding a new XID is O(1) and needs no lock (unless compression must
4615  * happen)
4616  * * Compressing the array is O(S) and requires exclusive lock
4617  * * Removing an XID is O(logS) and requires exclusive lock
4618  * * Taking a snapshot is O(S) and requires shared lock
4619  * * Checking for an XID is O(logS) and requires shared lock
4620  *
4621  * In comparison, using a hash table for KnownAssignedXids would mean that
4622  * taking snapshots would be O(M). If we can maintain S << M then the
4623  * sorted array technique will deliver significantly faster snapshots.
4624  * If we try to keep S too small then we will spend too much time compressing,
4625  * so there is an optimal point for any workload mix. We use a heuristic to
4626  * decide when to compress the array, though trimming also helps reduce
4627  * frequency of compressing. The heuristic requires us to track the number of
4628  * currently valid XIDs in the array (N). Except in special cases, we'll
4629  * compress when S >= 2N. Bounding S at 2N in turn bounds the time for
4630  * taking a snapshot to be O(N), which it would have to be anyway.
4631  */
4632 
4633 
4634 /*
4635  * Compress KnownAssignedXids by shifting valid data down to the start of the
4636  * array, removing any gaps.
4637  *
4638  * A compression step is forced if "reason" is KAX_NO_SPACE, otherwise
4639  * we do it only if a heuristic indicates it's a good time to do it.
4640  *
4641  * Compression requires holding ProcArrayLock in exclusive mode.
4642  * Caller must pass haveLock = true if it already holds the lock.
4643  */
4644 static void
4646 {
4647  ProcArrayStruct *pArray = procArray;
4648  int head,
4649  tail,
4650  nelements;
4651  int compress_index;
4652  int i;
4653 
4654  /* Counters for compression heuristics */
4655  static unsigned int transactionEndsCounter;
4656  static TimestampTz lastCompressTs;
4657 
4658  /* Tuning constants */
4659 #define KAX_COMPRESS_FREQUENCY 128 /* in transactions */
4660 #define KAX_COMPRESS_IDLE_INTERVAL 1000 /* in ms */
4661 
4662  /*
4663  * Since only the startup process modifies the head/tail pointers, we
4664  * don't need a lock to read them here.
4665  */
4666  head = pArray->headKnownAssignedXids;
4667  tail = pArray->tailKnownAssignedXids;
4668  nelements = head - tail;
4669 
4670  /*
4671  * If we can choose whether to compress, use a heuristic to avoid
4672  * compressing too often or not often enough. "Compress" here simply
4673  * means moving the values to the beginning of the array, so it is not as
4674  * complex or costly as typical data compression algorithms.
4675  */
4676  if (nelements == pArray->numKnownAssignedXids)
4677  {
4678  /*
4679  * When there are no gaps between head and tail, don't bother to
4680  * compress, except in the KAX_NO_SPACE case where we must compress to
4681  * create some space after the head.
4682  */
4683  if (reason != KAX_NO_SPACE)
4684  return;
4685  }
4686  else if (reason == KAX_TRANSACTION_END)
4687  {
4688  /*
4689  * Consider compressing only once every so many commits. Frequency
4690  * determined by benchmarks.
4691  */
4692  if ((transactionEndsCounter++) % KAX_COMPRESS_FREQUENCY != 0)
4693  return;
4694 
4695  /*
4696  * Furthermore, compress only if the used part of the array is less
4697  * than 50% full (see comments above).
4698  */
4699  if (nelements < 2 * pArray->numKnownAssignedXids)
4700  return;
4701  }
4702  else if (reason == KAX_STARTUP_PROCESS_IDLE)
4703  {
4704  /*
4705  * We're about to go idle for lack of new WAL, so we might as well
4706  * compress. But not too often, to avoid ProcArray lock contention
4707  * with readers.
4708  */
4709  if (lastCompressTs != 0)
4710  {
4711  TimestampTz compress_after;
4712 
4713  compress_after = TimestampTzPlusMilliseconds(lastCompressTs,
4715  if (GetCurrentTimestamp() < compress_after)
4716  return;
4717  }
4718  }
4719 
4720  /* Need to compress, so get the lock if we don't have it. */
4721  if (!haveLock)
4722  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4723 
4724  /*
4725  * We compress the array by reading the valid values from tail to head,
4726  * re-aligning data to 0th element.
4727  */
4728  compress_index = 0;
4729  for (i = tail; i < head; i++)
4730  {
4732  {
4733  KnownAssignedXids[compress_index] = KnownAssignedXids[i];
4734  KnownAssignedXidsValid[compress_index] = true;
4735  compress_index++;
4736  }
4737  }
4738  Assert(compress_index == pArray->numKnownAssignedXids);
4739 
4740  pArray->tailKnownAssignedXids = 0;
4741  pArray->headKnownAssignedXids = compress_index;
4742 
4743  if (!haveLock)
4744  LWLockRelease(ProcArrayLock);
4745 
4746  /* Update timestamp for maintenance. No need to hold lock for this. */
4747  lastCompressTs = GetCurrentTimestamp();
4748 }
4749 
4750 /*
4751  * Add xids into KnownAssignedXids at the head of the array.
4752  *
4753  * xids from from_xid to to_xid, inclusive, are added to the array.
4754  *
4755  * If exclusive_lock is true then caller already holds ProcArrayLock in
4756  * exclusive mode, so we need no extra locking here. Else caller holds no
4757  * lock, so we need to be sure we maintain sufficient interlocks against
4758  * concurrent readers. (Only the startup process ever calls this, so no need
4759  * to worry about concurrent writers.)
4760  */
4761 static void
4763  bool exclusive_lock)
4764 {
4765  ProcArrayStruct *pArray = procArray;
4766  TransactionId next_xid;
4767  int head,
4768  tail;
4769  int nxids;
4770  int i;
4771 
4772  Assert(TransactionIdPrecedesOrEquals(from_xid, to_xid));
4773 
4774  /*
4775  * Calculate how many array slots we'll need. Normally this is cheap; in
4776  * the unusual case where the XIDs cross the wrap point, we do it the hard
4777  * way.
4778  */
4779  if (to_xid >= from_xid)
4780  nxids = to_xid - from_xid + 1;
4781  else
4782  {
4783  nxids = 1;
4784  next_xid = from_xid;
4785  while (TransactionIdPrecedes(next_xid, to_xid))
4786  {
4787  nxids++;
4788  TransactionIdAdvance(next_xid);
4789  }
4790  }
4791 
4792  /*
4793  * Since only the startup process modifies the head/tail pointers, we
4794  * don't need a lock to read them here.
4795  */
4796  head = pArray->headKnownAssignedXids;
4797  tail = pArray->tailKnownAssignedXids;
4798 
4799  Assert(head >= 0 && head <= pArray->maxKnownAssignedXids);
4800  Assert(tail >= 0 && tail < pArray->maxKnownAssignedXids);
4801 
4802  /*
4803  * Verify that insertions occur in TransactionId sequence. Note that even
4804  * if the last existing element is marked invalid, it must still have a
4805  * correctly sequenced XID value.
4806  */
4807  if (head > tail &&
4808  TransactionIdFollowsOrEquals(KnownAssignedXids[head - 1], from_xid))
4809  {
4811  elog(ERROR, "out-of-order XID insertion in KnownAssignedXids");
4812  }
4813 
4814  /*
4815  * If our xids won't fit in the remaining space, compress out free space
4816  */
4817  if (head + nxids > pArray->maxKnownAssignedXids)
4818  {
4819  KnownAssignedXidsCompress(KAX_NO_SPACE, exclusive_lock);
4820 
4821  head = pArray->headKnownAssignedXids;
4822  /* note: we no longer care about the tail pointer */
4823 
4824  /*
4825  * If it still won't fit then we're out of memory
4826  */
4827  if (head + nxids > pArray->maxKnownAssignedXids)
4828  elog(ERROR, "too many KnownAssignedXids");
4829  }
4830 
4831  /* Now we can insert the xids into the space starting at head */
4832  next_xid = from_xid;
4833  for (i = 0; i < nxids; i++)
4834  {
4835  KnownAssignedXids[head] = next_xid;
4836  KnownAssignedXidsValid[head] = true;
4837  TransactionIdAdvance(next_xid);
4838  head++;
4839  }
4840 
4841  /* Adjust count of number of valid entries */
4842  pArray->numKnownAssignedXids += nxids;
4843 
4844  /*
4845  * Now update the head pointer. We use a write barrier to ensure that
4846  * other processors see the above array updates before they see the head
4847  * pointer change. The barrier isn't required if we're holding
4848  * ProcArrayLock exclusively.
4849  */
4850  if (!exclusive_lock)
4851  pg_write_barrier();
4852 
4853  pArray->headKnownAssignedXids = head;
4854 }
4855 
4856 /*
4857  * KnownAssignedXidsSearch
4858  *
4859  * Searches KnownAssignedXids for a specific xid and optionally removes it.
4860  * Returns true if it was found, false if not.
4861  *
4862  * Caller must hold ProcArrayLock in shared or exclusive mode.
4863  * Exclusive lock must be held for remove = true.
4864  */
4865 static bool
4867 {
4868  ProcArrayStruct *pArray = procArray;
4869  int first,
4870  last;
4871  int head;
4872  int tail;
4873  int result_index = -1;
4874 
4875  tail = pArray->tailKnownAssignedXids;
4876  head = pArray->headKnownAssignedXids;
4877 
4878  /*
4879  * Only the startup process removes entries, so we don't need the read
4880  * barrier in that case.
4881  */
4882  if (!remove)
4883  pg_read_barrier(); /* pairs with KnownAssignedXidsAdd */
4884 
4885  /*
4886  * Standard binary search. Note we can ignore the KnownAssignedXidsValid
4887  * array here, since even invalid entries will contain sorted XIDs.
4888  */
4889  first = tail;
4890  last = head - 1;
4891  while (first <= last)
4892  {
4893  int mid_index;
4894  TransactionId mid_xid;
4895 
4896  mid_index = (first + last) / 2;
4897  mid_xid = KnownAssignedXids[mid_index];
4898 
4899  if (xid == mid_xid)
4900  {
4901  result_index = mid_index;
4902  break;
4903  }
4904  else if (TransactionIdPrecedes(xid, mid_xid))
4905  last = mid_index - 1;
4906  else
4907  first = mid_index + 1;
4908  }
4909 
4910  if (result_index < 0)
4911  return false; /* not in array */
4912 
4913  if (!KnownAssignedXidsValid[result_index])
4914  return false; /* in array, but invalid */
4915 
4916  if (remove)
4917  {
4918  KnownAssignedXidsValid[result_index] = false;
4919 
4920  pArray->numKnownAssignedXids--;
4921  Assert(pArray->numKnownAssignedXids >= 0);
4922 
4923  /*
4924  * If we're removing the tail element then advance tail pointer over
4925  * any invalid elements. This will speed future searches.
4926  */
4927  if (result_index == tail)
4928  {
4929  tail++;
4930  while (tail < head && !KnownAssignedXidsValid[tail])
4931  tail++;
4932  if (tail >= head)
4933  {
4934  /* Array is empty, so we can reset both pointers */
4935  pArray->headKnownAssignedXids = 0;
4936  pArray->tailKnownAssignedXids = 0;
4937  }
4938  else
4939  {
4940  pArray->tailKnownAssignedXids = tail;
4941  }
4942  }
4943  }
4944 
4945  return true;
4946 }
4947 
4948 /*
4949  * Is the specified XID present in KnownAssignedXids[]?
4950  *
4951  * Caller must hold ProcArrayLock in shared or exclusive mode.
4952  */
4953 static bool
4955 {
4957 
4958  return KnownAssignedXidsSearch(xid, false);
4959 }
4960 
4961 /*
4962  * Remove the specified XID from KnownAssignedXids[].
4963  *
4964  * Caller must hold ProcArrayLock in exclusive mode.
4965  */
4966 static void
4968 {
4970 
4971  elog(DEBUG4, "remove KnownAssignedXid %u", xid);
4972 
4973  /*
4974  * Note: we cannot consider it an error to remove an XID that's not
4975  * present. We intentionally remove subxact IDs while processing
4976  * XLOG_XACT_ASSIGNMENT, to avoid array overflow. Then those XIDs will be
4977  * removed again when the top-level xact commits or aborts.
4978  *
4979  * It might be possible to track such XIDs to distinguish this case from
4980  * actual errors, but it would be complicated and probably not worth it.
4981  * So, just ignore the search result.
4982  */
4983  (void) KnownAssignedXidsSearch(xid, true);
4984 }
4985 
4986 /*
4987  * KnownAssignedXidsRemoveTree
4988  * Remove xid (if it's not InvalidTransactionId) and all the subxids.
4989  *
4990  * Caller must hold ProcArrayLock in exclusive mode.
4991  */
4992 static void
4994  TransactionId *subxids)
4995 {
4996  int i;
4997 
4998  if (TransactionIdIsValid(xid))
5000 
5001  for (i = 0; i < nsubxids; i++)
5002  KnownAssignedXidsRemove(subxids[i]);
5003 
5004  /* Opportunistically compress the array */
5006 }
5007 
5008 /*
5009  * Prune KnownAssignedXids up to, but *not* including xid. If xid is invalid
5010  * then clear the whole table.
5011  *
5012  * Caller must hold ProcArrayLock in exclusive mode.
5013  */
5014 static void
5016 {
5017  ProcArrayStruct *pArray = procArray;
5018  int count = 0;
5019  int head,
5020  tail,
5021  i;
5022 
5023  if (!TransactionIdIsValid(removeXid))
5024  {
5025  elog(DEBUG4, "removing all KnownAssignedXids");
5026  pArray->numKnownAssignedXids = 0;
5027  pArray->headKnownAssignedXids = pArray->tailKnownAssignedXids = 0;
5028  return;
5029  }
5030 
5031  elog(DEBUG4, "prune KnownAssignedXids to %u", removeXid);
5032 
5033  /*
5034  * Mark entries invalid starting at the tail. Since array is sorted, we
5035  * can stop as soon as we reach an entry >= removeXid.
5036  */
5037  tail = pArray->tailKnownAssignedXids;
5038  head = pArray->headKnownAssignedXids;
5039 
5040  for (i = tail; i < head; i++)
5041  {
5043  {
5044  TransactionId knownXid = KnownAssignedXids[i];
5045 
5046  if (TransactionIdFollowsOrEquals(knownXid, removeXid))
5047  break;
5048 
5049  if (!StandbyTransactionIdIsPrepared(knownXid))
5050  {
5051  KnownAssignedXidsValid[i] = false;
5052  count++;
5053  }
5054  }
5055  }
5056 
5057  pArray->numKnownAssignedXids -= count;
5058  Assert(pArray->numKnownAssignedXids >= 0);
5059 
5060  /*
5061  * Advance the tail pointer if we've marked the tail item invalid.
5062  */
5063  for (i = tail; i < head; i++)
5064  {
5066  break;
5067  }
5068  if (i >= head)
5069  {
5070  /* Array is empty, so we can reset both pointers */
5071  pArray->headKnownAssignedXids = 0;
5072  pArray->tailKnownAssignedXids = 0;
5073  }
5074  else
5075  {
5076  pArray->tailKnownAssignedXids = i;
5077  }
5078 
5079  /* Opportunistically compress the array */
5081 }
5082 
5083 /*
5084  * KnownAssignedXidsGet - Get an array of xids by scanning KnownAssignedXids.
5085  * We filter out anything >= xmax.
5086  *
5087  * Returns the number of XIDs stored into xarray[]. Caller is responsible
5088  * that array is large enough.
5089  *
5090  * Caller must hold ProcArrayLock in (at least) shared mode.
5091  */
5092 static int
5094 {
5096 
5097  return KnownAssignedXidsGetAndSetXmin(xarray, &xtmp, xmax);
5098 }
5099 
5100 /*
5101  * KnownAssignedXidsGetAndSetXmin - as KnownAssignedXidsGet, plus
5102  * we reduce *xmin to the lowest xid value seen if not already lower.
5103  *
5104  * Caller must hold ProcArrayLock in (at least) shared mode.
5105  */
5106 static int
5108  TransactionId xmax)
5109 {
5110  int count = 0;
5111  int head,
5112  tail;
5113  int i;
5114 
5115  /*
5116  * Fetch head just once, since it may change while we loop. We can stop
5117  * once we reach the initially seen head, since we are certain that an xid
5118  * cannot enter and then leave the array while we hold ProcArrayLock. We
5119  * might miss newly-added xids, but they should be >= xmax so irrelevant
5120  * anyway.
5121  */
5124 
5125  pg_read_barrier(); /* pairs with KnownAssignedXidsAdd */
5126 
5127  for (i = tail; i < head; i++)
5128  {
5129  /* Skip any gaps in the array */
5131  {
5132  TransactionId knownXid = KnownAssignedXids[i];
5133 
5134  /*
5135  * Update xmin if required. Only the first XID need be checked,
5136  * since the array is sorted.
5137  */
5138  if (count == 0 &&
5139  TransactionIdPrecedes(knownXid, *xmin))
5140  *xmin = knownXid;
5141 
5142  /*
5143  * Filter out anything >= xmax, again relying on sorted property
5144  * of array.
5145  */
5146  if (TransactionIdIsValid(xmax) &&
5147  TransactionIdFollowsOrEquals(knownXid, xmax))
5148  break;
5149 
5150  /* Add knownXid into output array */
5151  xarray[count++] = knownXid;
5152  }
5153  }
5154 
5155  return count;
5156 }
5157 
5158 /*
5159  * Get oldest XID in the KnownAssignedXids array, or InvalidTransactionId
5160  * if nothing there.
5161  */
5162 static TransactionId
5164 {
5165  int head,
5166  tail;
5167  int i;
5168 
5169  /*
5170  * Fetch head just once, since it may change while we loop.
5171  */
5174 
5175  pg_read_barrier(); /* pairs with KnownAssignedXidsAdd */
5176 
5177  for (i = tail; i < head; i++)
5178  {
5179  /* Skip any gaps in the array */
5181  return KnownAssignedXids[i];
5182  }
5183 
5184  return InvalidTransactionId;
5185 }
5186 
5187 /*
5188  * Display KnownAssignedXids to provide debug trail
5189  *
5190  * Currently this is only called within startup process, so we need no
5191  * special locking.
5192  *
5193  * Note this is pretty expensive, and much of the expense will be incurred
5194  * even if the elog message will get discarded. It's not currently called
5195  * in any performance-critical places, however, so no need to be tenser.
5196  */
5197 static void
5199 {
5200  ProcArrayStruct *pArray = procArray;
5202  int head,
5203  tail,
5204  i;
5205  int nxids = 0;
5206 
5207  tail = pArray->tailKnownAssignedXids;
5208  head = pArray->headKnownAssignedXids;
5209 
5210  initStringInfo(&buf);
5211 
5212  for (i = tail; i < head; i++)
5213  {
5215  {
5216  nxids++;
5217  appendStringInfo(&buf, "[%d]=%u ", i, KnownAssignedXids[i]);
5218  }
5219  }
5220 
5221  elog(trace_level, "%d KnownAssignedXids (num=%d tail=%d head=%d) %s",
5222  nxids,
5223  pArray->numKnownAssignedXids,
5224  pArray->tailKnownAssignedXids,
5225  pArray->headKnownAssignedXids,
5226  buf.data);
5227 
5228  pfree(buf.data);
5229 }
5230 
5231 /*
5232  * KnownAssignedXidsReset
5233  * Resets KnownAssignedXids to be empty
5234  */
5235 static void
5237 {
5238  ProcArrayStruct *pArray = procArray;
5239 
5240  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
5241 
5242  pArray->numKnownAssignedXids = 0;
5243  pArray->tailKnownAssignedXids = 0;
5244  pArray->headKnownAssignedXids = 0;
5245 
5246  LWLockRelease(ProcArrayLock);
5247 }
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:5060
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:344
#define pg_read_barrier()
Definition: atomics.h:151
#define pg_write_barrier()
Definition: atomics.h:152
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:271
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:234
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:325
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1655
unsigned int uint32
Definition: c.h:493
signed char int8
Definition: c.h:479
#define likely(x)
Definition: c.h:297
signed int int32
Definition: c.h:481
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:385
#define unlikely(x)
Definition: c.h:298
unsigned char uint8
Definition: c.h:491
uint32 TransactionId
Definition: c.h:639
#define OidIsValid(objectId)
Definition: c.h:762
size_t Size
Definition: c.h:592
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:105
int64 TimestampTz
Definition: timestamp.h:39
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3089
int errdetail(const char *fmt,...)
Definition: elog.c:1206
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1298
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1073
#define LOG
Definition: elog.h:31
#define DEBUG3
Definition: elog.h:28
#define FATAL
Definition: elog.h:41
#define WARNING
Definition: elog.h:36
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
#define DEBUG4
Definition: elog.h:27
bool IsUnderPostmaster
Definition: globals.c:116
Oid MyDatabaseId
Definition: globals.c:90
#define malloc(a)
Definition: header.h:50
int j
Definition: isn.c:74
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend_int(List *list, int datum)
Definition: list.c:357
#define VirtualTransactionIdIsValid(vxid)
Definition: lock.h:67
#define GET_VXID_FROM_PGPROC(vxid_dst, proc)
Definition: lock.h:77
#define InvalidLocalTransactionId
Definition: lock.h:65
#define VirtualTransactionIdEquals(vxid1, vxid2)
Definition: lock.h:71
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1900
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1175
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1944
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1788
bool LWLockConditionalAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1346
@ LW_SHARED
Definition: lwlock.h:117
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void pfree(void *pointer)
Definition: mcxt.c:1405
void * palloc(Size size)
Definition: mcxt.c:1201
#define AmStartupProcess()
Definition: miscadmin.h:456
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:419
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
Oid GetUserId(void)
Definition: miscinit.c:515
static bool pg_lfind32(uint32 key, uint32 *base, uint32 nelem)
Definition: pg_lfind.h:90
#define NIL
Definition: pg_list.h:68
#define lfirst_int(lc)
Definition: pg_list.h:173
static char * buf
Definition: pg_test_fsync.c:73
#define fprintf
Definition: port.h:242
#define qsort(a, b, c, d)
Definition: port.h:449
void PGSemaphoreUnlock(PGSemaphore sema)
Definition: posix_sema.c:340
void PGSemaphoreLock(PGSemaphore sema)
Definition: posix_sema.c:320
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
#define NUM_AUXILIARY_PROCS
Definition: proc.h:440
#define PROC_XMIN_FLAGS
Definition: proc.h:72
#define PROC_AFFECTS_ALL_HORIZONS
Definition: proc.h:62
#define PROC_IN_VACUUM
Definition: proc.h:58
#define GetPGProcByNumber(n)
Definition: proc.h:428
#define GetNumberFromPGProc(proc)
Definition: proc.h:429
#define PROC_VACUUM_STATE_MASK
Definition: proc.h:65
#define PROC_IS_AUTOVACUUM
Definition: proc.h:57
KAXCompressReason
Definition: procarray.c:262
@ KAX_PRUNE
Definition: procarray.c:264
@ KAX_NO_SPACE
Definition: procarray.c:263
@ KAX_TRANSACTION_END
Definition: procarray.c:265
@ KAX_STARTUP_PROCESS_IDLE
Definition: procarray.c:266
static GlobalVisState GlobalVisDataRels
Definition: procarray.c:300
bool GlobalVisTestIsRemovableFullXid(GlobalVisState *state, FullTransactionId fxid)
Definition: procarray.c:4196
TransactionId GetOldestNonRemovableTransactionId(Relation rel)
Definition: procarray.c:1994
VirtualTransactionId * GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
Definition: procarray.c:3020
#define TOTAL_MAX_CACHED_SUBXIDS
static GlobalVisState GlobalVisSharedRels
Definition: procarray.c:298
void ProcArrayGetReplicationSlotXmin(TransactionId *xmin, TransactionId *catalog_xmin)
Definition: procarray.c:3942
static GlobalVisState GlobalVisCatalogRels
Definition: procarray.c:299
PGPROC * ProcNumberGetProc(ProcNumber procNumber)
Definition: procarray.c:3115
bool GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid)
Definition: procarray.c:4238
bool GlobalVisCheckRemovableFullXid(Relation rel, FullTransactionId fxid)
Definition: procarray.c:4290
static void KnownAssignedXidsCompress(KAXCompressReason reason, bool haveLock)
Definition: procarray.c:4645
pid_t SignalVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode, bool conflictPending)
Definition: procarray.c:3474
Size ProcArrayShmemSize(void)
Definition: procarray.c:377
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2922
void XidCacheRemoveRunningXids(TransactionId xid, int nxids, const TransactionId *xids, TransactionId latestXid)
Definition: procarray.c:3965
bool TransactionIdIsActive(TransactionId xid)
Definition: procarray.c:1623
static FullTransactionId FullXidRelativeTo(FullTransactionId rel, TransactionId xid)
Definition: procarray.c:4325
bool MinimumActiveBackends(int min)
Definition: procarray.c:3523
void TerminateOtherDBBackends(Oid databaseId)
Definition: procarray.c:3805
#define xc_no_overflow_inc()
Definition: procarray.c:343
static TransactionId standbySnapshotPendingXmin
Definition: procarray.c:291
void ExpireAllKnownAssignedTransactionIds(void)
Definition: procarray.c:4502
#define UINT32_ACCESS_ONCE(var)
Definition: procarray.c:69
VirtualTransactionId * GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
Definition: procarray.c:3394
RunningTransactions GetRunningTransactionData(void)
Definition: procarray.c:2682
TransactionId GetOldestActiveTransactionId(void)
Definition: procarray.c:2857
static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids, TransactionId *subxids)
Definition: procarray.c:4993
static int KnownAssignedXidsGetAndSetXmin(TransactionId *xarray, TransactionId *xmin, TransactionId xmax)
Definition: procarray.c:5107
#define xc_by_recent_xmin_inc()
Definition: procarray.c:336
void ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:668
void ProcNumberGetTransactionIds(ProcNumber procNumber, TransactionId *xid, TransactionId *xmin, int *nsubxid, bool *overflowed)
Definition: procarray.c:3137
static PGPROC * allProcs
Definition: procarray.c:272
void RecordKnownAssignedTransactionIds(TransactionId xid)
Definition: procarray.c:4407
static int KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax)
Definition: procarray.c:5093
TransactionId GetOldestTransactionIdConsideredRunning(void)
Definition: procarray.c:2023
static TransactionId latestObservedXid
Definition: procarray.c:284
static ProcArrayStruct * procArray
Definition: procarray.c:270
int GetMaxSnapshotSubxidCount(void)
Definition: procarray.c:2069
int CountDBConnections(Oid databaseid)
Definition: procarray.c:3606
static GlobalVisState GlobalVisTempRels
Definition: procarray.c:301
#define xc_by_my_xact_inc()
Definition: procarray.c:338
#define xc_by_known_assigned_inc()
Definition: procarray.c:342
struct ProcArrayStruct ProcArrayStruct
void CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
Definition: procarray.c:3637
#define PROCARRAY_MAXPROCS
void GetReplicationHorizons(TransactionId *xmin, TransactionId *catalog_xmin)
Definition: procarray.c:2036
static bool GlobalVisTestShouldUpdate(GlobalVisState *state)
Definition: procarray.c:4121
static void ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:732
static void KnownAssignedXidsRemovePreceding(TransactionId removeXid)
Definition: procarray.c:5015
void ProcArrayAdd(PGPROC *proc)
Definition: procarray.c:469
struct ComputeXidHorizonsResult ComputeXidHorizonsResult
TransactionId GlobalVisTestNonRemovableHorizon(GlobalVisState *state)
Definition: procarray.c:4276
static TransactionId * KnownAssignedXids
Definition: procarray.c:282
#define xc_by_child_xid_inc()
Definition: procarray.c:341
pid_t CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
Definition: procarray.c:3468
Snapshot GetSnapshotData(Snapshot snapshot)
Definition: procarray.c:2166
static bool * KnownAssignedXidsValid
Definition: procarray.c:283
bool HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
Definition: procarray.c:3066
static void KnownAssignedXidsRemove(TransactionId xid)
Definition: procarray.c:4967
void KnownAssignedTransactionIdsIdleMaintenance(void)
Definition: procarray.c:4544
static void GlobalVisUpdateApply(ComputeXidHorizonsResult *horizons)
Definition: procarray.c:4140
int GetMaxSnapshotXidCount(void)
Definition: procarray.c:2058
GlobalVisState * GlobalVisTestFor(Relation rel)
Definition: procarray.c:4081
int CountDBBackends(Oid databaseid)
Definition: procarray.c:3576
bool GlobalVisCheckRemovableXid(Relation rel, TransactionId xid)
Definition: procarray.c:4304
#define MAXAUTOVACPIDS
bool ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
Definition: procarray.c:2609
#define KAX_COMPRESS_FREQUENCY
void CreateSharedProcArray(void)
Definition: procarray.c:419
static TransactionId KnownAssignedXidsGetOldestXmin(void)
Definition: procarray.c:5163
void ProcArrayApplyRecoveryInfo(RunningTransactions running)
Definition: procarray.c:1055
void ProcArrayClearTransaction(PGPROC *proc)
Definition: procarray.c:908
VirtualTransactionId * GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0, bool allDbs, int excludeVacuum, int *nvxids)
Definition: procarray.c:3301
int CountUserBackends(Oid roleid)
Definition: procarray.c:3677
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3173
static TransactionId ComputeXidHorizonsResultLastXmin
Definition: procarray.c:308
static void GlobalVisUpdate(void)
Definition: procarray.c:4179
#define xc_slow_answer_inc()
Definition: procarray.c:344
static void KnownAssignedXidsDisplay(int trace_level)
Definition: procarray.c:5198
#define xc_by_main_xid_inc()
Definition: procarray.c:340
PGPROC * BackendPidGetProcWithLock(int pid)
Definition: procarray.c:3196
static void MaintainLatestCompletedXidRecovery(TransactionId latestXid)
Definition: procarray.c:990
static void ComputeXidHorizons(ComputeXidHorizonsResult *h)
Definition: procarray.c:1724
void ProcArrayApplyXidAssignment(TransactionId topxid, int nsubxids, TransactionId *subxids)
Definition: procarray.c:1307
static bool KnownAssignedXidExists(TransactionId xid)
Definition: procarray.c:4954
bool CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
Definition: procarray.c:3727
GlobalVisHorizonKind
Definition: procarray.c:251
@ VISHORIZON_SHARED
Definition: procarray.c:252
@ VISHORIZON_DATA
Definition: procarray.c:254
@ VISHORIZON_CATALOG
Definition: procarray.c:253
@ VISHORIZON_TEMP
Definition: procarray.c:255
int BackendXidGetPid(TransactionId xid)
Definition: procarray.c:3233
#define xc_by_latest_xid_inc()
Definition: procarray.c:339
bool IsBackendPid(int pid)
Definition: procarray.c:3268
#define xc_by_known_xact_inc()
Definition: procarray.c:337
static bool KnownAssignedXidsSearch(TransactionId xid, bool remove)
Definition: procarray.c:4866
static void KnownAssignedXidsReset(void)
Definition: procarray.c:5236
FullTransactionId GlobalVisTestNonRemovableFullHorizon(GlobalVisState *state)
Definition: procarray.c:4265
static GlobalVisHorizonKind GlobalVisHorizonKindForRel(Relation rel)
Definition: procarray.c:1960
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
Definition: procarray.c:3917
void ProcArrayInitRecovery(TransactionId initializedUptoXID)
Definition: procarray.c:1024
void ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:566
#define KAX_COMPRESS_IDLE_INTERVAL
static void MaintainLatestCompletedXid(TransactionId latestXid)
Definition: procarray.c:968
static void ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:793
void ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids, TransactionId *subxids, TransactionId max_xid)
Definition: procarray.c:4476
static TransactionId cachedXidIsNotInProgress
Definition: procarray.c:277
bool ProcArrayInstallImportedXmin(TransactionId xmin, VirtualTransactionId *sourcevxid)
Definition: procarray.c:2525
static bool GetSnapshotDataReuse(Snapshot snapshot)
Definition: procarray.c:2084
static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid, bool exclusive_lock)
Definition: procarray.c:4762
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1391
void ExpireOldKnownAssignedTransactionIds(TransactionId xid)
Definition: procarray.c:4522
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
int ProcNumber
Definition: procnumber.h:24
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition: procsignal.c:258
ProcSignalReason
Definition: procsignal.h:31
#define RELATION_IS_LOCAL(relation)
Definition: rel.h:647
#define RelationIsAccessibleInLogicalDecoding(relation)
Definition: rel.h:683
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:388
Size mul_size(Size s1, Size s2)
Definition: shmem.c:511
void pg_usleep(long microsec)
Definition: signal.c:53
static pg_noinline void Size size
Definition: slab.c:607
TransactionId RecentXmin
Definition: snapmgr.c:106
TransactionId TransactionXmin
Definition: snapmgr.c:105
PGPROC * MyProc
Definition: proc.c:68
PROC_HDR * ProcGlobal
Definition: proc.c:80
void StandbyReleaseOldLocks(TransactionId oldxid)
Definition: standby.c:1128
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
TransactionId slot_catalog_xmin
Definition: procarray.c:193
TransactionId data_oldest_nonremovable
Definition: procarray.c:238
TransactionId temp_oldest_nonremovable
Definition: procarray.c:244
TransactionId shared_oldest_nonremovable
Definition: procarray.c:215
TransactionId oldest_considered_running
Definition: procarray.c:206
TransactionId slot_xmin
Definition: procarray.c:192
FullTransactionId latest_completed
Definition: procarray.c:186
TransactionId catalog_oldest_nonremovable
Definition: procarray.c:232
TransactionId shared_oldest_nonremovable_raw
Definition: procarray.c:226
FullTransactionId definitely_needed
Definition: procarray.c:171
FullTransactionId maybe_needed
Definition: procarray.c:174
Definition: pg_list.h:54
Definition: proc.h:157
TransactionId xmin
Definition: proc.h:173
bool procArrayGroupMember
Definition: proc.h:265
LocalTransactionId lxid
Definition: proc.h:196
pg_atomic_uint32 procArrayGroupNext
Definition: proc.h:267
uint8 statusFlags
Definition: proc.h:238
bool recoveryConflictPending
Definition: proc.h:216
Oid databaseId
Definition: proc.h:203
struct PGPROC::@112 vxid
ProcNumber procNumber
Definition: proc.h:191
int pid
Definition: proc.h:178
bool isBackgroundWorker
Definition: proc.h:209
int pgxactoff
Definition: proc.h:180
XidCacheStatus subxidStatus
Definition: proc.h:259
LOCK * waitLock
Definition: proc.h:228
TransactionId xid
Definition: proc.h:168
struct XidCache subxids
Definition: proc.h:261
int delayChkptFlags
Definition: proc.h:236
TransactionId procArrayGroupMemberXid
Definition: proc.h:273
PGSemaphore sem
Definition: proc.h:162
Oid roleId
Definition: proc.h:204
Definition: proc.h:378
uint8 * statusFlags
Definition: proc.h:395
XidCacheStatus * subxidStates
Definition: proc.h:389
PGPROC * allProcs
Definition: proc.h:380
TransactionId * xids
Definition: proc.h:383
pg_atomic_uint32 procArrayGroupFirst
Definition: proc.h:408
uint32 allProcCount
Definition: proc.h:398
TransactionId replication_slot_xmin
Definition: procarray.c:95
int maxKnownAssignedXids
Definition: procarray.c:80
TransactionId replication_slot_catalog_xmin
Definition: procarray.c:97
int numKnownAssignedXids
Definition: procarray.c:81
int pgprocnos[FLEXIBLE_ARRAY_MEMBER]
Definition: procarray.c:100
TransactionId lastOverflowedXid
Definition: procarray.c:92
int tailKnownAssignedXids
Definition: procarray.c:82
int headKnownAssignedXids
Definition: procarray.c:83
Form_pg_class rd_rel
Definition: rel.h:111
TransactionId oldestRunningXid
Definition: standby.h:84
TransactionId nextXid
Definition: standby.h:83
TransactionId latestCompletedXid
Definition: standby.h:85
TransactionId * xids
Definition: standby.h:87
TransactionId xmin
Definition: snapshot.h:157
int32 subxcnt
Definition: snapshot.h:181
bool copied
Definition: snapshot.h:185
uint32 regd_count
Definition: snapshot.h:205
uint32 active_count
Definition: snapshot.h:204
CommandId curcid
Definition: snapshot.h:187
TimestampTz whenTaken
Definition: snapshot.h:208
uint32 xcnt
Definition: snapshot.h:169