]> granicus.if.org Git - postgresql/blob - src/backend/storage/ipc/procarray.c
Simplify and improve ProcessStandbyHSFeedbackMessage logic.
[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  * During hot standby, we also keep a list of XIDs representing transactions
21  * that are known to be running in the master (or more precisely, were running
22  * as of the current point in the WAL stream).  This list is kept in the
23  * KnownAssignedXids array, and is updated by watching the sequence of
24  * arriving XIDs.  This is necessary because if we leave those XIDs out of
25  * snapshots taken for standby queries, then they will appear to be already
26  * complete, leading to MVCC failures.  Note that in hot standby, the PGPROC
27  * array represents standby processes, which by definition are not running
28  * transactions that have XIDs.
29  *
30  * It is perhaps possible for a backend on the master to terminate without
31  * writing an abort record for its transaction.  While that shouldn't really
32  * happen, it would tie up KnownAssignedXids indefinitely, so we protect
33  * ourselves by pruning the array when a valid list of running XIDs arrives.
34  *
35  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
36  * Portions Copyright (c) 1994, Regents of the University of California
37  *
38  *
39  * IDENTIFICATION
40  *        src/backend/storage/ipc/procarray.c
41  *
42  *-------------------------------------------------------------------------
43  */
44 #include "postgres.h"
45
46 #include <signal.h>
47
48 #include "access/clog.h"
49 #include "access/subtrans.h"
50 #include "access/transam.h"
51 #include "access/xact.h"
52 #include "access/twophase.h"
53 #include "miscadmin.h"
54 #include "storage/procarray.h"
55 #include "storage/spin.h"
56 #include "utils/builtins.h"
57 #include "utils/snapmgr.h"
58
59
60 /* Our shared memory area */
61 typedef struct ProcArrayStruct
62 {
63         int                     numProcs;               /* number of valid procs entries */
64         int                     maxProcs;               /* allocated size of procs array */
65
66         /*
67          * Known assigned XIDs handling
68          */
69         int                     maxKnownAssignedXids;   /* allocated size of array */
70         int                     numKnownAssignedXids;   /* currrent # of valid entries */
71         int                     tailKnownAssignedXids;  /* index of oldest valid element */
72         int                     headKnownAssignedXids;  /* index of newest element, + 1 */
73         slock_t         known_assigned_xids_lck;                /* protects head/tail pointers */
74
75         /*
76          * Highest subxid that has been removed from KnownAssignedXids array to
77          * prevent overflow; or InvalidTransactionId if none.  We track this for
78          * similar reasons to tracking overflowing cached subxids in PGPROC
79          * entries.  Must hold exclusive ProcArrayLock to change this, and shared
80          * lock to read it.
81          */
82         TransactionId lastOverflowedXid;
83
84         /*
85          * We declare procs[] as 1 entry because C wants a fixed-size array, but
86          * actually it is maxProcs entries long.
87          */
88         PGPROC     *procs[1];           /* VARIABLE LENGTH ARRAY */
89 } ProcArrayStruct;
90
91 static ProcArrayStruct *procArray;
92
93 /*
94  * Bookkeeping for tracking emulated transactions in recovery
95  */
96 static TransactionId *KnownAssignedXids;
97 static bool *KnownAssignedXidsValid;
98 static TransactionId latestObservedXid = InvalidTransactionId;
99
100 /*
101  * If we're in STANDBY_SNAPSHOT_PENDING state, standbySnapshotPendingXmin is
102  * the highest xid that might still be running that we don't have in
103  * KnownAssignedXids.
104  */
105 static TransactionId standbySnapshotPendingXmin;
106
107 #ifdef XIDCACHE_DEBUG
108
109 /* counters for XidCache measurement */
110 static long xc_by_recent_xmin = 0;
111 static long xc_by_known_xact = 0;
112 static long xc_by_my_xact = 0;
113 static long xc_by_latest_xid = 0;
114 static long xc_by_main_xid = 0;
115 static long xc_by_child_xid = 0;
116 static long xc_by_known_assigned = 0;
117 static long xc_no_overflow = 0;
118 static long xc_slow_answer = 0;
119
120 #define xc_by_recent_xmin_inc()         (xc_by_recent_xmin++)
121 #define xc_by_known_xact_inc()          (xc_by_known_xact++)
122 #define xc_by_my_xact_inc()                     (xc_by_my_xact++)
123 #define xc_by_latest_xid_inc()          (xc_by_latest_xid++)
124 #define xc_by_main_xid_inc()            (xc_by_main_xid++)
125 #define xc_by_child_xid_inc()           (xc_by_child_xid++)
126 #define xc_by_known_assigned_inc()      (xc_by_known_assigned++)
127 #define xc_no_overflow_inc()            (xc_no_overflow++)
128 #define xc_slow_answer_inc()            (xc_slow_answer++)
129
130 static void DisplayXidCache(void);
131 #else                                                   /* !XIDCACHE_DEBUG */
132
133 #define xc_by_recent_xmin_inc()         ((void) 0)
134 #define xc_by_known_xact_inc()          ((void) 0)
135 #define xc_by_my_xact_inc()                     ((void) 0)
136 #define xc_by_latest_xid_inc()          ((void) 0)
137 #define xc_by_main_xid_inc()            ((void) 0)
138 #define xc_by_child_xid_inc()           ((void) 0)
139 #define xc_by_known_assigned_inc()      ((void) 0)
140 #define xc_no_overflow_inc()            ((void) 0)
141 #define xc_slow_answer_inc()            ((void) 0)
142 #endif   /* XIDCACHE_DEBUG */
143
144 /* Primitives for KnownAssignedXids array handling for standby */
145 static void KnownAssignedXidsCompress(bool force);
146 static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
147                                          bool exclusive_lock);
148 static bool KnownAssignedXidsSearch(TransactionId xid, bool remove);
149 static bool KnownAssignedXidExists(TransactionId xid);
150 static void KnownAssignedXidsRemove(TransactionId xid);
151 static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
152                                                         TransactionId *subxids);
153 static void KnownAssignedXidsRemovePreceding(TransactionId xid);
154 static int      KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax);
155 static int KnownAssignedXidsGetAndSetXmin(TransactionId *xarray,
156                                                            TransactionId *xmin,
157                                                            TransactionId xmax);
158 static TransactionId KnownAssignedXidsGetOldestXmin(void);
159 static void KnownAssignedXidsDisplay(int trace_level);
160
161 /*
162  * Report shared-memory space needed by CreateSharedProcArray.
163  */
164 Size
165 ProcArrayShmemSize(void)
166 {
167         Size            size;
168
169         /* Size of the ProcArray structure itself */
170 #define PROCARRAY_MAXPROCS      (MaxBackends + max_prepared_xacts)
171
172         size = offsetof(ProcArrayStruct, procs);
173         size = add_size(size, mul_size(sizeof(PGPROC *), PROCARRAY_MAXPROCS));
174
175         /*
176          * During Hot Standby processing we have a data structure called
177          * KnownAssignedXids, created in shared memory. Local data structures are
178          * also created in various backends during GetSnapshotData(),
179          * TransactionIdIsInProgress() and GetRunningTransactionData(). All of the
180          * main structures created in those functions must be identically sized,
181          * since we may at times copy the whole of the data structures around. We
182          * refer to this size as TOTAL_MAX_CACHED_SUBXIDS.
183          *
184          * Ideally we'd only create this structure if we were actually doing hot
185          * standby in the current run, but we don't know that yet at the time
186          * shared memory is being set up.
187          */
188 #define TOTAL_MAX_CACHED_SUBXIDS \
189         ((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
190
191         if (EnableHotStandby)
192         {
193                 size = add_size(size,
194                                                 mul_size(sizeof(TransactionId),
195                                                                  TOTAL_MAX_CACHED_SUBXIDS));
196                 size = add_size(size,
197                                                 mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS));
198         }
199
200         return size;
201 }
202
203 /*
204  * Initialize the shared PGPROC array during postmaster startup.
205  */
206 void
207 CreateSharedProcArray(void)
208 {
209         bool            found;
210
211         /* Create or attach to the ProcArray shared structure */
212         procArray = (ProcArrayStruct *)
213                 ShmemInitStruct("Proc Array",
214                                                 add_size(offsetof(ProcArrayStruct, procs),
215                                                                  mul_size(sizeof(PGPROC *),
216                                                                                   PROCARRAY_MAXPROCS)),
217                                                 &found);
218
219         if (!found)
220         {
221                 /*
222                  * We're the first - initialize.
223                  */
224                 procArray->numProcs = 0;
225                 procArray->maxProcs = PROCARRAY_MAXPROCS;
226                 procArray->maxKnownAssignedXids = TOTAL_MAX_CACHED_SUBXIDS;
227                 procArray->numKnownAssignedXids = 0;
228                 procArray->tailKnownAssignedXids = 0;
229                 procArray->headKnownAssignedXids = 0;
230                 SpinLockInit(&procArray->known_assigned_xids_lck);
231                 procArray->lastOverflowedXid = InvalidTransactionId;
232         }
233
234         /* Create or attach to the KnownAssignedXids arrays too, if needed */
235         if (EnableHotStandby)
236         {
237                 KnownAssignedXids = (TransactionId *)
238                         ShmemInitStruct("KnownAssignedXids",
239                                                         mul_size(sizeof(TransactionId),
240                                                                          TOTAL_MAX_CACHED_SUBXIDS),
241                                                         &found);
242                 KnownAssignedXidsValid = (bool *)
243                         ShmemInitStruct("KnownAssignedXidsValid",
244                                                         mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS),
245                                                         &found);
246         }
247 }
248
249 /*
250  * Add the specified PGPROC to the shared array.
251  */
252 void
253 ProcArrayAdd(PGPROC *proc)
254 {
255         ProcArrayStruct *arrayP = procArray;
256
257         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
258
259         if (arrayP->numProcs >= arrayP->maxProcs)
260         {
261                 /*
262                  * Ooops, no room.      (This really shouldn't happen, since there is a
263                  * fixed supply of PGPROC structs too, and so we should have failed
264                  * earlier.)
265                  */
266                 LWLockRelease(ProcArrayLock);
267                 ereport(FATAL,
268                                 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
269                                  errmsg("sorry, too many clients already")));
270         }
271
272         arrayP->procs[arrayP->numProcs] = proc;
273         arrayP->numProcs++;
274
275         LWLockRelease(ProcArrayLock);
276 }
277
278 /*
279  * Remove the specified PGPROC from the shared array.
280  *
281  * When latestXid is a valid XID, we are removing a live 2PC gxact from the
282  * array, and thus causing it to appear as "not running" anymore.  In this
283  * case we must advance latestCompletedXid.  (This is essentially the same
284  * as ProcArrayEndTransaction followed by removal of the PGPROC, but we take
285  * the ProcArrayLock only once, and don't damage the content of the PGPROC;
286  * twophase.c depends on the latter.)
287  */
288 void
289 ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
290 {
291         ProcArrayStruct *arrayP = procArray;
292         int                     index;
293
294 #ifdef XIDCACHE_DEBUG
295         /* dump stats at backend shutdown, but not prepared-xact end */
296         if (proc->pid != 0)
297                 DisplayXidCache();
298 #endif
299
300         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
301
302         if (TransactionIdIsValid(latestXid))
303         {
304                 Assert(TransactionIdIsValid(proc->xid));
305
306                 /* Advance global latestCompletedXid while holding the lock */
307                 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
308                                                                   latestXid))
309                         ShmemVariableCache->latestCompletedXid = latestXid;
310         }
311         else
312         {
313                 /* Shouldn't be trying to remove a live transaction here */
314                 Assert(!TransactionIdIsValid(proc->xid));
315         }
316
317         for (index = 0; index < arrayP->numProcs; index++)
318         {
319                 if (arrayP->procs[index] == proc)
320                 {
321                         arrayP->procs[index] = arrayP->procs[arrayP->numProcs - 1];
322                         arrayP->procs[arrayP->numProcs - 1] = NULL; /* for debugging */
323                         arrayP->numProcs--;
324                         LWLockRelease(ProcArrayLock);
325                         return;
326                 }
327         }
328
329         /* Ooops */
330         LWLockRelease(ProcArrayLock);
331
332         elog(LOG, "failed to find proc %p in ProcArray", proc);
333 }
334
335
336 /*
337  * ProcArrayEndTransaction -- mark a transaction as no longer running
338  *
339  * This is used interchangeably for commit and abort cases.  The transaction
340  * commit/abort must already be reported to WAL and pg_clog.
341  *
342  * proc is currently always MyProc, but we pass it explicitly for flexibility.
343  * latestXid is the latest Xid among the transaction's main XID and
344  * subtransactions, or InvalidTransactionId if it has no XID.  (We must ask
345  * the caller to pass latestXid, instead of computing it from the PGPROC's
346  * contents, because the subxid information in the PGPROC might be
347  * incomplete.)
348  */
349 void
350 ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
351 {
352         if (TransactionIdIsValid(latestXid))
353         {
354                 /*
355                  * We must lock ProcArrayLock while clearing proc->xid, so that we do
356                  * not exit the set of "running" transactions while someone else is
357                  * taking a snapshot.  See discussion in
358                  * src/backend/access/transam/README.
359                  */
360                 Assert(TransactionIdIsValid(proc->xid));
361
362                 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
363
364                 proc->xid = InvalidTransactionId;
365                 proc->lxid = InvalidLocalTransactionId;
366                 proc->xmin = InvalidTransactionId;
367                 /* must be cleared with xid/xmin: */
368                 proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
369                 proc->inCommit = false; /* be sure this is cleared in abort */
370                 proc->recoveryConflictPending = false;
371
372                 /* Clear the subtransaction-XID cache too while holding the lock */
373                 proc->subxids.nxids = 0;
374                 proc->subxids.overflowed = false;
375
376                 /* Also advance global latestCompletedXid while holding the lock */
377                 if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
378                                                                   latestXid))
379                         ShmemVariableCache->latestCompletedXid = latestXid;
380
381                 LWLockRelease(ProcArrayLock);
382         }
383         else
384         {
385                 /*
386                  * If we have no XID, we don't need to lock, since we won't affect
387                  * anyone else's calculation of a snapshot.  We might change their
388                  * estimate of global xmin, but that's OK.
389                  */
390                 Assert(!TransactionIdIsValid(proc->xid));
391
392                 proc->lxid = InvalidLocalTransactionId;
393                 proc->xmin = InvalidTransactionId;
394                 /* must be cleared with xid/xmin: */
395                 proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
396                 proc->inCommit = false; /* be sure this is cleared in abort */
397                 proc->recoveryConflictPending = false;
398
399                 Assert(proc->subxids.nxids == 0);
400                 Assert(proc->subxids.overflowed == false);
401         }
402 }
403
404
405 /*
406  * ProcArrayClearTransaction -- clear the transaction fields
407  *
408  * This is used after successfully preparing a 2-phase transaction.  We are
409  * not actually reporting the transaction's XID as no longer running --- it
410  * will still appear as running because the 2PC's gxact is in the ProcArray
411  * too.  We just have to clear out our own PGPROC.
412  */
413 void
414 ProcArrayClearTransaction(PGPROC *proc)
415 {
416         /*
417          * We can skip locking ProcArrayLock here, because this action does not
418          * actually change anyone's view of the set of running XIDs: our entry is
419          * duplicate with the gxact that has already been inserted into the
420          * ProcArray.
421          */
422         proc->xid = InvalidTransactionId;
423         proc->lxid = InvalidLocalTransactionId;
424         proc->xmin = InvalidTransactionId;
425         proc->recoveryConflictPending = false;
426
427         /* redundant, but just in case */
428         proc->vacuumFlags &= ~PROC_VACUUM_STATE_MASK;
429         proc->inCommit = false;
430
431         /* Clear the subtransaction-XID cache too */
432         proc->subxids.nxids = 0;
433         proc->subxids.overflowed = false;
434 }
435
436 /*
437  * ProcArrayApplyRecoveryInfo -- apply recovery info about xids
438  *
439  * Takes us through 3 states: Initialized, Pending and Ready.
440  * Normal case is to go all the way to Ready straight away, though there
441  * are atypical cases where we need to take it in steps.
442  *
443  * Use the data about running transactions on master to create the initial
444  * state of KnownAssignedXids. We also use these records to regularly prune
445  * KnownAssignedXids because we know it is possible that some transactions
446  * with FATAL errors fail to write abort records, which could cause eventual
447  * overflow.
448  *
449  * See comments for LogStandbySnapshot().
450  */
451 void
452 ProcArrayApplyRecoveryInfo(RunningTransactions running)
453 {
454         TransactionId *xids;
455         int                     nxids;
456         TransactionId nextXid;
457         int                     i;
458
459         Assert(standbyState >= STANDBY_INITIALIZED);
460         Assert(TransactionIdIsValid(running->nextXid));
461         Assert(TransactionIdIsValid(running->oldestRunningXid));
462         Assert(TransactionIdIsNormal(running->latestCompletedXid));
463
464         /*
465          * Remove stale transactions, if any.
466          */
467         ExpireOldKnownAssignedTransactionIds(running->oldestRunningXid);
468         StandbyReleaseOldLocks(running->oldestRunningXid);
469
470         /*
471          * If our snapshot is already valid, nothing else to do...
472          */
473         if (standbyState == STANDBY_SNAPSHOT_READY)
474                 return;
475
476         /*
477          * If our initial RunningTransactionsData had an overflowed snapshot then
478          * we knew we were missing some subxids from our snapshot. We can use this
479          * data as an initial snapshot, but we cannot yet mark it valid. We know
480          * that the missing subxids are equal to or earlier than nextXid. After we
481          * initialise we continue to apply changes during recovery, so once the
482          * oldestRunningXid is later than the nextXid from the initial snapshot we
483          * know that we no longer have missing information and can mark the
484          * snapshot as valid.
485          */
486         if (standbyState == STANDBY_SNAPSHOT_PENDING)
487         {
488                 if (TransactionIdPrecedes(standbySnapshotPendingXmin,
489                                                                   running->oldestRunningXid))
490                 {
491                         standbyState = STANDBY_SNAPSHOT_READY;
492                         elog(trace_recovery(DEBUG2),
493                                  "running xact data now proven complete");
494                         elog(trace_recovery(DEBUG2),
495                                  "recovery snapshots are now enabled");
496                 }
497                 else
498                         elog(trace_recovery(DEBUG2),
499                                  "recovery snapshot waiting for %u oldest active xid on standby is %u",
500                                  standbySnapshotPendingXmin,
501                                  running->oldestRunningXid);
502                 return;
503         }
504
505         Assert(standbyState == STANDBY_INITIALIZED);
506
507         /*
508          * OK, we need to initialise from the RunningTransactionsData record
509          */
510
511         /*
512          * Release any locks belonging to old transactions that are not running
513          * according to the running-xacts record.
514          */
515         StandbyReleaseOldLocks(running->nextXid);
516
517         /*
518          * Nobody else is running yet, but take locks anyhow
519          */
520         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
521
522         /*
523          * KnownAssignedXids is sorted so we cannot just add the xids, we have to
524          * sort them first.
525          *
526          * Some of the new xids are top-level xids and some are subtransactions.
527          * We don't call SubtransSetParent because it doesn't matter yet. If we
528          * aren't overflowed then all xids will fit in snapshot and so we don't
529          * need subtrans. If we later overflow, an xid assignment record will add
530          * xids to subtrans. If RunningXacts is overflowed then we don't have
531          * enough information to correctly update subtrans anyway.
532          */
533         Assert(procArray->numKnownAssignedXids == 0);
534
535         /*
536          * Allocate a temporary array to avoid modifying the array passed as
537          * argument.
538          */
539         xids = palloc(sizeof(TransactionId) * running->xcnt);
540
541         /*
542          * Add to the temp array any xids which have not already completed.
543          */
544         nxids = 0;
545         for (i = 0; i < running->xcnt; i++)
546         {
547                 TransactionId xid = running->xids[i];
548
549                 /*
550                  * The running-xacts snapshot can contain xids that were still visible
551                  * in the procarray when the snapshot was taken, but were already
552                  * WAL-logged as completed. They're not running anymore, so ignore
553                  * them.
554                  */
555                 if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
556                         continue;
557
558                 xids[nxids++] = xid;
559         }
560
561         if (nxids > 0)
562         {
563                 /*
564                  * Sort the array so that we can add them safely into
565                  * KnownAssignedXids.
566                  */
567                 qsort(xids, nxids, sizeof(TransactionId), xidComparator);
568
569                 /*
570                  * Add the sorted snapshot into KnownAssignedXids
571                  */
572                 for (i = 0; i < nxids; i++)
573                         KnownAssignedXidsAdd(xids[i], xids[i], true);
574
575                 KnownAssignedXidsDisplay(trace_recovery(DEBUG3));
576         }
577
578         pfree(xids);
579
580         /*
581          * Now we've got the running xids we need to set the global values that
582          * are used to track snapshots as they evolve further.
583          *
584          * - latestCompletedXid which will be the xmax for snapshots -
585          * lastOverflowedXid which shows whether snapshots overflow - nextXid
586          *
587          * If the snapshot overflowed, then we still initialise with what we know,
588          * but the recovery snapshot isn't fully valid yet because we know there
589          * are some subxids missing. We don't know the specific subxids that are
590          * missing, so conservatively assume the last one is latestObservedXid.
591          */
592         latestObservedXid = running->nextXid;
593         TransactionIdRetreat(latestObservedXid);
594
595         if (running->subxid_overflow)
596         {
597                 standbyState = STANDBY_SNAPSHOT_PENDING;
598
599                 standbySnapshotPendingXmin = latestObservedXid;
600                 procArray->lastOverflowedXid = latestObservedXid;
601         }
602         else
603         {
604                 standbyState = STANDBY_SNAPSHOT_READY;
605
606                 standbySnapshotPendingXmin = InvalidTransactionId;
607                 procArray->lastOverflowedXid = InvalidTransactionId;
608         }
609
610         /*
611          * If a transaction wrote a commit record in the gap between taking and
612          * logging the snapshot then latestCompletedXid may already be higher than
613          * the value from the snapshot, so check before we use the incoming value.
614          */
615         if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
616                                                           running->latestCompletedXid))
617                 ShmemVariableCache->latestCompletedXid = running->latestCompletedXid;
618
619         /* nextXid must be beyond any observed xid */
620         nextXid = latestObservedXid;
621         TransactionIdAdvance(nextXid);
622         if (TransactionIdFollows(nextXid, ShmemVariableCache->nextXid))
623                 ShmemVariableCache->nextXid = nextXid;
624
625         Assert(TransactionIdIsNormal(ShmemVariableCache->latestCompletedXid));
626         Assert(TransactionIdIsValid(ShmemVariableCache->nextXid));
627
628         LWLockRelease(ProcArrayLock);
629
630         elog(trace_recovery(DEBUG2), "running transaction data initialized");
631         KnownAssignedXidsDisplay(trace_recovery(DEBUG3));
632         if (standbyState == STANDBY_SNAPSHOT_READY)
633                 elog(trace_recovery(DEBUG2), "recovery snapshots are now enabled");
634         else
635                 ereport(LOG,
636                                 (errmsg("consistent state delayed because recovery snapshot incomplete")));
637 }
638
639 /*
640  * ProcArrayApplyXidAssignment
641  *              Process an XLOG_XACT_ASSIGNMENT WAL record
642  */
643 void
644 ProcArrayApplyXidAssignment(TransactionId topxid,
645                                                         int nsubxids, TransactionId *subxids)
646 {
647         TransactionId max_xid;
648         int                     i;
649
650         Assert(standbyState >= STANDBY_INITIALIZED);
651
652         max_xid = TransactionIdLatest(topxid, nsubxids, subxids);
653
654         /*
655          * Mark all the subtransactions as observed.
656          *
657          * NOTE: This will fail if the subxid contains too many previously
658          * unobserved xids to fit into known-assigned-xids. That shouldn't happen
659          * as the code stands, because xid-assignment records should never contain
660          * more than PGPROC_MAX_CACHED_SUBXIDS entries.
661          */
662         RecordKnownAssignedTransactionIds(max_xid);
663
664         /*
665          * Notice that we update pg_subtrans with the top-level xid, rather than
666          * the parent xid. This is a difference between normal processing and
667          * recovery, yet is still correct in all cases. The reason is that
668          * subtransaction commit is not marked in clog until commit processing, so
669          * all aborted subtransactions have already been clearly marked in clog.
670          * As a result we are able to refer directly to the top-level
671          * transaction's state rather than skipping through all the intermediate
672          * states in the subtransaction tree. This should be the first time we
673          * have attempted to SubTransSetParent().
674          */
675         for (i = 0; i < nsubxids; i++)
676                 SubTransSetParent(subxids[i], topxid, false);
677
678         /*
679          * Uses same locking as transaction commit
680          */
681         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
682
683         /*
684          * Remove subxids from known-assigned-xacts.
685          */
686         KnownAssignedXidsRemoveTree(InvalidTransactionId, nsubxids, subxids);
687
688         /*
689          * Advance lastOverflowedXid to be at least the last of these subxids.
690          */
691         if (TransactionIdPrecedes(procArray->lastOverflowedXid, max_xid))
692                 procArray->lastOverflowedXid = max_xid;
693
694         LWLockRelease(ProcArrayLock);
695 }
696
697 /*
698  * TransactionIdIsInProgress -- is given transaction running in some backend
699  *
700  * Aside from some shortcuts such as checking RecentXmin and our own Xid,
701  * there are four possibilities for finding a running transaction:
702  *
703  * 1. The given Xid is a main transaction Id.  We will find this out cheaply
704  * by looking at the PGPROC struct for each backend.
705  *
706  * 2. The given Xid is one of the cached subxact Xids in the PGPROC array.
707  * We can find this out cheaply too.
708  *
709  * 3. In Hot Standby mode, we must search the KnownAssignedXids list to see
710  * if the Xid is running on the master.
711  *
712  * 4. Search the SubTrans tree to find the Xid's topmost parent, and then
713  * see if that is running according to PGPROC or KnownAssignedXids.  This is
714  * the slowest way, but sadly it has to be done always if the others failed,
715  * unless we see that the cached subxact sets are complete (none have
716  * overflowed).
717  *
718  * ProcArrayLock has to be held while we do 1, 2, 3.  If we save the top Xids
719  * while doing 1 and 3, we can release the ProcArrayLock while we do 4.
720  * This buys back some concurrency (and we can't retrieve the main Xids from
721  * PGPROC again anyway; see GetNewTransactionId).
722  */
723 bool
724 TransactionIdIsInProgress(TransactionId xid)
725 {
726         static TransactionId *xids = NULL;
727         int                     nxids = 0;
728         ProcArrayStruct *arrayP = procArray;
729         TransactionId topxid;
730         int                     i,
731                                 j;
732
733         /*
734          * Don't bother checking a transaction older than RecentXmin; it could not
735          * possibly still be running.  (Note: in particular, this guarantees that
736          * we reject InvalidTransactionId, FrozenTransactionId, etc as not
737          * running.)
738          */
739         if (TransactionIdPrecedes(xid, RecentXmin))
740         {
741                 xc_by_recent_xmin_inc();
742                 return false;
743         }
744
745         /*
746          * We may have just checked the status of this transaction, so if it is
747          * already known to be completed, we can fall out without any access to
748          * shared memory.
749          */
750         if (TransactionIdIsKnownCompleted(xid))
751         {
752                 xc_by_known_xact_inc();
753                 return false;
754         }
755
756         /*
757          * Also, we can handle our own transaction (and subtransactions) without
758          * any access to shared memory.
759          */
760         if (TransactionIdIsCurrentTransactionId(xid))
761         {
762                 xc_by_my_xact_inc();
763                 return true;
764         }
765
766         /*
767          * If first time through, get workspace to remember main XIDs in. We
768          * malloc it permanently to avoid repeated palloc/pfree overhead.
769          */
770         if (xids == NULL)
771         {
772                 /*
773                  * In hot standby mode, reserve enough space to hold all xids in the
774                  * known-assigned list. If we later finish recovery, we no longer need
775                  * the bigger array, but we don't bother to shrink it.
776                  */
777                 int                     maxxids = RecoveryInProgress() ? TOTAL_MAX_CACHED_SUBXIDS : arrayP->maxProcs;
778
779                 xids = (TransactionId *) malloc(maxxids * sizeof(TransactionId));
780                 if (xids == NULL)
781                         ereport(ERROR,
782                                         (errcode(ERRCODE_OUT_OF_MEMORY),
783                                          errmsg("out of memory")));
784         }
785
786         LWLockAcquire(ProcArrayLock, LW_SHARED);
787
788         /*
789          * Now that we have the lock, we can check latestCompletedXid; if the
790          * target Xid is after that, it's surely still running.
791          */
792         if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid, xid))
793         {
794                 LWLockRelease(ProcArrayLock);
795                 xc_by_latest_xid_inc();
796                 return true;
797         }
798
799         /* No shortcuts, gotta grovel through the array */
800         for (i = 0; i < arrayP->numProcs; i++)
801         {
802                 volatile PGPROC *proc = arrayP->procs[i];
803                 TransactionId pxid;
804
805                 /* Ignore my own proc --- dealt with it above */
806                 if (proc == MyProc)
807                         continue;
808
809                 /* Fetch xid just once - see GetNewTransactionId */
810                 pxid = proc->xid;
811
812                 if (!TransactionIdIsValid(pxid))
813                         continue;
814
815                 /*
816                  * Step 1: check the main Xid
817                  */
818                 if (TransactionIdEquals(pxid, xid))
819                 {
820                         LWLockRelease(ProcArrayLock);
821                         xc_by_main_xid_inc();
822                         return true;
823                 }
824
825                 /*
826                  * We can ignore main Xids that are younger than the target Xid, since
827                  * the target could not possibly be their child.
828                  */
829                 if (TransactionIdPrecedes(xid, pxid))
830                         continue;
831
832                 /*
833                  * Step 2: check the cached child-Xids arrays
834                  */
835                 for (j = proc->subxids.nxids - 1; j >= 0; j--)
836                 {
837                         /* Fetch xid just once - see GetNewTransactionId */
838                         TransactionId cxid = proc->subxids.xids[j];
839
840                         if (TransactionIdEquals(cxid, xid))
841                         {
842                                 LWLockRelease(ProcArrayLock);
843                                 xc_by_child_xid_inc();
844                                 return true;
845                         }
846                 }
847
848                 /*
849                  * Save the main Xid for step 4.  We only need to remember main Xids
850                  * that have uncached children.  (Note: there is no race condition
851                  * here because the overflowed flag cannot be cleared, only set, while
852                  * we hold ProcArrayLock.  So we can't miss an Xid that we need to
853                  * worry about.)
854                  */
855                 if (proc->subxids.overflowed)
856                         xids[nxids++] = pxid;
857         }
858
859         /*
860          * Step 3: in hot standby mode, check the known-assigned-xids list.  XIDs
861          * in the list must be treated as running.
862          */
863         if (RecoveryInProgress())
864         {
865                 /* none of the PGPROC entries should have XIDs in hot standby mode */
866                 Assert(nxids == 0);
867
868                 if (KnownAssignedXidExists(xid))
869                 {
870                         LWLockRelease(ProcArrayLock);
871                         xc_by_known_assigned_inc();
872                         return true;
873                 }
874
875                 /*
876                  * If the KnownAssignedXids overflowed, we have to check pg_subtrans
877                  * too.  Fetch all xids from KnownAssignedXids that are lower than
878                  * xid, since if xid is a subtransaction its parent will always have a
879                  * lower value.  Note we will collect both main and subXIDs here, but
880                  * there's no help for it.
881                  */
882                 if (TransactionIdPrecedesOrEquals(xid, procArray->lastOverflowedXid))
883                         nxids = KnownAssignedXidsGet(xids, xid);
884         }
885
886         LWLockRelease(ProcArrayLock);
887
888         /*
889          * If none of the relevant caches overflowed, we know the Xid is not
890          * running without even looking at pg_subtrans.
891          */
892         if (nxids == 0)
893         {
894                 xc_no_overflow_inc();
895                 return false;
896         }
897
898         /*
899          * Step 4: have to check pg_subtrans.
900          *
901          * At this point, we know it's either a subtransaction of one of the Xids
902          * in xids[], or it's not running.  If it's an already-failed
903          * subtransaction, we want to say "not running" even though its parent may
904          * still be running.  So first, check pg_clog to see if it's been aborted.
905          */
906         xc_slow_answer_inc();
907
908         if (TransactionIdDidAbort(xid))
909                 return false;
910
911         /*
912          * It isn't aborted, so check whether the transaction tree it belongs to
913          * is still running (or, more precisely, whether it was running when we
914          * held ProcArrayLock).
915          */
916         topxid = SubTransGetTopmostTransaction(xid);
917         Assert(TransactionIdIsValid(topxid));
918         if (!TransactionIdEquals(topxid, xid))
919         {
920                 for (i = 0; i < nxids; i++)
921                 {
922                         if (TransactionIdEquals(xids[i], topxid))
923                                 return true;
924                 }
925         }
926
927         return false;
928 }
929
930 /*
931  * TransactionIdIsActive -- is xid the top-level XID of an active backend?
932  *
933  * This differs from TransactionIdIsInProgress in that it ignores prepared
934  * transactions, as well as transactions running on the master if we're in
935  * hot standby.  Also, we ignore subtransactions since that's not needed
936  * for current uses.
937  */
938 bool
939 TransactionIdIsActive(TransactionId xid)
940 {
941         bool            result = false;
942         ProcArrayStruct *arrayP = procArray;
943         int                     i;
944
945         /*
946          * Don't bother checking a transaction older than RecentXmin; it could not
947          * possibly still be running.
948          */
949         if (TransactionIdPrecedes(xid, RecentXmin))
950                 return false;
951
952         LWLockAcquire(ProcArrayLock, LW_SHARED);
953
954         for (i = 0; i < arrayP->numProcs; i++)
955         {
956                 volatile PGPROC *proc = arrayP->procs[i];
957
958                 /* Fetch xid just once - see GetNewTransactionId */
959                 TransactionId pxid = proc->xid;
960
961                 if (!TransactionIdIsValid(pxid))
962                         continue;
963
964                 if (proc->pid == 0)
965                         continue;                       /* ignore prepared transactions */
966
967                 if (TransactionIdEquals(pxid, xid))
968                 {
969                         result = true;
970                         break;
971                 }
972         }
973
974         LWLockRelease(ProcArrayLock);
975
976         return result;
977 }
978
979
980 /*
981  * GetOldestXmin -- returns oldest transaction that was running
982  *                                      when any current transaction was started.
983  *
984  * If allDbs is TRUE then all backends are considered; if allDbs is FALSE
985  * then only backends running in my own database are considered.
986  *
987  * If ignoreVacuum is TRUE then backends with the PROC_IN_VACUUM flag set are
988  * ignored.
989  *
990  * This is used by VACUUM to decide which deleted tuples must be preserved
991  * in a table.  allDbs = TRUE is needed for shared relations, but allDbs =
992  * FALSE is sufficient for non-shared relations, since only backends in my
993  * own database could ever see the tuples in them.      Also, we can ignore
994  * concurrently running lazy VACUUMs because (a) they must be working on other
995  * tables, and (b) they don't need to do snapshot-based lookups.
996  *
997  * This is also used to determine where to truncate pg_subtrans.  allDbs
998  * must be TRUE for that case, and ignoreVacuum FALSE.
999  *
1000  * Note: we include all currently running xids in the set of considered xids.
1001  * This ensures that if a just-started xact has not yet set its snapshot,
1002  * when it does set the snapshot it cannot set xmin less than what we compute.
1003  * See notes in src/backend/access/transam/README.
1004  *
1005  * Note: despite the above, it's possible for the calculated value to move
1006  * backwards on repeated calls. The calculated value is conservative, so that
1007  * anything older is definitely not considered as running by anyone anymore,
1008  * but the exact value calculated depends on a number of things. For example,
1009  * if allDbs is FALSE and there are no transactions running in the current
1010  * database, GetOldestXmin() returns latestCompletedXid. If a transaction
1011  * begins after that, its xmin will include in-progress transactions in other
1012  * databases that started earlier, so another call will return a lower value.
1013  * Nonetheless it is safe to vacuum a table in the current database with the
1014  * first result.  There are also replication-related effects: a walsender
1015  * process can set its xmin based on transactions that are no longer running
1016  * in the master but are still being replayed on the standby, thus possibly
1017  * making the GetOldestXmin reading go backwards.  In this case there is a
1018  * possibility that we lose data that the standby would like to have, but
1019  * there is little we can do about that --- data is only protected if the
1020  * walsender runs continuously while queries are executed on the standby.
1021  * (The Hot Standby code deals with such cases by failing standby queries
1022  * that needed to access already-removed data, so there's no integrity bug.)
1023  * The return value is also adjusted with vacuum_defer_cleanup_age, so
1024  * increasing that setting on the fly is another easy way to make
1025  * GetOldestXmin() move backwards, with no consequences for data integrity.
1026  */
1027 TransactionId
1028 GetOldestXmin(bool allDbs, bool ignoreVacuum)
1029 {
1030         ProcArrayStruct *arrayP = procArray;
1031         TransactionId result;
1032         int                     index;
1033
1034         /* Cannot look for individual databases during recovery */
1035         Assert(allDbs || !RecoveryInProgress());
1036
1037         LWLockAcquire(ProcArrayLock, LW_SHARED);
1038
1039         /*
1040          * We initialize the MIN() calculation with latestCompletedXid + 1. This
1041          * is a lower bound for the XIDs that might appear in the ProcArray later,
1042          * and so protects us against overestimating the result due to future
1043          * additions.
1044          */
1045         result = ShmemVariableCache->latestCompletedXid;
1046         Assert(TransactionIdIsNormal(result));
1047         TransactionIdAdvance(result);
1048
1049         for (index = 0; index < arrayP->numProcs; index++)
1050         {
1051                 volatile PGPROC *proc = arrayP->procs[index];
1052
1053                 if (ignoreVacuum && (proc->vacuumFlags & PROC_IN_VACUUM))
1054                         continue;
1055
1056                 if (allDbs ||
1057                         proc->databaseId == MyDatabaseId ||
1058                         proc->databaseId == 0)          /* always include WalSender */
1059                 {
1060                         /* Fetch xid just once - see GetNewTransactionId */
1061                         TransactionId xid = proc->xid;
1062
1063                         /* First consider the transaction's own Xid, if any */
1064                         if (TransactionIdIsNormal(xid) &&
1065                                 TransactionIdPrecedes(xid, result))
1066                                 result = xid;
1067
1068                         /*
1069                          * Also consider the transaction's Xmin, if set.
1070                          *
1071                          * We must check both Xid and Xmin because a transaction might
1072                          * have an Xmin but not (yet) an Xid; conversely, if it has an
1073                          * Xid, that could determine some not-yet-set Xmin.
1074                          */
1075                         xid = proc->xmin;       /* Fetch just once */
1076                         if (TransactionIdIsNormal(xid) &&
1077                                 TransactionIdPrecedes(xid, result))
1078                                 result = xid;
1079                 }
1080         }
1081
1082         if (RecoveryInProgress())
1083         {
1084                 /*
1085                  * Check to see whether KnownAssignedXids contains an xid value older
1086                  * than the main procarray.
1087                  */
1088                 TransactionId kaxmin = KnownAssignedXidsGetOldestXmin();
1089
1090                 LWLockRelease(ProcArrayLock);
1091
1092                 if (TransactionIdIsNormal(kaxmin) &&
1093                         TransactionIdPrecedes(kaxmin, result))
1094                         result = kaxmin;
1095         }
1096         else
1097         {
1098                 /*
1099                  * No other information needed, so release the lock immediately.
1100                  */
1101                 LWLockRelease(ProcArrayLock);
1102
1103                 /*
1104                  * Compute the cutoff XID by subtracting vacuum_defer_cleanup_age,
1105                  * being careful not to generate a "permanent" XID.
1106                  *
1107                  * vacuum_defer_cleanup_age provides some additional "slop" for the
1108                  * benefit of hot standby queries on slave servers.  This is quick and
1109                  * dirty, and perhaps not all that useful unless the master has a
1110                  * predictable transaction rate, but it offers some protection when
1111                  * there's no walsender connection.  Note that we are assuming
1112                  * vacuum_defer_cleanup_age isn't large enough to cause wraparound ---
1113                  * so guc.c should limit it to no more than the xidStopLimit threshold
1114                  * in varsup.c.  Also note that we intentionally don't apply
1115                  * vacuum_defer_cleanup_age on standby servers.
1116                  */
1117                 result -= vacuum_defer_cleanup_age;
1118                 if (!TransactionIdIsNormal(result))
1119                         result = FirstNormalTransactionId;
1120         }
1121
1122         return result;
1123 }
1124
1125 /*
1126  * GetSnapshotData -- returns information about running transactions.
1127  *
1128  * The returned snapshot includes xmin (lowest still-running xact ID),
1129  * xmax (highest completed xact ID + 1), and a list of running xact IDs
1130  * in the range xmin <= xid < xmax.  It is used as follows:
1131  *              All xact IDs < xmin are considered finished.
1132  *              All xact IDs >= xmax are considered still running.
1133  *              For an xact ID xmin <= xid < xmax, consult list to see whether
1134  *              it is considered running or not.
1135  * This ensures that the set of transactions seen as "running" by the
1136  * current xact will not change after it takes the snapshot.
1137  *
1138  * All running top-level XIDs are included in the snapshot, except for lazy
1139  * VACUUM processes.  We also try to include running subtransaction XIDs,
1140  * but since PGPROC has only a limited cache area for subxact XIDs, full
1141  * information may not be available.  If we find any overflowed subxid arrays,
1142  * we have to mark the snapshot's subxid data as overflowed, and extra work
1143  * *may* need to be done to determine what's running (see XidInMVCCSnapshot()
1144  * in tqual.c).
1145  *
1146  * We also update the following backend-global variables:
1147  *              TransactionXmin: the oldest xmin of any snapshot in use in the
1148  *                      current transaction (this is the same as MyProc->xmin).
1149  *              RecentXmin: the xmin computed for the most recent snapshot.  XIDs
1150  *                      older than this are known not running any more.
1151  *              RecentGlobalXmin: the global xmin (oldest TransactionXmin across all
1152  *                      running transactions, except those running LAZY VACUUM).  This is
1153  *                      the same computation done by GetOldestXmin(true, true).
1154  *
1155  * Note: this function should probably not be called with an argument that's
1156  * not statically allocated (see xip allocation below).
1157  */
1158 Snapshot
1159 GetSnapshotData(Snapshot snapshot)
1160 {
1161         ProcArrayStruct *arrayP = procArray;
1162         TransactionId xmin;
1163         TransactionId xmax;
1164         TransactionId globalxmin;
1165         int                     index;
1166         int                     count = 0;
1167         int                     subcount = 0;
1168         bool            suboverflowed = false;
1169
1170         Assert(snapshot != NULL);
1171
1172         /*
1173          * Allocating space for maxProcs xids is usually overkill; numProcs would
1174          * be sufficient.  But it seems better to do the malloc while not holding
1175          * the lock, so we can't look at numProcs.  Likewise, we allocate much
1176          * more subxip storage than is probably needed.
1177          *
1178          * This does open a possibility for avoiding repeated malloc/free: since
1179          * maxProcs does not change at runtime, we can simply reuse the previous
1180          * xip arrays if any.  (This relies on the fact that all callers pass
1181          * static SnapshotData structs.)
1182          */
1183         if (snapshot->xip == NULL)
1184         {
1185                 /*
1186                  * First call for this snapshot. Snapshot is same size whether or not
1187                  * we are in recovery, see later comments.
1188                  */
1189                 snapshot->xip = (TransactionId *)
1190                         malloc(arrayP->maxProcs * sizeof(TransactionId));
1191                 if (snapshot->xip == NULL)
1192                         ereport(ERROR,
1193                                         (errcode(ERRCODE_OUT_OF_MEMORY),
1194                                          errmsg("out of memory")));
1195                 Assert(snapshot->subxip == NULL);
1196                 snapshot->subxip = (TransactionId *)
1197                         malloc(TOTAL_MAX_CACHED_SUBXIDS * sizeof(TransactionId));
1198                 if (snapshot->subxip == NULL)
1199                         ereport(ERROR,
1200                                         (errcode(ERRCODE_OUT_OF_MEMORY),
1201                                          errmsg("out of memory")));
1202         }
1203
1204         /*
1205          * It is sufficient to get shared lock on ProcArrayLock, even if we are
1206          * going to set MyProc->xmin.
1207          */
1208         LWLockAcquire(ProcArrayLock, LW_SHARED);
1209
1210         /* xmax is always latestCompletedXid + 1 */
1211         xmax = ShmemVariableCache->latestCompletedXid;
1212         Assert(TransactionIdIsNormal(xmax));
1213         TransactionIdAdvance(xmax);
1214
1215         /* initialize xmin calculation with xmax */
1216         globalxmin = xmin = xmax;
1217
1218         /*
1219          * If we're in recovery then snapshot data comes from a different place,
1220          * so decide which route we take before grab the lock. It is possible for
1221          * recovery to end before we finish taking snapshot, and for newly
1222          * assigned transaction ids to be added to the procarray. Xmax cannot
1223          * change while we hold ProcArrayLock, so those newly added transaction
1224          * ids would be filtered away, so we need not be concerned about them.
1225          */
1226         snapshot->takenDuringRecovery = RecoveryInProgress();
1227
1228         if (!snapshot->takenDuringRecovery)
1229         {
1230                 /*
1231                  * Spin over procArray checking xid, xmin, and subxids.  The goal is
1232                  * to gather all active xids, find the lowest xmin, and try to record
1233                  * subxids. During recovery no xids will be assigned, so all normal
1234                  * backends can be ignored, nor are there any VACUUMs running. All
1235                  * prepared transaction xids are held in KnownAssignedXids, so these
1236                  * will be seen without needing to loop through procs here.
1237                  */
1238                 for (index = 0; index < arrayP->numProcs; index++)
1239                 {
1240                         volatile PGPROC *proc = arrayP->procs[index];
1241                         TransactionId xid;
1242
1243                         /* Ignore procs running LAZY VACUUM */
1244                         if (proc->vacuumFlags & PROC_IN_VACUUM)
1245                                 continue;
1246
1247                         /* Update globalxmin to be the smallest valid xmin */
1248                         xid = proc->xmin;       /* fetch just once */
1249                         if (TransactionIdIsNormal(xid) &&
1250                                 TransactionIdPrecedes(xid, globalxmin))
1251                                 globalxmin = xid;
1252
1253                         /* Fetch xid just once - see GetNewTransactionId */
1254                         xid = proc->xid;
1255
1256                         /*
1257                          * If the transaction has been assigned an xid < xmax we add it to
1258                          * the snapshot, and update xmin if necessary.  There's no need to
1259                          * store XIDs >= xmax, since we'll treat them as running anyway.
1260                          * We don't bother to examine their subxids either.
1261                          *
1262                          * We don't include our own XID (if any) in the snapshot, but we
1263                          * must include it into xmin.
1264                          */
1265                         if (TransactionIdIsNormal(xid))
1266                         {
1267                                 if (TransactionIdFollowsOrEquals(xid, xmax))
1268                                         continue;
1269                                 if (proc != MyProc)
1270                                         snapshot->xip[count++] = xid;
1271                                 if (TransactionIdPrecedes(xid, xmin))
1272                                         xmin = xid;
1273                         }
1274
1275                         /*
1276                          * Save subtransaction XIDs if possible (if we've already
1277                          * overflowed, there's no point).  Note that the subxact XIDs must
1278                          * be later than their parent, so no need to check them against
1279                          * xmin.  We could filter against xmax, but it seems better not to
1280                          * do that much work while holding the ProcArrayLock.
1281                          *
1282                          * The other backend can add more subxids concurrently, but cannot
1283                          * remove any.  Hence it's important to fetch nxids just once.
1284                          * Should be safe to use memcpy, though.  (We needn't worry about
1285                          * missing any xids added concurrently, because they must postdate
1286                          * xmax.)
1287                          *
1288                          * Again, our own XIDs are not included in the snapshot.
1289                          */
1290                         if (!suboverflowed && proc != MyProc)
1291                         {
1292                                 if (proc->subxids.overflowed)
1293                                         suboverflowed = true;
1294                                 else
1295                                 {
1296                                         int                     nxids = proc->subxids.nxids;
1297
1298                                         if (nxids > 0)
1299                                         {
1300                                                 memcpy(snapshot->subxip + subcount,
1301                                                            (void *) proc->subxids.xids,
1302                                                            nxids * sizeof(TransactionId));
1303                                                 subcount += nxids;
1304                                         }
1305                                 }
1306                         }
1307                 }
1308         }
1309         else
1310         {
1311                 /*
1312                  * We're in hot standby, so get XIDs from KnownAssignedXids.
1313                  *
1314                  * We store all xids directly into subxip[]. Here's why:
1315                  *
1316                  * In recovery we don't know which xids are top-level and which are
1317                  * subxacts, a design choice that greatly simplifies xid processing.
1318                  *
1319                  * It seems like we would want to try to put xids into xip[] only, but
1320                  * that is fairly small. We would either need to make that bigger or
1321                  * to increase the rate at which we WAL-log xid assignment; neither is
1322                  * an appealing choice.
1323                  *
1324                  * We could try to store xids into xip[] first and then into subxip[]
1325                  * if there are too many xids. That only works if the snapshot doesn't
1326                  * overflow because we do not search subxip[] in that case. A simpler
1327                  * way is to just store all xids in the subxact array because this is
1328                  * by far the bigger array. We just leave the xip array empty.
1329                  *
1330                  * Either way we need to change the way XidInMVCCSnapshot() works
1331                  * depending upon when the snapshot was taken, or change normal
1332                  * snapshot processing so it matches.
1333                  */
1334                 subcount = KnownAssignedXidsGetAndSetXmin(snapshot->subxip, &xmin,
1335                                                                                                   xmax);
1336
1337                 if (TransactionIdPrecedesOrEquals(xmin, procArray->lastOverflowedXid))
1338                         suboverflowed = true;
1339         }
1340
1341         if (!TransactionIdIsValid(MyProc->xmin))
1342                 MyProc->xmin = TransactionXmin = xmin;
1343
1344         LWLockRelease(ProcArrayLock);
1345
1346         /*
1347          * Update globalxmin to include actual process xids.  This is a slightly
1348          * different way of computing it than GetOldestXmin uses, but should give
1349          * the same result.
1350          */
1351         if (TransactionIdPrecedes(xmin, globalxmin))
1352                 globalxmin = xmin;
1353
1354         /* Update global variables too */
1355         RecentGlobalXmin = globalxmin - vacuum_defer_cleanup_age;
1356         if (!TransactionIdIsNormal(RecentGlobalXmin))
1357                 RecentGlobalXmin = FirstNormalTransactionId;
1358         RecentXmin = xmin;
1359
1360         snapshot->xmin = xmin;
1361         snapshot->xmax = xmax;
1362         snapshot->xcnt = count;
1363         snapshot->subxcnt = subcount;
1364         snapshot->suboverflowed = suboverflowed;
1365
1366         snapshot->curcid = GetCurrentCommandId(false);
1367
1368         /*
1369          * This is a new snapshot, so set both refcounts are zero, and mark it as
1370          * not copied in persistent memory.
1371          */
1372         snapshot->active_count = 0;
1373         snapshot->regd_count = 0;
1374         snapshot->copied = false;
1375
1376         return snapshot;
1377 }
1378
1379 /*
1380  * GetRunningTransactionData -- returns information about running transactions.
1381  *
1382  * Similar to GetSnapshotData but returns more information. We include
1383  * all PGPROCs with an assigned TransactionId, even VACUUM processes.
1384  *
1385  * We acquire XidGenLock, but the caller is responsible for releasing it.
1386  * This ensures that no new XIDs enter the proc array until the caller has
1387  * WAL-logged this snapshot, and releases the lock.
1388  *
1389  * The returned data structure is statically allocated; caller should not
1390  * modify it, and must not assume it is valid past the next call.
1391  *
1392  * This is never executed during recovery so there is no need to look at
1393  * KnownAssignedXids.
1394  *
1395  * We don't worry about updating other counters, we want to keep this as
1396  * simple as possible and leave GetSnapshotData() as the primary code for
1397  * that bookkeeping.
1398  */
1399 RunningTransactions
1400 GetRunningTransactionData(void)
1401 {
1402         /* result workspace */
1403         static RunningTransactionsData CurrentRunningXactsData;
1404
1405         ProcArrayStruct *arrayP = procArray;
1406         RunningTransactions CurrentRunningXacts = &CurrentRunningXactsData;
1407         TransactionId latestCompletedXid;
1408         TransactionId oldestRunningXid;
1409         TransactionId *xids;
1410         int                     index;
1411         int                     count;
1412         int                     subcount;
1413         bool            suboverflowed;
1414
1415         Assert(!RecoveryInProgress());
1416
1417         /*
1418          * Allocating space for maxProcs xids is usually overkill; numProcs would
1419          * be sufficient.  But it seems better to do the malloc while not holding
1420          * the lock, so we can't look at numProcs.  Likewise, we allocate much
1421          * more subxip storage than is probably needed.
1422          *
1423          * Should only be allocated in bgwriter, since only ever executed during
1424          * checkpoints.
1425          */
1426         if (CurrentRunningXacts->xids == NULL)
1427         {
1428                 /*
1429                  * First call
1430                  */
1431                 CurrentRunningXacts->xids = (TransactionId *)
1432                         malloc(TOTAL_MAX_CACHED_SUBXIDS * sizeof(TransactionId));
1433                 if (CurrentRunningXacts->xids == NULL)
1434                         ereport(ERROR,
1435                                         (errcode(ERRCODE_OUT_OF_MEMORY),
1436                                          errmsg("out of memory")));
1437         }
1438
1439         xids = CurrentRunningXacts->xids;
1440
1441         count = subcount = 0;
1442         suboverflowed = false;
1443
1444         /*
1445          * Ensure that no xids enter or leave the procarray while we obtain
1446          * snapshot.
1447          */
1448         LWLockAcquire(ProcArrayLock, LW_SHARED);
1449         LWLockAcquire(XidGenLock, LW_SHARED);
1450
1451         latestCompletedXid = ShmemVariableCache->latestCompletedXid;
1452
1453         oldestRunningXid = ShmemVariableCache->nextXid;
1454
1455         /*
1456          * Spin over procArray collecting all xids and subxids.
1457          */
1458         for (index = 0; index < arrayP->numProcs; index++)
1459         {
1460                 volatile PGPROC *proc = arrayP->procs[index];
1461                 TransactionId xid;
1462                 int                     nxids;
1463
1464                 /* Fetch xid just once - see GetNewTransactionId */
1465                 xid = proc->xid;
1466
1467                 /*
1468                  * We don't need to store transactions that don't have a TransactionId
1469                  * yet because they will not show as running on a standby server.
1470                  */
1471                 if (!TransactionIdIsValid(xid))
1472                         continue;
1473
1474                 xids[count++] = xid;
1475
1476                 if (TransactionIdPrecedes(xid, oldestRunningXid))
1477                         oldestRunningXid = xid;
1478
1479                 /*
1480                  * Save subtransaction XIDs. Other backends can't add or remove
1481                  * entries while we're holding XidGenLock.
1482                  */
1483                 nxids = proc->subxids.nxids;
1484                 if (nxids > 0)
1485                 {
1486                         memcpy(&xids[count], (void *) proc->subxids.xids,
1487                                    nxids * sizeof(TransactionId));
1488                         count += nxids;
1489                         subcount += nxids;
1490
1491                         if (proc->subxids.overflowed)
1492                                 suboverflowed = true;
1493
1494                         /*
1495                          * Top-level XID of a transaction is always less than any of its
1496                          * subxids, so we don't need to check if any of the subxids are
1497                          * smaller than oldestRunningXid
1498                          */
1499                 }
1500         }
1501
1502         CurrentRunningXacts->xcnt = count;
1503         CurrentRunningXacts->subxid_overflow = suboverflowed;
1504         CurrentRunningXacts->nextXid = ShmemVariableCache->nextXid;
1505         CurrentRunningXacts->oldestRunningXid = oldestRunningXid;
1506         CurrentRunningXacts->latestCompletedXid = latestCompletedXid;
1507
1508         /* We don't release XidGenLock here, the caller is responsible for that */
1509         LWLockRelease(ProcArrayLock);
1510
1511         Assert(TransactionIdIsValid(CurrentRunningXacts->nextXid));
1512         Assert(TransactionIdIsValid(CurrentRunningXacts->oldestRunningXid));
1513         Assert(TransactionIdIsNormal(CurrentRunningXacts->latestCompletedXid));
1514
1515         return CurrentRunningXacts;
1516 }
1517
1518 /*
1519  * GetTransactionsInCommit -- Get the XIDs of transactions that are committing
1520  *
1521  * Constructs an array of XIDs of transactions that are currently in commit
1522  * critical sections, as shown by having inCommit set in their PGPROC entries.
1523  *
1524  * *xids_p is set to a palloc'd array that should be freed by the caller.
1525  * The return value is the number of valid entries.
1526  *
1527  * Note that because backends set or clear inCommit without holding any lock,
1528  * the result is somewhat indeterminate, but we don't really care.  Even in
1529  * a multiprocessor with delayed writes to shared memory, it should be certain
1530  * that setting of inCommit will propagate to shared memory when the backend
1531  * takes the WALInsertLock, so we cannot fail to see an xact as inCommit if
1532  * it's already inserted its commit record.  Whether it takes a little while
1533  * for clearing of inCommit to propagate is unimportant for correctness.
1534  */
1535 int
1536 GetTransactionsInCommit(TransactionId **xids_p)
1537 {
1538         ProcArrayStruct *arrayP = procArray;
1539         TransactionId *xids;
1540         int                     nxids;
1541         int                     index;
1542
1543         xids = (TransactionId *) palloc(arrayP->maxProcs * sizeof(TransactionId));
1544         nxids = 0;
1545
1546         LWLockAcquire(ProcArrayLock, LW_SHARED);
1547
1548         for (index = 0; index < arrayP->numProcs; index++)
1549         {
1550                 volatile PGPROC *proc = arrayP->procs[index];
1551
1552                 /* Fetch xid just once - see GetNewTransactionId */
1553                 TransactionId pxid = proc->xid;
1554
1555                 if (proc->inCommit && TransactionIdIsValid(pxid))
1556                         xids[nxids++] = pxid;
1557         }
1558
1559         LWLockRelease(ProcArrayLock);
1560
1561         *xids_p = xids;
1562         return nxids;
1563 }
1564
1565 /*
1566  * HaveTransactionsInCommit -- Are any of the specified XIDs in commit?
1567  *
1568  * This is used with the results of GetTransactionsInCommit to see if any
1569  * of the specified XIDs are still in their commit critical sections.
1570  *
1571  * Note: this is O(N^2) in the number of xacts that are/were in commit, but
1572  * those numbers should be small enough for it not to be a problem.
1573  */
1574 bool
1575 HaveTransactionsInCommit(TransactionId *xids, int nxids)
1576 {
1577         bool            result = false;
1578         ProcArrayStruct *arrayP = procArray;
1579         int                     index;
1580
1581         LWLockAcquire(ProcArrayLock, LW_SHARED);
1582
1583         for (index = 0; index < arrayP->numProcs; index++)
1584         {
1585                 volatile PGPROC *proc = arrayP->procs[index];
1586
1587                 /* Fetch xid just once - see GetNewTransactionId */
1588                 TransactionId pxid = proc->xid;
1589
1590                 if (proc->inCommit && TransactionIdIsValid(pxid))
1591                 {
1592                         int                     i;
1593
1594                         for (i = 0; i < nxids; i++)
1595                         {
1596                                 if (xids[i] == pxid)
1597                                 {
1598                                         result = true;
1599                                         break;
1600                                 }
1601                         }
1602                         if (result)
1603                                 break;
1604                 }
1605         }
1606
1607         LWLockRelease(ProcArrayLock);
1608
1609         return result;
1610 }
1611
1612 /*
1613  * BackendPidGetProc -- get a backend's PGPROC given its PID
1614  *
1615  * Returns NULL if not found.  Note that it is up to the caller to be
1616  * sure that the question remains meaningful for long enough for the
1617  * answer to be used ...
1618  */
1619 PGPROC *
1620 BackendPidGetProc(int pid)
1621 {
1622         PGPROC     *result = NULL;
1623         ProcArrayStruct *arrayP = procArray;
1624         int                     index;
1625
1626         if (pid == 0)                           /* never match dummy PGPROCs */
1627                 return NULL;
1628
1629         LWLockAcquire(ProcArrayLock, LW_SHARED);
1630
1631         for (index = 0; index < arrayP->numProcs; index++)
1632         {
1633                 PGPROC     *proc = arrayP->procs[index];
1634
1635                 if (proc->pid == pid)
1636                 {
1637                         result = proc;
1638                         break;
1639                 }
1640         }
1641
1642         LWLockRelease(ProcArrayLock);
1643
1644         return result;
1645 }
1646
1647 /*
1648  * BackendXidGetPid -- get a backend's pid given its XID
1649  *
1650  * Returns 0 if not found or it's a prepared transaction.  Note that
1651  * it is up to the caller to be sure that the question remains
1652  * meaningful for long enough for the answer to be used ...
1653  *
1654  * Only main transaction Ids are considered.  This function is mainly
1655  * useful for determining what backend owns a lock.
1656  *
1657  * Beware that not every xact has an XID assigned.      However, as long as you
1658  * only call this using an XID found on disk, you're safe.
1659  */
1660 int
1661 BackendXidGetPid(TransactionId xid)
1662 {
1663         int                     result = 0;
1664         ProcArrayStruct *arrayP = procArray;
1665         int                     index;
1666
1667         if (xid == InvalidTransactionId)        /* never match invalid xid */
1668                 return 0;
1669
1670         LWLockAcquire(ProcArrayLock, LW_SHARED);
1671
1672         for (index = 0; index < arrayP->numProcs; index++)
1673         {
1674                 volatile PGPROC *proc = arrayP->procs[index];
1675
1676                 if (proc->xid == xid)
1677                 {
1678                         result = proc->pid;
1679                         break;
1680                 }
1681         }
1682
1683         LWLockRelease(ProcArrayLock);
1684
1685         return result;
1686 }
1687
1688 /*
1689  * IsBackendPid -- is a given pid a running backend
1690  */
1691 bool
1692 IsBackendPid(int pid)
1693 {
1694         return (BackendPidGetProc(pid) != NULL);
1695 }
1696
1697
1698 /*
1699  * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
1700  *
1701  * The array is palloc'd. The number of valid entries is returned into *nvxids.
1702  *
1703  * The arguments allow filtering the set of VXIDs returned.  Our own process
1704  * is always skipped.  In addition:
1705  *      If limitXmin is not InvalidTransactionId, skip processes with
1706  *              xmin > limitXmin.
1707  *      If excludeXmin0 is true, skip processes with xmin = 0.
1708  *      If allDbs is false, skip processes attached to other databases.
1709  *      If excludeVacuum isn't zero, skip processes for which
1710  *              (vacuumFlags & excludeVacuum) is not zero.
1711  *
1712  * Note: the purpose of the limitXmin and excludeXmin0 parameters is to
1713  * allow skipping backends whose oldest live snapshot is no older than
1714  * some snapshot we have.  Since we examine the procarray with only shared
1715  * lock, there are race conditions: a backend could set its xmin just after
1716  * we look.  Indeed, on multiprocessors with weak memory ordering, the
1717  * other backend could have set its xmin *before* we look.      We know however
1718  * that such a backend must have held shared ProcArrayLock overlapping our
1719  * own hold of ProcArrayLock, else we would see its xmin update.  Therefore,
1720  * any snapshot the other backend is taking concurrently with our scan cannot
1721  * consider any transactions as still running that we think are committed
1722  * (since backends must hold ProcArrayLock exclusive to commit).
1723  */
1724 VirtualTransactionId *
1725 GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
1726                                           bool allDbs, int excludeVacuum,
1727                                           int *nvxids)
1728 {
1729         VirtualTransactionId *vxids;
1730         ProcArrayStruct *arrayP = procArray;
1731         int                     count = 0;
1732         int                     index;
1733
1734         /* allocate what's certainly enough result space */
1735         vxids = (VirtualTransactionId *)
1736                 palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
1737
1738         LWLockAcquire(ProcArrayLock, LW_SHARED);
1739
1740         for (index = 0; index < arrayP->numProcs; index++)
1741         {
1742                 volatile PGPROC *proc = arrayP->procs[index];
1743
1744                 if (proc == MyProc)
1745                         continue;
1746
1747                 if (excludeVacuum & proc->vacuumFlags)
1748                         continue;
1749
1750                 if (allDbs || proc->databaseId == MyDatabaseId)
1751                 {
1752                         /* Fetch xmin just once - might change on us */
1753                         TransactionId pxmin = proc->xmin;
1754
1755                         if (excludeXmin0 && !TransactionIdIsValid(pxmin))
1756                                 continue;
1757
1758                         /*
1759                          * InvalidTransactionId precedes all other XIDs, so a proc that
1760                          * hasn't set xmin yet will not be rejected by this test.
1761                          */
1762                         if (!TransactionIdIsValid(limitXmin) ||
1763                                 TransactionIdPrecedesOrEquals(pxmin, limitXmin))
1764                         {
1765                                 VirtualTransactionId vxid;
1766
1767                                 GET_VXID_FROM_PGPROC(vxid, *proc);
1768                                 if (VirtualTransactionIdIsValid(vxid))
1769                                         vxids[count++] = vxid;
1770                         }
1771                 }
1772         }
1773
1774         LWLockRelease(ProcArrayLock);
1775
1776         *nvxids = count;
1777         return vxids;
1778 }
1779
1780 /*
1781  * GetConflictingVirtualXIDs -- returns an array of currently active VXIDs.
1782  *
1783  * Usage is limited to conflict resolution during recovery on standby servers.
1784  * limitXmin is supplied as either latestRemovedXid, or InvalidTransactionId
1785  * in cases where we cannot accurately determine a value for latestRemovedXid.
1786  *
1787  * If limitXmin is InvalidTransactionId then we want to kill everybody,
1788  * so we're not worried if they have a snapshot or not, nor does it really
1789  * matter what type of lock we hold.
1790  *
1791  * All callers that are checking xmins always now supply a valid and useful
1792  * value for limitXmin. The limitXmin is always lower than the lowest
1793  * numbered KnownAssignedXid that is not already a FATAL error. This is
1794  * because we only care about cleanup records that are cleaning up tuple
1795  * versions from committed transactions. In that case they will only occur
1796  * at the point where the record is less than the lowest running xid. That
1797  * allows us to say that if any backend takes a snapshot concurrently with
1798  * us then the conflict assessment made here would never include the snapshot
1799  * that is being derived. So we take LW_SHARED on the ProcArray and allow
1800  * concurrent snapshots when limitXmin is valid. We might think about adding
1801  *       Assert(limitXmin < lowest(KnownAssignedXids))
1802  * but that would not be true in the case of FATAL errors lagging in array,
1803  * but we already know those are bogus anyway, so we skip that test.
1804  *
1805  * If dbOid is valid we skip backends attached to other databases.
1806  *
1807  * Be careful to *not* pfree the result from this function. We reuse
1808  * this array sufficiently often that we use malloc for the result.
1809  */
1810 VirtualTransactionId *
1811 GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
1812 {
1813         static VirtualTransactionId *vxids;
1814         ProcArrayStruct *arrayP = procArray;
1815         int                     count = 0;
1816         int                     index;
1817
1818         /*
1819          * If first time through, get workspace to remember main XIDs in. We
1820          * malloc it permanently to avoid repeated palloc/pfree overhead. Allow
1821          * result space, remembering room for a terminator.
1822          */
1823         if (vxids == NULL)
1824         {
1825                 vxids = (VirtualTransactionId *)
1826                         malloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
1827                 if (vxids == NULL)
1828                         ereport(ERROR,
1829                                         (errcode(ERRCODE_OUT_OF_MEMORY),
1830                                          errmsg("out of memory")));
1831         }
1832
1833         LWLockAcquire(ProcArrayLock, LW_SHARED);
1834
1835         for (index = 0; index < arrayP->numProcs; index++)
1836         {
1837                 volatile PGPROC *proc = arrayP->procs[index];
1838
1839                 /* Exclude prepared transactions */
1840                 if (proc->pid == 0)
1841                         continue;
1842
1843                 if (!OidIsValid(dbOid) ||
1844                         proc->databaseId == dbOid)
1845                 {
1846                         /* Fetch xmin just once - can't change on us, but good coding */
1847                         TransactionId pxmin = proc->xmin;
1848
1849                         /*
1850                          * We ignore an invalid pxmin because this means that backend has
1851                          * no snapshot and cannot get another one while we hold exclusive
1852                          * lock.
1853                          */
1854                         if (!TransactionIdIsValid(limitXmin) ||
1855                                 (TransactionIdIsValid(pxmin) && !TransactionIdFollows(pxmin, limitXmin)))
1856                         {
1857                                 VirtualTransactionId vxid;
1858
1859                                 GET_VXID_FROM_PGPROC(vxid, *proc);
1860                                 if (VirtualTransactionIdIsValid(vxid))
1861                                         vxids[count++] = vxid;
1862                         }
1863                 }
1864         }
1865
1866         LWLockRelease(ProcArrayLock);
1867
1868         /* add the terminator */
1869         vxids[count].backendId = InvalidBackendId;
1870         vxids[count].localTransactionId = InvalidLocalTransactionId;
1871
1872         return vxids;
1873 }
1874
1875 /*
1876  * CancelVirtualTransaction - used in recovery conflict processing
1877  *
1878  * Returns pid of the process signaled, or 0 if not found.
1879  */
1880 pid_t
1881 CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
1882 {
1883         ProcArrayStruct *arrayP = procArray;
1884         int                     index;
1885         pid_t           pid = 0;
1886
1887         LWLockAcquire(ProcArrayLock, LW_SHARED);
1888
1889         for (index = 0; index < arrayP->numProcs; index++)
1890         {
1891                 VirtualTransactionId procvxid;
1892                 PGPROC     *proc = arrayP->procs[index];
1893
1894                 GET_VXID_FROM_PGPROC(procvxid, *proc);
1895
1896                 if (procvxid.backendId == vxid.backendId &&
1897                         procvxid.localTransactionId == vxid.localTransactionId)
1898                 {
1899                         proc->recoveryConflictPending = true;
1900                         pid = proc->pid;
1901                         if (pid != 0)
1902                         {
1903                                 /*
1904                                  * Kill the pid if it's still here. If not, that's what we
1905                                  * wanted so ignore any errors.
1906                                  */
1907                                 (void) SendProcSignal(pid, sigmode, vxid.backendId);
1908                         }
1909                         break;
1910                 }
1911         }
1912
1913         LWLockRelease(ProcArrayLock);
1914
1915         return pid;
1916 }
1917
1918 /*
1919  * MinimumActiveBackends --- count backends (other than myself) that are
1920  *              in active transactions.  Return true if the count exceeds the
1921  *              minimum threshold passed.  This is used as a heuristic to decide if
1922  *              a pre-XLOG-flush delay is worthwhile during commit.
1923  *
1924  * Do not count backends that are blocked waiting for locks, since they are
1925  * not going to get to run until someone else commits.
1926  */
1927 bool
1928 MinimumActiveBackends(int min)
1929 {
1930         ProcArrayStruct *arrayP = procArray;
1931         int                     count = 0;
1932         int                     index;
1933
1934         /* Quick short-circuit if no minimum is specified */
1935         if (min == 0)
1936                 return true;
1937
1938         /*
1939          * Note: for speed, we don't acquire ProcArrayLock.  This is a little bit
1940          * bogus, but since we are only testing fields for zero or nonzero, it
1941          * should be OK.  The result is only used for heuristic purposes anyway...
1942          */
1943         for (index = 0; index < arrayP->numProcs; index++)
1944         {
1945                 volatile PGPROC *proc = arrayP->procs[index];
1946
1947                 /*
1948                  * Since we're not holding a lock, need to check that the pointer is
1949                  * valid. Someone holding the lock could have incremented numProcs
1950                  * already, but not yet inserted a valid pointer to the array.
1951                  *
1952                  * If someone just decremented numProcs, 'proc' could also point to a
1953                  * PGPROC entry that's no longer in the array. It still points to a
1954                  * PGPROC struct, though, because freed PGPPROC entries just go to the
1955                  * free list and are recycled. Its contents are nonsense in that case,
1956                  * but that's acceptable for this function.
1957                  */
1958                 if (proc == NULL)
1959                         continue;
1960
1961                 if (proc == MyProc)
1962                         continue;                       /* do not count myself */
1963                 if (proc->pid == 0)
1964                         continue;                       /* do not count prepared xacts */
1965                 if (proc->xid == InvalidTransactionId)
1966                         continue;                       /* do not count if no XID assigned */
1967                 if (proc->waitLock != NULL)
1968                         continue;                       /* do not count if blocked on a lock */
1969                 count++;
1970                 if (count >= min)
1971                         break;
1972         }
1973
1974         return count >= min;
1975 }
1976
1977 /*
1978  * CountDBBackends --- count backends that are using specified database
1979  */
1980 int
1981 CountDBBackends(Oid databaseid)
1982 {
1983         ProcArrayStruct *arrayP = procArray;
1984         int                     count = 0;
1985         int                     index;
1986
1987         LWLockAcquire(ProcArrayLock, LW_SHARED);
1988
1989         for (index = 0; index < arrayP->numProcs; index++)
1990         {
1991                 volatile PGPROC *proc = arrayP->procs[index];
1992
1993                 if (proc->pid == 0)
1994                         continue;                       /* do not count prepared xacts */
1995                 if (!OidIsValid(databaseid) ||
1996                         proc->databaseId == databaseid)
1997                         count++;
1998         }
1999
2000         LWLockRelease(ProcArrayLock);
2001
2002         return count;
2003 }
2004
2005 /*
2006  * CancelDBBackends --- cancel backends that are using specified database
2007  */
2008 void
2009 CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
2010 {
2011         ProcArrayStruct *arrayP = procArray;
2012         int                     index;
2013         pid_t           pid = 0;
2014
2015         /* tell all backends to die */
2016         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2017
2018         for (index = 0; index < arrayP->numProcs; index++)
2019         {
2020                 volatile PGPROC *proc = arrayP->procs[index];
2021
2022                 if (databaseid == InvalidOid || proc->databaseId == databaseid)
2023                 {
2024                         VirtualTransactionId procvxid;
2025
2026                         GET_VXID_FROM_PGPROC(procvxid, *proc);
2027
2028                         proc->recoveryConflictPending = conflictPending;
2029                         pid = proc->pid;
2030                         if (pid != 0)
2031                         {
2032                                 /*
2033                                  * Kill the pid if it's still here. If not, that's what we
2034                                  * wanted so ignore any errors.
2035                                  */
2036                                 (void) SendProcSignal(pid, sigmode, procvxid.backendId);
2037                         }
2038                 }
2039         }
2040
2041         LWLockRelease(ProcArrayLock);
2042 }
2043
2044 /*
2045  * CountUserBackends --- count backends that are used by specified user
2046  */
2047 int
2048 CountUserBackends(Oid roleid)
2049 {
2050         ProcArrayStruct *arrayP = procArray;
2051         int                     count = 0;
2052         int                     index;
2053
2054         LWLockAcquire(ProcArrayLock, LW_SHARED);
2055
2056         for (index = 0; index < arrayP->numProcs; index++)
2057         {
2058                 volatile PGPROC *proc = arrayP->procs[index];
2059
2060                 if (proc->pid == 0)
2061                         continue;                       /* do not count prepared xacts */
2062                 if (proc->roleId == roleid)
2063                         count++;
2064         }
2065
2066         LWLockRelease(ProcArrayLock);
2067
2068         return count;
2069 }
2070
2071 /*
2072  * CountOtherDBBackends -- check for other backends running in the given DB
2073  *
2074  * If there are other backends in the DB, we will wait a maximum of 5 seconds
2075  * for them to exit.  Autovacuum backends are encouraged to exit early by
2076  * sending them SIGTERM, but normal user backends are just waited for.
2077  *
2078  * The current backend is always ignored; it is caller's responsibility to
2079  * check whether the current backend uses the given DB, if it's important.
2080  *
2081  * Returns TRUE if there are (still) other backends in the DB, FALSE if not.
2082  * Also, *nbackends and *nprepared are set to the number of other backends
2083  * and prepared transactions in the DB, respectively.
2084  *
2085  * This function is used to interlock DROP DATABASE and related commands
2086  * against there being any active backends in the target DB --- dropping the
2087  * DB while active backends remain would be a Bad Thing.  Note that we cannot
2088  * detect here the possibility of a newly-started backend that is trying to
2089  * connect to the doomed database, so additional interlocking is needed during
2090  * backend startup.  The caller should normally hold an exclusive lock on the
2091  * target DB before calling this, which is one reason we mustn't wait
2092  * indefinitely.
2093  */
2094 bool
2095 CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
2096 {
2097         ProcArrayStruct *arrayP = procArray;
2098
2099 #define MAXAUTOVACPIDS  10              /* max autovacs to SIGTERM per iteration */
2100         int                     autovac_pids[MAXAUTOVACPIDS];
2101         int                     tries;
2102
2103         /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
2104         for (tries = 0; tries < 50; tries++)
2105         {
2106                 int                     nautovacs = 0;
2107                 bool            found = false;
2108                 int                     index;
2109
2110                 CHECK_FOR_INTERRUPTS();
2111
2112                 *nbackends = *nprepared = 0;
2113
2114                 LWLockAcquire(ProcArrayLock, LW_SHARED);
2115
2116                 for (index = 0; index < arrayP->numProcs; index++)
2117                 {
2118                         volatile PGPROC *proc = arrayP->procs[index];
2119
2120                         if (proc->databaseId != databaseId)
2121                                 continue;
2122                         if (proc == MyProc)
2123                                 continue;
2124
2125                         found = true;
2126
2127                         if (proc->pid == 0)
2128                                 (*nprepared)++;
2129                         else
2130                         {
2131                                 (*nbackends)++;
2132                                 if ((proc->vacuumFlags & PROC_IS_AUTOVACUUM) &&
2133                                         nautovacs < MAXAUTOVACPIDS)
2134                                         autovac_pids[nautovacs++] = proc->pid;
2135                         }
2136                 }
2137
2138                 LWLockRelease(ProcArrayLock);
2139
2140                 if (!found)
2141                         return false;           /* no conflicting backends, so done */
2142
2143                 /*
2144                  * Send SIGTERM to any conflicting autovacuums before sleeping. We
2145                  * postpone this step until after the loop because we don't want to
2146                  * hold ProcArrayLock while issuing kill(). We have no idea what might
2147                  * block kill() inside the kernel...
2148                  */
2149                 for (index = 0; index < nautovacs; index++)
2150                         (void) kill(autovac_pids[index], SIGTERM);      /* ignore any error */
2151
2152                 /* sleep, then try again */
2153                 pg_usleep(100 * 1000L); /* 100ms */
2154         }
2155
2156         return true;                            /* timed out, still conflicts */
2157 }
2158
2159
2160 #define XidCacheRemove(i) \
2161         do { \
2162                 MyProc->subxids.xids[i] = MyProc->subxids.xids[MyProc->subxids.nxids - 1]; \
2163                 MyProc->subxids.nxids--; \
2164         } while (0)
2165
2166 /*
2167  * XidCacheRemoveRunningXids
2168  *
2169  * Remove a bunch of TransactionIds from the list of known-running
2170  * subtransactions for my backend.      Both the specified xid and those in
2171  * the xids[] array (of length nxids) are removed from the subxids cache.
2172  * latestXid must be the latest XID among the group.
2173  */
2174 void
2175 XidCacheRemoveRunningXids(TransactionId xid,
2176                                                   int nxids, const TransactionId *xids,
2177                                                   TransactionId latestXid)
2178 {
2179         int                     i,
2180                                 j;
2181
2182         Assert(TransactionIdIsValid(xid));
2183
2184         /*
2185          * We must hold ProcArrayLock exclusively in order to remove transactions
2186          * from the PGPROC array.  (See src/backend/access/transam/README.)  It's
2187          * possible this could be relaxed since we know this routine is only used
2188          * to abort subtransactions, but pending closer analysis we'd best be
2189          * conservative.
2190          */
2191         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2192
2193         /*
2194          * Under normal circumstances xid and xids[] will be in increasing order,
2195          * as will be the entries in subxids.  Scan backwards to avoid O(N^2)
2196          * behavior when removing a lot of xids.
2197          */
2198         for (i = nxids - 1; i >= 0; i--)
2199         {
2200                 TransactionId anxid = xids[i];
2201
2202                 for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
2203                 {
2204                         if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
2205                         {
2206                                 XidCacheRemove(j);
2207                                 break;
2208                         }
2209                 }
2210
2211                 /*
2212                  * Ordinarily we should have found it, unless the cache has
2213                  * overflowed. However it's also possible for this routine to be
2214                  * invoked multiple times for the same subtransaction, in case of an
2215                  * error during AbortSubTransaction.  So instead of Assert, emit a
2216                  * debug warning.
2217                  */
2218                 if (j < 0 && !MyProc->subxids.overflowed)
2219                         elog(WARNING, "did not find subXID %u in MyProc", anxid);
2220         }
2221
2222         for (j = MyProc->subxids.nxids - 1; j >= 0; j--)
2223         {
2224                 if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
2225                 {
2226                         XidCacheRemove(j);
2227                         break;
2228                 }
2229         }
2230         /* Ordinarily we should have found it, unless the cache has overflowed */
2231         if (j < 0 && !MyProc->subxids.overflowed)
2232                 elog(WARNING, "did not find subXID %u in MyProc", xid);
2233
2234         /* Also advance global latestCompletedXid while holding the lock */
2235         if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
2236                                                           latestXid))
2237                 ShmemVariableCache->latestCompletedXid = latestXid;
2238
2239         LWLockRelease(ProcArrayLock);
2240 }
2241
2242 #ifdef XIDCACHE_DEBUG
2243
2244 /*
2245  * Print stats about effectiveness of XID cache
2246  */
2247 static void
2248 DisplayXidCache(void)
2249 {
2250         fprintf(stderr,
2251                         "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, knownassigned: %ld, nooflo: %ld, slow: %ld\n",
2252                         xc_by_recent_xmin,
2253                         xc_by_known_xact,
2254                         xc_by_my_xact,
2255                         xc_by_latest_xid,
2256                         xc_by_main_xid,
2257                         xc_by_child_xid,
2258                         xc_by_known_assigned,
2259                         xc_no_overflow,
2260                         xc_slow_answer);
2261 }
2262 #endif   /* XIDCACHE_DEBUG */
2263
2264
2265 /* ----------------------------------------------
2266  *              KnownAssignedTransactions sub-module
2267  * ----------------------------------------------
2268  */
2269
2270 /*
2271  * In Hot Standby mode, we maintain a list of transactions that are (or were)
2272  * running in the master at the current point in WAL.  These XIDs must be
2273  * treated as running by standby transactions, even though they are not in
2274  * the standby server's PGPROC array.
2275  *
2276  * We record all XIDs that we know have been assigned.  That includes all the
2277  * XIDs seen in WAL records, plus all unobserved XIDs that we can deduce have
2278  * been assigned.  We can deduce the existence of unobserved XIDs because we
2279  * know XIDs are assigned in sequence, with no gaps.  The KnownAssignedXids
2280  * list expands as new XIDs are observed or inferred, and contracts when
2281  * transaction completion records arrive.
2282  *
2283  * During hot standby we do not fret too much about the distinction between
2284  * top-level XIDs and subtransaction XIDs. We store both together in the
2285  * KnownAssignedXids list.      In backends, this is copied into snapshots in
2286  * GetSnapshotData(), taking advantage of the fact that XidInMVCCSnapshot()
2287  * doesn't care about the distinction either.  Subtransaction XIDs are
2288  * effectively treated as top-level XIDs and in the typical case pg_subtrans
2289  * links are *not* maintained (which does not affect visibility).
2290  *
2291  * We have room in KnownAssignedXids and in snapshots to hold maxProcs *
2292  * (1 + PGPROC_MAX_CACHED_SUBXIDS) XIDs, so every master transaction must
2293  * report its subtransaction XIDs in a WAL XLOG_XACT_ASSIGNMENT record at
2294  * least every PGPROC_MAX_CACHED_SUBXIDS.  When we receive one of these
2295  * records, we mark the subXIDs as children of the top XID in pg_subtrans,
2296  * and then remove them from KnownAssignedXids.  This prevents overflow of
2297  * KnownAssignedXids and snapshots, at the cost that status checks for these
2298  * subXIDs will take a slower path through TransactionIdIsInProgress().
2299  * This means that KnownAssignedXids is not necessarily complete for subXIDs,
2300  * though it should be complete for top-level XIDs; this is the same situation
2301  * that holds with respect to the PGPROC entries in normal running.
2302  *
2303  * When we throw away subXIDs from KnownAssignedXids, we need to keep track of
2304  * that, similarly to tracking overflow of a PGPROC's subxids array.  We do
2305  * that by remembering the lastOverflowedXID, ie the last thrown-away subXID.
2306  * As long as that is within the range of interesting XIDs, we have to assume
2307  * that subXIDs are missing from snapshots.  (Note that subXID overflow occurs
2308  * on primary when 65th subXID arrives, whereas on standby it occurs when 64th
2309  * subXID arrives - that is not an error.)
2310  *
2311  * Should a backend on primary somehow disappear before it can write an abort
2312  * record, then we just leave those XIDs in KnownAssignedXids. They actually
2313  * aborted but we think they were running; the distinction is irrelevant
2314  * because either way any changes done by the transaction are not visible to
2315  * backends in the standby.  We prune KnownAssignedXids when
2316  * XLOG_RUNNING_XACTS arrives, to forestall possible overflow of the
2317  * array due to such dead XIDs.
2318  */
2319
2320 /*
2321  * RecordKnownAssignedTransactionIds
2322  *              Record the given XID in KnownAssignedXids, as well as any preceding
2323  *              unobserved XIDs.
2324  *
2325  * RecordKnownAssignedTransactionIds() should be run for *every* WAL record
2326  * associated with a transaction. Must be called for each record after we
2327  * have executed StartupCLOG() et al, since we must ExtendCLOG() etc..
2328  *
2329  * Called during recovery in analogy with and in place of GetNewTransactionId()
2330  */
2331 void
2332 RecordKnownAssignedTransactionIds(TransactionId xid)
2333 {
2334         Assert(standbyState >= STANDBY_INITIALIZED);
2335         Assert(TransactionIdIsValid(xid));
2336
2337         elog(trace_recovery(DEBUG4), "record known xact %u latestObservedXid %u",
2338                  xid, latestObservedXid);
2339
2340         /*
2341          * If the KnownAssignedXids machinery isn't up yet, do nothing.
2342          */
2343         if (standbyState <= STANDBY_INITIALIZED)
2344                 return;
2345
2346         Assert(TransactionIdIsValid(latestObservedXid));
2347
2348         /*
2349          * When a newly observed xid arrives, it is frequently the case that it is
2350          * *not* the next xid in sequence. When this occurs, we must treat the
2351          * intervening xids as running also.
2352          */
2353         if (TransactionIdFollows(xid, latestObservedXid))
2354         {
2355                 TransactionId next_expected_xid;
2356
2357                 /*
2358                  * Extend clog and subtrans like we do in GetNewTransactionId() during
2359                  * normal operation using individual extend steps. Typical case
2360                  * requires almost no activity.
2361                  */
2362                 next_expected_xid = latestObservedXid;
2363                 TransactionIdAdvance(next_expected_xid);
2364                 while (TransactionIdPrecedesOrEquals(next_expected_xid, xid))
2365                 {
2366                         ExtendCLOG(next_expected_xid);
2367                         ExtendSUBTRANS(next_expected_xid);
2368
2369                         TransactionIdAdvance(next_expected_xid);
2370                 }
2371
2372                 /*
2373                  * Add the new xids onto the KnownAssignedXids array.
2374                  */
2375                 next_expected_xid = latestObservedXid;
2376                 TransactionIdAdvance(next_expected_xid);
2377                 KnownAssignedXidsAdd(next_expected_xid, xid, false);
2378
2379                 /*
2380                  * Now we can advance latestObservedXid
2381                  */
2382                 latestObservedXid = xid;
2383
2384                 /* ShmemVariableCache->nextXid must be beyond any observed xid */
2385                 next_expected_xid = latestObservedXid;
2386                 TransactionIdAdvance(next_expected_xid);
2387                 ShmemVariableCache->nextXid = next_expected_xid;
2388         }
2389 }
2390
2391 /*
2392  * ExpireTreeKnownAssignedTransactionIds
2393  *              Remove the given XIDs from KnownAssignedXids.
2394  *
2395  * Called during recovery in analogy with and in place of ProcArrayEndTransaction()
2396  */
2397 void
2398 ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids,
2399                                                            TransactionId *subxids, TransactionId max_xid)
2400 {
2401         Assert(standbyState >= STANDBY_INITIALIZED);
2402
2403         /*
2404          * Uses same locking as transaction commit
2405          */
2406         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2407
2408         KnownAssignedXidsRemoveTree(xid, nsubxids, subxids);
2409
2410         /* As in ProcArrayEndTransaction, advance latestCompletedXid */
2411         if (TransactionIdPrecedes(ShmemVariableCache->latestCompletedXid,
2412                                                           max_xid))
2413                 ShmemVariableCache->latestCompletedXid = max_xid;
2414
2415         LWLockRelease(ProcArrayLock);
2416 }
2417
2418 /*
2419  * ExpireAllKnownAssignedTransactionIds
2420  *              Remove all entries in KnownAssignedXids
2421  */
2422 void
2423 ExpireAllKnownAssignedTransactionIds(void)
2424 {
2425         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2426         KnownAssignedXidsRemovePreceding(InvalidTransactionId);
2427         LWLockRelease(ProcArrayLock);
2428 }
2429
2430 /*
2431  * ExpireOldKnownAssignedTransactionIds
2432  *              Remove KnownAssignedXids entries preceding the given XID
2433  */
2434 void
2435 ExpireOldKnownAssignedTransactionIds(TransactionId xid)
2436 {
2437         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2438         KnownAssignedXidsRemovePreceding(xid);
2439         LWLockRelease(ProcArrayLock);
2440 }
2441
2442
2443 /*
2444  * Private module functions to manipulate KnownAssignedXids
2445  *
2446  * There are 5 main uses of the KnownAssignedXids data structure:
2447  *
2448  *      * backends taking snapshots - all valid XIDs need to be copied out
2449  *      * backends seeking to determine presence of a specific XID
2450  *      * startup process adding new known-assigned XIDs
2451  *      * startup process removing specific XIDs as transactions end
2452  *      * startup process pruning array when special WAL records arrive
2453  *
2454  * This data structure is known to be a hot spot during Hot Standby, so we
2455  * go to some lengths to make these operations as efficient and as concurrent
2456  * as possible.
2457  *
2458  * The XIDs are stored in an array in sorted order --- TransactionIdPrecedes
2459  * order, to be exact --- to allow binary search for specific XIDs.  Note:
2460  * in general TransactionIdPrecedes would not provide a total order, but
2461  * we know that the entries present at any instant should not extend across
2462  * a large enough fraction of XID space to wrap around (the master would
2463  * shut down for fear of XID wrap long before that happens).  So it's OK to
2464  * use TransactionIdPrecedes as a binary-search comparator.
2465  *
2466  * It's cheap to maintain the sortedness during insertions, since new known
2467  * XIDs are always reported in XID order; we just append them at the right.
2468  *
2469  * To keep individual deletions cheap, we need to allow gaps in the array.
2470  * This is implemented by marking array elements as valid or invalid using
2471  * the parallel boolean array KnownAssignedXidsValid[].  A deletion is done
2472  * by setting KnownAssignedXidsValid[i] to false, *without* clearing the
2473  * XID entry itself.  This preserves the property that the XID entries are
2474  * sorted, so we can do binary searches easily.  Periodically we compress
2475  * out the unused entries; that's much cheaper than having to compress the
2476  * array immediately on every deletion.
2477  *
2478  * The actually valid items in KnownAssignedXids[] and KnownAssignedXidsValid[]
2479  * are those with indexes tail <= i < head; items outside this subscript range
2480  * have unspecified contents.  When head reaches the end of the array, we
2481  * force compression of unused entries rather than wrapping around, since
2482  * allowing wraparound would greatly complicate the search logic.  We maintain
2483  * an explicit tail pointer so that pruning of old XIDs can be done without
2484  * immediately moving the array contents.  In most cases only a small fraction
2485  * of the array contains valid entries at any instant.
2486  *
2487  * Although only the startup process can ever change the KnownAssignedXids
2488  * data structure, we still need interlocking so that standby backends will
2489  * not observe invalid intermediate states.  The convention is that backends
2490  * must hold shared ProcArrayLock to examine the array.  To remove XIDs from
2491  * the array, the startup process must hold ProcArrayLock exclusively, for
2492  * the usual transactional reasons (compare commit/abort of a transaction
2493  * during normal running).      Compressing unused entries out of the array
2494  * likewise requires exclusive lock.  To add XIDs to the array, we just insert
2495  * them into slots to the right of the head pointer and then advance the head
2496  * pointer.  This wouldn't require any lock at all, except that on machines
2497  * with weak memory ordering we need to be careful that other processors
2498  * see the array element changes before they see the head pointer change.
2499  * We handle this by using a spinlock to protect reads and writes of the
2500  * head/tail pointers.  (We could dispense with the spinlock if we were to
2501  * create suitable memory access barrier primitives and use those instead.)
2502  * The spinlock must be taken to read or write the head/tail pointers unless
2503  * the caller holds ProcArrayLock exclusively.
2504  *
2505  * Algorithmic analysis:
2506  *
2507  * If we have a maximum of M slots, with N XIDs currently spread across
2508  * S elements then we have N <= S <= M always.
2509  *
2510  *      * Adding a new XID is O(1) and needs little locking (unless compression
2511  *              must happen)
2512  *      * Compressing the array is O(S) and requires exclusive lock
2513  *      * Removing an XID is O(logS) and requires exclusive lock
2514  *      * Taking a snapshot is O(S) and requires shared lock
2515  *      * Checking for an XID is O(logS) and requires shared lock
2516  *
2517  * In comparison, using a hash table for KnownAssignedXids would mean that
2518  * taking snapshots would be O(M). If we can maintain S << M then the
2519  * sorted array technique will deliver significantly faster snapshots.
2520  * If we try to keep S too small then we will spend too much time compressing,
2521  * so there is an optimal point for any workload mix. We use a heuristic to
2522  * decide when to compress the array, though trimming also helps reduce
2523  * frequency of compressing. The heuristic requires us to track the number of
2524  * currently valid XIDs in the array.
2525  */
2526
2527
2528 /*
2529  * Compress KnownAssignedXids by shifting valid data down to the start of the
2530  * array, removing any gaps.
2531  *
2532  * A compression step is forced if "force" is true, otherwise we do it
2533  * only if a heuristic indicates it's a good time to do it.
2534  *
2535  * Caller must hold ProcArrayLock in exclusive mode.
2536  */
2537 static void
2538 KnownAssignedXidsCompress(bool force)
2539 {
2540         /* use volatile pointer to prevent code rearrangement */
2541         volatile ProcArrayStruct *pArray = procArray;
2542         int                     head,
2543                                 tail;
2544         int                     compress_index;
2545         int                     i;
2546
2547         /* no spinlock required since we hold ProcArrayLock exclusively */
2548         head = pArray->headKnownAssignedXids;
2549         tail = pArray->tailKnownAssignedXids;
2550
2551         if (!force)
2552         {
2553                 /*
2554                  * If we can choose how much to compress, use a heuristic to avoid
2555                  * compressing too often or not often enough.
2556                  *
2557                  * Heuristic is if we have a large enough current spread and less than
2558                  * 50% of the elements are currently in use, then compress. This
2559                  * should ensure we compress fairly infrequently. We could compress
2560                  * less often though the virtual array would spread out more and
2561                  * snapshots would become more expensive.
2562                  */
2563                 int                     nelements = head - tail;
2564
2565                 if (nelements < 4 * PROCARRAY_MAXPROCS ||
2566                         nelements < 2 * pArray->numKnownAssignedXids)
2567                         return;
2568         }
2569
2570         /*
2571          * We compress the array by reading the valid values from tail to head,
2572          * re-aligning data to 0th element.
2573          */
2574         compress_index = 0;
2575         for (i = tail; i < head; i++)
2576         {
2577                 if (KnownAssignedXidsValid[i])
2578                 {
2579                         KnownAssignedXids[compress_index] = KnownAssignedXids[i];
2580                         KnownAssignedXidsValid[compress_index] = true;
2581                         compress_index++;
2582                 }
2583         }
2584
2585         pArray->tailKnownAssignedXids = 0;
2586         pArray->headKnownAssignedXids = compress_index;
2587 }
2588
2589 /*
2590  * Add xids into KnownAssignedXids at the head of the array.
2591  *
2592  * xids from from_xid to to_xid, inclusive, are added to the array.
2593  *
2594  * If exclusive_lock is true then caller already holds ProcArrayLock in
2595  * exclusive mode, so we need no extra locking here.  Else caller holds no
2596  * lock, so we need to be sure we maintain sufficient interlocks against
2597  * concurrent readers.  (Only the startup process ever calls this, so no need
2598  * to worry about concurrent writers.)
2599  */
2600 static void
2601 KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
2602                                          bool exclusive_lock)
2603 {
2604         /* use volatile pointer to prevent code rearrangement */
2605         volatile ProcArrayStruct *pArray = procArray;
2606         TransactionId next_xid;
2607         int                     head,
2608                                 tail;
2609         int                     nxids;
2610         int                     i;
2611
2612         Assert(TransactionIdPrecedesOrEquals(from_xid, to_xid));
2613
2614         /*
2615          * Calculate how many array slots we'll need.  Normally this is cheap; in
2616          * the unusual case where the XIDs cross the wrap point, we do it the hard
2617          * way.
2618          */
2619         if (to_xid >= from_xid)
2620                 nxids = to_xid - from_xid + 1;
2621         else
2622         {
2623                 nxids = 1;
2624                 next_xid = from_xid;
2625                 while (TransactionIdPrecedes(next_xid, to_xid))
2626                 {
2627                         nxids++;
2628                         TransactionIdAdvance(next_xid);
2629                 }
2630         }
2631
2632         /*
2633          * Since only the startup process modifies the head/tail pointers, we
2634          * don't need a lock to read them here.
2635          */
2636         head = pArray->headKnownAssignedXids;
2637         tail = pArray->tailKnownAssignedXids;
2638
2639         Assert(head >= 0 && head <= pArray->maxKnownAssignedXids);
2640         Assert(tail >= 0 && tail < pArray->maxKnownAssignedXids);
2641
2642         /*
2643          * Verify that insertions occur in TransactionId sequence.      Note that even
2644          * if the last existing element is marked invalid, it must still have a
2645          * correctly sequenced XID value.
2646          */
2647         if (head > tail &&
2648                 TransactionIdFollowsOrEquals(KnownAssignedXids[head - 1], from_xid))
2649         {
2650                 KnownAssignedXidsDisplay(LOG);
2651                 elog(ERROR, "out-of-order XID insertion in KnownAssignedXids");
2652         }
2653
2654         /*
2655          * If our xids won't fit in the remaining space, compress out free space
2656          */
2657         if (head + nxids > pArray->maxKnownAssignedXids)
2658         {
2659                 /* must hold lock to compress */
2660                 if (!exclusive_lock)
2661                         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2662
2663                 KnownAssignedXidsCompress(true);
2664
2665                 head = pArray->headKnownAssignedXids;
2666                 /* note: we no longer care about the tail pointer */
2667
2668                 if (!exclusive_lock)
2669                         LWLockRelease(ProcArrayLock);
2670
2671                 /*
2672                  * If it still won't fit then we're out of memory
2673                  */
2674                 if (head + nxids > pArray->maxKnownAssignedXids)
2675                         elog(ERROR, "too many KnownAssignedXids");
2676         }
2677
2678         /* Now we can insert the xids into the space starting at head */
2679         next_xid = from_xid;
2680         for (i = 0; i < nxids; i++)
2681         {
2682                 KnownAssignedXids[head] = next_xid;
2683                 KnownAssignedXidsValid[head] = true;
2684                 TransactionIdAdvance(next_xid);
2685                 head++;
2686         }
2687
2688         /* Adjust count of number of valid entries */
2689         pArray->numKnownAssignedXids += nxids;
2690
2691         /*
2692          * Now update the head pointer.  We use a spinlock to protect this
2693          * pointer, not because the update is likely to be non-atomic, but to
2694          * ensure that other processors see the above array updates before they
2695          * see the head pointer change.
2696          *
2697          * If we're holding ProcArrayLock exclusively, there's no need to take the
2698          * spinlock.
2699          */
2700         if (exclusive_lock)
2701                 pArray->headKnownAssignedXids = head;
2702         else
2703         {
2704                 SpinLockAcquire(&pArray->known_assigned_xids_lck);
2705                 pArray->headKnownAssignedXids = head;
2706                 SpinLockRelease(&pArray->known_assigned_xids_lck);
2707         }
2708 }
2709
2710 /*
2711  * KnownAssignedXidsSearch
2712  *
2713  * Searches KnownAssignedXids for a specific xid and optionally removes it.
2714  * Returns true if it was found, false if not.
2715  *
2716  * Caller must hold ProcArrayLock in shared or exclusive mode.
2717  * Exclusive lock must be held for remove = true.
2718  */
2719 static bool
2720 KnownAssignedXidsSearch(TransactionId xid, bool remove)
2721 {
2722         /* use volatile pointer to prevent code rearrangement */
2723         volatile ProcArrayStruct *pArray = procArray;
2724         int                     first,
2725                                 last;
2726         int                     head;
2727         int                     tail;
2728         int                     result_index = -1;
2729
2730         if (remove)
2731         {
2732                 /* we hold ProcArrayLock exclusively, so no need for spinlock */
2733                 tail = pArray->tailKnownAssignedXids;
2734                 head = pArray->headKnownAssignedXids;
2735         }
2736         else
2737         {
2738                 /* take spinlock to ensure we see up-to-date array contents */
2739                 SpinLockAcquire(&pArray->known_assigned_xids_lck);
2740                 tail = pArray->tailKnownAssignedXids;
2741                 head = pArray->headKnownAssignedXids;
2742                 SpinLockRelease(&pArray->known_assigned_xids_lck);
2743         }
2744
2745         /*
2746          * Standard binary search.      Note we can ignore the KnownAssignedXidsValid
2747          * array here, since even invalid entries will contain sorted XIDs.
2748          */
2749         first = tail;
2750         last = head - 1;
2751         while (first <= last)
2752         {
2753                 int                     mid_index;
2754                 TransactionId mid_xid;
2755
2756                 mid_index = (first + last) / 2;
2757                 mid_xid = KnownAssignedXids[mid_index];
2758
2759                 if (xid == mid_xid)
2760                 {
2761                         result_index = mid_index;
2762                         break;
2763                 }
2764                 else if (TransactionIdPrecedes(xid, mid_xid))
2765                         last = mid_index - 1;
2766                 else
2767                         first = mid_index + 1;
2768         }
2769
2770         if (result_index < 0)
2771                 return false;                   /* not in array */
2772
2773         if (!KnownAssignedXidsValid[result_index])
2774                 return false;                   /* in array, but invalid */
2775
2776         if (remove)
2777         {
2778                 KnownAssignedXidsValid[result_index] = false;
2779
2780                 pArray->numKnownAssignedXids--;
2781                 Assert(pArray->numKnownAssignedXids >= 0);
2782
2783                 /*
2784                  * If we're removing the tail element then advance tail pointer over
2785                  * any invalid elements.  This will speed future searches.
2786                  */
2787                 if (result_index == tail)
2788                 {
2789                         tail++;
2790                         while (tail < head && !KnownAssignedXidsValid[tail])
2791                                 tail++;
2792                         if (tail >= head)
2793                         {
2794                                 /* Array is empty, so we can reset both pointers */
2795                                 pArray->headKnownAssignedXids = 0;
2796                                 pArray->tailKnownAssignedXids = 0;
2797                         }
2798                         else
2799                         {
2800                                 pArray->tailKnownAssignedXids = tail;
2801                         }
2802                 }
2803         }
2804
2805         return true;
2806 }
2807
2808 /*
2809  * Is the specified XID present in KnownAssignedXids[]?
2810  *
2811  * Caller must hold ProcArrayLock in shared or exclusive mode.
2812  */
2813 static bool
2814 KnownAssignedXidExists(TransactionId xid)
2815 {
2816         Assert(TransactionIdIsValid(xid));
2817
2818         return KnownAssignedXidsSearch(xid, false);
2819 }
2820
2821 /*
2822  * Remove the specified XID from KnownAssignedXids[].
2823  *
2824  * Caller must hold ProcArrayLock in exclusive mode.
2825  */
2826 static void
2827 KnownAssignedXidsRemove(TransactionId xid)
2828 {
2829         Assert(TransactionIdIsValid(xid));
2830
2831         elog(trace_recovery(DEBUG4), "remove KnownAssignedXid %u", xid);
2832
2833         /*
2834          * Note: we cannot consider it an error to remove an XID that's not
2835          * present.  We intentionally remove subxact IDs while processing
2836          * XLOG_XACT_ASSIGNMENT, to avoid array overflow.  Then those XIDs will be
2837          * removed again when the top-level xact commits or aborts.
2838          *
2839          * It might be possible to track such XIDs to distinguish this case from
2840          * actual errors, but it would be complicated and probably not worth it.
2841          * So, just ignore the search result.
2842          */
2843         (void) KnownAssignedXidsSearch(xid, true);
2844 }
2845
2846 /*
2847  * KnownAssignedXidsRemoveTree
2848  *              Remove xid (if it's not InvalidTransactionId) and all the subxids.
2849  *
2850  * Caller must hold ProcArrayLock in exclusive mode.
2851  */
2852 static void
2853 KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
2854                                                         TransactionId *subxids)
2855 {
2856         int                     i;
2857
2858         if (TransactionIdIsValid(xid))
2859                 KnownAssignedXidsRemove(xid);
2860
2861         for (i = 0; i < nsubxids; i++)
2862                 KnownAssignedXidsRemove(subxids[i]);
2863
2864         /* Opportunistically compress the array */
2865         KnownAssignedXidsCompress(false);
2866 }
2867
2868 /*
2869  * Prune KnownAssignedXids up to, but *not* including xid. If xid is invalid
2870  * then clear the whole table.
2871  *
2872  * Caller must hold ProcArrayLock in exclusive mode.
2873  */
2874 static void
2875 KnownAssignedXidsRemovePreceding(TransactionId removeXid)
2876 {
2877         /* use volatile pointer to prevent code rearrangement */
2878         volatile ProcArrayStruct *pArray = procArray;
2879         int                     count = 0;
2880         int                     head,
2881                                 tail,
2882                                 i;
2883
2884         if (!TransactionIdIsValid(removeXid))
2885         {
2886                 elog(trace_recovery(DEBUG4), "removing all KnownAssignedXids");
2887                 pArray->numKnownAssignedXids = 0;
2888                 pArray->headKnownAssignedXids = pArray->tailKnownAssignedXids = 0;
2889                 return;
2890         }
2891
2892         elog(trace_recovery(DEBUG4), "prune KnownAssignedXids to %u", removeXid);
2893
2894         /*
2895          * Mark entries invalid starting at the tail.  Since array is sorted, we
2896          * can stop as soon as we reach a entry >= removeXid.
2897          */
2898         tail = pArray->tailKnownAssignedXids;
2899         head = pArray->headKnownAssignedXids;
2900
2901         for (i = tail; i < head; i++)
2902         {
2903                 if (KnownAssignedXidsValid[i])
2904                 {
2905                         TransactionId knownXid = KnownAssignedXids[i];
2906
2907                         if (TransactionIdFollowsOrEquals(knownXid, removeXid))
2908                                 break;
2909
2910                         if (!StandbyTransactionIdIsPrepared(knownXid))
2911                         {
2912                                 KnownAssignedXidsValid[i] = false;
2913                                 count++;
2914                         }
2915                 }
2916         }
2917
2918         pArray->numKnownAssignedXids -= count;
2919         Assert(pArray->numKnownAssignedXids >= 0);
2920
2921         /*
2922          * Advance the tail pointer if we've marked the tail item invalid.
2923          */
2924         for (i = tail; i < head; i++)
2925         {
2926                 if (KnownAssignedXidsValid[i])
2927                         break;
2928         }
2929         if (i >= head)
2930         {
2931                 /* Array is empty, so we can reset both pointers */
2932                 pArray->headKnownAssignedXids = 0;
2933                 pArray->tailKnownAssignedXids = 0;
2934         }
2935         else
2936         {
2937                 pArray->tailKnownAssignedXids = i;
2938         }
2939
2940         /* Opportunistically compress the array */
2941         KnownAssignedXidsCompress(false);
2942 }
2943
2944 /*
2945  * KnownAssignedXidsGet - Get an array of xids by scanning KnownAssignedXids.
2946  * We filter out anything >= xmax.
2947  *
2948  * Returns the number of XIDs stored into xarray[].  Caller is responsible
2949  * that array is large enough.
2950  *
2951  * Caller must hold ProcArrayLock in (at least) shared mode.
2952  */
2953 static int
2954 KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax)
2955 {
2956         TransactionId xtmp = InvalidTransactionId;
2957
2958         return KnownAssignedXidsGetAndSetXmin(xarray, &xtmp, xmax);
2959 }
2960
2961 /*
2962  * KnownAssignedXidsGetAndSetXmin - as KnownAssignedXidsGet, plus
2963  * we reduce *xmin to the lowest xid value seen if not already lower.
2964  *
2965  * Caller must hold ProcArrayLock in (at least) shared mode.
2966  */
2967 static int
2968 KnownAssignedXidsGetAndSetXmin(TransactionId *xarray, TransactionId *xmin,
2969                                                            TransactionId xmax)
2970 {
2971         /* use volatile pointer to prevent code rearrangement */
2972         volatile ProcArrayStruct *pArray = procArray;
2973         int                     count = 0;
2974         int                     head,
2975                                 tail;
2976         int                     i;
2977
2978         /*
2979          * Fetch head just once, since it may change while we loop. We can stop
2980          * once we reach the initially seen head, since we are certain that an xid
2981          * cannot enter and then leave the array while we hold ProcArrayLock.  We
2982          * might miss newly-added xids, but they should be >= xmax so irrelevant
2983          * anyway.
2984          *
2985          * Must take spinlock to ensure we see up-to-date array contents.
2986          */
2987         SpinLockAcquire(&pArray->known_assigned_xids_lck);
2988         tail = pArray->tailKnownAssignedXids;
2989         head = pArray->headKnownAssignedXids;
2990         SpinLockRelease(&pArray->known_assigned_xids_lck);
2991
2992         for (i = tail; i < head; i++)
2993         {
2994                 /* Skip any gaps in the array */
2995                 if (KnownAssignedXidsValid[i])
2996                 {
2997                         TransactionId knownXid = KnownAssignedXids[i];
2998
2999                         /*
3000                          * Update xmin if required.  Only the first XID need be checked,
3001                          * since the array is sorted.
3002                          */
3003                         if (count == 0 &&
3004                                 TransactionIdPrecedes(knownXid, *xmin))
3005                                 *xmin = knownXid;
3006
3007                         /*
3008                          * Filter out anything >= xmax, again relying on sorted property
3009                          * of array.
3010                          */
3011                         if (TransactionIdIsValid(xmax) &&
3012                                 TransactionIdFollowsOrEquals(knownXid, xmax))
3013                                 break;
3014
3015                         /* Add knownXid into output array */
3016                         xarray[count++] = knownXid;
3017                 }
3018         }
3019
3020         return count;
3021 }
3022
3023 /*
3024  * Get oldest XID in the KnownAssignedXids array, or InvalidTransactionId
3025  * if nothing there.
3026  */
3027 static TransactionId
3028 KnownAssignedXidsGetOldestXmin(void)
3029 {
3030         /* use volatile pointer to prevent code rearrangement */
3031         volatile ProcArrayStruct *pArray = procArray;
3032         int                     head,
3033                                 tail;
3034         int                     i;
3035
3036         /*
3037          * Fetch head just once, since it may change while we loop.
3038          */
3039         SpinLockAcquire(&pArray->known_assigned_xids_lck);
3040         tail = pArray->tailKnownAssignedXids;
3041         head = pArray->headKnownAssignedXids;
3042         SpinLockRelease(&pArray->known_assigned_xids_lck);
3043
3044         for (i = tail; i < head; i++)
3045         {
3046                 /* Skip any gaps in the array */
3047                 if (KnownAssignedXidsValid[i])
3048                         return KnownAssignedXids[i];
3049         }
3050
3051         return InvalidTransactionId;
3052 }
3053
3054 /*
3055  * Display KnownAssignedXids to provide debug trail
3056  *
3057  * Currently this is only called within startup process, so we need no
3058  * special locking.
3059  *
3060  * Note this is pretty expensive, and much of the expense will be incurred
3061  * even if the elog message will get discarded.  It's not currently called
3062  * in any performance-critical places, however, so no need to be tenser.
3063  */
3064 static void
3065 KnownAssignedXidsDisplay(int trace_level)
3066 {
3067         /* use volatile pointer to prevent code rearrangement */
3068         volatile ProcArrayStruct *pArray = procArray;
3069         StringInfoData buf;
3070         int                     head,
3071                                 tail,
3072                                 i;
3073         int                     nxids = 0;
3074
3075         tail = pArray->tailKnownAssignedXids;
3076         head = pArray->headKnownAssignedXids;
3077
3078         initStringInfo(&buf);
3079
3080         for (i = tail; i < head; i++)
3081         {
3082                 if (KnownAssignedXidsValid[i])
3083                 {
3084                         nxids++;
3085                         appendStringInfo(&buf, "[%d]=%u ", i, KnownAssignedXids[i]);
3086                 }
3087         }
3088
3089         elog(trace_level, "%d KnownAssignedXids (num=%d tail=%d head=%d) %s",
3090                  nxids,
3091                  pArray->numKnownAssignedXids,
3092                  pArray->tailKnownAssignedXids,
3093                  pArray->headKnownAssignedXids,
3094                  buf.data);
3095
3096         pfree(buf.data);
3097 }