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