1 /*-------------------------------------------------------------------------
4 * POSTGRES process array code.
7 * This module maintains an unsorted array of the PGPROC 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 MyProc->xid field.
13 * See notes in src/backend/access/transam/README.
15 * The process array now also includes PGPROC structures representing
16 * prepared transactions. The xid and subxids fields of these are valid,
17 * as are the myProcLocks lists. They can be distinguished from regular
18 * backend PGPROCs at need by checking for pid == 0.
21 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
22 * Portions Copyright (c) 1994, Regents of the University of California
26 * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.46 2008/08/04 18:03:46 tgl Exp $
28 *-------------------------------------------------------------------------
34 #include "access/subtrans.h"
35 #include "access/transam.h"
36 #include "access/xact.h"
37 #include "access/twophase.h"
38 #include "miscadmin.h"
39 #include "storage/procarray.h"
40 #include "utils/snapmgr.h"
43 /* Our shared memory area */
44 typedef struct ProcArrayStruct
46 int numProcs; /* number of valid procs entries */
47 int maxProcs; /* allocated size of procs array */
50 * We declare procs[] as 1 entry because C wants a fixed-size array, but
51 * actually it is maxProcs entries long.
53 PGPROC *procs[1]; /* VARIABLE LENGTH ARRAY */
56 static ProcArrayStruct *procArray;
61 /* counters for XidCache measurement */
62 static long xc_by_recent_xmin = 0;
63 static long xc_by_known_xact = 0;
64 static long xc_by_my_xact = 0;
65 static long xc_by_latest_xid = 0;
66 static long xc_by_main_xid = 0;
67 static long xc_by_child_xid = 0;
68 static long xc_no_overflow = 0;
69 static long xc_slow_answer = 0;
71 #define xc_by_recent_xmin_inc() (xc_by_recent_xmin++)
72 #define xc_by_known_xact_inc() (xc_by_known_xact++)
73 #define xc_by_my_xact_inc() (xc_by_my_xact++)
74 #define xc_by_latest_xid_inc() (xc_by_latest_xid++)
75 #define xc_by_main_xid_inc() (xc_by_main_xid++)
76 #define xc_by_child_xid_inc() (xc_by_child_xid++)
77 #define xc_no_overflow_inc() (xc_no_overflow++)
78 #define xc_slow_answer_inc() (xc_slow_answer++)
80 static void DisplayXidCache(void);
81 #else /* !XIDCACHE_DEBUG */
83 #define xc_by_recent_xmin_inc() ((void) 0)
84 #define xc_by_known_xact_inc() ((void) 0)
85 #define xc_by_my_xact_inc() ((void) 0)
86 #define xc_by_latest_xid_inc() ((void) 0)
87 #define xc_by_main_xid_inc() ((void) 0)
88 #define xc_by_child_xid_inc() ((void) 0)
89 #define xc_no_overflow_inc() ((void) 0)
90 #define xc_slow_answer_inc() ((void) 0)
91 #endif /* XIDCACHE_DEBUG */
95 * Report shared-memory space needed by CreateSharedProcArray.
98 ProcArrayShmemSize(void)
102 size = offsetof(ProcArrayStruct, procs);
103 size = add_size(size, mul_size(sizeof(PGPROC *),
104 add_size(MaxBackends, max_prepared_xacts)));
110 * Initialize the shared PGPROC array during postmaster startup.
113 CreateSharedProcArray(void)
117 /* Create or attach to the ProcArray shared structure */
118 procArray = (ProcArrayStruct *)
119 ShmemInitStruct("Proc Array", ProcArrayShmemSize(), &found);
124 * We're the first - initialize.
126 procArray->numProcs = 0;
127 procArray->maxProcs = MaxBackends + max_prepared_xacts;
132 * Add the specified PGPROC to the shared array.
135 ProcArrayAdd(PGPROC *proc)
137 ProcArrayStruct *arrayP = procArray;
139 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
141 if (arrayP->numProcs >= arrayP->maxProcs)
144 * Ooops, no room. (This really shouldn't happen, since there is a
145 * fixed supply of PGPROC structs too, and so we should have failed
148 LWLockRelease(ProcArrayLock);
150 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
151 errmsg("sorry, too many clients already")));
154 arrayP->procs[arrayP->numProcs] = proc;
157 LWLockRelease(ProcArrayLock);
161 * Remove the specified PGPROC from the shared array.
163 * When latestXid is a valid XID, we are removing a live 2PC gxact from the
164 * array, and thus causing it to appear as "not running" anymore. In this
165 * case we must advance latestCompletedXid. (This is essentially the same
166 * as ProcArrayEndTransaction followed by removal of the PGPROC, but we take
167 * the ProcArrayLock only once, and don't damage the content of the PGPROC;
168 * twophase.c depends on the latter.)
171 ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
173 ProcArrayStruct *arrayP = procArray;
176 #ifdef XIDCACHE_DEBUG
177 /* dump stats at backend shutdown, but not prepared-xact end */
182 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
184 if (TransactionIdIsValid(latestXid))
186 Assert(TransactionIdIsValid(proc->xid));
188 /* Advance global latestCompletedXid while holding the lock */
189 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
191 ShmemVariableCache->latestCompletedXid = latestXid;
195 /* Shouldn't be trying to remove a live transaction here */
196 Assert(!TransactionIdIsValid(proc->xid));
199 for (index = 0; index < arrayP->numProcs; index++)
201 if (arrayP->procs[index] == proc)
203 arrayP->procs[index] = arrayP->procs[arrayP->numProcs - 1];
205 LWLockRelease(ProcArrayLock);
211 LWLockRelease(ProcArrayLock);
213 elog(LOG, "failed to find proc %p in ProcArray", proc);
218 * ProcArrayEndTransaction -- mark a transaction as no longer running
220 * This is used interchangeably for commit and abort cases. The transaction
221 * commit/abort must already be reported to WAL and pg_clog.
223 * proc is currently always MyProc, but we pass it explicitly for flexibility.
224 * latestXid is the latest Xid among the transaction's main XID and
225 * subtransactions, or InvalidTransactionId if it has no XID. (We must ask
226 * the caller to pass latestXid, instead of computing it from the PGPROC's
227 * contents, because the subxid information in the PGPROC might be
231 ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
233 if (TransactionIdIsValid(latestXid))
236 * We must lock ProcArrayLock while clearing proc->xid, so that we do
237 * not exit the set of "running" transactions while someone else is
238 * taking a snapshot. See discussion in
239 * src/backend/access/transam/README.
241 Assert(TransactionIdIsValid(proc->xid));
243 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
245 proc->xid = InvalidTransactionId;
246 proc->lxid = InvalidLocalTransactionId;
247 proc->xmin = InvalidTransactionId;
248 /* must be cleared with xid/xmin: */
249 proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
250 proc->inCommit = false; /* be sure this is cleared in abort */
252 /* Clear the subtransaction-XID cache too while holding the lock */
253 proc->subxids.nxids = 0;
254 proc->subxids.overflowed = false;
256 /* Also advance global latestCompletedXid while holding the lock */
257 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
259 ShmemVariableCache->latestCompletedXid = latestXid;
261 LWLockRelease(ProcArrayLock);
266 * If we have no XID, we don't need to lock, since we won't affect
267 * anyone else's calculation of a snapshot. We might change their
268 * estimate of global xmin, but that's OK.
270 Assert(!TransactionIdIsValid(proc->xid));
272 proc->lxid = InvalidLocalTransactionId;
273 proc->xmin = InvalidTransactionId;
274 /* must be cleared with xid/xmin: */
275 proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
276 proc->inCommit = false; /* be sure this is cleared in abort */
278 Assert(proc->subxids.nxids == 0);
279 Assert(proc->subxids.overflowed == false);
285 * ProcArrayClearTransaction -- clear the transaction fields
287 * This is used after successfully preparing a 2-phase transaction. We are
288 * not actually reporting the transaction's XID as no longer running --- it
289 * will still appear as running because the 2PC's gxact is in the ProcArray
290 * too. We just have to clear out our own PGPROC.
293 ProcArrayClearTransaction(PGPROC *proc)
296 * We can skip locking ProcArrayLock here, because this action does not
297 * actually change anyone's view of the set of running XIDs: our entry is
298 * duplicate with the gxact that has already been inserted into the
301 proc->xid = InvalidTransactionId;
302 proc->lxid = InvalidLocalTransactionId;
303 proc->xmin = InvalidTransactionId;
305 /* redundant, but just in case */
306 proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
307 proc->inCommit = false;
309 /* Clear the subtransaction-XID cache too */
310 proc->subxids.nxids = 0;
311 proc->subxids.overflowed = false;
316 * TransactionIdIsInProgress -- is given transaction running in some backend
318 * Aside from some shortcuts such as checking RecentXmin and our own Xid,
319 * there are three possibilities for finding a running transaction:
321 * 1. the given Xid is a main transaction Id. We will find this out cheaply
322 * by looking at the PGPROC struct for each backend.
324 * 2. the given Xid is one of the cached subxact Xids in the PGPROC array.
325 * We can find this out cheaply too.
327 * 3. Search the SubTrans tree to find the Xid's topmost parent, and then
328 * see if that is running according to PGPROC. This is the slowest, but
329 * sadly it has to be done always if the other two failed, unless we see
330 * that the cached subxact sets are complete (none have overflowed).
332 * ProcArrayLock has to be held while we do 1 and 2. If we save the top Xids
333 * while doing 1, we can release the ProcArrayLock while we do 3. This buys
334 * back some concurrency (we can't retrieve the main Xids from PGPROC again
335 * anyway; see GetNewTransactionId).
338 TransactionIdIsInProgress(TransactionId xid)
340 static TransactionId *xids = NULL;
342 ProcArrayStruct *arrayP = procArray;
343 TransactionId topxid;
348 * Don't bother checking a transaction older than RecentXmin; it could not
349 * possibly still be running. (Note: in particular, this guarantees that
350 * we reject InvalidTransactionId, FrozenTransactionId, etc as not
353 if (TransactionIdPrecedes(xid, RecentXmin))
355 xc_by_recent_xmin_inc();
360 * We may have just checked the status of this transaction, so if it is
361 * already known to be completed, we can fall out without any access to
364 if (TransactionIdIsKnownCompleted(xid))
366 xc_by_known_xact_inc();
371 * Also, we can handle our own transaction (and subtransactions) without
372 * any access to shared memory.
374 if (TransactionIdIsCurrentTransactionId(xid))
381 * If not first time through, get workspace to remember main XIDs in. We
382 * malloc it permanently to avoid repeated palloc/pfree overhead.
386 xids = (TransactionId *)
387 malloc(arrayP->maxProcs * sizeof(TransactionId));
390 (errcode(ERRCODE_OUT_OF_MEMORY),
391 errmsg("out of memory")));
394 LWLockAcquire(ProcArrayLock, LW_SHARED);
397 * Now that we have the lock, we can check latestCompletedXid; if the
398 * target Xid is after that, it's surely still running.
400 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid, xid))
402 LWLockRelease(ProcArrayLock);
403 xc_by_latest_xid_inc();
407 /* No shortcuts, gotta grovel through the array */
408 for (i = 0; i < arrayP->numProcs; i++)
410 volatile PGPROC *proc = arrayP->procs[i];
413 /* Ignore my own proc --- dealt with it above */
417 /* Fetch xid just once - see GetNewTransactionId */
420 if (!TransactionIdIsValid(pxid))
424 * Step 1: check the main Xid
426 if (TransactionIdEquals(pxid, xid))
428 LWLockRelease(ProcArrayLock);
429 xc_by_main_xid_inc();
434 * We can ignore main Xids that are younger than the target Xid, since
435 * the target could not possibly be their child.
437 if (TransactionIdPrecedes(xid, pxid))
441 * Step 2: check the cached child-Xids arrays
443 for (j = proc->subxids.nxids - 1; j >= 0; j--)
445 /* Fetch xid just once - see GetNewTransactionId */
446 TransactionId cxid = proc->subxids.xids[j];
448 if (TransactionIdEquals(cxid, xid))
450 LWLockRelease(ProcArrayLock);
451 xc_by_child_xid_inc();
457 * Save the main Xid for step 3. We only need to remember main Xids
458 * that have uncached children. (Note: there is no race condition
459 * here because the overflowed flag cannot be cleared, only set, while
460 * we hold ProcArrayLock. So we can't miss an Xid that we need to
463 if (proc->subxids.overflowed)
464 xids[nxids++] = pxid;
467 LWLockRelease(ProcArrayLock);
470 * If none of the relevant caches overflowed, we know the Xid is not
471 * running without looking at pg_subtrans.
475 xc_no_overflow_inc();
480 * Step 3: have to check pg_subtrans.
482 * At this point, we know it's either a subtransaction of one of the Xids
483 * in xids[], or it's not running. If it's an already-failed
484 * subtransaction, we want to say "not running" even though its parent may
485 * still be running. So first, check pg_clog to see if it's been aborted.
487 xc_slow_answer_inc();
489 if (TransactionIdDidAbort(xid))
493 * It isn't aborted, so check whether the transaction tree it belongs to
494 * is still running (or, more precisely, whether it was running when we
495 * held ProcArrayLock).
497 topxid = SubTransGetTopmostTransaction(xid);
498 Assert(TransactionIdIsValid(topxid));
499 if (!TransactionIdEquals(topxid, xid))
501 for (i = 0; i < nxids; i++)
503 if (TransactionIdEquals(xids[i], topxid))
512 * TransactionIdIsActive -- is xid the top-level XID of an active backend?
514 * This differs from TransactionIdIsInProgress in that it ignores prepared
515 * transactions. Also, we ignore subtransactions since that's not needed
519 TransactionIdIsActive(TransactionId xid)
522 ProcArrayStruct *arrayP = procArray;
526 * Don't bother checking a transaction older than RecentXmin; it could not
527 * possibly still be running.
529 if (TransactionIdPrecedes(xid, RecentXmin))
532 LWLockAcquire(ProcArrayLock, LW_SHARED);
534 for (i = 0; i < arrayP->numProcs; i++)
536 volatile PGPROC *proc = arrayP->procs[i];
538 /* Fetch xid just once - see GetNewTransactionId */
539 TransactionId pxid = proc->xid;
541 if (!TransactionIdIsValid(pxid))
545 continue; /* ignore prepared transactions */
547 if (TransactionIdEquals(pxid, xid))
554 LWLockRelease(ProcArrayLock);
561 * GetOldestXmin -- returns oldest transaction that was running
562 * when any current transaction was started.
564 * If allDbs is TRUE then all backends are considered; if allDbs is FALSE
565 * then only backends running in my own database are considered.
567 * If ignoreVacuum is TRUE then backends with the PROC_IN_VACUUM flag set are
570 * This is used by VACUUM to decide which deleted tuples must be preserved
571 * in a table. allDbs = TRUE is needed for shared relations, but allDbs =
572 * FALSE is sufficient for non-shared relations, since only backends in my
573 * own database could ever see the tuples in them. Also, we can ignore
574 * concurrently running lazy VACUUMs because (a) they must be working on other
575 * tables, and (b) they don't need to do snapshot-based lookups.
577 * This is also used to determine where to truncate pg_subtrans. allDbs
578 * must be TRUE for that case, and ignoreVacuum FALSE.
580 * Note: we include all currently running xids in the set of considered xids.
581 * This ensures that if a just-started xact has not yet set its snapshot,
582 * when it does set the snapshot it cannot set xmin less than what we compute.
583 * See notes in src/backend/access/transam/README.
586 GetOldestXmin(bool allDbs, bool ignoreVacuum)
588 ProcArrayStruct *arrayP = procArray;
589 TransactionId result;
592 LWLockAcquire(ProcArrayLock, LW_SHARED);
595 * We initialize the MIN() calculation with latestCompletedXid + 1. This
596 * is a lower bound for the XIDs that might appear in the ProcArray later,
597 * and so protects us against overestimating the result due to future
600 result = ShmemVariableCache->latestCompletedXid;
601 Assert(TransactionIdIsNormal(result));
602 TransactionIdAdvance(result);
604 for (index = 0; index < arrayP->numProcs; index++)
606 volatile PGPROC *proc = arrayP->procs[index];
608 if (ignoreVacuum && (proc->vacuumFlags & PROC_IN_VACUUM))
611 if (allDbs || proc->databaseId == MyDatabaseId)
613 /* Fetch xid just once - see GetNewTransactionId */
614 TransactionId xid = proc->xid;
616 /* First consider the transaction's own Xid, if any */
617 if (TransactionIdIsNormal(xid) &&
618 TransactionIdPrecedes(xid, result))
622 * Also consider the transaction's Xmin, if set.
624 * We must check both Xid and Xmin because a transaction might
625 * have an Xmin but not (yet) an Xid; conversely, if it has an
626 * Xid, that could determine some not-yet-set Xmin.
628 xid = proc->xmin; /* Fetch just once */
629 if (TransactionIdIsNormal(xid) &&
630 TransactionIdPrecedes(xid, result))
635 LWLockRelease(ProcArrayLock);
641 * GetSnapshotData -- returns information about running transactions.
643 * The returned snapshot includes xmin (lowest still-running xact ID),
644 * xmax (highest completed xact ID + 1), and a list of running xact IDs
645 * in the range xmin <= xid < xmax. It is used as follows:
646 * All xact IDs < xmin are considered finished.
647 * All xact IDs >= xmax are considered still running.
648 * For an xact ID xmin <= xid < xmax, consult list to see whether
649 * it is considered running or not.
650 * This ensures that the set of transactions seen as "running" by the
651 * current xact will not change after it takes the snapshot.
653 * All running top-level XIDs are included in the snapshot, except for lazy
654 * VACUUM processes. We also try to include running subtransaction XIDs,
655 * but since PGPROC has only a limited cache area for subxact XIDs, full
656 * information may not be available. If we find any overflowed subxid arrays,
657 * we have to mark the snapshot's subxid data as overflowed, and extra work
658 * will need to be done to determine what's running (see XidInMVCCSnapshot()
661 * We also update the following backend-global variables:
662 * TransactionXmin: the oldest xmin of any snapshot in use in the
663 * current transaction (this is the same as MyProc->xmin).
664 * RecentXmin: the xmin computed for the most recent snapshot. XIDs
665 * older than this are known not running any more.
666 * RecentGlobalXmin: the global xmin (oldest TransactionXmin across all
667 * running transactions, except those running LAZY VACUUM). This is
668 * the same computation done by GetOldestXmin(true, true).
670 * Note: this function should probably not be called with an argument that's
671 * not statically allocated (see xip allocation below).
674 GetSnapshotData(Snapshot snapshot)
676 ProcArrayStruct *arrayP = procArray;
679 TransactionId globalxmin;
684 Assert(snapshot != NULL);
687 * Allocating space for maxProcs xids is usually overkill; numProcs would
688 * be sufficient. But it seems better to do the malloc while not holding
689 * the lock, so we can't look at numProcs. Likewise, we allocate much
690 * more subxip storage than is probably needed.
692 * This does open a possibility for avoiding repeated malloc/free: since
693 * maxProcs does not change at runtime, we can simply reuse the previous
694 * xip arrays if any. (This relies on the fact that all callers pass
695 * static SnapshotData structs.)
697 if (snapshot->xip == NULL)
700 * First call for this snapshot
702 snapshot->xip = (TransactionId *)
703 malloc(arrayP->maxProcs * sizeof(TransactionId));
704 if (snapshot->xip == NULL)
706 (errcode(ERRCODE_OUT_OF_MEMORY),
707 errmsg("out of memory")));
708 Assert(snapshot->subxip == NULL);
709 snapshot->subxip = (TransactionId *)
710 malloc(arrayP->maxProcs * PGPROC_MAX_CACHED_SUBXIDS * sizeof(TransactionId));
711 if (snapshot->subxip == NULL)
713 (errcode(ERRCODE_OUT_OF_MEMORY),
714 errmsg("out of memory")));
718 * It is sufficient to get shared lock on ProcArrayLock, even if we are
719 * going to set MyProc->xmin.
721 LWLockAcquire(ProcArrayLock, LW_SHARED);
723 /* xmax is always latestCompletedXid + 1 */
724 xmax = ShmemVariableCache->latestCompletedXid;
725 Assert(TransactionIdIsNormal(xmax));
726 TransactionIdAdvance(xmax);
728 /* initialize xmin calculation with xmax */
729 globalxmin = xmin = xmax;
732 * Spin over procArray checking xid, xmin, and subxids. The goal is to
733 * gather all active xids, find the lowest xmin, and try to record
736 for (index = 0; index < arrayP->numProcs; index++)
738 volatile PGPROC *proc = arrayP->procs[index];
741 /* Ignore procs running LAZY VACUUM */
742 if (proc->vacuumFlags & PROC_IN_VACUUM)
745 /* Update globalxmin to be the smallest valid xmin */
746 xid = proc->xmin; /* fetch just once */
747 if (TransactionIdIsNormal(xid) &&
748 TransactionIdPrecedes(xid, globalxmin))
751 /* Fetch xid just once - see GetNewTransactionId */
755 * If the transaction has been assigned an xid < xmax we add it to the
756 * snapshot, and update xmin if necessary. There's no need to store
757 * XIDs >= xmax, since we'll treat them as running anyway. We don't
758 * bother to examine their subxids either.
760 * We don't include our own XID (if any) in the snapshot, but we must
761 * include it into xmin.
763 if (TransactionIdIsNormal(xid))
765 if (TransactionIdFollowsOrEquals(xid, xmax))
768 snapshot->xip[count++] = xid;
769 if (TransactionIdPrecedes(xid, xmin))
774 * Save subtransaction XIDs if possible (if we've already overflowed,
775 * there's no point). Note that the subxact XIDs must be later than
776 * their parent, so no need to check them against xmin. We could
777 * filter against xmax, but it seems better not to do that much work
778 * while holding the ProcArrayLock.
780 * The other backend can add more subxids concurrently, but cannot
781 * remove any. Hence it's important to fetch nxids just once. Should
782 * be safe to use memcpy, though. (We needn't worry about missing any
783 * xids added concurrently, because they must postdate xmax.)
785 * Again, our own XIDs are not included in the snapshot.
787 if (subcount >= 0 && proc != MyProc)
789 if (proc->subxids.overflowed)
790 subcount = -1; /* overflowed */
793 int nxids = proc->subxids.nxids;
797 memcpy(snapshot->subxip + subcount,
798 (void *) proc->subxids.xids,
799 nxids * sizeof(TransactionId));
806 if (!TransactionIdIsValid(MyProc->xmin))
807 MyProc->xmin = TransactionXmin = xmin;
809 LWLockRelease(ProcArrayLock);
812 * Update globalxmin to include actual process xids. This is a slightly
813 * different way of computing it than GetOldestXmin uses, but should give
816 if (TransactionIdPrecedes(xmin, globalxmin))
819 /* Update global variables too */
820 RecentGlobalXmin = globalxmin;
823 snapshot->xmin = xmin;
824 snapshot->xmax = xmax;
825 snapshot->xcnt = count;
826 snapshot->subxcnt = subcount;
828 snapshot->curcid = GetCurrentCommandId(false);
831 * This is a new snapshot, so set both refcounts are zero, and mark it
832 * as not copied in persistent memory.
834 snapshot->active_count = 0;
835 snapshot->regd_count = 0;
836 snapshot->copied = false;
842 * GetTransactionsInCommit -- Get the XIDs of transactions that are committing
844 * Constructs an array of XIDs of transactions that are currently in commit
845 * critical sections, as shown by having inCommit set in their PGPROC entries.
847 * *xids_p is set to a palloc'd array that should be freed by the caller.
848 * The return value is the number of valid entries.
850 * Note that because backends set or clear inCommit without holding any lock,
851 * the result is somewhat indeterminate, but we don't really care. Even in
852 * a multiprocessor with delayed writes to shared memory, it should be certain
853 * that setting of inCommit will propagate to shared memory when the backend
854 * takes the WALInsertLock, so we cannot fail to see an xact as inCommit if
855 * it's already inserted its commit record. Whether it takes a little while
856 * for clearing of inCommit to propagate is unimportant for correctness.
859 GetTransactionsInCommit(TransactionId **xids_p)
861 ProcArrayStruct *arrayP = procArray;
866 xids = (TransactionId *) palloc(arrayP->maxProcs * sizeof(TransactionId));
869 LWLockAcquire(ProcArrayLock, LW_SHARED);
871 for (index = 0; index < arrayP->numProcs; index++)
873 volatile PGPROC *proc = arrayP->procs[index];
875 /* Fetch xid just once - see GetNewTransactionId */
876 TransactionId pxid = proc->xid;
878 if (proc->inCommit && TransactionIdIsValid(pxid))
879 xids[nxids++] = pxid;
882 LWLockRelease(ProcArrayLock);
889 * HaveTransactionsInCommit -- Are any of the specified XIDs in commit?
891 * This is used with the results of GetTransactionsInCommit to see if any
892 * of the specified XIDs are still in their commit critical sections.
894 * Note: this is O(N^2) in the number of xacts that are/were in commit, but
895 * those numbers should be small enough for it not to be a problem.
898 HaveTransactionsInCommit(TransactionId *xids, int nxids)
901 ProcArrayStruct *arrayP = procArray;
904 LWLockAcquire(ProcArrayLock, LW_SHARED);
906 for (index = 0; index < arrayP->numProcs; index++)
908 volatile PGPROC *proc = arrayP->procs[index];
910 /* Fetch xid just once - see GetNewTransactionId */
911 TransactionId pxid = proc->xid;
913 if (proc->inCommit && TransactionIdIsValid(pxid))
917 for (i = 0; i < nxids; i++)
930 LWLockRelease(ProcArrayLock);
936 * BackendPidGetProc -- get a backend's PGPROC given its PID
938 * Returns NULL if not found. Note that it is up to the caller to be
939 * sure that the question remains meaningful for long enough for the
940 * answer to be used ...
943 BackendPidGetProc(int pid)
945 PGPROC *result = NULL;
946 ProcArrayStruct *arrayP = procArray;
949 if (pid == 0) /* never match dummy PGPROCs */
952 LWLockAcquire(ProcArrayLock, LW_SHARED);
954 for (index = 0; index < arrayP->numProcs; index++)
956 PGPROC *proc = arrayP->procs[index];
958 if (proc->pid == pid)
965 LWLockRelease(ProcArrayLock);
971 * BackendXidGetPid -- get a backend's pid given its XID
973 * Returns 0 if not found or it's a prepared transaction. Note that
974 * it is up to the caller to be sure that the question remains
975 * meaningful for long enough for the answer to be used ...
977 * Only main transaction Ids are considered. This function is mainly
978 * useful for determining what backend owns a lock.
980 * Beware that not every xact has an XID assigned. However, as long as you
981 * only call this using an XID found on disk, you're safe.
984 BackendXidGetPid(TransactionId xid)
987 ProcArrayStruct *arrayP = procArray;
990 if (xid == InvalidTransactionId) /* never match invalid xid */
993 LWLockAcquire(ProcArrayLock, LW_SHARED);
995 for (index = 0; index < arrayP->numProcs; index++)
997 volatile PGPROC *proc = arrayP->procs[index];
999 if (proc->xid == xid)
1006 LWLockRelease(ProcArrayLock);
1012 * IsBackendPid -- is a given pid a running backend
1015 IsBackendPid(int pid)
1017 return (BackendPidGetProc(pid) != NULL);
1022 * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
1024 * The array is palloc'd and is terminated with an invalid VXID.
1026 * If limitXmin is not InvalidTransactionId, we skip any backends
1027 * with xmin >= limitXmin. If allDbs is false, we skip backends attached
1028 * to other databases. If excludeVacuum isn't zero, we skip processes for
1029 * which (excludeVacuum & vacuumFlags) is not zero. Also, our own process
1030 * is always skipped.
1032 VirtualTransactionId *
1033 GetCurrentVirtualXIDs(TransactionId limitXmin, bool allDbs, int excludeVacuum)
1035 VirtualTransactionId *vxids;
1036 ProcArrayStruct *arrayP = procArray;
1040 /* allocate result space with room for a terminator */
1041 vxids = (VirtualTransactionId *)
1042 palloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
1044 LWLockAcquire(ProcArrayLock, LW_SHARED);
1046 for (index = 0; index < arrayP->numProcs; index++)
1048 volatile PGPROC *proc = arrayP->procs[index];
1053 if (excludeVacuum & proc->vacuumFlags)
1056 if (allDbs || proc->databaseId == MyDatabaseId)
1058 /* Fetch xmin just once - might change on us? */
1059 TransactionId pxmin = proc->xmin;
1062 * Note that InvalidTransactionId precedes all other XIDs, so a
1063 * proc that hasn't set xmin yet will always be included.
1065 if (!TransactionIdIsValid(limitXmin) ||
1066 TransactionIdPrecedes(pxmin, limitXmin))
1068 VirtualTransactionId vxid;
1070 GET_VXID_FROM_PGPROC(vxid, *proc);
1071 if (VirtualTransactionIdIsValid(vxid))
1072 vxids[count++] = vxid;
1077 LWLockRelease(ProcArrayLock);
1079 /* add the terminator */
1080 vxids[count].backendId = InvalidBackendId;
1081 vxids[count].localTransactionId = InvalidLocalTransactionId;
1088 * CountActiveBackends --- count backends (other than myself) that are in
1089 * active transactions. This is used as a heuristic to decide if
1090 * a pre-XLOG-flush delay is worthwhile during commit.
1092 * Do not count backends that are blocked waiting for locks, since they are
1093 * not going to get to run until someone else commits.
1096 CountActiveBackends(void)
1098 ProcArrayStruct *arrayP = procArray;
1103 * Note: for speed, we don't acquire ProcArrayLock. This is a little bit
1104 * bogus, but since we are only testing fields for zero or nonzero, it
1105 * should be OK. The result is only used for heuristic purposes anyway...
1107 for (index = 0; index < arrayP->numProcs; index++)
1109 volatile PGPROC *proc = arrayP->procs[index];
1112 continue; /* do not count myself */
1114 continue; /* do not count prepared xacts */
1115 if (proc->xid == InvalidTransactionId)
1116 continue; /* do not count if no XID assigned */
1117 if (proc->waitLock != NULL)
1118 continue; /* do not count if blocked on a lock */
1126 * CountDBBackends --- count backends that are using specified database
1129 CountDBBackends(Oid databaseid)
1131 ProcArrayStruct *arrayP = procArray;
1135 LWLockAcquire(ProcArrayLock, LW_SHARED);
1137 for (index = 0; index < arrayP->numProcs; index++)
1139 volatile PGPROC *proc = arrayP->procs[index];
1142 continue; /* do not count prepared xacts */
1143 if (proc->databaseId == databaseid)
1147 LWLockRelease(ProcArrayLock);
1153 * CountUserBackends --- count backends that are used by specified user
1156 CountUserBackends(Oid roleid)
1158 ProcArrayStruct *arrayP = procArray;
1162 LWLockAcquire(ProcArrayLock, LW_SHARED);
1164 for (index = 0; index < arrayP->numProcs; index++)
1166 volatile PGPROC *proc = arrayP->procs[index];
1169 continue; /* do not count prepared xacts */
1170 if (proc->roleId == roleid)
1174 LWLockRelease(ProcArrayLock);
1180 * CountOtherDBBackends -- check for other backends running in the given DB
1182 * If there are other backends in the DB, we will wait a maximum of 5 seconds
1183 * for them to exit. Autovacuum backends are encouraged to exit early by
1184 * sending them SIGTERM, but normal user backends are just waited for.
1186 * The current backend is always ignored; it is caller's responsibility to
1187 * check whether the current backend uses the given DB, if it's important.
1189 * Returns TRUE if there are (still) other backends in the DB, FALSE if not.
1190 * Also, *nbackends and *nprepared are set to the number of other backends
1191 * and prepared transactions in the DB, respectively.
1193 * This function is used to interlock DROP DATABASE and related commands
1194 * against there being any active backends in the target DB --- dropping the
1195 * DB while active backends remain would be a Bad Thing. Note that we cannot
1196 * detect here the possibility of a newly-started backend that is trying to
1197 * connect to the doomed database, so additional interlocking is needed during
1198 * backend startup. The caller should normally hold an exclusive lock on the
1199 * target DB before calling this, which is one reason we mustn't wait
1203 CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
1205 ProcArrayStruct *arrayP = procArray;
1206 #define MAXAUTOVACPIDS 10 /* max autovacs to SIGTERM per iteration */
1207 int autovac_pids[MAXAUTOVACPIDS];
1210 /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
1211 for (tries = 0; tries < 50; tries++)
1217 CHECK_FOR_INTERRUPTS();
1219 *nbackends = *nprepared = 0;
1221 LWLockAcquire(ProcArrayLock, LW_SHARED);
1223 for (index = 0; index < arrayP->numProcs; index++)
1225 volatile PGPROC *proc = arrayP->procs[index];
1227 if (proc->databaseId != databaseId)
1239 if ((proc->vacuumFlags & PROC_IS_AUTOVACUUM) &&
1240 nautovacs < MAXAUTOVACPIDS)
1241 autovac_pids[nautovacs++] = proc->pid;
1245 LWLockRelease(ProcArrayLock);
1248 return false; /* no conflicting backends, so done */
1251 * Send SIGTERM to any conflicting autovacuums before sleeping.
1252 * We postpone this step until after the loop because we don't
1253 * want to hold ProcArrayLock while issuing kill().
1254 * We have no idea what might block kill() inside the kernel...
1256 for (index = 0; index < nautovacs; index++)
1257 (void) kill(autovac_pids[index], SIGTERM); /* ignore any error */
1259 /* sleep, then try again */
1260 pg_usleep(100 * 1000L); /* 100ms */
1263 return true; /* timed out, still conflicts */
1267 #define XidCacheRemove(i) \
1269 MyProc->subxids.xids[i] = MyProc->subxids.xids[MyProc->subxids.nxids - 1]; \
1270 MyProc->subxids.nxids--; \
1274 * XidCacheRemoveRunningXids
1276 * Remove a bunch of TransactionIds from the list of known-running
1277 * subtransactions for my backend. Both the specified xid and those in
1278 * the xids[] array (of length nxids) are removed from the subxids cache.
1279 * latestXid must be the latest XID among the group.
1282 XidCacheRemoveRunningXids(TransactionId xid,
1283 int nxids, const TransactionId *xids,
1284 TransactionId latestXid)
1289 Assert(TransactionIdIsValid(xid));
1292 * We must hold ProcArrayLock exclusively in order to remove transactions
1293 * from the PGPROC array. (See src/backend/access/transam/README.) It's
1294 * possible this could be relaxed since we know this routine is only used
1295 * to abort subtransactions, but pending closer analysis we'd best be
1298 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1301 * Under normal circumstances xid and xids[] will be in increasing order,
1302 * as will be the entries in subxids. Scan backwards to avoid O(N^2)
1303 * behavior when removing a lot of xids.
1305 for (i = nxids - 1; i >= 0; i--)
1307 TransactionId anxid = xids[i];
1309 for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
1311 if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
1319 * Ordinarily we should have found it, unless the cache has
1320 * overflowed. However it's also possible for this routine to be
1321 * invoked multiple times for the same subtransaction, in case of an
1322 * error during AbortSubTransaction. So instead of Assert, emit a
1325 if (j < 0 && !MyProc->subxids.overflowed)
1326 elog(WARNING, "did not find subXID %u in MyProc", anxid);
1329 for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
1331 if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
1337 /* Ordinarily we should have found it, unless the cache has overflowed */
1338 if (j < 0 && !MyProc->subxids.overflowed)
1339 elog(WARNING, "did not find subXID %u in MyProc", xid);
1341 /* Also advance global latestCompletedXid while holding the lock */
1342 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
1344 ShmemVariableCache->latestCompletedXid = latestXid;
1346 LWLockRelease(ProcArrayLock);
1349 #ifdef XIDCACHE_DEBUG
1352 * Print stats about effectiveness of XID cache
1355 DisplayXidCache(void)
1358 "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, nooflo: %ld, slow: %ld\n",
1369 #endif /* XIDCACHE_DEBUG */