1 /*-------------------------------------------------------------------------
4 * POSTGRES process array code.
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.
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.
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.
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.
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.
35 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
36 * Portions Copyright (c) 1994, Regents of the University of California
40 * src/backend/storage/ipc/procarray.c
42 *-------------------------------------------------------------------------
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"
57 #include "storage/proc.h"
58 #include "storage/procarray.h"
59 #include "storage/spin.h"
60 #include "utils/builtins.h"
61 #include "utils/rel.h"
62 #include "utils/snapmgr.h"
65 /* Our shared memory area */
66 typedef struct ProcArrayStruct
68 int numProcs; /* number of valid procs entries */
69 int maxProcs; /* allocated size of procs array */
72 * Known assigned XIDs handling
74 int maxKnownAssignedXids; /* allocated size of array */
75 int numKnownAssignedXids; /* current # of valid entries */
76 int tailKnownAssignedXids; /* index of oldest valid element */
77 int headKnownAssignedXids; /* index of newest element, + 1 */
78 slock_t known_assigned_xids_lck; /* protects head/tail pointers */
81 * Highest subxid that has been removed from KnownAssignedXids array to
82 * prevent overflow; or InvalidTransactionId if none. We track this for
83 * similar reasons to tracking overflowing cached subxids in PGXACT
84 * entries. Must hold exclusive ProcArrayLock to change this, and shared
87 TransactionId lastOverflowedXid;
89 /* oldest xmin of any replication slot */
90 TransactionId replication_slot_xmin;
91 /* oldest catalog xmin of any replication slot */
92 TransactionId replication_slot_catalog_xmin;
94 /* indexes into allPgXact[], has PROCARRAY_MAXPROCS entries */
95 int pgprocnos[FLEXIBLE_ARRAY_MEMBER];
98 static ProcArrayStruct *procArray;
100 static PGPROC *allProcs;
101 static PGXACT *allPgXact;
104 * Bookkeeping for tracking emulated transactions in recovery
106 static TransactionId *KnownAssignedXids;
107 static bool *KnownAssignedXidsValid;
108 static TransactionId latestObservedXid = InvalidTransactionId;
111 * If we're in STANDBY_SNAPSHOT_PENDING state, standbySnapshotPendingXmin is
112 * the highest xid that might still be running that we don't have in
115 static TransactionId standbySnapshotPendingXmin;
117 #ifdef XIDCACHE_DEBUG
119 /* counters for XidCache measurement */
120 static long xc_by_recent_xmin = 0;
121 static long xc_by_known_xact = 0;
122 static long xc_by_my_xact = 0;
123 static long xc_by_latest_xid = 0;
124 static long xc_by_main_xid = 0;
125 static long xc_by_child_xid = 0;
126 static long xc_by_known_assigned = 0;
127 static long xc_no_overflow = 0;
128 static long xc_slow_answer = 0;
130 #define xc_by_recent_xmin_inc() (xc_by_recent_xmin++)
131 #define xc_by_known_xact_inc() (xc_by_known_xact++)
132 #define xc_by_my_xact_inc() (xc_by_my_xact++)
133 #define xc_by_latest_xid_inc() (xc_by_latest_xid++)
134 #define xc_by_main_xid_inc() (xc_by_main_xid++)
135 #define xc_by_child_xid_inc() (xc_by_child_xid++)
136 #define xc_by_known_assigned_inc() (xc_by_known_assigned++)
137 #define xc_no_overflow_inc() (xc_no_overflow++)
138 #define xc_slow_answer_inc() (xc_slow_answer++)
140 static void DisplayXidCache(void);
141 #else /* !XIDCACHE_DEBUG */
143 #define xc_by_recent_xmin_inc() ((void) 0)
144 #define xc_by_known_xact_inc() ((void) 0)
145 #define xc_by_my_xact_inc() ((void) 0)
146 #define xc_by_latest_xid_inc() ((void) 0)
147 #define xc_by_main_xid_inc() ((void) 0)
148 #define xc_by_child_xid_inc() ((void) 0)
149 #define xc_by_known_assigned_inc() ((void) 0)
150 #define xc_no_overflow_inc() ((void) 0)
151 #define xc_slow_answer_inc() ((void) 0)
152 #endif /* XIDCACHE_DEBUG */
154 /* Primitives for KnownAssignedXids array handling for standby */
155 static void KnownAssignedXidsCompress(bool force);
156 static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
157 bool exclusive_lock);
158 static bool KnownAssignedXidsSearch(TransactionId xid, bool remove);
159 static bool KnownAssignedXidExists(TransactionId xid);
160 static void KnownAssignedXidsRemove(TransactionId xid);
161 static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
162 TransactionId *subxids);
163 static void KnownAssignedXidsRemovePreceding(TransactionId xid);
164 static int KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax);
165 static int KnownAssignedXidsGetAndSetXmin(TransactionId *xarray,
168 static TransactionId KnownAssignedXidsGetOldestXmin(void);
169 static void KnownAssignedXidsDisplay(int trace_level);
170 static void KnownAssignedXidsReset(void);
171 static inline void ProcArrayEndTransactionInternal(PGPROC *proc,
172 PGXACT *pgxact, TransactionId latestXid);
173 static void ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid);
176 * Report shared-memory space needed by CreateSharedProcArray.
179 ProcArrayShmemSize(void)
183 /* Size of the ProcArray structure itself */
184 #define PROCARRAY_MAXPROCS (MaxBackends + max_prepared_xacts)
186 size = offsetof(ProcArrayStruct, pgprocnos);
187 size = add_size(size, mul_size(sizeof(int), PROCARRAY_MAXPROCS));
190 * During Hot Standby processing we have a data structure called
191 * KnownAssignedXids, created in shared memory. Local data structures are
192 * also created in various backends during GetSnapshotData(),
193 * TransactionIdIsInProgress() and GetRunningTransactionData(). All of the
194 * main structures created in those functions must be identically sized,
195 * since we may at times copy the whole of the data structures around. We
196 * refer to this size as TOTAL_MAX_CACHED_SUBXIDS.
198 * Ideally we'd only create this structure if we were actually doing hot
199 * standby in the current run, but we don't know that yet at the time
200 * shared memory is being set up.
202 #define TOTAL_MAX_CACHED_SUBXIDS \
203 ((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
205 if (EnableHotStandby)
207 size = add_size(size,
208 mul_size(sizeof(TransactionId),
209 TOTAL_MAX_CACHED_SUBXIDS));
210 size = add_size(size,
211 mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS));
218 * Initialize the shared PGPROC array during postmaster startup.
221 CreateSharedProcArray(void)
225 /* Create or attach to the ProcArray shared structure */
226 procArray = (ProcArrayStruct *)
227 ShmemInitStruct("Proc Array",
228 add_size(offsetof(ProcArrayStruct, pgprocnos),
229 mul_size(sizeof(int),
230 PROCARRAY_MAXPROCS)),
236 * We're the first - initialize.
238 procArray->numProcs = 0;
239 procArray->maxProcs = PROCARRAY_MAXPROCS;
240 procArray->replication_slot_xmin = InvalidTransactionId;
241 procArray->maxKnownAssignedXids = TOTAL_MAX_CACHED_SUBXIDS;
242 procArray->numKnownAssignedXids = 0;
243 procArray->tailKnownAssignedXids = 0;
244 procArray->headKnownAssignedXids = 0;
245 SpinLockInit(&procArray->known_assigned_xids_lck);
246 procArray->lastOverflowedXid = InvalidTransactionId;
249 allProcs = ProcGlobal->allProcs;
250 allPgXact = ProcGlobal->allPgXact;
252 /* Create or attach to the KnownAssignedXids arrays too, if needed */
253 if (EnableHotStandby)
255 KnownAssignedXids = (TransactionId *)
256 ShmemInitStruct("KnownAssignedXids",
257 mul_size(sizeof(TransactionId),
258 TOTAL_MAX_CACHED_SUBXIDS),
260 KnownAssignedXidsValid = (bool *)
261 ShmemInitStruct("KnownAssignedXidsValid",
262 mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS),
266 /* Register and initialize fields of ProcLWLockTranche */
267 LWLockRegisterTranche(LWTRANCHE_PROC, "proc");
271 * Add the specified PGPROC to the shared array.
274 ProcArrayAdd(PGPROC *proc)
276 ProcArrayStruct *arrayP = procArray;
279 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
281 if (arrayP->numProcs >= arrayP->maxProcs)
284 * Oops, no room. (This really shouldn't happen, since there is a
285 * fixed supply of PGPROC structs too, and so we should have failed
288 LWLockRelease(ProcArrayLock);
290 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
291 errmsg("sorry, too many clients already")));
295 * Keep the procs array sorted by (PGPROC *) so that we can utilize
296 * locality of references much better. This is useful while traversing the
297 * ProcArray because there is an increased likelihood of finding the next
298 * PGPROC structure in the cache.
300 * Since the occurrence of adding/removing a proc is much lower than the
301 * access to the ProcArray itself, the overhead should be marginal
303 for (index = 0; index < arrayP->numProcs; index++)
306 * If we are the first PGPROC or if we have found our right position
307 * in the array, break
309 if ((arrayP->pgprocnos[index] == -1) || (arrayP->pgprocnos[index] > proc->pgprocno))
313 memmove(&arrayP->pgprocnos[index + 1], &arrayP->pgprocnos[index],
314 (arrayP->numProcs - index) * sizeof(int));
315 arrayP->pgprocnos[index] = proc->pgprocno;
318 LWLockRelease(ProcArrayLock);
322 * Remove the specified PGPROC from the shared array.
324 * When latestXid is a valid XID, we are removing a live 2PC gxact from the
325 * array, and thus causing it to appear as "not running" anymore. In this
326 * case we must advance latestCompletedXid. (This is essentially the same
327 * as ProcArrayEndTransaction followed by removal of the PGPROC, but we take
328 * the ProcArrayLock only once, and don't damage the content of the PGPROC;
329 * twophase.c depends on the latter.)
332 ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
334 ProcArrayStruct *arrayP = procArray;
337 #ifdef XIDCACHE_DEBUG
338 /* dump stats at backend shutdown, but not prepared-xact end */
343 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
345 if (TransactionIdIsValid(latestXid))
347 Assert(TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
349 /* Advance global latestCompletedXid while holding the lock */
350 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
352 ShmemVariableCache->latestCompletedXid = latestXid;
356 /* Shouldn't be trying to remove a live transaction here */
357 Assert(!TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
360 for (index = 0; index < arrayP->numProcs; index++)
362 if (arrayP->pgprocnos[index] == proc->pgprocno)
364 /* Keep the PGPROC array sorted. See notes above */
365 memmove(&arrayP->pgprocnos[index], &arrayP->pgprocnos[index + 1],
366 (arrayP->numProcs - index - 1) * sizeof(int));
367 arrayP->pgprocnos[arrayP->numProcs - 1] = -1; /* for debugging */
369 LWLockRelease(ProcArrayLock);
375 LWLockRelease(ProcArrayLock);
377 elog(LOG, "failed to find proc %p in ProcArray", proc);
382 * ProcArrayEndTransaction -- mark a transaction as no longer running
384 * This is used interchangeably for commit and abort cases. The transaction
385 * commit/abort must already be reported to WAL and pg_xact.
387 * proc is currently always MyProc, but we pass it explicitly for flexibility.
388 * latestXid is the latest Xid among the transaction's main XID and
389 * subtransactions, or InvalidTransactionId if it has no XID. (We must ask
390 * the caller to pass latestXid, instead of computing it from the PGPROC's
391 * contents, because the subxid information in the PGPROC might be
395 ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
397 PGXACT *pgxact = &allPgXact[proc->pgprocno];
399 if (TransactionIdIsValid(latestXid))
402 * We must lock ProcArrayLock while clearing our advertised XID, so
403 * that we do not exit the set of "running" transactions while someone
404 * else is taking a snapshot. See discussion in
405 * src/backend/access/transam/README.
407 Assert(TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
410 * If we can immediately acquire ProcArrayLock, we clear our own XID
411 * and release the lock. If not, use group XID clearing to improve
414 if (LWLockConditionalAcquire(ProcArrayLock, LW_EXCLUSIVE))
416 ProcArrayEndTransactionInternal(proc, pgxact, latestXid);
417 LWLockRelease(ProcArrayLock);
420 ProcArrayGroupClearXid(proc, latestXid);
425 * If we have no XID, we don't need to lock, since we won't affect
426 * anyone else's calculation of a snapshot. We might change their
427 * estimate of global xmin, but that's OK.
429 Assert(!TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
431 proc->lxid = InvalidLocalTransactionId;
432 pgxact->xmin = InvalidTransactionId;
433 /* must be cleared with xid/xmin: */
434 pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
435 pgxact->delayChkpt = false; /* be sure this is cleared in abort */
436 proc->recoveryConflictPending = false;
438 Assert(pgxact->nxids == 0);
439 Assert(pgxact->overflowed == false);
444 * Mark a write transaction as no longer running.
446 * We don't do any locking here; caller must handle that.
449 ProcArrayEndTransactionInternal(PGPROC *proc, PGXACT *pgxact,
450 TransactionId latestXid)
452 pgxact->xid = InvalidTransactionId;
453 proc->lxid = InvalidLocalTransactionId;
454 pgxact->xmin = InvalidTransactionId;
455 /* must be cleared with xid/xmin: */
456 pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
457 pgxact->delayChkpt = false; /* be sure this is cleared in abort */
458 proc->recoveryConflictPending = false;
460 /* Clear the subtransaction-XID cache too while holding the lock */
462 pgxact->overflowed = false;
464 /* Also advance global latestCompletedXid while holding the lock */
465 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
467 ShmemVariableCache->latestCompletedXid = latestXid;
471 * ProcArrayGroupClearXid -- group XID clearing
473 * When we cannot immediately acquire ProcArrayLock in exclusive mode at
474 * commit time, add ourselves to a list of processes that need their XIDs
475 * cleared. The first process to add itself to the list will acquire
476 * ProcArrayLock in exclusive mode and perform ProcArrayEndTransactionInternal
477 * on behalf of all group members. This avoids a great deal of contention
478 * around ProcArrayLock when many processes are trying to commit at once,
479 * since the lock need not be repeatedly handed off from one committing
480 * process to the next.
483 ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
485 volatile PROC_HDR *procglobal = ProcGlobal;
489 /* We should definitely have an XID to clear. */
490 Assert(TransactionIdIsValid(allPgXact[proc->pgprocno].xid));
492 /* Add ourselves to the list of processes needing a group XID clear. */
493 proc->procArrayGroupMember = true;
494 proc->procArrayGroupMemberXid = latestXid;
497 nextidx = pg_atomic_read_u32(&procglobal->procArrayGroupFirst);
498 pg_atomic_write_u32(&proc->procArrayGroupNext, nextidx);
500 if (pg_atomic_compare_exchange_u32(&procglobal->procArrayGroupFirst,
502 (uint32) proc->pgprocno))
507 * If the list was not empty, the leader will clear our XID. It is
508 * impossible to have followers without a leader because the first process
509 * that has added itself to the list will always have nextidx as
512 if (nextidx != INVALID_PGPROCNO)
516 /* Sleep until the leader clears our XID. */
517 pgstat_report_wait_start(WAIT_EVENT_PROCARRAY_GROUP_UPDATE);
520 /* acts as a read barrier */
521 PGSemaphoreLock(proc->sem);
522 if (!proc->procArrayGroupMember)
526 pgstat_report_wait_end();
528 Assert(pg_atomic_read_u32(&proc->procArrayGroupNext) == INVALID_PGPROCNO);
530 /* Fix semaphore count for any absorbed wakeups */
531 while (extraWaits-- > 0)
532 PGSemaphoreUnlock(proc->sem);
536 /* We are the leader. Acquire the lock on behalf of everyone. */
537 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
540 * Now that we've got the lock, clear the list of processes waiting for
541 * group XID clearing, saving a pointer to the head of the list. Trying
542 * to pop elements one at a time could lead to an ABA problem.
546 nextidx = pg_atomic_read_u32(&procglobal->procArrayGroupFirst);
547 if (pg_atomic_compare_exchange_u32(&procglobal->procArrayGroupFirst,
553 /* Remember head of list so we can perform wakeups after dropping lock. */
556 /* Walk the list and clear all XIDs. */
557 while (nextidx != INVALID_PGPROCNO)
559 PGPROC *proc = &allProcs[nextidx];
560 PGXACT *pgxact = &allPgXact[nextidx];
562 ProcArrayEndTransactionInternal(proc, pgxact, proc->procArrayGroupMemberXid);
564 /* Move to next proc in list. */
565 nextidx = pg_atomic_read_u32(&proc->procArrayGroupNext);
568 /* We're done with the lock now. */
569 LWLockRelease(ProcArrayLock);
572 * Now that we've released the lock, go back and wake everybody up. We
573 * don't do this under the lock so as to keep lock hold times to a
574 * minimum. The system calls we need to perform to wake other processes
575 * up are probably much slower than the simple memory writes we did while
578 while (wakeidx != INVALID_PGPROCNO)
580 PGPROC *proc = &allProcs[wakeidx];
582 wakeidx = pg_atomic_read_u32(&proc->procArrayGroupNext);
583 pg_atomic_write_u32(&proc->procArrayGroupNext, INVALID_PGPROCNO);
585 /* ensure all previous writes are visible before follower continues. */
588 proc->procArrayGroupMember = false;
591 PGSemaphoreUnlock(proc->sem);
596 * ProcArrayClearTransaction -- clear the transaction fields
598 * This is used after successfully preparing a 2-phase transaction. We are
599 * not actually reporting the transaction's XID as no longer running --- it
600 * will still appear as running because the 2PC's gxact is in the ProcArray
601 * too. We just have to clear out our own PGXACT.
604 ProcArrayClearTransaction(PGPROC *proc)
606 PGXACT *pgxact = &allPgXact[proc->pgprocno];
609 * We can skip locking ProcArrayLock here, because this action does not
610 * actually change anyone's view of the set of running XIDs: our entry is
611 * duplicate with the gxact that has already been inserted into the
614 pgxact->xid = InvalidTransactionId;
615 proc->lxid = InvalidLocalTransactionId;
616 pgxact->xmin = InvalidTransactionId;
617 proc->recoveryConflictPending = false;
619 /* redundant, but just in case */
620 pgxact->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
621 pgxact->delayChkpt = false;
623 /* Clear the subtransaction-XID cache too */
625 pgxact->overflowed = false;
629 * ProcArrayInitRecovery -- initialize recovery xid mgmt environment
631 * Remember up to where the startup process initialized the CLOG and subtrans
632 * so we can ensure it's initialized gaplessly up to the point where necessary
636 ProcArrayInitRecovery(TransactionId initializedUptoXID)
638 Assert(standbyState == STANDBY_INITIALIZED);
639 Assert(TransactionIdIsNormal(initializedUptoXID));
642 * we set latestObservedXid to the xid SUBTRANS has been initialized up
643 * to, so we can extend it from that point onwards in
644 * RecordKnownAssignedTransactionIds, and when we get consistent in
645 * ProcArrayApplyRecoveryInfo().
647 latestObservedXid = initializedUptoXID;
648 TransactionIdRetreat(latestObservedXid);
652 * ProcArrayApplyRecoveryInfo -- apply recovery info about xids
654 * Takes us through 3 states: Initialized, Pending and Ready.
655 * Normal case is to go all the way to Ready straight away, though there
656 * are atypical cases where we need to take it in steps.
658 * Use the data about running transactions on master to create the initial
659 * state of KnownAssignedXids. We also use these records to regularly prune
660 * KnownAssignedXids because we know it is possible that some transactions
661 * with FATAL errors fail to write abort records, which could cause eventual
664 * See comments for LogStandbySnapshot().
667 ProcArrayApplyRecoveryInfo(RunningTransactions running)
671 TransactionId nextXid;
674 Assert(standbyState >= STANDBY_INITIALIZED);
675 Assert(TransactionIdIsValid(running->nextXid));
676 Assert(TransactionIdIsValid(running->oldestRunningXid));
677 Assert(TransactionIdIsNormal(running->latestCompletedXid));
680 * Remove stale transactions, if any.
682 ExpireOldKnownAssignedTransactionIds(running->oldestRunningXid);
685 * Remove stale locks, if any.
687 * Locks are always assigned to the toplevel xid so we don't need to care
688 * about subxcnt/subxids (and by extension not about ->suboverflowed).
690 StandbyReleaseOldLocks(running->xcnt, running->xids);
693 * If our snapshot is already valid, nothing else to do...
695 if (standbyState == STANDBY_SNAPSHOT_READY)
699 * If our initial RunningTransactionsData had an overflowed snapshot then
700 * we knew we were missing some subxids from our snapshot. If we continue
701 * to see overflowed snapshots then we might never be able to start up, so
702 * we make another test to see if our snapshot is now valid. We know that
703 * the missing subxids are equal to or earlier than nextXid. After we
704 * initialise we continue to apply changes during recovery, so once the
705 * oldestRunningXid is later than the nextXid from the initial snapshot we
706 * know that we no longer have missing information and can mark the
709 if (standbyState == STANDBY_SNAPSHOT_PENDING)
712 * If the snapshot isn't overflowed or if its empty we can reset our
713 * pending state and use this snapshot instead.
715 if (!running->subxid_overflow || running->xcnt == 0)
718 * If we have already collected known assigned xids, we need to
719 * throw them away before we apply the recovery snapshot.
721 KnownAssignedXidsReset();
722 standbyState = STANDBY_INITIALIZED;
726 if (TransactionIdPrecedes(standbySnapshotPendingXmin,
727 running->oldestRunningXid))
729 standbyState = STANDBY_SNAPSHOT_READY;
730 elog(trace_recovery(DEBUG1),
731 "recovery snapshots are now enabled");
734 elog(trace_recovery(DEBUG1),
735 "recovery snapshot waiting for non-overflowed snapshot or "
736 "until oldest active xid on standby is at least %u (now %u)",
737 standbySnapshotPendingXmin,
738 running->oldestRunningXid);
743 Assert(standbyState == STANDBY_INITIALIZED);
746 * OK, we need to initialise from the RunningTransactionsData record.
748 * NB: this can be reached at least twice, so make sure new code can deal
753 * Nobody else is running yet, but take locks anyhow
755 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
758 * KnownAssignedXids is sorted so we cannot just add the xids, we have to
761 * Some of the new xids are top-level xids and some are subtransactions.
762 * We don't call SubtransSetParent because it doesn't matter yet. If we
763 * aren't overflowed then all xids will fit in snapshot and so we don't
764 * need subtrans. If we later overflow, an xid assignment record will add
765 * xids to subtrans. If RunningXacts is overflowed then we don't have
766 * enough information to correctly update subtrans anyway.
770 * Allocate a temporary array to avoid modifying the array passed as
773 xids = palloc(sizeof(TransactionId) * (running->xcnt + running->subxcnt));
776 * Add to the temp array any xids which have not already completed.
779 for (i = 0; i < running->xcnt + running->subxcnt; i++)
781 TransactionId xid = running->xids[i];
784 * The running-xacts snapshot can contain xids that were still visible
785 * in the procarray when the snapshot was taken, but were already
786 * WAL-logged as completed. They're not running anymore, so ignore
789 if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
797 if (procArray->numKnownAssignedXids != 0)
799 LWLockRelease(ProcArrayLock);
800 elog(ERROR, "KnownAssignedXids is not empty");
804 * Sort the array so that we can add them safely into
807 qsort(xids, nxids, sizeof(TransactionId), xidComparator);
810 * Add the sorted snapshot into KnownAssignedXids
812 for (i = 0; i < nxids; i++)
813 KnownAssignedXidsAdd(xids[i], xids[i], true);
815 KnownAssignedXidsDisplay(trace_recovery(DEBUG3));
821 * latestObservedXid is at least set to the point where SUBTRANS was
822 * started up to (c.f. ProcArrayInitRecovery()) or to the biggest xid
823 * RecordKnownAssignedTransactionIds() was called for. Initialize
824 * subtrans from thereon, up to nextXid - 1.
826 * We need to duplicate parts of RecordKnownAssignedTransactionId() here,
827 * because we've just added xids to the known assigned xids machinery that
828 * haven't gone through RecordKnownAssignedTransactionId().
830 Assert(TransactionIdIsNormal(latestObservedXid));
831 TransactionIdAdvance(latestObservedXid);
832 while (TransactionIdPrecedes(latestObservedXid, running->nextXid))
834 ExtendSUBTRANS(latestObservedXid);
835 TransactionIdAdvance(latestObservedXid);
837 TransactionIdRetreat(latestObservedXid); /* = running->nextXid - 1 */
840 * Now we've got the running xids we need to set the global values that
841 * are used to track snapshots as they evolve further.
843 * - latestCompletedXid which will be the xmax for snapshots
844 * - lastOverflowedXid which shows whether snapshots overflow
847 * If the snapshot overflowed, then we still initialise with what we know,
848 * but the recovery snapshot isn't fully valid yet because we know there
849 * are some subxids missing. We don't know the specific subxids that are
850 * missing, so conservatively assume the last one is latestObservedXid.
853 if (running->subxid_overflow)
855 standbyState = STANDBY_SNAPSHOT_PENDING;
857 standbySnapshotPendingXmin = latestObservedXid;
858 procArray->lastOverflowedXid = latestObservedXid;
862 standbyState = STANDBY_SNAPSHOT_READY;
864 standbySnapshotPendingXmin = InvalidTransactionId;
868 * If a transaction wrote a commit record in the gap between taking and
869 * logging the snapshot then latestCompletedXid may already be higher than
870 * the value from the snapshot, so check before we use the incoming value.
872 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
873 running->latestCompletedXid))
874 ShmemVariableCache->latestCompletedXid = running->latestCompletedXid;
876 Assert(TransactionIdIsNormal(ShmemVariableCache->latestCompletedXid));
878 LWLockRelease(ProcArrayLock);
881 * ShmemVariableCache->nextXid must be beyond any observed xid.
883 * We don't expect anyone else to modify nextXid, hence we don't need to
884 * hold a lock while examining it. We still acquire the lock to modify
887 nextXid = latestObservedXid;
888 TransactionIdAdvance(nextXid);
889 if (TransactionIdFollows(nextXid, ShmemVariableCache->nextXid))
891 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
892 ShmemVariableCache->nextXid = nextXid;
893 LWLockRelease(XidGenLock);
896 Assert(TransactionIdIsValid(ShmemVariableCache->nextXid));
898 KnownAssignedXidsDisplay(trace_recovery(DEBUG3));
899 if (standbyState == STANDBY_SNAPSHOT_READY)
900 elog(trace_recovery(DEBUG1), "recovery snapshots are now enabled");
902 elog(trace_recovery(DEBUG1),
903 "recovery snapshot waiting for non-overflowed snapshot or "
904 "until oldest active xid on standby is at least %u (now %u)",
905 standbySnapshotPendingXmin,
906 running->oldestRunningXid);
910 * ProcArrayApplyXidAssignment
911 * Process an XLOG_XACT_ASSIGNMENT WAL record
914 ProcArrayApplyXidAssignment(TransactionId topxid,
915 int nsubxids, TransactionId *subxids)
917 TransactionId max_xid;
920 Assert(standbyState >= STANDBY_INITIALIZED);
922 max_xid = TransactionIdLatest(topxid, nsubxids, subxids);
925 * Mark all the subtransactions as observed.
927 * NOTE: This will fail if the subxid contains too many previously
928 * unobserved xids to fit into known-assigned-xids. That shouldn't happen
929 * as the code stands, because xid-assignment records should never contain
930 * more than PGPROC_MAX_CACHED_SUBXIDS entries.
932 RecordKnownAssignedTransactionIds(max_xid);
935 * Notice that we update pg_subtrans with the top-level xid, rather than
936 * the parent xid. This is a difference between normal processing and
937 * recovery, yet is still correct in all cases. The reason is that
938 * subtransaction commit is not marked in clog until commit processing, so
939 * all aborted subtransactions have already been clearly marked in clog.
940 * As a result we are able to refer directly to the top-level
941 * transaction's state rather than skipping through all the intermediate
942 * states in the subtransaction tree. This should be the first time we
943 * have attempted to SubTransSetParent().
945 for (i = 0; i < nsubxids; i++)
946 SubTransSetParent(subxids[i], topxid);
948 /* KnownAssignedXids isn't maintained yet, so we're done for now */
949 if (standbyState == STANDBY_INITIALIZED)
953 * Uses same locking as transaction commit
955 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
958 * Remove subxids from known-assigned-xacts.
960 KnownAssignedXidsRemoveTree(InvalidTransactionId, nsubxids, subxids);
963 * Advance lastOverflowedXid to be at least the last of these subxids.
965 if (TransactionIdPrecedes(procArray->lastOverflowedXid, max_xid))
966 procArray->lastOverflowedXid = max_xid;
968 LWLockRelease(ProcArrayLock);
972 * TransactionIdIsInProgress -- is given transaction running in some backend
974 * Aside from some shortcuts such as checking RecentXmin and our own Xid,
975 * there are four possibilities for finding a running transaction:
977 * 1. The given Xid is a main transaction Id. We will find this out cheaply
978 * by looking at the PGXACT struct for each backend.
980 * 2. The given Xid is one of the cached subxact Xids in the PGPROC array.
981 * We can find this out cheaply too.
983 * 3. In Hot Standby mode, we must search the KnownAssignedXids list to see
984 * if the Xid is running on the master.
986 * 4. Search the SubTrans tree to find the Xid's topmost parent, and then see
987 * if that is running according to PGXACT or KnownAssignedXids. This is the
988 * slowest way, but sadly it has to be done always if the others failed,
989 * unless we see that the cached subxact sets are complete (none have
992 * ProcArrayLock has to be held while we do 1, 2, 3. If we save the top Xids
993 * while doing 1 and 3, we can release the ProcArrayLock while we do 4.
994 * This buys back some concurrency (and we can't retrieve the main Xids from
995 * PGXACT again anyway; see GetNewTransactionId).
998 TransactionIdIsInProgress(TransactionId xid)
1000 static TransactionId *xids = NULL;
1002 ProcArrayStruct *arrayP = procArray;
1003 TransactionId topxid;
1008 * Don't bother checking a transaction older than RecentXmin; it could not
1009 * possibly still be running. (Note: in particular, this guarantees that
1010 * we reject InvalidTransactionId, FrozenTransactionId, etc as not
1013 if (TransactionIdPrecedes(xid, RecentXmin))
1015 xc_by_recent_xmin_inc();
1020 * We may have just checked the status of this transaction, so if it is
1021 * already known to be completed, we can fall out without any access to
1024 if (TransactionIdIsKnownCompleted(xid))
1026 xc_by_known_xact_inc();
1031 * Also, we can handle our own transaction (and subtransactions) without
1032 * any access to shared memory.
1034 if (TransactionIdIsCurrentTransactionId(xid))
1036 xc_by_my_xact_inc();
1041 * If first time through, get workspace to remember main XIDs in. We
1042 * malloc it permanently to avoid repeated palloc/pfree overhead.
1047 * In hot standby mode, reserve enough space to hold all xids in the
1048 * known-assigned list. If we later finish recovery, we no longer need
1049 * the bigger array, but we don't bother to shrink it.
1051 int maxxids = RecoveryInProgress() ? TOTAL_MAX_CACHED_SUBXIDS : arrayP->maxProcs;
1053 xids = (TransactionId *) malloc(maxxids * sizeof(TransactionId));
1056 (errcode(ERRCODE_OUT_OF_MEMORY),
1057 errmsg("out of memory")));
1060 LWLockAcquire(ProcArrayLock, LW_SHARED);
1063 * Now that we have the lock, we can check latestCompletedXid; if the
1064 * target Xid is after that, it's surely still running.
1066 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid, xid))
1068 LWLockRelease(ProcArrayLock);
1069 xc_by_latest_xid_inc();
1073 /* No shortcuts, gotta grovel through the array */
1074 for (i = 0; i < arrayP->numProcs; i++)
1076 int pgprocno = arrayP->pgprocnos[i];
1077 volatile PGPROC *proc = &allProcs[pgprocno];
1078 volatile PGXACT *pgxact = &allPgXact[pgprocno];
1081 /* Ignore my own proc --- dealt with it above */
1085 /* Fetch xid just once - see GetNewTransactionId */
1088 if (!TransactionIdIsValid(pxid))
1092 * Step 1: check the main Xid
1094 if (TransactionIdEquals(pxid, xid))
1096 LWLockRelease(ProcArrayLock);
1097 xc_by_main_xid_inc();
1102 * We can ignore main Xids that are younger than the target Xid, since
1103 * the target could not possibly be their child.
1105 if (TransactionIdPrecedes(xid, pxid))
1109 * Step 2: check the cached child-Xids arrays
1111 for (j = pgxact->nxids - 1; j >= 0; j--)
1113 /* Fetch xid just once - see GetNewTransactionId */
1114 TransactionId cxid = proc->subxids.xids[j];
1116 if (TransactionIdEquals(cxid, xid))
1118 LWLockRelease(ProcArrayLock);
1119 xc_by_child_xid_inc();
1125 * Save the main Xid for step 4. We only need to remember main Xids
1126 * that have uncached children. (Note: there is no race condition
1127 * here because the overflowed flag cannot be cleared, only set, while
1128 * we hold ProcArrayLock. So we can't miss an Xid that we need to
1131 if (pgxact->overflowed)
1132 xids[nxids++] = pxid;
1136 * Step 3: in hot standby mode, check the known-assigned-xids list. XIDs
1137 * in the list must be treated as running.
1139 if (RecoveryInProgress())
1141 /* none of the PGXACT entries should have XIDs in hot standby mode */
1144 if (KnownAssignedXidExists(xid))
1146 LWLockRelease(ProcArrayLock);
1147 xc_by_known_assigned_inc();
1152 * If the KnownAssignedXids overflowed, we have to check pg_subtrans
1153 * too. Fetch all xids from KnownAssignedXids that are lower than
1154 * xid, since if xid is a subtransaction its parent will always have a
1155 * lower value. Note we will collect both main and subXIDs here, but
1156 * there's no help for it.
1158 if (TransactionIdPrecedesOrEquals(xid, procArray->lastOverflowedXid))
1159 nxids = KnownAssignedXidsGet(xids, xid);
1162 LWLockRelease(ProcArrayLock);
1165 * If none of the relevant caches overflowed, we know the Xid is not
1166 * running without even looking at pg_subtrans.
1170 xc_no_overflow_inc();
1175 * Step 4: have to check pg_subtrans.
1177 * At this point, we know it's either a subtransaction of one of the Xids
1178 * in xids[], or it's not running. If it's an already-failed
1179 * subtransaction, we want to say "not running" even though its parent may
1180 * still be running. So first, check pg_xact to see if it's been aborted.
1182 xc_slow_answer_inc();
1184 if (TransactionIdDidAbort(xid))
1188 * It isn't aborted, so check whether the transaction tree it belongs to
1189 * is still running (or, more precisely, whether it was running when we
1190 * held ProcArrayLock).
1192 topxid = SubTransGetTopmostTransaction(xid);
1193 Assert(TransactionIdIsValid(topxid));
1194 if (!TransactionIdEquals(topxid, xid))
1196 for (i = 0; i < nxids; i++)
1198 if (TransactionIdEquals(xids[i], topxid))
1207 * TransactionIdIsActive -- is xid the top-level XID of an active backend?
1209 * This differs from TransactionIdIsInProgress in that it ignores prepared
1210 * transactions, as well as transactions running on the master if we're in
1211 * hot standby. Also, we ignore subtransactions since that's not needed
1215 TransactionIdIsActive(TransactionId xid)
1217 bool result = false;
1218 ProcArrayStruct *arrayP = procArray;
1222 * Don't bother checking a transaction older than RecentXmin; it could not
1223 * possibly still be running.
1225 if (TransactionIdPrecedes(xid, RecentXmin))
1228 LWLockAcquire(ProcArrayLock, LW_SHARED);
1230 for (i = 0; i < arrayP->numProcs; i++)
1232 int pgprocno = arrayP->pgprocnos[i];
1233 volatile PGPROC *proc = &allProcs[pgprocno];
1234 volatile PGXACT *pgxact = &allPgXact[pgprocno];
1237 /* Fetch xid just once - see GetNewTransactionId */
1240 if (!TransactionIdIsValid(pxid))
1244 continue; /* ignore prepared transactions */
1246 if (TransactionIdEquals(pxid, xid))
1253 LWLockRelease(ProcArrayLock);
1260 * GetOldestXmin -- returns oldest transaction that was running
1261 * when any current transaction was started.
1263 * If rel is NULL or a shared relation, all backends are considered, otherwise
1264 * only backends running in this database are considered.
1266 * The flags are used to ignore the backends in calculation when any of the
1267 * corresponding flags is set. Typically, if you want to ignore ones with
1268 * PROC_IN_VACUUM flag, you can use PROCARRAY_FLAGS_VACUUM.
1270 * PROCARRAY_SLOTS_XMIN causes GetOldestXmin to ignore the xmin and
1271 * catalog_xmin of any replication slots that exist in the system when
1272 * calculating the oldest xmin.
1274 * This is used by VACUUM to decide which deleted tuples must be preserved in
1275 * the passed in table. For shared relations backends in all databases must be
1276 * considered, but for non-shared relations that's not required, since only
1277 * backends in my own database could ever see the tuples in them. Also, we can
1278 * ignore concurrently running lazy VACUUMs because (a) they must be working
1279 * on other tables, and (b) they don't need to do snapshot-based lookups.
1281 * This is also used to determine where to truncate pg_subtrans. For that
1282 * backends in all databases have to be considered, so rel = NULL has to be
1285 * Note: we include all currently running xids in the set of considered xids.
1286 * This ensures that if a just-started xact has not yet set its snapshot,
1287 * when it does set the snapshot it cannot set xmin less than what we compute.
1288 * See notes in src/backend/access/transam/README.
1290 * Note: despite the above, it's possible for the calculated value to move
1291 * backwards on repeated calls. The calculated value is conservative, so that
1292 * anything older is definitely not considered as running by anyone anymore,
1293 * but the exact value calculated depends on a number of things. For example,
1294 * if rel = NULL and there are no transactions running in the current
1295 * database, GetOldestXmin() returns latestCompletedXid. If a transaction
1296 * begins after that, its xmin will include in-progress transactions in other
1297 * databases that started earlier, so another call will return a lower value.
1298 * Nonetheless it is safe to vacuum a table in the current database with the
1299 * first result. There are also replication-related effects: a walsender
1300 * process can set its xmin based on transactions that are no longer running
1301 * in the master but are still being replayed on the standby, thus possibly
1302 * making the GetOldestXmin reading go backwards. In this case there is a
1303 * possibility that we lose data that the standby would like to have, but
1304 * unless the standby uses a replication slot to make its xmin persistent
1305 * there is little we can do about that --- data is only protected if the
1306 * walsender runs continuously while queries are executed on the standby.
1307 * (The Hot Standby code deals with such cases by failing standby queries
1308 * that needed to access already-removed data, so there's no integrity bug.)
1309 * The return value is also adjusted with vacuum_defer_cleanup_age, so
1310 * increasing that setting on the fly is another easy way to make
1311 * GetOldestXmin() move backwards, with no consequences for data integrity.
1314 GetOldestXmin(Relation rel, int flags)
1316 ProcArrayStruct *arrayP = procArray;
1317 TransactionId result;
1321 volatile TransactionId replication_slot_xmin = InvalidTransactionId;
1322 volatile TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
1325 * If we're not computing a relation specific limit, or if a shared
1326 * relation has been passed in, backends in all databases have to be
1329 allDbs = rel == NULL || rel->rd_rel->relisshared;
1331 /* Cannot look for individual databases during recovery */
1332 Assert(allDbs || !RecoveryInProgress());
1334 LWLockAcquire(ProcArrayLock, LW_SHARED);
1337 * We initialize the MIN() calculation with latestCompletedXid + 1. This
1338 * is a lower bound for the XIDs that might appear in the ProcArray later,
1339 * and so protects us against overestimating the result due to future
1342 result = ShmemVariableCache->latestCompletedXid;
1343 Assert(TransactionIdIsNormal(result));
1344 TransactionIdAdvance(result);
1346 for (index = 0; index < arrayP->numProcs; index++)
1348 int pgprocno = arrayP->pgprocnos[index];
1349 volatile PGPROC *proc = &allProcs[pgprocno];
1350 volatile PGXACT *pgxact = &allPgXact[pgprocno];
1352 if (pgxact->vacuumFlags & (flags & PROCARRAY_PROC_FLAGS_MASK))
1356 proc->databaseId == MyDatabaseId ||
1357 proc->databaseId == 0) /* always include WalSender */
1359 /* Fetch xid just once - see GetNewTransactionId */
1360 TransactionId xid = pgxact->xid;
1362 /* First consider the transaction's own Xid, if any */
1363 if (TransactionIdIsNormal(xid) &&
1364 TransactionIdPrecedes(xid, result))
1368 * Also consider the transaction's Xmin, if set.
1370 * We must check both Xid and Xmin because a transaction might
1371 * have an Xmin but not (yet) an Xid; conversely, if it has an
1372 * Xid, that could determine some not-yet-set Xmin.
1374 xid = pgxact->xmin; /* Fetch just once */
1375 if (TransactionIdIsNormal(xid) &&
1376 TransactionIdPrecedes(xid, result))
1381 /* fetch into volatile var while ProcArrayLock is held */
1382 replication_slot_xmin = procArray->replication_slot_xmin;
1383 replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
1385 if (RecoveryInProgress())
1388 * Check to see whether KnownAssignedXids contains an xid value older
1389 * than the main procarray.
1391 TransactionId kaxmin = KnownAssignedXidsGetOldestXmin();
1393 LWLockRelease(ProcArrayLock);
1395 if (TransactionIdIsNormal(kaxmin) &&
1396 TransactionIdPrecedes(kaxmin, result))
1402 * No other information needed, so release the lock immediately.
1404 LWLockRelease(ProcArrayLock);
1407 * Compute the cutoff XID by subtracting vacuum_defer_cleanup_age,
1408 * being careful not to generate a "permanent" XID.
1410 * vacuum_defer_cleanup_age provides some additional "slop" for the
1411 * benefit of hot standby queries on standby servers. This is quick
1412 * and dirty, and perhaps not all that useful unless the master has a
1413 * predictable transaction rate, but it offers some protection when
1414 * there's no walsender connection. Note that we are assuming
1415 * vacuum_defer_cleanup_age isn't large enough to cause wraparound ---
1416 * so guc.c should limit it to no more than the xidStopLimit threshold
1417 * in varsup.c. Also note that we intentionally don't apply
1418 * vacuum_defer_cleanup_age on standby servers.
1420 result -= vacuum_defer_cleanup_age;
1421 if (!TransactionIdIsNormal(result))
1422 result = FirstNormalTransactionId;
1426 * Check whether there are replication slots requiring an older xmin.
1428 if (!(flags & PROCARRAY_SLOTS_XMIN) &&
1429 TransactionIdIsValid(replication_slot_xmin) &&
1430 NormalTransactionIdPrecedes(replication_slot_xmin, result))
1431 result = replication_slot_xmin;
1434 * After locks have been released and defer_cleanup_age has been applied,
1435 * check whether we need to back up further to make logical decoding
1436 * possible. We need to do so if we're computing the global limit (rel =
1437 * NULL) or if the passed relation is a catalog relation of some kind.
1439 if (!(flags & PROCARRAY_SLOTS_XMIN) &&
1441 RelationIsAccessibleInLogicalDecoding(rel)) &&
1442 TransactionIdIsValid(replication_slot_catalog_xmin) &&
1443 NormalTransactionIdPrecedes(replication_slot_catalog_xmin, result))
1444 result = replication_slot_catalog_xmin;
1450 * GetMaxSnapshotXidCount -- get max size for snapshot XID array
1452 * We have to export this for use by snapmgr.c.
1455 GetMaxSnapshotXidCount(void)
1457 return procArray->maxProcs;
1461 * GetMaxSnapshotSubxidCount -- get max size for snapshot sub-XID array
1463 * We have to export this for use by snapmgr.c.
1466 GetMaxSnapshotSubxidCount(void)
1468 return TOTAL_MAX_CACHED_SUBXIDS;
1472 * GetSnapshotData -- returns information about running transactions.
1474 * The returned snapshot includes xmin (lowest still-running xact ID),
1475 * xmax (highest completed xact ID + 1), and a list of running xact IDs
1476 * in the range xmin <= xid < xmax. It is used as follows:
1477 * All xact IDs < xmin are considered finished.
1478 * All xact IDs >= xmax are considered still running.
1479 * For an xact ID xmin <= xid < xmax, consult list to see whether
1480 * it is considered running or not.
1481 * This ensures that the set of transactions seen as "running" by the
1482 * current xact will not change after it takes the snapshot.
1484 * All running top-level XIDs are included in the snapshot, except for lazy
1485 * VACUUM processes. We also try to include running subtransaction XIDs,
1486 * but since PGPROC has only a limited cache area for subxact XIDs, full
1487 * information may not be available. If we find any overflowed subxid arrays,
1488 * we have to mark the snapshot's subxid data as overflowed, and extra work
1489 * *may* need to be done to determine what's running (see XidInMVCCSnapshot()
1492 * We also update the following backend-global variables:
1493 * TransactionXmin: the oldest xmin of any snapshot in use in the
1494 * current transaction (this is the same as MyPgXact->xmin).
1495 * RecentXmin: the xmin computed for the most recent snapshot. XIDs
1496 * older than this are known not running any more.
1497 * RecentGlobalXmin: the global xmin (oldest TransactionXmin across all
1498 * running transactions, except those running LAZY VACUUM). This is
1499 * the same computation done by
1500 * GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM).
1501 * RecentGlobalDataXmin: the global xmin for non-catalog tables
1502 * >= RecentGlobalXmin
1504 * Note: this function should probably not be called with an argument that's
1505 * not statically allocated (see xip allocation below).
1508 GetSnapshotData(Snapshot snapshot)
1510 ProcArrayStruct *arrayP = procArray;
1513 TransactionId globalxmin;
1517 bool suboverflowed = false;
1518 volatile TransactionId replication_slot_xmin = InvalidTransactionId;
1519 volatile TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
1521 Assert(snapshot != NULL);
1524 * Allocating space for maxProcs xids is usually overkill; numProcs would
1525 * be sufficient. But it seems better to do the malloc while not holding
1526 * the lock, so we can't look at numProcs. Likewise, we allocate much
1527 * more subxip storage than is probably needed.
1529 * This does open a possibility for avoiding repeated malloc/free: since
1530 * maxProcs does not change at runtime, we can simply reuse the previous
1531 * xip arrays if any. (This relies on the fact that all callers pass
1532 * static SnapshotData structs.)
1534 if (snapshot->xip == NULL)
1537 * First call for this snapshot. Snapshot is same size whether or not
1538 * we are in recovery, see later comments.
1540 snapshot->xip = (TransactionId *)
1541 malloc(GetMaxSnapshotXidCount() * sizeof(TransactionId));
1542 if (snapshot->xip == NULL)
1544 (errcode(ERRCODE_OUT_OF_MEMORY),
1545 errmsg("out of memory")));
1546 Assert(snapshot->subxip == NULL);
1547 snapshot->subxip = (TransactionId *)
1548 malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
1549 if (snapshot->subxip == NULL)
1551 (errcode(ERRCODE_OUT_OF_MEMORY),
1552 errmsg("out of memory")));
1556 * It is sufficient to get shared lock on ProcArrayLock, even if we are
1557 * going to set MyPgXact->xmin.
1559 LWLockAcquire(ProcArrayLock, LW_SHARED);
1561 /* xmax is always latestCompletedXid + 1 */
1562 xmax = ShmemVariableCache->latestCompletedXid;
1563 Assert(TransactionIdIsNormal(xmax));
1564 TransactionIdAdvance(xmax);
1566 /* initialize xmin calculation with xmax */
1567 globalxmin = xmin = xmax;
1569 snapshot->takenDuringRecovery = RecoveryInProgress();
1571 if (!snapshot->takenDuringRecovery)
1573 int *pgprocnos = arrayP->pgprocnos;
1577 * Spin over procArray checking xid, xmin, and subxids. The goal is
1578 * to gather all active xids, find the lowest xmin, and try to record
1581 numProcs = arrayP->numProcs;
1582 for (index = 0; index < numProcs; index++)
1584 int pgprocno = pgprocnos[index];
1585 volatile PGXACT *pgxact = &allPgXact[pgprocno];
1589 * Backend is doing logical decoding which manages xmin
1590 * separately, check below.
1592 if (pgxact->vacuumFlags & PROC_IN_LOGICAL_DECODING)
1595 /* Ignore procs running LAZY VACUUM */
1596 if (pgxact->vacuumFlags & PROC_IN_VACUUM)
1599 /* Update globalxmin to be the smallest valid xmin */
1600 xid = pgxact->xmin; /* fetch just once */
1601 if (TransactionIdIsNormal(xid) &&
1602 NormalTransactionIdPrecedes(xid, globalxmin))
1605 /* Fetch xid just once - see GetNewTransactionId */
1609 * If the transaction has no XID assigned, we can skip it; it
1610 * won't have sub-XIDs either. If the XID is >= xmax, we can also
1611 * skip it; such transactions will be treated as running anyway
1612 * (and any sub-XIDs will also be >= xmax).
1614 if (!TransactionIdIsNormal(xid)
1615 || !NormalTransactionIdPrecedes(xid, xmax))
1619 * We don't include our own XIDs (if any) in the snapshot, but we
1620 * must include them in xmin.
1622 if (NormalTransactionIdPrecedes(xid, xmin))
1624 if (pgxact == MyPgXact)
1627 /* Add XID to snapshot. */
1628 snapshot->xip[count++] = xid;
1631 * Save subtransaction XIDs if possible (if we've already
1632 * overflowed, there's no point). Note that the subxact XIDs must
1633 * be later than their parent, so no need to check them against
1634 * xmin. We could filter against xmax, but it seems better not to
1635 * do that much work while holding the ProcArrayLock.
1637 * The other backend can add more subxids concurrently, but cannot
1638 * remove any. Hence it's important to fetch nxids just once.
1639 * Should be safe to use memcpy, though. (We needn't worry about
1640 * missing any xids added concurrently, because they must postdate
1643 * Again, our own XIDs are not included in the snapshot.
1647 if (pgxact->overflowed)
1648 suboverflowed = true;
1651 int nxids = pgxact->nxids;
1655 volatile PGPROC *proc = &allProcs[pgprocno];
1657 memcpy(snapshot->subxip + subcount,
1658 (void *) proc->subxids.xids,
1659 nxids * sizeof(TransactionId));
1669 * We're in hot standby, so get XIDs from KnownAssignedXids.
1671 * We store all xids directly into subxip[]. Here's why:
1673 * In recovery we don't know which xids are top-level and which are
1674 * subxacts, a design choice that greatly simplifies xid processing.
1676 * It seems like we would want to try to put xids into xip[] only, but
1677 * that is fairly small. We would either need to make that bigger or
1678 * to increase the rate at which we WAL-log xid assignment; neither is
1679 * an appealing choice.
1681 * We could try to store xids into xip[] first and then into subxip[]
1682 * if there are too many xids. That only works if the snapshot doesn't
1683 * overflow because we do not search subxip[] in that case. A simpler
1684 * way is to just store all xids in the subxact array because this is
1685 * by far the bigger array. We just leave the xip array empty.
1687 * Either way we need to change the way XidInMVCCSnapshot() works
1688 * depending upon when the snapshot was taken, or change normal
1689 * snapshot processing so it matches.
1691 * Note: It is possible for recovery to end before we finish taking
1692 * the snapshot, and for newly assigned transaction ids to be added to
1693 * the ProcArray. xmax cannot change while we hold ProcArrayLock, so
1694 * those newly added transaction ids would be filtered away, so we
1695 * need not be concerned about them.
1697 subcount = KnownAssignedXidsGetAndSetXmin(snapshot->subxip, &xmin,
1700 if (TransactionIdPrecedesOrEquals(xmin, procArray->lastOverflowedXid))
1701 suboverflowed = true;
1705 /* fetch into volatile var while ProcArrayLock is held */
1706 replication_slot_xmin = procArray->replication_slot_xmin;
1707 replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
1709 if (!TransactionIdIsValid(MyPgXact->xmin))
1710 MyPgXact->xmin = TransactionXmin = xmin;
1712 LWLockRelease(ProcArrayLock);
1715 * Update globalxmin to include actual process xids. This is a slightly
1716 * different way of computing it than GetOldestXmin uses, but should give
1719 if (TransactionIdPrecedes(xmin, globalxmin))
1722 /* Update global variables too */
1723 RecentGlobalXmin = globalxmin - vacuum_defer_cleanup_age;
1724 if (!TransactionIdIsNormal(RecentGlobalXmin))
1725 RecentGlobalXmin = FirstNormalTransactionId;
1727 /* Check whether there's a replication slot requiring an older xmin. */
1728 if (TransactionIdIsValid(replication_slot_xmin) &&
1729 NormalTransactionIdPrecedes(replication_slot_xmin, RecentGlobalXmin))
1730 RecentGlobalXmin = replication_slot_xmin;
1732 /* Non-catalog tables can be vacuumed if older than this xid */
1733 RecentGlobalDataXmin = RecentGlobalXmin;
1736 * Check whether there's a replication slot requiring an older catalog
1739 if (TransactionIdIsNormal(replication_slot_catalog_xmin) &&
1740 NormalTransactionIdPrecedes(replication_slot_catalog_xmin, RecentGlobalXmin))
1741 RecentGlobalXmin = replication_slot_catalog_xmin;
1745 snapshot->xmin = xmin;
1746 snapshot->xmax = xmax;
1747 snapshot->xcnt = count;
1748 snapshot->subxcnt = subcount;
1749 snapshot->suboverflowed = suboverflowed;
1751 snapshot->curcid = GetCurrentCommandId(false);
1754 * This is a new snapshot, so set both refcounts are zero, and mark it as
1755 * not copied in persistent memory.
1757 snapshot->active_count = 0;
1758 snapshot->regd_count = 0;
1759 snapshot->copied = false;
1761 if (old_snapshot_threshold < 0)
1764 * If not using "snapshot too old" feature, fill related fields with
1765 * dummy values that don't require any locking.
1767 snapshot->lsn = InvalidXLogRecPtr;
1768 snapshot->whenTaken = 0;
1773 * Capture the current time and WAL stream location in case this
1774 * snapshot becomes old enough to need to fall back on the special
1775 * "old snapshot" logic.
1777 snapshot->lsn = GetXLogInsertRecPtr();
1778 snapshot->whenTaken = GetSnapshotCurrentTimestamp();
1779 MaintainOldSnapshotTimeMapping(snapshot->whenTaken, xmin);
1786 * ProcArrayInstallImportedXmin -- install imported xmin into MyPgXact->xmin
1788 * This is called when installing a snapshot imported from another
1789 * transaction. To ensure that OldestXmin doesn't go backwards, we must
1790 * check that the source transaction is still running, and we'd better do
1791 * that atomically with installing the new xmin.
1793 * Returns TRUE if successful, FALSE if source xact is no longer running.
1796 ProcArrayInstallImportedXmin(TransactionId xmin,
1797 VirtualTransactionId *sourcevxid)
1799 bool result = false;
1800 ProcArrayStruct *arrayP = procArray;
1803 Assert(TransactionIdIsNormal(xmin));
1807 /* Get lock so source xact can't end while we're doing this */
1808 LWLockAcquire(ProcArrayLock, LW_SHARED);
1810 for (index = 0; index < arrayP->numProcs; index++)
1812 int pgprocno = arrayP->pgprocnos[index];
1813 volatile PGPROC *proc = &allProcs[pgprocno];
1814 volatile PGXACT *pgxact = &allPgXact[pgprocno];
1817 /* Ignore procs running LAZY VACUUM */
1818 if (pgxact->vacuumFlags & PROC_IN_VACUUM)
1821 /* We are only interested in the specific virtual transaction. */
1822 if (proc->backendId != sourcevxid->backendId)
1824 if (proc->lxid != sourcevxid->localTransactionId)
1828 * We check the transaction's database ID for paranoia's sake: if it's
1829 * in another DB then its xmin does not cover us. Caller should have
1830 * detected this already, so we just treat any funny cases as
1831 * "transaction not found".
1833 if (proc->databaseId != MyDatabaseId)
1837 * Likewise, let's just make real sure its xmin does cover us.
1839 xid = pgxact->xmin; /* fetch just once */
1840 if (!TransactionIdIsNormal(xid) ||
1841 !TransactionIdPrecedesOrEquals(xid, xmin))
1845 * We're good. Install the new xmin. As in GetSnapshotData, set
1846 * TransactionXmin too. (Note that because snapmgr.c called
1847 * GetSnapshotData first, we'll be overwriting a valid xmin here, so
1848 * we don't check that.)
1850 MyPgXact->xmin = TransactionXmin = xmin;
1856 LWLockRelease(ProcArrayLock);
1862 * ProcArrayInstallRestoredXmin -- install restored xmin into MyPgXact->xmin
1864 * This is like ProcArrayInstallImportedXmin, but we have a pointer to the
1865 * PGPROC of the transaction from which we imported the snapshot, rather than
1868 * Returns TRUE if successful, FALSE if source xact is no longer running.
1871 ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
1873 bool result = false;
1875 volatile PGXACT *pgxact;
1877 Assert(TransactionIdIsNormal(xmin));
1878 Assert(proc != NULL);
1880 /* Get lock so source xact can't end while we're doing this */
1881 LWLockAcquire(ProcArrayLock, LW_SHARED);
1883 pgxact = &allPgXact[proc->pgprocno];
1886 * Be certain that the referenced PGPROC has an advertised xmin which is
1887 * no later than the one we're installing, so that the system-wide xmin
1888 * can't go backwards. Also, make sure it's running in the same database,
1889 * so that the per-database xmin cannot go backwards.
1891 xid = pgxact->xmin; /* fetch just once */
1892 if (proc->databaseId == MyDatabaseId &&
1893 TransactionIdIsNormal(xid) &&
1894 TransactionIdPrecedesOrEquals(xid, xmin))
1896 MyPgXact->xmin = TransactionXmin = xmin;
1900 LWLockRelease(ProcArrayLock);
1906 * GetRunningTransactionData -- returns information about running transactions.
1908 * Similar to GetSnapshotData but returns more information. We include
1909 * all PGXACTs with an assigned TransactionId, even VACUUM processes.
1911 * We acquire XidGenLock and ProcArrayLock, but the caller is responsible for
1912 * releasing them. Acquiring XidGenLock ensures that no new XIDs enter the proc
1913 * array until the caller has WAL-logged this snapshot, and releases the
1914 * lock. Acquiring ProcArrayLock ensures that no transactions commit until the
1917 * The returned data structure is statically allocated; caller should not
1918 * modify it, and must not assume it is valid past the next call.
1920 * This is never executed during recovery so there is no need to look at
1921 * KnownAssignedXids.
1923 * We don't worry about updating other counters, we want to keep this as
1924 * simple as possible and leave GetSnapshotData() as the primary code for
1927 * Note that if any transaction has overflowed its cached subtransactions
1928 * then there is no real need include any subtransactions. That isn't a
1929 * common enough case to worry about optimising the size of the WAL record,
1930 * and we may wish to see that data for diagnostic purposes anyway.
1933 GetRunningTransactionData(void)
1935 /* result workspace */
1936 static RunningTransactionsData CurrentRunningXactsData;
1938 ProcArrayStruct *arrayP = procArray;
1939 RunningTransactions CurrentRunningXacts = &CurrentRunningXactsData;
1940 TransactionId latestCompletedXid;
1941 TransactionId oldestRunningXid;
1942 TransactionId *xids;
1948 Assert(!RecoveryInProgress());
1951 * Allocating space for maxProcs xids is usually overkill; numProcs would
1952 * be sufficient. But it seems better to do the malloc while not holding
1953 * the lock, so we can't look at numProcs. Likewise, we allocate much
1954 * more subxip storage than is probably needed.
1956 * Should only be allocated in bgwriter, since only ever executed during
1959 if (CurrentRunningXacts->xids == NULL)
1964 CurrentRunningXacts->xids = (TransactionId *)
1965 malloc(TOTAL_MAX_CACHED_SUBXIDS * sizeof(TransactionId));
1966 if (CurrentRunningXacts->xids == NULL)
1968 (errcode(ERRCODE_OUT_OF_MEMORY),
1969 errmsg("out of memory")));
1972 xids = CurrentRunningXacts->xids;
1974 count = subcount = 0;
1975 suboverflowed = false;
1978 * Ensure that no xids enter or leave the procarray while we obtain
1981 LWLockAcquire(ProcArrayLock, LW_SHARED);
1982 LWLockAcquire(XidGenLock, LW_SHARED);
1984 latestCompletedXid = ShmemVariableCache->latestCompletedXid;
1986 oldestRunningXid = ShmemVariableCache->nextXid;
1989 * Spin over procArray collecting all xids
1991 for (index = 0; index < arrayP->numProcs; index++)
1993 int pgprocno = arrayP->pgprocnos[index];
1994 volatile PGXACT *pgxact = &allPgXact[pgprocno];
1997 /* Fetch xid just once - see GetNewTransactionId */
2001 * We don't need to store transactions that don't have a TransactionId
2002 * yet because they will not show as running on a standby server.
2004 if (!TransactionIdIsValid(xid))
2007 xids[count++] = xid;
2009 if (TransactionIdPrecedes(xid, oldestRunningXid))
2010 oldestRunningXid = xid;
2012 if (pgxact->overflowed)
2013 suboverflowed = true;
2017 * Spin over procArray collecting all subxids, but only if there hasn't
2018 * been a suboverflow.
2022 for (index = 0; index < arrayP->numProcs; index++)
2024 int pgprocno = arrayP->pgprocnos[index];
2025 volatile PGPROC *proc = &allProcs[pgprocno];
2026 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2030 * Save subtransaction XIDs. Other backends can't add or remove
2031 * entries while we're holding XidGenLock.
2033 nxids = pgxact->nxids;
2036 memcpy(&xids[count], (void *) proc->subxids.xids,
2037 nxids * sizeof(TransactionId));
2042 * Top-level XID of a transaction is always less than any of
2043 * its subxids, so we don't need to check if any of the
2044 * subxids are smaller than oldestRunningXid
2051 * It's important *not* to include the limits set by slots here because
2052 * snapbuild.c uses oldestRunningXid to manage its xmin horizon. If those
2053 * were to be included here the initial value could never increase because
2054 * of a circular dependency where slots only increase their limits when
2055 * running xacts increases oldestRunningXid and running xacts only
2056 * increases if slots do.
2059 CurrentRunningXacts->xcnt = count - subcount;
2060 CurrentRunningXacts->subxcnt = subcount;
2061 CurrentRunningXacts->subxid_overflow = suboverflowed;
2062 CurrentRunningXacts->nextXid = ShmemVariableCache->nextXid;
2063 CurrentRunningXacts->oldestRunningXid = oldestRunningXid;
2064 CurrentRunningXacts->latestCompletedXid = latestCompletedXid;
2066 Assert(TransactionIdIsValid(CurrentRunningXacts->nextXid));
2067 Assert(TransactionIdIsValid(CurrentRunningXacts->oldestRunningXid));
2068 Assert(TransactionIdIsNormal(CurrentRunningXacts->latestCompletedXid));
2070 /* We don't release the locks here, the caller is responsible for that */
2072 return CurrentRunningXacts;
2076 * GetOldestActiveTransactionId()
2078 * Similar to GetSnapshotData but returns just oldestActiveXid. We include
2079 * all PGXACTs with an assigned TransactionId, even VACUUM processes.
2080 * We look at all databases, though there is no need to include WALSender
2081 * since this has no effect on hot standby conflicts.
2083 * This is never executed during recovery so there is no need to look at
2084 * KnownAssignedXids.
2086 * We don't worry about updating other counters, we want to keep this as
2087 * simple as possible and leave GetSnapshotData() as the primary code for
2091 GetOldestActiveTransactionId(void)
2093 ProcArrayStruct *arrayP = procArray;
2094 TransactionId oldestRunningXid;
2097 Assert(!RecoveryInProgress());
2100 * Read nextXid, as the upper bound of what's still active.
2102 * Reading a TransactionId is atomic, but we must grab the lock to make
2103 * sure that all XIDs < nextXid are already present in the proc array (or
2104 * have already completed), when we spin over it.
2106 LWLockAcquire(XidGenLock, LW_SHARED);
2107 oldestRunningXid = ShmemVariableCache->nextXid;
2108 LWLockRelease(XidGenLock);
2111 * Spin over procArray collecting all xids and subxids.
2113 LWLockAcquire(ProcArrayLock, LW_SHARED);
2114 for (index = 0; index < arrayP->numProcs; index++)
2116 int pgprocno = arrayP->pgprocnos[index];
2117 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2120 /* Fetch xid just once - see GetNewTransactionId */
2123 if (!TransactionIdIsNormal(xid))
2126 if (TransactionIdPrecedes(xid, oldestRunningXid))
2127 oldestRunningXid = xid;
2130 * Top-level XID of a transaction is always less than any of its
2131 * subxids, so we don't need to check if any of the subxids are
2132 * smaller than oldestRunningXid
2135 LWLockRelease(ProcArrayLock);
2137 return oldestRunningXid;
2141 * GetOldestSafeDecodingTransactionId -- lowest xid not affected by vacuum
2143 * Returns the oldest xid that we can guarantee not to have been affected by
2144 * vacuum, i.e. no rows >= that xid have been vacuumed away unless the
2145 * transaction aborted. Note that the value can (and most of the time will) be
2146 * much more conservative than what really has been affected by vacuum, but we
2147 * currently don't have better data available.
2149 * This is useful to initialize the cutoff xid after which a new changeset
2150 * extraction replication slot can start decoding changes.
2152 * Must be called with ProcArrayLock held either shared or exclusively,
2153 * although most callers will want to use exclusive mode since it is expected
2154 * that the caller will immediately use the xid to peg the xmin horizon.
2157 GetOldestSafeDecodingTransactionId(bool catalogOnly)
2159 ProcArrayStruct *arrayP = procArray;
2160 TransactionId oldestSafeXid;
2162 bool recovery_in_progress = RecoveryInProgress();
2164 Assert(LWLockHeldByMe(ProcArrayLock));
2167 * Acquire XidGenLock, so no transactions can acquire an xid while we're
2168 * running. If no transaction with xid were running concurrently a new xid
2169 * could influence the RecentXmin et al.
2171 * We initialize the computation to nextXid since that's guaranteed to be
2172 * a safe, albeit pessimal, value.
2174 LWLockAcquire(XidGenLock, LW_SHARED);
2175 oldestSafeXid = ShmemVariableCache->nextXid;
2178 * If there's already a slot pegging the xmin horizon, we can start with
2179 * that value, it's guaranteed to be safe since it's computed by this
2180 * routine initially and has been enforced since. We can always use the
2181 * slot's general xmin horizon, but the catalog horizon is only usable
2182 * when we only catalog data is going to be looked at.
2184 if (TransactionIdIsValid(procArray->replication_slot_xmin) &&
2185 TransactionIdPrecedes(procArray->replication_slot_xmin,
2187 oldestSafeXid = procArray->replication_slot_xmin;
2190 TransactionIdIsValid(procArray->replication_slot_catalog_xmin) &&
2191 TransactionIdPrecedes(procArray->replication_slot_catalog_xmin,
2193 oldestSafeXid = procArray->replication_slot_catalog_xmin;
2196 * If we're not in recovery, we walk over the procarray and collect the
2197 * lowest xid. Since we're called with ProcArrayLock held and have
2198 * acquired XidGenLock, no entries can vanish concurrently, since
2199 * PGXACT->xid is only set with XidGenLock held and only cleared with
2200 * ProcArrayLock held.
2202 * In recovery we can't lower the safe value besides what we've computed
2203 * above, so we'll have to wait a bit longer there. We unfortunately can
2204 * *not* use KnownAssignedXidsGetOldestXmin() since the KnownAssignedXids
2205 * machinery can miss values and return an older value than is safe.
2207 if (!recovery_in_progress)
2210 * Spin over procArray collecting all min(PGXACT->xid)
2212 for (index = 0; index < arrayP->numProcs; index++)
2214 int pgprocno = arrayP->pgprocnos[index];
2215 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2218 /* Fetch xid just once - see GetNewTransactionId */
2221 if (!TransactionIdIsNormal(xid))
2224 if (TransactionIdPrecedes(xid, oldestSafeXid))
2225 oldestSafeXid = xid;
2229 LWLockRelease(XidGenLock);
2231 return oldestSafeXid;
2235 * GetVirtualXIDsDelayingChkpt -- Get the VXIDs of transactions that are
2236 * delaying checkpoint because they have critical actions in progress.
2238 * Constructs an array of VXIDs of transactions that are currently in commit
2239 * critical sections, as shown by having delayChkpt set in their PGXACT.
2241 * Returns a palloc'd array that should be freed by the caller.
2242 * *nvxids is the number of valid entries.
2244 * Note that because backends set or clear delayChkpt without holding any lock,
2245 * the result is somewhat indeterminate, but we don't really care. Even in
2246 * a multiprocessor with delayed writes to shared memory, it should be certain
2247 * that setting of delayChkpt will propagate to shared memory when the backend
2248 * takes a lock, so we cannot fail to see a virtual xact as delayChkpt if
2249 * it's already inserted its commit record. Whether it takes a little while
2250 * for clearing of delayChkpt to propagate is unimportant for correctness.
2252 VirtualTransactionId *
2253 GetVirtualXIDsDelayingChkpt(int *nvxids)
2255 VirtualTransactionId *vxids;
2256 ProcArrayStruct *arrayP = procArray;
2260 /* allocate what's certainly enough result space */
2261 vxids = (VirtualTransactionId *)
2262 palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
2264 LWLockAcquire(ProcArrayLock, LW_SHARED);
2266 for (index = 0; index < arrayP->numProcs; index++)
2268 int pgprocno = arrayP->pgprocnos[index];
2269 volatile PGPROC *proc = &allProcs[pgprocno];
2270 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2272 if (pgxact->delayChkpt)
2274 VirtualTransactionId vxid;
2276 GET_VXID_FROM_PGPROC(vxid, *proc);
2277 if (VirtualTransactionIdIsValid(vxid))
2278 vxids[count++] = vxid;
2282 LWLockRelease(ProcArrayLock);
2289 * HaveVirtualXIDsDelayingChkpt -- Are any of the specified VXIDs delaying?
2291 * This is used with the results of GetVirtualXIDsDelayingChkpt to see if any
2292 * of the specified VXIDs are still in critical sections of code.
2294 * Note: this is O(N^2) in the number of vxacts that are/were delaying, but
2295 * those numbers should be small enough for it not to be a problem.
2298 HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids)
2300 bool result = false;
2301 ProcArrayStruct *arrayP = procArray;
2304 LWLockAcquire(ProcArrayLock, LW_SHARED);
2306 for (index = 0; index < arrayP->numProcs; index++)
2308 int pgprocno = arrayP->pgprocnos[index];
2309 volatile PGPROC *proc = &allProcs[pgprocno];
2310 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2311 VirtualTransactionId vxid;
2313 GET_VXID_FROM_PGPROC(vxid, *proc);
2315 if (pgxact->delayChkpt && VirtualTransactionIdIsValid(vxid))
2319 for (i = 0; i < nvxids; i++)
2321 if (VirtualTransactionIdEquals(vxid, vxids[i]))
2332 LWLockRelease(ProcArrayLock);
2338 * BackendPidGetProc -- get a backend's PGPROC given its PID
2340 * Returns NULL if not found. Note that it is up to the caller to be
2341 * sure that the question remains meaningful for long enough for the
2342 * answer to be used ...
2345 BackendPidGetProc(int pid)
2349 if (pid == 0) /* never match dummy PGPROCs */
2352 LWLockAcquire(ProcArrayLock, LW_SHARED);
2354 result = BackendPidGetProcWithLock(pid);
2356 LWLockRelease(ProcArrayLock);
2362 * BackendPidGetProcWithLock -- get a backend's PGPROC given its PID
2364 * Same as above, except caller must be holding ProcArrayLock. The found
2365 * entry, if any, can be assumed to be valid as long as the lock remains held.
2368 BackendPidGetProcWithLock(int pid)
2370 PGPROC *result = NULL;
2371 ProcArrayStruct *arrayP = procArray;
2374 if (pid == 0) /* never match dummy PGPROCs */
2377 for (index = 0; index < arrayP->numProcs; index++)
2379 PGPROC *proc = &allProcs[arrayP->pgprocnos[index]];
2381 if (proc->pid == pid)
2392 * BackendXidGetPid -- get a backend's pid given its XID
2394 * Returns 0 if not found or it's a prepared transaction. Note that
2395 * it is up to the caller to be sure that the question remains
2396 * meaningful for long enough for the answer to be used ...
2398 * Only main transaction Ids are considered. This function is mainly
2399 * useful for determining what backend owns a lock.
2401 * Beware that not every xact has an XID assigned. However, as long as you
2402 * only call this using an XID found on disk, you're safe.
2405 BackendXidGetPid(TransactionId xid)
2408 ProcArrayStruct *arrayP = procArray;
2411 if (xid == InvalidTransactionId) /* never match invalid xid */
2414 LWLockAcquire(ProcArrayLock, LW_SHARED);
2416 for (index = 0; index < arrayP->numProcs; index++)
2418 int pgprocno = arrayP->pgprocnos[index];
2419 volatile PGPROC *proc = &allProcs[pgprocno];
2420 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2422 if (pgxact->xid == xid)
2429 LWLockRelease(ProcArrayLock);
2435 * IsBackendPid -- is a given pid a running backend
2437 * This is not called by the backend, but is called by external modules.
2440 IsBackendPid(int pid)
2442 return (BackendPidGetProc(pid) != NULL);
2447 * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
2449 * The array is palloc'd. The number of valid entries is returned into *nvxids.
2451 * The arguments allow filtering the set of VXIDs returned. Our own process
2452 * is always skipped. In addition:
2453 * If limitXmin is not InvalidTransactionId, skip processes with
2455 * If excludeXmin0 is true, skip processes with xmin = 0.
2456 * If allDbs is false, skip processes attached to other databases.
2457 * If excludeVacuum isn't zero, skip processes for which
2458 * (vacuumFlags & excludeVacuum) is not zero.
2460 * Note: the purpose of the limitXmin and excludeXmin0 parameters is to
2461 * allow skipping backends whose oldest live snapshot is no older than
2462 * some snapshot we have. Since we examine the procarray with only shared
2463 * lock, there are race conditions: a backend could set its xmin just after
2464 * we look. Indeed, on multiprocessors with weak memory ordering, the
2465 * other backend could have set its xmin *before* we look. We know however
2466 * that such a backend must have held shared ProcArrayLock overlapping our
2467 * own hold of ProcArrayLock, else we would see its xmin update. Therefore,
2468 * any snapshot the other backend is taking concurrently with our scan cannot
2469 * consider any transactions as still running that we think are committed
2470 * (since backends must hold ProcArrayLock exclusive to commit).
2472 VirtualTransactionId *
2473 GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
2474 bool allDbs, int excludeVacuum,
2477 VirtualTransactionId *vxids;
2478 ProcArrayStruct *arrayP = procArray;
2482 /* allocate what's certainly enough result space */
2483 vxids = (VirtualTransactionId *)
2484 palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
2486 LWLockAcquire(ProcArrayLock, LW_SHARED);
2488 for (index = 0; index < arrayP->numProcs; index++)
2490 int pgprocno = arrayP->pgprocnos[index];
2491 volatile PGPROC *proc = &allProcs[pgprocno];
2492 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2497 if (excludeVacuum & pgxact->vacuumFlags)
2500 if (allDbs || proc->databaseId == MyDatabaseId)
2502 /* Fetch xmin just once - might change on us */
2503 TransactionId pxmin = pgxact->xmin;
2505 if (excludeXmin0 && !TransactionIdIsValid(pxmin))
2509 * InvalidTransactionId precedes all other XIDs, so a proc that
2510 * hasn't set xmin yet will not be rejected by this test.
2512 if (!TransactionIdIsValid(limitXmin) ||
2513 TransactionIdPrecedesOrEquals(pxmin, limitXmin))
2515 VirtualTransactionId vxid;
2517 GET_VXID_FROM_PGPROC(vxid, *proc);
2518 if (VirtualTransactionIdIsValid(vxid))
2519 vxids[count++] = vxid;
2524 LWLockRelease(ProcArrayLock);
2531 * GetConflictingVirtualXIDs -- returns an array of currently active VXIDs.
2533 * Usage is limited to conflict resolution during recovery on standby servers.
2534 * limitXmin is supplied as either latestRemovedXid, or InvalidTransactionId
2535 * in cases where we cannot accurately determine a value for latestRemovedXid.
2537 * If limitXmin is InvalidTransactionId then we want to kill everybody,
2538 * so we're not worried if they have a snapshot or not, nor does it really
2539 * matter what type of lock we hold.
2541 * All callers that are checking xmins always now supply a valid and useful
2542 * value for limitXmin. The limitXmin is always lower than the lowest
2543 * numbered KnownAssignedXid that is not already a FATAL error. This is
2544 * because we only care about cleanup records that are cleaning up tuple
2545 * versions from committed transactions. In that case they will only occur
2546 * at the point where the record is less than the lowest running xid. That
2547 * allows us to say that if any backend takes a snapshot concurrently with
2548 * us then the conflict assessment made here would never include the snapshot
2549 * that is being derived. So we take LW_SHARED on the ProcArray and allow
2550 * concurrent snapshots when limitXmin is valid. We might think about adding
2551 * Assert(limitXmin < lowest(KnownAssignedXids))
2552 * but that would not be true in the case of FATAL errors lagging in array,
2553 * but we already know those are bogus anyway, so we skip that test.
2555 * If dbOid is valid we skip backends attached to other databases.
2557 * Be careful to *not* pfree the result from this function. We reuse
2558 * this array sufficiently often that we use malloc for the result.
2560 VirtualTransactionId *
2561 GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
2563 static VirtualTransactionId *vxids;
2564 ProcArrayStruct *arrayP = procArray;
2569 * If first time through, get workspace to remember main XIDs in. We
2570 * malloc it permanently to avoid repeated palloc/pfree overhead. Allow
2571 * result space, remembering room for a terminator.
2575 vxids = (VirtualTransactionId *)
2576 malloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
2579 (errcode(ERRCODE_OUT_OF_MEMORY),
2580 errmsg("out of memory")));
2583 LWLockAcquire(ProcArrayLock, LW_SHARED);
2585 for (index = 0; index < arrayP->numProcs; index++)
2587 int pgprocno = arrayP->pgprocnos[index];
2588 volatile PGPROC *proc = &allProcs[pgprocno];
2589 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2591 /* Exclude prepared transactions */
2595 if (!OidIsValid(dbOid) ||
2596 proc->databaseId == dbOid)
2598 /* Fetch xmin just once - can't change on us, but good coding */
2599 TransactionId pxmin = pgxact->xmin;
2602 * We ignore an invalid pxmin because this means that backend has
2603 * no snapshot currently. We hold a Share lock to avoid contention
2604 * with users taking snapshots. That is not a problem because the
2605 * current xmin is always at least one higher than the latest
2606 * removed xid, so any new snapshot would never conflict with the
2609 if (!TransactionIdIsValid(limitXmin) ||
2610 (TransactionIdIsValid(pxmin) && !TransactionIdFollows(pxmin, limitXmin)))
2612 VirtualTransactionId vxid;
2614 GET_VXID_FROM_PGPROC(vxid, *proc);
2615 if (VirtualTransactionIdIsValid(vxid))
2616 vxids[count++] = vxid;
2621 LWLockRelease(ProcArrayLock);
2623 /* add the terminator */
2624 vxids[count].backendId = InvalidBackendId;
2625 vxids[count].localTransactionId = InvalidLocalTransactionId;
2631 * CancelVirtualTransaction - used in recovery conflict processing
2633 * Returns pid of the process signaled, or 0 if not found.
2636 CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
2638 ProcArrayStruct *arrayP = procArray;
2642 LWLockAcquire(ProcArrayLock, LW_SHARED);
2644 for (index = 0; index < arrayP->numProcs; index++)
2646 int pgprocno = arrayP->pgprocnos[index];
2647 volatile PGPROC *proc = &allProcs[pgprocno];
2648 VirtualTransactionId procvxid;
2650 GET_VXID_FROM_PGPROC(procvxid, *proc);
2652 if (procvxid.backendId == vxid.backendId &&
2653 procvxid.localTransactionId == vxid.localTransactionId)
2655 proc->recoveryConflictPending = true;
2660 * Kill the pid if it's still here. If not, that's what we
2661 * wanted so ignore any errors.
2663 (void) SendProcSignal(pid, sigmode, vxid.backendId);
2669 LWLockRelease(ProcArrayLock);
2675 * MinimumActiveBackends --- count backends (other than myself) that are
2676 * in active transactions. Return true if the count exceeds the
2677 * minimum threshold passed. This is used as a heuristic to decide if
2678 * a pre-XLOG-flush delay is worthwhile during commit.
2680 * Do not count backends that are blocked waiting for locks, since they are
2681 * not going to get to run until someone else commits.
2684 MinimumActiveBackends(int min)
2686 ProcArrayStruct *arrayP = procArray;
2690 /* Quick short-circuit if no minimum is specified */
2695 * Note: for speed, we don't acquire ProcArrayLock. This is a little bit
2696 * bogus, but since we are only testing fields for zero or nonzero, it
2697 * should be OK. The result is only used for heuristic purposes anyway...
2699 for (index = 0; index < arrayP->numProcs; index++)
2701 int pgprocno = arrayP->pgprocnos[index];
2702 volatile PGPROC *proc = &allProcs[pgprocno];
2703 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2706 * Since we're not holding a lock, need to be prepared to deal with
2707 * garbage, as someone could have incremented numProcs but not yet
2708 * filled the structure.
2710 * If someone just decremented numProcs, 'proc' could also point to a
2711 * PGPROC entry that's no longer in the array. It still points to a
2712 * PGPROC struct, though, because freed PGPROC entries just go to the
2713 * free list and are recycled. Its contents are nonsense in that case,
2714 * but that's acceptable for this function.
2717 continue; /* do not count deleted entries */
2719 continue; /* do not count myself */
2720 if (pgxact->xid == InvalidTransactionId)
2721 continue; /* do not count if no XID assigned */
2723 continue; /* do not count prepared xacts */
2724 if (proc->waitLock != NULL)
2725 continue; /* do not count if blocked on a lock */
2731 return count >= min;
2735 * CountDBBackends --- count backends that are using specified database
2738 CountDBBackends(Oid databaseid)
2740 ProcArrayStruct *arrayP = procArray;
2744 LWLockAcquire(ProcArrayLock, LW_SHARED);
2746 for (index = 0; index < arrayP->numProcs; index++)
2748 int pgprocno = arrayP->pgprocnos[index];
2749 volatile PGPROC *proc = &allProcs[pgprocno];
2752 continue; /* do not count prepared xacts */
2753 if (!OidIsValid(databaseid) ||
2754 proc->databaseId == databaseid)
2758 LWLockRelease(ProcArrayLock);
2764 * CountDBConnections --- counts database backends ignoring any background
2768 CountDBConnections(Oid databaseid)
2770 ProcArrayStruct *arrayP = procArray;
2774 LWLockAcquire(ProcArrayLock, LW_SHARED);
2776 for (index = 0; index < arrayP->numProcs; index++)
2778 int pgprocno = arrayP->pgprocnos[index];
2779 volatile PGPROC *proc = &allProcs[pgprocno];
2782 continue; /* do not count prepared xacts */
2783 if (proc->isBackgroundWorker)
2784 continue; /* do not count background workers */
2785 if (!OidIsValid(databaseid) ||
2786 proc->databaseId == databaseid)
2790 LWLockRelease(ProcArrayLock);
2796 * CancelDBBackends --- cancel backends that are using specified database
2799 CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
2801 ProcArrayStruct *arrayP = procArray;
2805 /* tell all backends to die */
2806 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2808 for (index = 0; index < arrayP->numProcs; index++)
2810 int pgprocno = arrayP->pgprocnos[index];
2811 volatile PGPROC *proc = &allProcs[pgprocno];
2813 if (databaseid == InvalidOid || proc->databaseId == databaseid)
2815 VirtualTransactionId procvxid;
2817 GET_VXID_FROM_PGPROC(procvxid, *proc);
2819 proc->recoveryConflictPending = conflictPending;
2824 * Kill the pid if it's still here. If not, that's what we
2825 * wanted so ignore any errors.
2827 (void) SendProcSignal(pid, sigmode, procvxid.backendId);
2832 LWLockRelease(ProcArrayLock);
2836 * CountUserBackends --- count backends that are used by specified user
2839 CountUserBackends(Oid roleid)
2841 ProcArrayStruct *arrayP = procArray;
2845 LWLockAcquire(ProcArrayLock, LW_SHARED);
2847 for (index = 0; index < arrayP->numProcs; index++)
2849 int pgprocno = arrayP->pgprocnos[index];
2850 volatile PGPROC *proc = &allProcs[pgprocno];
2853 continue; /* do not count prepared xacts */
2854 if (proc->isBackgroundWorker)
2855 continue; /* do not count background workers */
2856 if (proc->roleId == roleid)
2860 LWLockRelease(ProcArrayLock);
2866 * CountOtherDBBackends -- check for other backends running in the given DB
2868 * If there are other backends in the DB, we will wait a maximum of 5 seconds
2869 * for them to exit. Autovacuum backends are encouraged to exit early by
2870 * sending them SIGTERM, but normal user backends are just waited for.
2872 * The current backend is always ignored; it is caller's responsibility to
2873 * check whether the current backend uses the given DB, if it's important.
2875 * Returns TRUE if there are (still) other backends in the DB, FALSE if not.
2876 * Also, *nbackends and *nprepared are set to the number of other backends
2877 * and prepared transactions in the DB, respectively.
2879 * This function is used to interlock DROP DATABASE and related commands
2880 * against there being any active backends in the target DB --- dropping the
2881 * DB while active backends remain would be a Bad Thing. Note that we cannot
2882 * detect here the possibility of a newly-started backend that is trying to
2883 * connect to the doomed database, so additional interlocking is needed during
2884 * backend startup. The caller should normally hold an exclusive lock on the
2885 * target DB before calling this, which is one reason we mustn't wait
2889 CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
2891 ProcArrayStruct *arrayP = procArray;
2893 #define MAXAUTOVACPIDS 10 /* max autovacs to SIGTERM per iteration */
2894 int autovac_pids[MAXAUTOVACPIDS];
2897 /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
2898 for (tries = 0; tries < 50; tries++)
2904 CHECK_FOR_INTERRUPTS();
2906 *nbackends = *nprepared = 0;
2908 LWLockAcquire(ProcArrayLock, LW_SHARED);
2910 for (index = 0; index < arrayP->numProcs; index++)
2912 int pgprocno = arrayP->pgprocnos[index];
2913 volatile PGPROC *proc = &allProcs[pgprocno];
2914 volatile PGXACT *pgxact = &allPgXact[pgprocno];
2916 if (proc->databaseId != databaseId)
2928 if ((pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
2929 nautovacs < MAXAUTOVACPIDS)
2930 autovac_pids[nautovacs++] = proc->pid;
2934 LWLockRelease(ProcArrayLock);
2937 return false; /* no conflicting backends, so done */
2940 * Send SIGTERM to any conflicting autovacuums before sleeping. We
2941 * postpone this step until after the loop because we don't want to
2942 * hold ProcArrayLock while issuing kill(). We have no idea what might
2943 * block kill() inside the kernel...
2945 for (index = 0; index < nautovacs; index++)
2946 (void) kill(autovac_pids[index], SIGTERM); /* ignore any error */
2948 /* sleep, then try again */
2949 pg_usleep(100 * 1000L); /* 100ms */
2952 return true; /* timed out, still conflicts */
2956 * ProcArraySetReplicationSlotXmin
2958 * Install limits to future computations of the xmin horizon to prevent vacuum
2959 * and HOT pruning from removing affected rows still needed by clients with
2963 ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin,
2964 bool already_locked)
2966 Assert(!already_locked || LWLockHeldByMe(ProcArrayLock));
2968 if (!already_locked)
2969 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2971 procArray->replication_slot_xmin = xmin;
2972 procArray->replication_slot_catalog_xmin = catalog_xmin;
2974 if (!already_locked)
2975 LWLockRelease(ProcArrayLock);
2979 * ProcArrayGetReplicationSlotXmin
2981 * Return the current slot xmin limits. That's useful to be able to remove
2982 * data that's older than those limits.
2985 ProcArrayGetReplicationSlotXmin(TransactionId *xmin,
2986 TransactionId *catalog_xmin)
2988 LWLockAcquire(ProcArrayLock, LW_SHARED);
2991 *xmin = procArray->replication_slot_xmin;
2993 if (catalog_xmin != NULL)
2994 *catalog_xmin = procArray->replication_slot_catalog_xmin;
2996 LWLockRelease(ProcArrayLock);
3000 #define XidCacheRemove(i) \
3002 MyProc->subxids.xids[i] = MyProc->subxids.xids[MyPgXact->nxids - 1]; \
3003 MyPgXact->nxids--; \
3007 * XidCacheRemoveRunningXids
3009 * Remove a bunch of TransactionIds from the list of known-running
3010 * subtransactions for my backend. Both the specified xid and those in
3011 * the xids[] array (of length nxids) are removed from the subxids cache.
3012 * latestXid must be the latest XID among the group.
3015 XidCacheRemoveRunningXids(TransactionId xid,
3016 int nxids, const TransactionId *xids,
3017 TransactionId latestXid)
3022 Assert(TransactionIdIsValid(xid));
3025 * We must hold ProcArrayLock exclusively in order to remove transactions
3026 * from the PGPROC array. (See src/backend/access/transam/README.) It's
3027 * possible this could be relaxed since we know this routine is only used
3028 * to abort subtransactions, but pending closer analysis we'd best be
3031 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3034 * Under normal circumstances xid and xids[] will be in increasing order,
3035 * as will be the entries in subxids. Scan backwards to avoid O(N^2)
3036 * behavior when removing a lot of xids.
3038 for (i = nxids - 1; i >= 0; i--)
3040 TransactionId anxid = xids[i];
3042 for (j = MyPgXact->nxids - 1; j >= 0; j--)
3044 if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
3052 * Ordinarily we should have found it, unless the cache has
3053 * overflowed. However it's also possible for this routine to be
3054 * invoked multiple times for the same subtransaction, in case of an
3055 * error during AbortSubTransaction. So instead of Assert, emit a
3058 if (j < 0 && !MyPgXact->overflowed)
3059 elog(WARNING, "did not find subXID %u in MyProc", anxid);
3062 for (j = MyPgXact->nxids - 1; j >= 0; j--)
3064 if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
3070 /* Ordinarily we should have found it, unless the cache has overflowed */
3071 if (j < 0 && !MyPgXact->overflowed)
3072 elog(WARNING, "did not find subXID %u in MyProc", xid);
3074 /* Also advance global latestCompletedXid while holding the lock */
3075 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
3077 ShmemVariableCache->latestCompletedXid = latestXid;
3079 LWLockRelease(ProcArrayLock);
3082 #ifdef XIDCACHE_DEBUG
3085 * Print stats about effectiveness of XID cache
3088 DisplayXidCache(void)
3091 "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, knownassigned: %ld, nooflo: %ld, slow: %ld\n",
3098 xc_by_known_assigned,
3102 #endif /* XIDCACHE_DEBUG */
3105 /* ----------------------------------------------
3106 * KnownAssignedTransactions sub-module
3107 * ----------------------------------------------
3111 * In Hot Standby mode, we maintain a list of transactions that are (or were)
3112 * running in the master at the current point in WAL. These XIDs must be
3113 * treated as running by standby transactions, even though they are not in
3114 * the standby server's PGXACT array.
3116 * We record all XIDs that we know have been assigned. That includes all the
3117 * XIDs seen in WAL records, plus all unobserved XIDs that we can deduce have
3118 * been assigned. We can deduce the existence of unobserved XIDs because we
3119 * know XIDs are assigned in sequence, with no gaps. The KnownAssignedXids
3120 * list expands as new XIDs are observed or inferred, and contracts when
3121 * transaction completion records arrive.
3123 * During hot standby we do not fret too much about the distinction between
3124 * top-level XIDs and subtransaction XIDs. We store both together in the
3125 * KnownAssignedXids list. In backends, this is copied into snapshots in
3126 * GetSnapshotData(), taking advantage of the fact that XidInMVCCSnapshot()
3127 * doesn't care about the distinction either. Subtransaction XIDs are
3128 * effectively treated as top-level XIDs and in the typical case pg_subtrans
3129 * links are *not* maintained (which does not affect visibility).
3131 * We have room in KnownAssignedXids and in snapshots to hold maxProcs *
3132 * (1 + PGPROC_MAX_CACHED_SUBXIDS) XIDs, so every master transaction must
3133 * report its subtransaction XIDs in a WAL XLOG_XACT_ASSIGNMENT record at
3134 * least every PGPROC_MAX_CACHED_SUBXIDS. When we receive one of these
3135 * records, we mark the subXIDs as children of the top XID in pg_subtrans,
3136 * and then remove them from KnownAssignedXids. This prevents overflow of
3137 * KnownAssignedXids and snapshots, at the cost that status checks for these
3138 * subXIDs will take a slower path through TransactionIdIsInProgress().
3139 * This means that KnownAssignedXids is not necessarily complete for subXIDs,
3140 * though it should be complete for top-level XIDs; this is the same situation
3141 * that holds with respect to the PGPROC entries in normal running.
3143 * When we throw away subXIDs from KnownAssignedXids, we need to keep track of
3144 * that, similarly to tracking overflow of a PGPROC's subxids array. We do
3145 * that by remembering the lastOverflowedXID, ie the last thrown-away subXID.
3146 * As long as that is within the range of interesting XIDs, we have to assume
3147 * that subXIDs are missing from snapshots. (Note that subXID overflow occurs
3148 * on primary when 65th subXID arrives, whereas on standby it occurs when 64th
3149 * subXID arrives - that is not an error.)
3151 * Should a backend on primary somehow disappear before it can write an abort
3152 * record, then we just leave those XIDs in KnownAssignedXids. They actually
3153 * aborted but we think they were running; the distinction is irrelevant
3154 * because either way any changes done by the transaction are not visible to
3155 * backends in the standby. We prune KnownAssignedXids when
3156 * XLOG_RUNNING_XACTS arrives, to forestall possible overflow of the
3157 * array due to such dead XIDs.
3161 * RecordKnownAssignedTransactionIds
3162 * Record the given XID in KnownAssignedXids, as well as any preceding
3165 * RecordKnownAssignedTransactionIds() should be run for *every* WAL record
3166 * associated with a transaction. Must be called for each record after we
3167 * have executed StartupCLOG() et al, since we must ExtendCLOG() etc..
3169 * Called during recovery in analogy with and in place of GetNewTransactionId()
3172 RecordKnownAssignedTransactionIds(TransactionId xid)
3174 Assert(standbyState >= STANDBY_INITIALIZED);
3175 Assert(TransactionIdIsValid(xid));
3176 Assert(TransactionIdIsValid(latestObservedXid));
3178 elog(trace_recovery(DEBUG4), "record known xact %u latestObservedXid %u",
3179 xid, latestObservedXid);
3182 * When a newly observed xid arrives, it is frequently the case that it is
3183 * *not* the next xid in sequence. When this occurs, we must treat the
3184 * intervening xids as running also.
3186 if (TransactionIdFollows(xid, latestObservedXid))
3188 TransactionId next_expected_xid;
3191 * Extend subtrans like we do in GetNewTransactionId() during normal
3192 * operation using individual extend steps. Note that we do not need
3193 * to extend clog since its extensions are WAL logged.
3195 * This part has to be done regardless of standbyState since we
3196 * immediately start assigning subtransactions to their toplevel
3199 next_expected_xid = latestObservedXid;
3200 while (TransactionIdPrecedes(next_expected_xid, xid))
3202 TransactionIdAdvance(next_expected_xid);
3203 ExtendSUBTRANS(next_expected_xid);
3205 Assert(next_expected_xid == xid);
3208 * If the KnownAssignedXids machinery isn't up yet, there's nothing
3209 * more to do since we don't track assigned xids yet.
3211 if (standbyState <= STANDBY_INITIALIZED)
3213 latestObservedXid = xid;
3218 * Add (latestObservedXid, xid] onto the KnownAssignedXids array.
3220 next_expected_xid = latestObservedXid;
3221 TransactionIdAdvance(next_expected_xid);
3222 KnownAssignedXidsAdd(next_expected_xid, xid, false);
3225 * Now we can advance latestObservedXid
3227 latestObservedXid = xid;
3229 /* ShmemVariableCache->nextXid must be beyond any observed xid */
3230 next_expected_xid = latestObservedXid;
3231 TransactionIdAdvance(next_expected_xid);
3232 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
3233 ShmemVariableCache->nextXid = next_expected_xid;
3234 LWLockRelease(XidGenLock);
3239 * ExpireTreeKnownAssignedTransactionIds
3240 * Remove the given XIDs from KnownAssignedXids.
3242 * Called during recovery in analogy with and in place of ProcArrayEndTransaction()
3245 ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids,
3246 TransactionId *subxids, TransactionId max_xid)
3248 Assert(standbyState >= STANDBY_INITIALIZED);
3251 * Uses same locking as transaction commit
3253 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3255 KnownAssignedXidsRemoveTree(xid, nsubxids, subxids);
3257 /* As in ProcArrayEndTransaction, advance latestCompletedXid */
3258 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
3260 ShmemVariableCache->latestCompletedXid = max_xid;
3262 LWLockRelease(ProcArrayLock);
3266 * ExpireAllKnownAssignedTransactionIds
3267 * Remove all entries in KnownAssignedXids
3270 ExpireAllKnownAssignedTransactionIds(void)
3272 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3273 KnownAssignedXidsRemovePreceding(InvalidTransactionId);
3274 LWLockRelease(ProcArrayLock);
3278 * ExpireOldKnownAssignedTransactionIds
3279 * Remove KnownAssignedXids entries preceding the given XID
3282 ExpireOldKnownAssignedTransactionIds(TransactionId xid)
3284 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3285 KnownAssignedXidsRemovePreceding(xid);
3286 LWLockRelease(ProcArrayLock);
3291 * Private module functions to manipulate KnownAssignedXids
3293 * There are 5 main uses of the KnownAssignedXids data structure:
3295 * * backends taking snapshots - all valid XIDs need to be copied out
3296 * * backends seeking to determine presence of a specific XID
3297 * * startup process adding new known-assigned XIDs
3298 * * startup process removing specific XIDs as transactions end
3299 * * startup process pruning array when special WAL records arrive
3301 * This data structure is known to be a hot spot during Hot Standby, so we
3302 * go to some lengths to make these operations as efficient and as concurrent
3305 * The XIDs are stored in an array in sorted order --- TransactionIdPrecedes
3306 * order, to be exact --- to allow binary search for specific XIDs. Note:
3307 * in general TransactionIdPrecedes would not provide a total order, but
3308 * we know that the entries present at any instant should not extend across
3309 * a large enough fraction of XID space to wrap around (the master would
3310 * shut down for fear of XID wrap long before that happens). So it's OK to
3311 * use TransactionIdPrecedes as a binary-search comparator.
3313 * It's cheap to maintain the sortedness during insertions, since new known
3314 * XIDs are always reported in XID order; we just append them at the right.
3316 * To keep individual deletions cheap, we need to allow gaps in the array.
3317 * This is implemented by marking array elements as valid or invalid using
3318 * the parallel boolean array KnownAssignedXidsValid[]. A deletion is done
3319 * by setting KnownAssignedXidsValid[i] to false, *without* clearing the
3320 * XID entry itself. This preserves the property that the XID entries are
3321 * sorted, so we can do binary searches easily. Periodically we compress
3322 * out the unused entries; that's much cheaper than having to compress the
3323 * array immediately on every deletion.
3325 * The actually valid items in KnownAssignedXids[] and KnownAssignedXidsValid[]
3326 * are those with indexes tail <= i < head; items outside this subscript range
3327 * have unspecified contents. When head reaches the end of the array, we
3328 * force compression of unused entries rather than wrapping around, since
3329 * allowing wraparound would greatly complicate the search logic. We maintain
3330 * an explicit tail pointer so that pruning of old XIDs can be done without
3331 * immediately moving the array contents. In most cases only a small fraction
3332 * of the array contains valid entries at any instant.
3334 * Although only the startup process can ever change the KnownAssignedXids
3335 * data structure, we still need interlocking so that standby backends will
3336 * not observe invalid intermediate states. The convention is that backends
3337 * must hold shared ProcArrayLock to examine the array. To remove XIDs from
3338 * the array, the startup process must hold ProcArrayLock exclusively, for
3339 * the usual transactional reasons (compare commit/abort of a transaction
3340 * during normal running). Compressing unused entries out of the array
3341 * likewise requires exclusive lock. To add XIDs to the array, we just insert
3342 * them into slots to the right of the head pointer and then advance the head
3343 * pointer. This wouldn't require any lock at all, except that on machines
3344 * with weak memory ordering we need to be careful that other processors
3345 * see the array element changes before they see the head pointer change.
3346 * We handle this by using a spinlock to protect reads and writes of the
3347 * head/tail pointers. (We could dispense with the spinlock if we were to
3348 * create suitable memory access barrier primitives and use those instead.)
3349 * The spinlock must be taken to read or write the head/tail pointers unless
3350 * the caller holds ProcArrayLock exclusively.
3352 * Algorithmic analysis:
3354 * If we have a maximum of M slots, with N XIDs currently spread across
3355 * S elements then we have N <= S <= M always.
3357 * * Adding a new XID is O(1) and needs little locking (unless compression
3359 * * Compressing the array is O(S) and requires exclusive lock
3360 * * Removing an XID is O(logS) and requires exclusive lock
3361 * * Taking a snapshot is O(S) and requires shared lock
3362 * * Checking for an XID is O(logS) and requires shared lock
3364 * In comparison, using a hash table for KnownAssignedXids would mean that
3365 * taking snapshots would be O(M). If we can maintain S << M then the
3366 * sorted array technique will deliver significantly faster snapshots.
3367 * If we try to keep S too small then we will spend too much time compressing,
3368 * so there is an optimal point for any workload mix. We use a heuristic to
3369 * decide when to compress the array, though trimming also helps reduce
3370 * frequency of compressing. The heuristic requires us to track the number of
3371 * currently valid XIDs in the array.
3376 * Compress KnownAssignedXids by shifting valid data down to the start of the
3377 * array, removing any gaps.
3379 * A compression step is forced if "force" is true, otherwise we do it
3380 * only if a heuristic indicates it's a good time to do it.
3382 * Caller must hold ProcArrayLock in exclusive mode.
3385 KnownAssignedXidsCompress(bool force)
3387 /* use volatile pointer to prevent code rearrangement */
3388 volatile ProcArrayStruct *pArray = procArray;
3394 /* no spinlock required since we hold ProcArrayLock exclusively */
3395 head = pArray->headKnownAssignedXids;
3396 tail = pArray->tailKnownAssignedXids;
3401 * If we can choose how much to compress, use a heuristic to avoid
3402 * compressing too often or not often enough.
3404 * Heuristic is if we have a large enough current spread and less than
3405 * 50% of the elements are currently in use, then compress. This
3406 * should ensure we compress fairly infrequently. We could compress
3407 * less often though the virtual array would spread out more and
3408 * snapshots would become more expensive.
3410 int nelements = head - tail;
3412 if (nelements < 4 * PROCARRAY_MAXPROCS ||
3413 nelements < 2 * pArray->numKnownAssignedXids)
3418 * We compress the array by reading the valid values from tail to head,
3419 * re-aligning data to 0th element.
3422 for (i = tail; i < head; i++)
3424 if (KnownAssignedXidsValid[i])
3426 KnownAssignedXids[compress_index] = KnownAssignedXids[i];
3427 KnownAssignedXidsValid[compress_index] = true;
3432 pArray->tailKnownAssignedXids = 0;
3433 pArray->headKnownAssignedXids = compress_index;
3437 * Add xids into KnownAssignedXids at the head of the array.
3439 * xids from from_xid to to_xid, inclusive, are added to the array.
3441 * If exclusive_lock is true then caller already holds ProcArrayLock in
3442 * exclusive mode, so we need no extra locking here. Else caller holds no
3443 * lock, so we need to be sure we maintain sufficient interlocks against
3444 * concurrent readers. (Only the startup process ever calls this, so no need
3445 * to worry about concurrent writers.)
3448 KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
3449 bool exclusive_lock)
3451 /* use volatile pointer to prevent code rearrangement */
3452 volatile ProcArrayStruct *pArray = procArray;
3453 TransactionId next_xid;
3459 Assert(TransactionIdPrecedesOrEquals(from_xid, to_xid));
3462 * Calculate how many array slots we'll need. Normally this is cheap; in
3463 * the unusual case where the XIDs cross the wrap point, we do it the hard
3466 if (to_xid >= from_xid)
3467 nxids = to_xid - from_xid + 1;
3471 next_xid = from_xid;
3472 while (TransactionIdPrecedes(next_xid, to_xid))
3475 TransactionIdAdvance(next_xid);
3480 * Since only the startup process modifies the head/tail pointers, we
3481 * don't need a lock to read them here.
3483 head = pArray->headKnownAssignedXids;
3484 tail = pArray->tailKnownAssignedXids;
3486 Assert(head >= 0 && head <= pArray->maxKnownAssignedXids);
3487 Assert(tail >= 0 && tail < pArray->maxKnownAssignedXids);
3490 * Verify that insertions occur in TransactionId sequence. Note that even
3491 * if the last existing element is marked invalid, it must still have a
3492 * correctly sequenced XID value.
3495 TransactionIdFollowsOrEquals(KnownAssignedXids[head - 1], from_xid))
3497 KnownAssignedXidsDisplay(LOG);
3498 elog(ERROR, "out-of-order XID insertion in KnownAssignedXids");
3502 * If our xids won't fit in the remaining space, compress out free space
3504 if (head + nxids > pArray->maxKnownAssignedXids)
3506 /* must hold lock to compress */
3507 if (!exclusive_lock)
3508 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3510 KnownAssignedXidsCompress(true);
3512 head = pArray->headKnownAssignedXids;
3513 /* note: we no longer care about the tail pointer */
3515 if (!exclusive_lock)
3516 LWLockRelease(ProcArrayLock);
3519 * If it still won't fit then we're out of memory
3521 if (head + nxids > pArray->maxKnownAssignedXids)
3522 elog(ERROR, "too many KnownAssignedXids");
3525 /* Now we can insert the xids into the space starting at head */
3526 next_xid = from_xid;
3527 for (i = 0; i < nxids; i++)
3529 KnownAssignedXids[head] = next_xid;
3530 KnownAssignedXidsValid[head] = true;
3531 TransactionIdAdvance(next_xid);
3535 /* Adjust count of number of valid entries */
3536 pArray->numKnownAssignedXids += nxids;
3539 * Now update the head pointer. We use a spinlock to protect this
3540 * pointer, not because the update is likely to be non-atomic, but to
3541 * ensure that other processors see the above array updates before they
3542 * see the head pointer change.
3544 * If we're holding ProcArrayLock exclusively, there's no need to take the
3548 pArray->headKnownAssignedXids = head;
3551 SpinLockAcquire(&pArray->known_assigned_xids_lck);
3552 pArray->headKnownAssignedXids = head;
3553 SpinLockRelease(&pArray->known_assigned_xids_lck);
3558 * KnownAssignedXidsSearch
3560 * Searches KnownAssignedXids for a specific xid and optionally removes it.
3561 * Returns true if it was found, false if not.
3563 * Caller must hold ProcArrayLock in shared or exclusive mode.
3564 * Exclusive lock must be held for remove = true.
3567 KnownAssignedXidsSearch(TransactionId xid, bool remove)
3569 /* use volatile pointer to prevent code rearrangement */
3570 volatile ProcArrayStruct *pArray = procArray;
3575 int result_index = -1;
3579 /* we hold ProcArrayLock exclusively, so no need for spinlock */
3580 tail = pArray->tailKnownAssignedXids;
3581 head = pArray->headKnownAssignedXids;
3585 /* take spinlock to ensure we see up-to-date array contents */
3586 SpinLockAcquire(&pArray->known_assigned_xids_lck);
3587 tail = pArray->tailKnownAssignedXids;
3588 head = pArray->headKnownAssignedXids;
3589 SpinLockRelease(&pArray->known_assigned_xids_lck);
3593 * Standard binary search. Note we can ignore the KnownAssignedXidsValid
3594 * array here, since even invalid entries will contain sorted XIDs.
3598 while (first <= last)
3601 TransactionId mid_xid;
3603 mid_index = (first + last) / 2;
3604 mid_xid = KnownAssignedXids[mid_index];
3608 result_index = mid_index;
3611 else if (TransactionIdPrecedes(xid, mid_xid))
3612 last = mid_index - 1;
3614 first = mid_index + 1;
3617 if (result_index < 0)
3618 return false; /* not in array */
3620 if (!KnownAssignedXidsValid[result_index])
3621 return false; /* in array, but invalid */
3625 KnownAssignedXidsValid[result_index] = false;
3627 pArray->numKnownAssignedXids--;
3628 Assert(pArray->numKnownAssignedXids >= 0);
3631 * If we're removing the tail element then advance tail pointer over
3632 * any invalid elements. This will speed future searches.
3634 if (result_index == tail)
3637 while (tail < head && !KnownAssignedXidsValid[tail])
3641 /* Array is empty, so we can reset both pointers */
3642 pArray->headKnownAssignedXids = 0;
3643 pArray->tailKnownAssignedXids = 0;
3647 pArray->tailKnownAssignedXids = tail;
3656 * Is the specified XID present in KnownAssignedXids[]?
3658 * Caller must hold ProcArrayLock in shared or exclusive mode.
3661 KnownAssignedXidExists(TransactionId xid)
3663 Assert(TransactionIdIsValid(xid));
3665 return KnownAssignedXidsSearch(xid, false);
3669 * Remove the specified XID from KnownAssignedXids[].
3671 * Caller must hold ProcArrayLock in exclusive mode.
3674 KnownAssignedXidsRemove(TransactionId xid)
3676 Assert(TransactionIdIsValid(xid));
3678 elog(trace_recovery(DEBUG4), "remove KnownAssignedXid %u", xid);
3681 * Note: we cannot consider it an error to remove an XID that's not
3682 * present. We intentionally remove subxact IDs while processing
3683 * XLOG_XACT_ASSIGNMENT, to avoid array overflow. Then those XIDs will be
3684 * removed again when the top-level xact commits or aborts.
3686 * It might be possible to track such XIDs to distinguish this case from
3687 * actual errors, but it would be complicated and probably not worth it.
3688 * So, just ignore the search result.
3690 (void) KnownAssignedXidsSearch(xid, true);
3694 * KnownAssignedXidsRemoveTree
3695 * Remove xid (if it's not InvalidTransactionId) and all the subxids.
3697 * Caller must hold ProcArrayLock in exclusive mode.
3700 KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
3701 TransactionId *subxids)
3705 if (TransactionIdIsValid(xid))
3706 KnownAssignedXidsRemove(xid);
3708 for (i = 0; i < nsubxids; i++)
3709 KnownAssignedXidsRemove(subxids[i]);
3711 /* Opportunistically compress the array */
3712 KnownAssignedXidsCompress(false);
3716 * Prune KnownAssignedXids up to, but *not* including xid. If xid is invalid
3717 * then clear the whole table.
3719 * Caller must hold ProcArrayLock in exclusive mode.
3722 KnownAssignedXidsRemovePreceding(TransactionId removeXid)
3724 /* use volatile pointer to prevent code rearrangement */
3725 volatile ProcArrayStruct *pArray = procArray;
3731 if (!TransactionIdIsValid(removeXid))
3733 elog(trace_recovery(DEBUG4), "removing all KnownAssignedXids");
3734 pArray->numKnownAssignedXids = 0;
3735 pArray->headKnownAssignedXids = pArray->tailKnownAssignedXids = 0;
3739 elog(trace_recovery(DEBUG4), "prune KnownAssignedXids to %u", removeXid);
3742 * Mark entries invalid starting at the tail. Since array is sorted, we
3743 * can stop as soon as we reach an entry >= removeXid.
3745 tail = pArray->tailKnownAssignedXids;
3746 head = pArray->headKnownAssignedXids;
3748 for (i = tail; i < head; i++)
3750 if (KnownAssignedXidsValid[i])
3752 TransactionId knownXid = KnownAssignedXids[i];
3754 if (TransactionIdFollowsOrEquals(knownXid, removeXid))
3757 if (!StandbyTransactionIdIsPrepared(knownXid))
3759 KnownAssignedXidsValid[i] = false;
3765 pArray->numKnownAssignedXids -= count;
3766 Assert(pArray->numKnownAssignedXids >= 0);
3769 * Advance the tail pointer if we've marked the tail item invalid.
3771 for (i = tail; i < head; i++)
3773 if (KnownAssignedXidsValid[i])
3778 /* Array is empty, so we can reset both pointers */
3779 pArray->headKnownAssignedXids = 0;
3780 pArray->tailKnownAssignedXids = 0;
3784 pArray->tailKnownAssignedXids = i;
3787 /* Opportunistically compress the array */
3788 KnownAssignedXidsCompress(false);
3792 * KnownAssignedXidsGet - Get an array of xids by scanning KnownAssignedXids.
3793 * We filter out anything >= xmax.
3795 * Returns the number of XIDs stored into xarray[]. Caller is responsible
3796 * that array is large enough.
3798 * Caller must hold ProcArrayLock in (at least) shared mode.
3801 KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax)
3803 TransactionId xtmp = InvalidTransactionId;
3805 return KnownAssignedXidsGetAndSetXmin(xarray, &xtmp, xmax);
3809 * KnownAssignedXidsGetAndSetXmin - as KnownAssignedXidsGet, plus
3810 * we reduce *xmin to the lowest xid value seen if not already lower.
3812 * Caller must hold ProcArrayLock in (at least) shared mode.
3815 KnownAssignedXidsGetAndSetXmin(TransactionId *xarray, TransactionId *xmin,
3824 * Fetch head just once, since it may change while we loop. We can stop
3825 * once we reach the initially seen head, since we are certain that an xid
3826 * cannot enter and then leave the array while we hold ProcArrayLock. We
3827 * might miss newly-added xids, but they should be >= xmax so irrelevant
3830 * Must take spinlock to ensure we see up-to-date array contents.
3832 SpinLockAcquire(&procArray->known_assigned_xids_lck);
3833 tail = procArray->tailKnownAssignedXids;
3834 head = procArray->headKnownAssignedXids;
3835 SpinLockRelease(&procArray->known_assigned_xids_lck);
3837 for (i = tail; i < head; i++)
3839 /* Skip any gaps in the array */
3840 if (KnownAssignedXidsValid[i])
3842 TransactionId knownXid = KnownAssignedXids[i];
3845 * Update xmin if required. Only the first XID need be checked,
3846 * since the array is sorted.
3849 TransactionIdPrecedes(knownXid, *xmin))
3853 * Filter out anything >= xmax, again relying on sorted property
3856 if (TransactionIdIsValid(xmax) &&
3857 TransactionIdFollowsOrEquals(knownXid, xmax))
3860 /* Add knownXid into output array */
3861 xarray[count++] = knownXid;
3869 * Get oldest XID in the KnownAssignedXids array, or InvalidTransactionId
3872 static TransactionId
3873 KnownAssignedXidsGetOldestXmin(void)
3880 * Fetch head just once, since it may change while we loop.
3882 SpinLockAcquire(&procArray->known_assigned_xids_lck);
3883 tail = procArray->tailKnownAssignedXids;
3884 head = procArray->headKnownAssignedXids;
3885 SpinLockRelease(&procArray->known_assigned_xids_lck);
3887 for (i = tail; i < head; i++)
3889 /* Skip any gaps in the array */
3890 if (KnownAssignedXidsValid[i])
3891 return KnownAssignedXids[i];
3894 return InvalidTransactionId;
3898 * Display KnownAssignedXids to provide debug trail
3900 * Currently this is only called within startup process, so we need no
3903 * Note this is pretty expensive, and much of the expense will be incurred
3904 * even if the elog message will get discarded. It's not currently called
3905 * in any performance-critical places, however, so no need to be tenser.
3908 KnownAssignedXidsDisplay(int trace_level)
3910 /* use volatile pointer to prevent code rearrangement */
3911 volatile ProcArrayStruct *pArray = procArray;
3918 tail = pArray->tailKnownAssignedXids;
3919 head = pArray->headKnownAssignedXids;
3921 initStringInfo(&buf);
3923 for (i = tail; i < head; i++)
3925 if (KnownAssignedXidsValid[i])
3928 appendStringInfo(&buf, "[%d]=%u ", i, KnownAssignedXids[i]);
3932 elog(trace_level, "%d KnownAssignedXids (num=%d tail=%d head=%d) %s",
3934 pArray->numKnownAssignedXids,
3935 pArray->tailKnownAssignedXids,
3936 pArray->headKnownAssignedXids,
3943 * KnownAssignedXidsReset
3944 * Resets KnownAssignedXids to be empty
3947 KnownAssignedXidsReset(void)
3949 /* use volatile pointer to prevent code rearrangement */
3950 volatile ProcArrayStruct *pArray = procArray;
3952 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3954 pArray->numKnownAssignedXids = 0;
3955 pArray->tailKnownAssignedXids = 0;
3956 pArray->headKnownAssignedXids = 0;
3958 LWLockRelease(ProcArrayLock);