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