]> granicus.if.org Git - postgresql/blob - src/backend/storage/ipc/procarray.c
42ed5865d9c80408c90201c3044afba758ccad43
[postgresql] / src / backend / storage / ipc / procarray.c
1 /*-------------------------------------------------------------------------
2  *
3  * procarray.c
4  *        POSTGRES process array code.
5  *
6  *
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.
10  *
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.
14  *
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.
19  *
20  *
21  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
22  * Portions Copyright (c) 1994, Regents of the University of California
23  *
24  *
25  * IDENTIFICATION
26  *        $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.46 2008/08/04 18:03:46 tgl Exp $
27  *
28  *-------------------------------------------------------------------------
29  */
30 #include "postgres.h"
31
32 #include <signal.h>
33
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"
41
42
43 /* Our shared memory area */
44 typedef struct ProcArrayStruct
45 {
46         int                     numProcs;               /* number of valid procs entries */
47         int                     maxProcs;               /* allocated size of procs array */
48
49         /*
50          * We declare procs[] as 1 entry because C wants a fixed-size array, but
51          * actually it is maxProcs entries long.
52          */
53         PGPROC     *procs[1];           /* VARIABLE LENGTH ARRAY */
54 } ProcArrayStruct;
55
56 static ProcArrayStruct *procArray;
57
58
59 #ifdef XIDCACHE_DEBUG
60
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;
70
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++)
79
80 static void DisplayXidCache(void);
81 #else                                                   /* !XIDCACHE_DEBUG */
82
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 */
92
93
94 /*
95  * Report shared-memory space needed by CreateSharedProcArray.
96  */
97 Size
98 ProcArrayShmemSize(void)
99 {
100         Size            size;
101
102         size = offsetof(ProcArrayStruct, procs);
103         size = add_size(size, mul_size(sizeof(PGPROC *),
104                                                                  add_size(MaxBackends, max_prepared_xacts)));
105
106         return size;
107 }
108
109 /*
110  * Initialize the shared PGPROC array during postmaster startup.
111  */
112 void
113 CreateSharedProcArray(void)
114 {
115         bool            found;
116
117         /* Create or attach to the ProcArray shared structure */
118         procArray = (ProcArrayStruct *)
119                 ShmemInitStruct("Proc Array", ProcArrayShmemSize(), &found);
120
121         if (!found)
122         {
123                 /*
124                  * We're the first - initialize.
125                  */
126                 procArray->numProcs = 0;
127                 procArray->maxProcs = MaxBackends + max_prepared_xacts;
128         }
129 }
130
131 /*
132  * Add the specified PGPROC to the shared array.
133  */
134 void
135 ProcArrayAdd(PGPROC *proc)
136 {
137         ProcArrayStruct *arrayP = procArray;
138
139         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
140
141         if (arrayP->numProcs >= arrayP->maxProcs)
142         {
143                 /*
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
146                  * earlier.)
147                  */
148                 LWLockRelease(ProcArrayLock);
149                 ereport(FATAL,
150                                 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
151                                  errmsg("sorry, too many clients already")));
152         }
153
154         arrayP->procs[arrayP->numProcs] = proc;
155         arrayP->numProcs++;
156
157         LWLockRelease(ProcArrayLock);
158 }
159
160 /*
161  * Remove the specified PGPROC from the shared array.
162  *
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.)
169  */
170 void
171 ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
172 {
173         ProcArrayStruct *arrayP = procArray;
174         int                     index;
175
176 #ifdef XIDCACHE_DEBUG
177         /* dump stats at backend shutdown, but not prepared-xact end */
178         if (proc->pid != 0)
179                 DisplayXidCache();
180 #endif
181
182         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
183
184         if (TransactionIdIsValid(latestXid))
185         {
186                 Assert(TransactionIdIsValid(proc->xid));
187
188                 /* Advance global latestCompletedXid while holding the lock */
189                 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
190                                                                   latestXid))
191                         ShmemVariableCache->latestCompletedXid = latestXid;
192         }
193         else
194         {
195                 /* Shouldn't be trying to remove a live transaction here */
196                 Assert(!TransactionIdIsValid(proc->xid));
197         }
198
199         for (index = 0; index < arrayP->numProcs; index++)
200         {
201                 if (arrayP->procs[index] == proc)
202                 {
203                         arrayP->procs[index] = arrayP->procs[arrayP->numProcs - 1];
204                         arrayP->numProcs--;
205                         LWLockRelease(ProcArrayLock);
206                         return;
207                 }
208         }
209
210         /* Ooops */
211         LWLockRelease(ProcArrayLock);
212
213         elog(LOG, "failed to find proc %p in ProcArray", proc);
214 }
215
216
217 /*
218  * ProcArrayEndTransaction -- mark a transaction as no longer running
219  *
220  * This is used interchangeably for commit and abort cases.  The transaction
221  * commit/abort must already be reported to WAL and pg_clog.
222  *
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
228  * incomplete.)
229  */
230 void
231 ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
232 {
233         if (TransactionIdIsValid(latestXid))
234         {
235                 /*
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.
240                  */
241                 Assert(TransactionIdIsValid(proc->xid));
242
243                 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
244
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 */
251
252                 /* Clear the subtransaction-XID cache too while holding the lock */
253                 proc->subxids.nxids = 0;
254                 proc->subxids.overflowed = false;
255
256                 /* Also advance global latestCompletedXid while holding the lock */
257                 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
258                                                                   latestXid))
259                         ShmemVariableCache->latestCompletedXid = latestXid;
260
261                 LWLockRelease(ProcArrayLock);
262         }
263         else
264         {
265                 /*
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.
269                  */
270                 Assert(!TransactionIdIsValid(proc->xid));
271
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 */
277
278                 Assert(proc->subxids.nxids == 0);
279                 Assert(proc->subxids.overflowed == false);
280         }
281 }
282
283
284 /*
285  * ProcArrayClearTransaction -- clear the transaction fields
286  *
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.
291  */
292 void
293 ProcArrayClearTransaction(PGPROC *proc)
294 {
295         /*
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
299          * ProcArray.
300          */
301         proc->xid = InvalidTransactionId;
302         proc->lxid = InvalidLocalTransactionId;
303         proc->xmin = InvalidTransactionId;
304
305         /* redundant, but just in case */
306         proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
307         proc->inCommit = false;
308
309         /* Clear the subtransaction-XID cache too */
310         proc->subxids.nxids = 0;
311         proc->subxids.overflowed = false;
312 }
313
314
315 /*
316  * TransactionIdIsInProgress -- is given transaction running in some backend
317  *
318  * Aside from some shortcuts such as checking RecentXmin and our own Xid,
319  * there are three possibilities for finding a running transaction:
320  *
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.
323  *
324  * 2. the given Xid is one of the cached subxact Xids in the PGPROC array.
325  * We can find this out cheaply too.
326  *
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).
331  *
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).
336  */
337 bool
338 TransactionIdIsInProgress(TransactionId xid)
339 {
340         static TransactionId *xids = NULL;
341         int                     nxids = 0;
342         ProcArrayStruct *arrayP = procArray;
343         TransactionId topxid;
344         int                     i,
345                                 j;
346
347         /*
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
351          * running.)
352          */
353         if (TransactionIdPrecedes(xid, RecentXmin))
354         {
355                 xc_by_recent_xmin_inc();
356                 return false;
357         }
358
359         /*
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
362          * shared memory.
363          */
364         if (TransactionIdIsKnownCompleted(xid))
365         {
366                 xc_by_known_xact_inc();
367                 return false;
368         }
369
370         /*
371          * Also, we can handle our own transaction (and subtransactions) without
372          * any access to shared memory.
373          */
374         if (TransactionIdIsCurrentTransactionId(xid))
375         {
376                 xc_by_my_xact_inc();
377                 return true;
378         }
379
380         /*
381          * If not first time through, get workspace to remember main XIDs in. We
382          * malloc it permanently to avoid repeated palloc/pfree overhead.
383          */
384         if (xids == NULL)
385         {
386                 xids = (TransactionId *)
387                         malloc(arrayP->maxProcs * sizeof(TransactionId));
388                 if (xids == NULL)
389                         ereport(ERROR,
390                                         (errcode(ERRCODE_OUT_OF_MEMORY),
391                                          errmsg("out of memory")));
392         }
393
394         LWLockAcquire(ProcArrayLock, LW_SHARED);
395
396         /*
397          * Now that we have the lock, we can check latestCompletedXid; if the
398          * target Xid is after that, it's surely still running.
399          */
400         if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid, xid))
401         {
402                 LWLockRelease(ProcArrayLock);
403                 xc_by_latest_xid_inc();
404                 return true;
405         }
406
407         /* No shortcuts, gotta grovel through the array */
408         for (i = 0; i < arrayP->numProcs; i++)
409         {
410                 volatile PGPROC *proc = arrayP->procs[i];
411                 TransactionId pxid;
412
413                 /* Ignore my own proc --- dealt with it above */
414                 if (proc == MyProc)
415                         continue;
416
417                 /* Fetch xid just once - see GetNewTransactionId */
418                 pxid = proc->xid;
419
420                 if (!TransactionIdIsValid(pxid))
421                         continue;
422
423                 /*
424                  * Step 1: check the main Xid
425                  */
426                 if (TransactionIdEquals(pxid, xid))
427                 {
428                         LWLockRelease(ProcArrayLock);
429                         xc_by_main_xid_inc();
430                         return true;
431                 }
432
433                 /*
434                  * We can ignore main Xids that are younger than the target Xid, since
435                  * the target could not possibly be their child.
436                  */
437                 if (TransactionIdPrecedes(xid, pxid))
438                         continue;
439
440                 /*
441                  * Step 2: check the cached child-Xids arrays
442                  */
443                 for (j = proc->subxids.nxids - 1; j >= 0; j--)
444                 {
445                         /* Fetch xid just once - see GetNewTransactionId */
446                         TransactionId cxid = proc->subxids.xids[j];
447
448                         if (TransactionIdEquals(cxid, xid))
449                         {
450                                 LWLockRelease(ProcArrayLock);
451                                 xc_by_child_xid_inc();
452                                 return true;
453                         }
454                 }
455
456                 /*
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
461                  * worry about.)
462                  */
463                 if (proc->subxids.overflowed)
464                         xids[nxids++] = pxid;
465         }
466
467         LWLockRelease(ProcArrayLock);
468
469         /*
470          * If none of the relevant caches overflowed, we know the Xid is not
471          * running without looking at pg_subtrans.
472          */
473         if (nxids == 0)
474         {
475                 xc_no_overflow_inc();
476                 return false;
477         }
478
479         /*
480          * Step 3: have to check pg_subtrans.
481          *
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.
486          */
487         xc_slow_answer_inc();
488
489         if (TransactionIdDidAbort(xid))
490                 return false;
491
492         /*
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).
496          */
497         topxid = SubTransGetTopmostTransaction(xid);
498         Assert(TransactionIdIsValid(topxid));
499         if (!TransactionIdEquals(topxid, xid))
500         {
501                 for (i = 0; i < nxids; i++)
502                 {
503                         if (TransactionIdEquals(xids[i], topxid))
504                                 return true;
505                 }
506         }
507
508         return false;
509 }
510
511 /*
512  * TransactionIdIsActive -- is xid the top-level XID of an active backend?
513  *
514  * This differs from TransactionIdIsInProgress in that it ignores prepared
515  * transactions.  Also, we ignore subtransactions since that's not needed
516  * for current uses.
517  */
518 bool
519 TransactionIdIsActive(TransactionId xid)
520 {
521         bool            result = false;
522         ProcArrayStruct *arrayP = procArray;
523         int                     i;
524
525         /*
526          * Don't bother checking a transaction older than RecentXmin; it could not
527          * possibly still be running.
528          */
529         if (TransactionIdPrecedes(xid, RecentXmin))
530                 return false;
531
532         LWLockAcquire(ProcArrayLock, LW_SHARED);
533
534         for (i = 0; i < arrayP->numProcs; i++)
535         {
536                 volatile PGPROC *proc = arrayP->procs[i];
537
538                 /* Fetch xid just once - see GetNewTransactionId */
539                 TransactionId pxid = proc->xid;
540
541                 if (!TransactionIdIsValid(pxid))
542                         continue;
543
544                 if (proc->pid == 0)
545                         continue;                       /* ignore prepared transactions */
546
547                 if (TransactionIdEquals(pxid, xid))
548                 {
549                         result = true;
550                         break;
551                 }
552         }
553
554         LWLockRelease(ProcArrayLock);
555
556         return result;
557 }
558
559
560 /*
561  * GetOldestXmin -- returns oldest transaction that was running
562  *                                      when any current transaction was started.
563  *
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.
566  *
567  * If ignoreVacuum is TRUE then backends with the PROC_IN_VACUUM flag set are
568  * ignored.
569  *
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.
576  *
577  * This is also used to determine where to truncate pg_subtrans.  allDbs
578  * must be TRUE for that case, and ignoreVacuum FALSE.
579  *
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.
584  */
585 TransactionId
586 GetOldestXmin(bool allDbs, bool ignoreVacuum)
587 {
588         ProcArrayStruct *arrayP = procArray;
589         TransactionId result;
590         int                     index;
591
592         LWLockAcquire(ProcArrayLock, LW_SHARED);
593
594         /*
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
598          * additions.
599          */
600         result = ShmemVariableCache->latestCompletedXid;
601         Assert(TransactionIdIsNormal(result));
602         TransactionIdAdvance(result);
603
604         for (index = 0; index < arrayP->numProcs; index++)
605         {
606                 volatile PGPROC *proc = arrayP->procs[index];
607
608                 if (ignoreVacuum && (proc->vacuumFlags & PROC_IN_VACUUM))
609                         continue;
610
611                 if (allDbs || proc->databaseId == MyDatabaseId)
612                 {
613                         /* Fetch xid just once - see GetNewTransactionId */
614                         TransactionId xid = proc->xid;
615
616                         /* First consider the transaction's own Xid, if any */
617                         if (TransactionIdIsNormal(xid) &&
618                                 TransactionIdPrecedes(xid, result))
619                                 result = xid;
620
621                         /*
622                          * Also consider the transaction's Xmin, if set.
623                          *
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.
627                          */
628                         xid = proc->xmin;       /* Fetch just once */
629                         if (TransactionIdIsNormal(xid) &&
630                                 TransactionIdPrecedes(xid, result))
631                                 result = xid;
632                 }
633         }
634
635         LWLockRelease(ProcArrayLock);
636
637         return result;
638 }
639
640 /*
641  * GetSnapshotData -- returns information about running transactions.
642  *
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.
652  *
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()
659  * in tqual.c).
660  *
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).
669  *
670  * Note: this function should probably not be called with an argument that's
671  * not statically allocated (see xip allocation below).
672  */
673 Snapshot
674 GetSnapshotData(Snapshot snapshot)
675 {
676         ProcArrayStruct *arrayP = procArray;
677         TransactionId xmin;
678         TransactionId xmax;
679         TransactionId globalxmin;
680         int                     index;
681         int                     count = 0;
682         int                     subcount = 0;
683
684         Assert(snapshot != NULL);
685
686         /*
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.
691          *
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.)
696          */
697         if (snapshot->xip == NULL)
698         {
699                 /*
700                  * First call for this snapshot
701                  */
702                 snapshot->xip = (TransactionId *)
703                         malloc(arrayP->maxProcs * sizeof(TransactionId));
704                 if (snapshot->xip == NULL)
705                         ereport(ERROR,
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)
712                         ereport(ERROR,
713                                         (errcode(ERRCODE_OUT_OF_MEMORY),
714                                          errmsg("out of memory")));
715         }
716
717         /*
718          * It is sufficient to get shared lock on ProcArrayLock, even if we are
719          * going to set MyProc->xmin.
720          */
721         LWLockAcquire(ProcArrayLock, LW_SHARED);
722
723         /* xmax is always latestCompletedXid + 1 */
724         xmax = ShmemVariableCache->latestCompletedXid;
725         Assert(TransactionIdIsNormal(xmax));
726         TransactionIdAdvance(xmax);
727
728         /* initialize xmin calculation with xmax */
729         globalxmin = xmin = xmax;
730
731         /*
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
734          * subxids.
735          */
736         for (index = 0; index < arrayP->numProcs; index++)
737         {
738                 volatile PGPROC *proc = arrayP->procs[index];
739                 TransactionId xid;
740
741                 /* Ignore procs running LAZY VACUUM */
742                 if (proc->vacuumFlags & PROC_IN_VACUUM)
743                         continue;
744
745                 /* Update globalxmin to be the smallest valid xmin */
746                 xid = proc->xmin;               /* fetch just once */
747                 if (TransactionIdIsNormal(xid) &&
748                         TransactionIdPrecedes(xid, globalxmin))
749                         globalxmin = xid;
750
751                 /* Fetch xid just once - see GetNewTransactionId */
752                 xid = proc->xid;
753
754                 /*
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.
759                  *
760                  * We don't include our own XID (if any) in the snapshot, but we must
761                  * include it into xmin.
762                  */
763                 if (TransactionIdIsNormal(xid))
764                 {
765                         if (TransactionIdFollowsOrEquals(xid, xmax))
766                                 continue;
767                         if (proc != MyProc)
768                                 snapshot->xip[count++] = xid;
769                         if (TransactionIdPrecedes(xid, xmin))
770                                 xmin = xid;
771                 }
772
773                 /*
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.
779                  *
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.)
784                  *
785                  * Again, our own XIDs are not included in the snapshot.
786                  */
787                 if (subcount >= 0 && proc != MyProc)
788                 {
789                         if (proc->subxids.overflowed)
790                                 subcount = -1;  /* overflowed */
791                         else
792                         {
793                                 int                     nxids = proc->subxids.nxids;
794
795                                 if (nxids > 0)
796                                 {
797                                         memcpy(snapshot->subxip + subcount,
798                                                    (void *) proc->subxids.xids,
799                                                    nxids * sizeof(TransactionId));
800                                         subcount += nxids;
801                                 }
802                         }
803                 }
804         }
805
806         if (!TransactionIdIsValid(MyProc->xmin))
807                 MyProc->xmin = TransactionXmin = xmin;
808
809         LWLockRelease(ProcArrayLock);
810
811         /*
812          * Update globalxmin to include actual process xids.  This is a slightly
813          * different way of computing it than GetOldestXmin uses, but should give
814          * the same result.
815          */
816         if (TransactionIdPrecedes(xmin, globalxmin))
817                 globalxmin = xmin;
818
819         /* Update global variables too */
820         RecentGlobalXmin = globalxmin;
821         RecentXmin = xmin;
822
823         snapshot->xmin = xmin;
824         snapshot->xmax = xmax;
825         snapshot->xcnt = count;
826         snapshot->subxcnt = subcount;
827
828         snapshot->curcid = GetCurrentCommandId(false);
829
830         /*
831          * This is a new snapshot, so set both refcounts are zero, and mark it
832          * as not copied in persistent memory.
833          */
834         snapshot->active_count = 0;
835         snapshot->regd_count = 0;
836         snapshot->copied = false;
837
838         return snapshot;
839 }
840
841 /*
842  * GetTransactionsInCommit -- Get the XIDs of transactions that are committing
843  *
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.
846  *
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.
849  *
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.
857  */
858 int
859 GetTransactionsInCommit(TransactionId **xids_p)
860 {
861         ProcArrayStruct *arrayP = procArray;
862         TransactionId *xids;
863         int                     nxids;
864         int                     index;
865
866         xids = (TransactionId *) palloc(arrayP->maxProcs * sizeof(TransactionId));
867         nxids = 0;
868
869         LWLockAcquire(ProcArrayLock, LW_SHARED);
870
871         for (index = 0; index < arrayP->numProcs; index++)
872         {
873                 volatile PGPROC *proc = arrayP->procs[index];
874
875                 /* Fetch xid just once - see GetNewTransactionId */
876                 TransactionId pxid = proc->xid;
877
878                 if (proc->inCommit && TransactionIdIsValid(pxid))
879                         xids[nxids++] = pxid;
880         }
881
882         LWLockRelease(ProcArrayLock);
883
884         *xids_p = xids;
885         return nxids;
886 }
887
888 /*
889  * HaveTransactionsInCommit -- Are any of the specified XIDs in commit?
890  *
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.
893  *
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.
896  */
897 bool
898 HaveTransactionsInCommit(TransactionId *xids, int nxids)
899 {
900         bool            result = false;
901         ProcArrayStruct *arrayP = procArray;
902         int                     index;
903
904         LWLockAcquire(ProcArrayLock, LW_SHARED);
905
906         for (index = 0; index < arrayP->numProcs; index++)
907         {
908                 volatile PGPROC *proc = arrayP->procs[index];
909
910                 /* Fetch xid just once - see GetNewTransactionId */
911                 TransactionId pxid = proc->xid;
912
913                 if (proc->inCommit && TransactionIdIsValid(pxid))
914                 {
915                         int                     i;
916
917                         for (i = 0; i < nxids; i++)
918                         {
919                                 if (xids[i] == pxid)
920                                 {
921                                         result = true;
922                                         break;
923                                 }
924                         }
925                         if (result)
926                                 break;
927                 }
928         }
929
930         LWLockRelease(ProcArrayLock);
931
932         return result;
933 }
934
935 /*
936  * BackendPidGetProc -- get a backend's PGPROC given its PID
937  *
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 ...
941  */
942 PGPROC *
943 BackendPidGetProc(int pid)
944 {
945         PGPROC     *result = NULL;
946         ProcArrayStruct *arrayP = procArray;
947         int                     index;
948
949         if (pid == 0)                           /* never match dummy PGPROCs */
950                 return NULL;
951
952         LWLockAcquire(ProcArrayLock, LW_SHARED);
953
954         for (index = 0; index < arrayP->numProcs; index++)
955         {
956                 PGPROC     *proc = arrayP->procs[index];
957
958                 if (proc->pid == pid)
959                 {
960                         result = proc;
961                         break;
962                 }
963         }
964
965         LWLockRelease(ProcArrayLock);
966
967         return result;
968 }
969
970 /*
971  * BackendXidGetPid -- get a backend's pid given its XID
972  *
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 ...
976  *
977  * Only main transaction Ids are considered.  This function is mainly
978  * useful for determining what backend owns a lock.
979  *
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.
982  */
983 int
984 BackendXidGetPid(TransactionId xid)
985 {
986         int                     result = 0;
987         ProcArrayStruct *arrayP = procArray;
988         int                     index;
989
990         if (xid == InvalidTransactionId)        /* never match invalid xid */
991                 return 0;
992
993         LWLockAcquire(ProcArrayLock, LW_SHARED);
994
995         for (index = 0; index < arrayP->numProcs; index++)
996         {
997                 volatile PGPROC *proc = arrayP->procs[index];
998
999                 if (proc->xid == xid)
1000                 {
1001                         result = proc->pid;
1002                         break;
1003                 }
1004         }
1005
1006         LWLockRelease(ProcArrayLock);
1007
1008         return result;
1009 }
1010
1011 /*
1012  * IsBackendPid -- is a given pid a running backend
1013  */
1014 bool
1015 IsBackendPid(int pid)
1016 {
1017         return (BackendPidGetProc(pid) != NULL);
1018 }
1019
1020
1021 /*
1022  * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
1023  *
1024  * The array is palloc'd and is terminated with an invalid VXID.
1025  *
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.
1031  */
1032 VirtualTransactionId *
1033 GetCurrentVirtualXIDs(TransactionId limitXmin, bool allDbs, int excludeVacuum)
1034 {
1035         VirtualTransactionId *vxids;
1036         ProcArrayStruct *arrayP = procArray;
1037         int                     count = 0;
1038         int                     index;
1039
1040         /* allocate result space with room for a terminator */
1041         vxids = (VirtualTransactionId *)
1042                 palloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
1043
1044         LWLockAcquire(ProcArrayLock, LW_SHARED);
1045
1046         for (index = 0; index < arrayP->numProcs; index++)
1047         {
1048                 volatile PGPROC *proc = arrayP->procs[index];
1049
1050                 if (proc == MyProc)
1051                         continue;
1052
1053                 if (excludeVacuum & proc->vacuumFlags)
1054                         continue;
1055
1056                 if (allDbs || proc->databaseId == MyDatabaseId)
1057                 {
1058                         /* Fetch xmin just once - might change on us? */
1059                         TransactionId pxmin = proc->xmin;
1060
1061                         /*
1062                          * Note that InvalidTransactionId precedes all other XIDs, so a
1063                          * proc that hasn't set xmin yet will always be included.
1064                          */
1065                         if (!TransactionIdIsValid(limitXmin) ||
1066                                 TransactionIdPrecedes(pxmin, limitXmin))
1067                         {
1068                                 VirtualTransactionId vxid;
1069
1070                                 GET_VXID_FROM_PGPROC(vxid, *proc);
1071                                 if (VirtualTransactionIdIsValid(vxid))
1072                                         vxids[count++] = vxid;
1073                         }
1074                 }
1075         }
1076
1077         LWLockRelease(ProcArrayLock);
1078
1079         /* add the terminator */
1080         vxids[count].backendId = InvalidBackendId;
1081         vxids[count].localTransactionId = InvalidLocalTransactionId;
1082
1083         return vxids;
1084 }
1085
1086
1087 /*
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.
1091  *
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.
1094  */
1095 int
1096 CountActiveBackends(void)
1097 {
1098         ProcArrayStruct *arrayP = procArray;
1099         int                     count = 0;
1100         int                     index;
1101
1102         /*
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...
1106          */
1107         for (index = 0; index < arrayP->numProcs; index++)
1108         {
1109                 volatile PGPROC *proc = arrayP->procs[index];
1110
1111                 if (proc == MyProc)
1112                         continue;                       /* do not count myself */
1113                 if (proc->pid == 0)
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 */
1119                 count++;
1120         }
1121
1122         return count;
1123 }
1124
1125 /*
1126  * CountDBBackends --- count backends that are using specified database
1127  */
1128 int
1129 CountDBBackends(Oid databaseid)
1130 {
1131         ProcArrayStruct *arrayP = procArray;
1132         int                     count = 0;
1133         int                     index;
1134
1135         LWLockAcquire(ProcArrayLock, LW_SHARED);
1136
1137         for (index = 0; index < arrayP->numProcs; index++)
1138         {
1139                 volatile PGPROC *proc = arrayP->procs[index];
1140
1141                 if (proc->pid == 0)
1142                         continue;                       /* do not count prepared xacts */
1143                 if (proc->databaseId == databaseid)
1144                         count++;
1145         }
1146
1147         LWLockRelease(ProcArrayLock);
1148
1149         return count;
1150 }
1151
1152 /*
1153  * CountUserBackends --- count backends that are used by specified user
1154  */
1155 int
1156 CountUserBackends(Oid roleid)
1157 {
1158         ProcArrayStruct *arrayP = procArray;
1159         int                     count = 0;
1160         int                     index;
1161
1162         LWLockAcquire(ProcArrayLock, LW_SHARED);
1163
1164         for (index = 0; index < arrayP->numProcs; index++)
1165         {
1166                 volatile PGPROC *proc = arrayP->procs[index];
1167
1168                 if (proc->pid == 0)
1169                         continue;                       /* do not count prepared xacts */
1170                 if (proc->roleId == roleid)
1171                         count++;
1172         }
1173
1174         LWLockRelease(ProcArrayLock);
1175
1176         return count;
1177 }
1178
1179 /*
1180  * CountOtherDBBackends -- check for other backends running in the given DB
1181  *
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.
1185  *
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.
1188  *
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.
1192  *
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
1200  * indefinitely.
1201  */
1202 bool
1203 CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
1204 {
1205         ProcArrayStruct *arrayP = procArray;
1206 #define MAXAUTOVACPIDS  10              /* max autovacs to SIGTERM per iteration */
1207         int                     autovac_pids[MAXAUTOVACPIDS];
1208         int                     tries;
1209
1210         /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
1211         for (tries = 0; tries < 50; tries++)
1212         {
1213                 int                     nautovacs = 0;
1214                 bool            found = false;
1215                 int                     index;
1216
1217                 CHECK_FOR_INTERRUPTS();
1218
1219                 *nbackends = *nprepared = 0;
1220
1221                 LWLockAcquire(ProcArrayLock, LW_SHARED);
1222
1223                 for (index = 0; index < arrayP->numProcs; index++)
1224                 {
1225                         volatile PGPROC *proc = arrayP->procs[index];
1226
1227                         if (proc->databaseId != databaseId)
1228                                 continue;
1229                         if (proc == MyProc)
1230                                 continue;
1231
1232                         found = true;
1233
1234                         if (proc->pid == 0)
1235                                 (*nprepared)++;
1236                         else
1237                         {
1238                                 (*nbackends)++;
1239                                 if ((proc->vacuumFlags & PROC_IS_AUTOVACUUM) &&
1240                                         nautovacs < MAXAUTOVACPIDS)
1241                                         autovac_pids[nautovacs++] = proc->pid;
1242                         }
1243                 }
1244
1245                 LWLockRelease(ProcArrayLock);
1246
1247                 if (!found)
1248                         return false;           /* no conflicting backends, so done */
1249
1250                 /*
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...
1255                  */
1256                 for (index = 0; index < nautovacs; index++)
1257                         (void) kill(autovac_pids[index], SIGTERM);      /* ignore any error */
1258
1259                 /* sleep, then try again */
1260                 pg_usleep(100 * 1000L); /* 100ms */
1261         }
1262
1263         return true;                            /* timed out, still conflicts */
1264 }
1265
1266
1267 #define XidCacheRemove(i) \
1268         do { \
1269                 MyProc->subxids.xids[i] = MyProc->subxids.xids[MyProc->subxids.nxids - 1]; \
1270                 MyProc->subxids.nxids--; \
1271         } while (0)
1272
1273 /*
1274  * XidCacheRemoveRunningXids
1275  *
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.
1280  */
1281 void
1282 XidCacheRemoveRunningXids(TransactionId xid,
1283                                                   int nxids, const TransactionId *xids,
1284                                                   TransactionId latestXid)
1285 {
1286         int                     i,
1287                                 j;
1288
1289         Assert(TransactionIdIsValid(xid));
1290
1291         /*
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
1296          * conservative.
1297          */
1298         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1299
1300         /*
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.
1304          */
1305         for (i = nxids - 1; i >= 0; i--)
1306         {
1307                 TransactionId anxid = xids[i];
1308
1309                 for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
1310                 {
1311                         if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
1312                         {
1313                                 XidCacheRemove(j);
1314                                 break;
1315                         }
1316                 }
1317
1318                 /*
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
1323                  * debug warning.
1324                  */
1325                 if (j < 0 && !MyProc->subxids.overflowed)
1326                         elog(WARNING, "did not find subXID %u in MyProc", anxid);
1327         }
1328
1329         for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
1330         {
1331                 if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
1332                 {
1333                         XidCacheRemove(j);
1334                         break;
1335                 }
1336         }
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);
1340
1341         /* Also advance global latestCompletedXid while holding the lock */
1342         if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
1343                                                           latestXid))
1344                 ShmemVariableCache->latestCompletedXid = latestXid;
1345
1346         LWLockRelease(ProcArrayLock);
1347 }
1348
1349 #ifdef XIDCACHE_DEBUG
1350
1351 /*
1352  * Print stats about effectiveness of XID cache
1353  */
1354 static void
1355 DisplayXidCache(void)
1356 {
1357         fprintf(stderr,
1358                         "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, nooflo: %ld, slow: %ld\n",
1359                         xc_by_recent_xmin,
1360                         xc_by_known_xact,
1361                         xc_by_my_xact,
1362                         xc_by_latest_xid,
1363                         xc_by_main_xid,
1364                         xc_by_child_xid,
1365                         xc_no_overflow,
1366                         xc_slow_answer);
1367 }
1368
1369 #endif   /* XIDCACHE_DEBUG */