Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * procarray.c
4 : : * POSTGRES process array code.
5 : : *
6 : : *
7 : : * This module maintains arrays of PGPROC substructures, as well as associated
8 : : * arrays in ProcGlobal, for all active backends. Although there are several
9 : : * uses for this, the principal one is as a means of determining the set of
10 : : * currently running transactions.
11 : : *
12 : : * Because of various subtle race conditions it is critical that a backend
13 : : * hold the correct locks while setting or clearing its xid (in
14 : : * ProcGlobal->xids[]/MyProc->xid). See notes in
15 : : * src/backend/access/transam/README.
16 : : *
17 : : * The process arrays now also include structures representing prepared
18 : : * transactions. The xid and subxids fields of these are valid, as are the
19 : : * myProcLocks lists. They can be distinguished from regular backend PGPROCs
20 : : * at need by checking for pid == 0.
21 : : *
22 : : * During hot standby, we also keep a list of XIDs representing transactions
23 : : * that are known to be running on the primary (or more precisely, were running
24 : : * as of the current point in the WAL stream). This list is kept in the
25 : : * KnownAssignedXids array, and is updated by watching the sequence of
26 : : * arriving XIDs. This is necessary because if we leave those XIDs out of
27 : : * snapshots taken for standby queries, then they will appear to be already
28 : : * complete, leading to MVCC failures. Note that in hot standby, the PGPROC
29 : : * array represents standby processes, which by definition are not running
30 : : * transactions that have XIDs.
31 : : *
32 : : * It is perhaps possible for a backend on the primary to terminate without
33 : : * writing an abort record for its transaction. While that shouldn't really
34 : : * happen, it would tie up KnownAssignedXids indefinitely, so we protect
35 : : * ourselves by pruning the array when a valid list of running XIDs arrives.
36 : : *
37 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
38 : : * Portions Copyright (c) 1994, Regents of the University of California
39 : : *
40 : : *
41 : : * IDENTIFICATION
42 : : * src/backend/storage/ipc/procarray.c
43 : : *
44 : : *-------------------------------------------------------------------------
45 : : */
46 : : #include "postgres.h"
47 : :
48 : : #include <signal.h>
49 : :
50 : : #include "access/subtrans.h"
51 : : #include "access/transam.h"
52 : : #include "access/twophase.h"
53 : : #include "access/xact.h"
54 : : #include "access/xlogutils.h"
55 : : #include "catalog/catalog.h"
56 : : #include "catalog/pg_authid.h"
57 : : #include "miscadmin.h"
58 : : #include "pgstat.h"
59 : : #include "port/pg_lfind.h"
60 : : #include "storage/proc.h"
61 : : #include "storage/procarray.h"
62 : : #include "utils/acl.h"
63 : : #include "utils/builtins.h"
64 : : #include "utils/lsyscache.h"
65 : : #include "utils/rel.h"
66 : : #include "utils/snapmgr.h"
67 : :
68 : : #define UINT32_ACCESS_ONCE(var) ((uint32)(*((volatile uint32 *)&(var))))
69 : :
70 : : /* Our shared memory area */
71 : : typedef struct ProcArrayStruct
72 : : {
73 : : int numProcs; /* number of valid procs entries */
74 : : int maxProcs; /* allocated size of procs array */
75 : :
76 : : /*
77 : : * Known assigned XIDs handling
78 : : */
79 : : int maxKnownAssignedXids; /* allocated size of array */
80 : : int numKnownAssignedXids; /* current # of valid entries */
81 : : int tailKnownAssignedXids; /* index of oldest valid element */
82 : : int headKnownAssignedXids; /* index of newest element, + 1 */
83 : :
84 : : /*
85 : : * Highest subxid that has been removed from KnownAssignedXids array to
86 : : * prevent overflow; or InvalidTransactionId if none. We track this for
87 : : * similar reasons to tracking overflowing cached subxids in PGPROC
88 : : * entries. Must hold exclusive ProcArrayLock to change this, and shared
89 : : * lock to read it.
90 : : */
91 : : TransactionId lastOverflowedXid;
92 : :
93 : : /* oldest xmin of any replication slot */
94 : : TransactionId replication_slot_xmin;
95 : : /* oldest catalog xmin of any replication slot */
96 : : TransactionId replication_slot_catalog_xmin;
97 : :
98 : : /* indexes into allProcs[], has PROCARRAY_MAXPROCS entries */
99 : : int pgprocnos[FLEXIBLE_ARRAY_MEMBER];
100 : : } ProcArrayStruct;
101 : :
102 : : /*
103 : : * State for the GlobalVisTest* family of functions. Those functions can
104 : : * e.g. be used to decide if a deleted row can be removed without violating
105 : : * MVCC semantics: If the deleted row's xmax is not considered to be running
106 : : * by anyone, the row can be removed.
107 : : *
108 : : * To avoid slowing down GetSnapshotData(), we don't calculate a precise
109 : : * cutoff XID while building a snapshot (looking at the frequently changing
110 : : * xmins scales badly). Instead we compute two boundaries while building the
111 : : * snapshot:
112 : : *
113 : : * 1) definitely_needed, indicating that rows deleted by XIDs >=
114 : : * definitely_needed are definitely still visible.
115 : : *
116 : : * 2) maybe_needed, indicating that rows deleted by XIDs < maybe_needed can
117 : : * definitely be removed
118 : : *
119 : : * When testing an XID that falls in between the two (i.e. XID >= maybe_needed
120 : : * && XID < definitely_needed), the boundaries can be recomputed (using
121 : : * ComputeXidHorizons()) to get a more accurate answer. This is cheaper than
122 : : * maintaining an accurate value all the time.
123 : : *
124 : : * As it is not cheap to compute accurate boundaries, we limit the number of
125 : : * times that happens in short succession. See GlobalVisTestShouldUpdate().
126 : : *
127 : : *
128 : : * There are three backend lifetime instances of this struct, optimized for
129 : : * different types of relations. As e.g. a normal user defined table in one
130 : : * database is inaccessible to backends connected to another database, a test
131 : : * specific to a relation can be more aggressive than a test for a shared
132 : : * relation. Currently we track four different states:
133 : : *
134 : : * 1) GlobalVisSharedRels, which only considers an XID's
135 : : * effects visible-to-everyone if neither snapshots in any database, nor a
136 : : * replication slot's xmin, nor a replication slot's catalog_xmin might
137 : : * still consider XID as running.
138 : : *
139 : : * 2) GlobalVisCatalogRels, which only considers an XID's
140 : : * effects visible-to-everyone if neither snapshots in the current
141 : : * database, nor a replication slot's xmin, nor a replication slot's
142 : : * catalog_xmin might still consider XID as running.
143 : : *
144 : : * I.e. the difference to GlobalVisSharedRels is that
145 : : * snapshot in other databases are ignored.
146 : : *
147 : : * 3) GlobalVisDataRels, which only considers an XID's
148 : : * effects visible-to-everyone if neither snapshots in the current
149 : : * database, nor a replication slot's xmin consider XID as running.
150 : : *
151 : : * I.e. the difference to GlobalVisCatalogRels is that
152 : : * replication slot's catalog_xmin is not taken into account.
153 : : *
154 : : * 4) GlobalVisTempRels, which only considers the current session, as temp
155 : : * tables are not visible to other sessions.
156 : : *
157 : : * GlobalVisTestFor(relation) returns the appropriate state
158 : : * for the relation.
159 : : *
160 : : * The boundaries are FullTransactionIds instead of TransactionIds to avoid
161 : : * wraparound dangers. There e.g. would otherwise exist no procarray state to
162 : : * prevent maybe_needed to become old enough after the GetSnapshotData()
163 : : * call.
164 : : *
165 : : * The typedef is in the header.
166 : : */
167 : : struct GlobalVisState
168 : : {
169 : : /* XIDs >= are considered running by some backend */
170 : : FullTransactionId definitely_needed;
171 : :
172 : : /* XIDs < are not considered to be running by any backend */
173 : : FullTransactionId maybe_needed;
174 : : };
175 : :
176 : : /*
177 : : * Result of ComputeXidHorizons().
178 : : */
179 : : typedef struct ComputeXidHorizonsResult
180 : : {
181 : : /*
182 : : * The value of TransamVariables->latestCompletedXid when
183 : : * ComputeXidHorizons() held ProcArrayLock.
184 : : */
185 : : FullTransactionId latest_completed;
186 : :
187 : : /*
188 : : * The same for procArray->replication_slot_xmin and
189 : : * procArray->replication_slot_catalog_xmin.
190 : : */
191 : : TransactionId slot_xmin;
192 : : TransactionId slot_catalog_xmin;
193 : :
194 : : /*
195 : : * Oldest xid that any backend might still consider running. This needs to
196 : : * include processes running VACUUM, in contrast to the normal visibility
197 : : * cutoffs, as vacuum needs to be able to perform pg_subtrans lookups when
198 : : * determining visibility, but doesn't care about rows above its xmin to
199 : : * be removed.
200 : : *
201 : : * This likely should only be needed to determine whether pg_subtrans can
202 : : * be truncated. It currently includes the effects of replication slots,
203 : : * for historical reasons. But that could likely be changed.
204 : : */
205 : : TransactionId oldest_considered_running;
206 : :
207 : : /*
208 : : * Oldest xid for which deleted tuples need to be retained in shared
209 : : * tables.
210 : : *
211 : : * This includes the effects of replication slots. If that's not desired,
212 : : * look at shared_oldest_nonremovable_raw;
213 : : */
214 : : TransactionId shared_oldest_nonremovable;
215 : :
216 : : /*
217 : : * Oldest xid that may be necessary to retain in shared tables. This is
218 : : * the same as shared_oldest_nonremovable, except that is not affected by
219 : : * replication slot's catalog_xmin.
220 : : *
221 : : * This is mainly useful to be able to send the catalog_xmin to upstream
222 : : * streaming replication servers via hot_standby_feedback, so they can
223 : : * apply the limit only when accessing catalog tables.
224 : : */
225 : : TransactionId shared_oldest_nonremovable_raw;
226 : :
227 : : /*
228 : : * Oldest xid for which deleted tuples need to be retained in non-shared
229 : : * catalog tables.
230 : : */
231 : : TransactionId catalog_oldest_nonremovable;
232 : :
233 : : /*
234 : : * Oldest xid for which deleted tuples need to be retained in normal user
235 : : * defined tables.
236 : : */
237 : : TransactionId data_oldest_nonremovable;
238 : :
239 : : /*
240 : : * Oldest xid for which deleted tuples need to be retained in this
241 : : * session's temporary tables.
242 : : */
243 : : TransactionId temp_oldest_nonremovable;
244 : : } ComputeXidHorizonsResult;
245 : :
246 : : /*
247 : : * Return value for GlobalVisHorizonKindForRel().
248 : : */
249 : : typedef enum GlobalVisHorizonKind
250 : : {
251 : : VISHORIZON_SHARED,
252 : : VISHORIZON_CATALOG,
253 : : VISHORIZON_DATA,
254 : : VISHORIZON_TEMP,
255 : : } GlobalVisHorizonKind;
256 : :
257 : : /*
258 : : * Reason codes for KnownAssignedXidsCompress().
259 : : */
260 : : typedef enum KAXCompressReason
261 : : {
262 : : KAX_NO_SPACE, /* need to free up space at array end */
263 : : KAX_PRUNE, /* we just pruned old entries */
264 : : KAX_TRANSACTION_END, /* we just committed/removed some XIDs */
265 : : KAX_STARTUP_PROCESS_IDLE, /* startup process is about to sleep */
266 : : } KAXCompressReason;
267 : :
268 : :
269 : : static ProcArrayStruct *procArray;
270 : :
271 : : static PGPROC *allProcs;
272 : :
273 : : /*
274 : : * Cache to reduce overhead of repeated calls to TransactionIdIsInProgress()
275 : : */
276 : : static TransactionId cachedXidIsNotInProgress = InvalidTransactionId;
277 : :
278 : : /*
279 : : * Bookkeeping for tracking emulated transactions in recovery
280 : : */
281 : : static TransactionId *KnownAssignedXids;
282 : : static bool *KnownAssignedXidsValid;
283 : : static TransactionId latestObservedXid = InvalidTransactionId;
284 : :
285 : : /*
286 : : * If we're in STANDBY_SNAPSHOT_PENDING state, standbySnapshotPendingXmin is
287 : : * the highest xid that might still be running that we don't have in
288 : : * KnownAssignedXids.
289 : : */
290 : : static TransactionId standbySnapshotPendingXmin;
291 : :
292 : : /*
293 : : * State for visibility checks on different types of relations. See struct
294 : : * GlobalVisState for details. As shared, catalog, normal and temporary
295 : : * relations can have different horizons, one such state exists for each.
296 : : */
297 : : static GlobalVisState GlobalVisSharedRels;
298 : : static GlobalVisState GlobalVisCatalogRels;
299 : : static GlobalVisState GlobalVisDataRels;
300 : : static GlobalVisState GlobalVisTempRels;
301 : :
302 : : /*
303 : : * This backend's RecentXmin at the last time the accurate xmin horizon was
304 : : * recomputed, or InvalidTransactionId if it has not. Used to limit how many
305 : : * times accurate horizons are recomputed. See GlobalVisTestShouldUpdate().
306 : : */
307 : : static TransactionId ComputeXidHorizonsResultLastXmin;
308 : :
309 : : #ifdef XIDCACHE_DEBUG
310 : :
311 : : /* counters for XidCache measurement */
312 : : static long xc_by_recent_xmin = 0;
313 : : static long xc_by_known_xact = 0;
314 : : static long xc_by_my_xact = 0;
315 : : static long xc_by_latest_xid = 0;
316 : : static long xc_by_main_xid = 0;
317 : : static long xc_by_child_xid = 0;
318 : : static long xc_by_known_assigned = 0;
319 : : static long xc_no_overflow = 0;
320 : : static long xc_slow_answer = 0;
321 : :
322 : : #define xc_by_recent_xmin_inc() (xc_by_recent_xmin++)
323 : : #define xc_by_known_xact_inc() (xc_by_known_xact++)
324 : : #define xc_by_my_xact_inc() (xc_by_my_xact++)
325 : : #define xc_by_latest_xid_inc() (xc_by_latest_xid++)
326 : : #define xc_by_main_xid_inc() (xc_by_main_xid++)
327 : : #define xc_by_child_xid_inc() (xc_by_child_xid++)
328 : : #define xc_by_known_assigned_inc() (xc_by_known_assigned++)
329 : : #define xc_no_overflow_inc() (xc_no_overflow++)
330 : : #define xc_slow_answer_inc() (xc_slow_answer++)
331 : :
332 : : static void DisplayXidCache(void);
333 : : #else /* !XIDCACHE_DEBUG */
334 : :
335 : : #define xc_by_recent_xmin_inc() ((void) 0)
336 : : #define xc_by_known_xact_inc() ((void) 0)
337 : : #define xc_by_my_xact_inc() ((void) 0)
338 : : #define xc_by_latest_xid_inc() ((void) 0)
339 : : #define xc_by_main_xid_inc() ((void) 0)
340 : : #define xc_by_child_xid_inc() ((void) 0)
341 : : #define xc_by_known_assigned_inc() ((void) 0)
342 : : #define xc_no_overflow_inc() ((void) 0)
343 : : #define xc_slow_answer_inc() ((void) 0)
344 : : #endif /* XIDCACHE_DEBUG */
345 : :
346 : : /* Primitives for KnownAssignedXids array handling for standby */
347 : : static void KnownAssignedXidsCompress(KAXCompressReason reason, bool haveLock);
348 : : static void KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
349 : : bool exclusive_lock);
350 : : static bool KnownAssignedXidsSearch(TransactionId xid, bool remove);
351 : : static bool KnownAssignedXidExists(TransactionId xid);
352 : : static void KnownAssignedXidsRemove(TransactionId xid);
353 : : static void KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
354 : : TransactionId *subxids);
355 : : static void KnownAssignedXidsRemovePreceding(TransactionId removeXid);
356 : : static int KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax);
357 : : static int KnownAssignedXidsGetAndSetXmin(TransactionId *xarray,
358 : : TransactionId *xmin,
359 : : TransactionId xmax);
360 : : static TransactionId KnownAssignedXidsGetOldestXmin(void);
361 : : static void KnownAssignedXidsDisplay(int trace_level);
362 : : static void KnownAssignedXidsReset(void);
363 : : static inline void ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid);
364 : : static void ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid);
365 : : static void MaintainLatestCompletedXid(TransactionId latestXid);
366 : : static void MaintainLatestCompletedXidRecovery(TransactionId latestXid);
367 : :
368 : : static inline FullTransactionId FullXidRelativeTo(FullTransactionId rel,
369 : : TransactionId xid);
370 : : static void GlobalVisUpdateApply(ComputeXidHorizonsResult *horizons);
371 : :
372 : : /*
373 : : * Report shared-memory space needed by ProcArrayShmemInit
374 : : */
375 : : Size
7386 tgl@sss.pgh.pa.us 376 :CBC 1909 : ProcArrayShmemSize(void)
377 : : {
378 : : Size size;
379 : :
380 : : /* Size of the ProcArray structure itself */
381 : : #define PROCARRAY_MAXPROCS (MaxBackends + max_prepared_xacts)
382 : :
5034 rhaas@postgresql.org 383 : 1909 : size = offsetof(ProcArrayStruct, pgprocnos);
1243 384 : 1909 : size = add_size(size, mul_size(sizeof(int), PROCARRAY_MAXPROCS));
385 : :
386 : : /*
387 : : * During Hot Standby processing we have a data structure called
388 : : * KnownAssignedXids, created in shared memory. Local data structures are
389 : : * also created in various backends during GetSnapshotData(),
390 : : * TransactionIdIsInProgress() and GetRunningTransactionData(). All of the
391 : : * main structures created in those functions must be identically sized,
392 : : * since we may at times copy the whole of the data structures around. We
393 : : * refer to this size as TOTAL_MAX_CACHED_SUBXIDS.
394 : : *
395 : : * Ideally we'd only create this structure if we were actually doing hot
396 : : * standby in the current run, but we don't know that yet at the time
397 : : * shared memory is being set up.
398 : : */
399 : : #define TOTAL_MAX_CACHED_SUBXIDS \
400 : : ((PGPROC_MAX_CACHED_SUBXIDS + 1) * PROCARRAY_MAXPROCS)
401 : :
5609 tgl@sss.pgh.pa.us 402 [ + + ]: 1909 : if (EnableHotStandby)
403 : : {
5610 404 : 1899 : size = add_size(size,
405 : : mul_size(sizeof(TransactionId),
406 : 1899 : TOTAL_MAX_CACHED_SUBXIDS));
5712 407 : 1899 : size = add_size(size,
5610 408 : 1899 : mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS));
409 : : }
410 : :
7322 411 : 1909 : return size;
412 : : }
413 : :
414 : : /*
415 : : * Initialize the shared PGPROC array during postmaster startup.
416 : : */
417 : : void
373 heikki.linnakangas@i 418 : 1029 : ProcArrayShmemInit(void)
419 : : {
420 : : bool found;
421 : :
422 : : /* Create or attach to the ProcArray shared structure */
7415 tgl@sss.pgh.pa.us 423 : 1029 : procArray = (ProcArrayStruct *)
5740 simon@2ndQuadrant.co 424 : 1029 : ShmemInitStruct("Proc Array",
425 : : add_size(offsetof(ProcArrayStruct, pgprocnos),
426 : : mul_size(sizeof(int),
1243 rhaas@postgresql.org 427 : 1029 : PROCARRAY_MAXPROCS)),
428 : : &found);
429 : :
7415 tgl@sss.pgh.pa.us 430 [ + - ]: 1029 : if (!found)
431 : : {
432 : : /*
433 : : * We're the first - initialize.
434 : : */
435 : 1029 : procArray->numProcs = 0;
1243 rhaas@postgresql.org 436 : 1029 : procArray->maxProcs = PROCARRAY_MAXPROCS;
5712 tgl@sss.pgh.pa.us 437 : 1029 : procArray->maxKnownAssignedXids = TOTAL_MAX_CACHED_SUBXIDS;
5610 438 : 1029 : procArray->numKnownAssignedXids = 0;
439 : 1029 : procArray->tailKnownAssignedXids = 0;
440 : 1029 : procArray->headKnownAssignedXids = 0;
5712 441 : 1029 : procArray->lastOverflowedXid = InvalidTransactionId;
2944 peter_e@gmx.net 442 : 1029 : procArray->replication_slot_xmin = InvalidTransactionId;
443 : 1029 : procArray->replication_slot_catalog_xmin = InvalidTransactionId;
638 heikki.linnakangas@i 444 : 1029 : TransamVariables->xactCompletionCount = 1;
445 : : }
446 : :
5034 rhaas@postgresql.org 447 : 1029 : allProcs = ProcGlobal->allProcs;
448 : :
449 : : /* Create or attach to the KnownAssignedXids arrays too, if needed */
5609 tgl@sss.pgh.pa.us 450 [ + + ]: 1029 : if (EnableHotStandby)
451 : : {
5610 452 : 1024 : KnownAssignedXids = (TransactionId *)
453 : 1024 : ShmemInitStruct("KnownAssignedXids",
454 : : mul_size(sizeof(TransactionId),
455 : 1024 : TOTAL_MAX_CACHED_SUBXIDS),
456 : : &found);
457 : 1024 : KnownAssignedXidsValid = (bool *)
458 : 1024 : ShmemInitStruct("KnownAssignedXidsValid",
459 : 1024 : mul_size(sizeof(bool), TOTAL_MAX_CACHED_SUBXIDS),
460 : : &found);
461 : : }
7415 462 : 1029 : }
463 : :
464 : : /*
465 : : * Add the specified PGPROC to the shared array.
466 : : */
467 : : void
7386 468 : 15158 : ProcArrayAdd(PGPROC *proc)
469 : : {
562 heikki.linnakangas@i 470 : 15158 : int pgprocno = GetNumberFromPGProc(proc);
7415 tgl@sss.pgh.pa.us 471 : 15158 : ProcArrayStruct *arrayP = procArray;
472 : : int index;
473 : : int movecount;
474 : :
475 : : /* See ProcGlobal comment explaining why both locks are held */
476 : 15158 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1849 andres@anarazel.de 477 : 15158 : LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
478 : :
7415 tgl@sss.pgh.pa.us 479 [ - + ]: 15158 : if (arrayP->numProcs >= arrayP->maxProcs)
480 : : {
481 : : /*
482 : : * Oops, no room. (This really shouldn't happen, since there is a
483 : : * fixed supply of PGPROC structs too, and so we should have failed
484 : : * earlier.)
485 : : */
7415 tgl@sss.pgh.pa.us 486 [ # # ]:UBC 0 : ereport(FATAL,
487 : : (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
488 : : errmsg("sorry, too many clients already")));
489 : : }
490 : :
491 : : /*
492 : : * Keep the procs array sorted by (PGPROC *) so that we can utilize
493 : : * locality of references much better. This is useful while traversing the
494 : : * ProcArray because there is an increased likelihood of finding the next
495 : : * PGPROC structure in the cache.
496 : : *
497 : : * Since the occurrence of adding/removing a proc is much lower than the
498 : : * access to the ProcArray itself, the overhead should be marginal
499 : : */
5034 rhaas@postgresql.org 500 [ + + ]:CBC 34856 : for (index = 0; index < arrayP->numProcs; index++)
501 : : {
562 heikki.linnakangas@i 502 : 30762 : int this_procno = arrayP->pgprocnos[index];
503 : :
504 [ + - - + ]: 30762 : Assert(this_procno >= 0 && this_procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
505 [ - + ]: 30762 : Assert(allProcs[this_procno].pgxactoff == index);
506 : :
507 : : /* If we have found our right position in the array, break */
508 [ + + ]: 30762 : if (this_procno > pgprocno)
5034 rhaas@postgresql.org 509 : 11064 : break;
510 : : }
511 : :
1548 andres@anarazel.de 512 : 15158 : movecount = arrayP->numProcs - index;
513 : 15158 : memmove(&arrayP->pgprocnos[index + 1],
514 : 15158 : &arrayP->pgprocnos[index],
515 : : movecount * sizeof(*arrayP->pgprocnos));
516 : 15158 : memmove(&ProcGlobal->xids[index + 1],
517 : 15158 : &ProcGlobal->xids[index],
518 : : movecount * sizeof(*ProcGlobal->xids));
519 : 15158 : memmove(&ProcGlobal->subxidStates[index + 1],
520 : 15158 : &ProcGlobal->subxidStates[index],
521 : : movecount * sizeof(*ProcGlobal->subxidStates));
522 : 15158 : memmove(&ProcGlobal->statusFlags[index + 1],
523 : 15158 : &ProcGlobal->statusFlags[index],
524 : : movecount * sizeof(*ProcGlobal->statusFlags));
525 : :
562 heikki.linnakangas@i 526 : 15158 : arrayP->pgprocnos[index] = GetNumberFromPGProc(proc);
1548 andres@anarazel.de 527 : 15158 : proc->pgxactoff = index;
1849 528 : 15158 : ProcGlobal->xids[index] = proc->xid;
529 : 15158 : ProcGlobal->subxidStates[index] = proc->subxidStatus;
1755 alvherre@alvh.no-ip. 530 : 15158 : ProcGlobal->statusFlags[index] = proc->statusFlags;
531 : :
7415 tgl@sss.pgh.pa.us 532 : 15158 : arrayP->numProcs++;
533 : :
534 : : /* adjust pgxactoff for all following PGPROCs */
1548 andres@anarazel.de 535 : 15158 : index++;
1849 536 [ + + ]: 42624 : for (; index < arrayP->numProcs; index++)
537 : : {
1548 538 : 27466 : int procno = arrayP->pgprocnos[index];
539 : :
540 [ + - - + ]: 27466 : Assert(procno >= 0 && procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
541 [ - + ]: 27466 : Assert(allProcs[procno].pgxactoff == index - 1);
542 : :
543 : 27466 : allProcs[procno].pgxactoff = index;
544 : : }
545 : :
546 : : /*
547 : : * Release in reversed acquisition order, to reduce frequency of having to
548 : : * wait for XidGenLock while holding ProcArrayLock.
549 : : */
1849 550 : 15158 : LWLockRelease(XidGenLock);
7415 tgl@sss.pgh.pa.us 551 : 15158 : LWLockRelease(ProcArrayLock);
552 : 15158 : }
553 : :
554 : : /*
555 : : * Remove the specified PGPROC from the shared array.
556 : : *
557 : : * When latestXid is a valid XID, we are removing a live 2PC gxact from the
558 : : * array, and thus causing it to appear as "not running" anymore. In this
559 : : * case we must advance latestCompletedXid. (This is essentially the same
560 : : * as ProcArrayEndTransaction followed by removal of the PGPROC, but we take
561 : : * the ProcArrayLock only once, and don't damage the content of the PGPROC;
562 : : * twophase.c depends on the latter.)
563 : : */
564 : : void
6573 565 : 15132 : ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
566 : : {
7415 567 : 15132 : ProcArrayStruct *arrayP = procArray;
568 : : int myoff;
569 : : int movecount;
570 : :
571 : : #ifdef XIDCACHE_DEBUG
572 : : /* dump stats at backend shutdown, but not prepared-xact end */
573 : : if (proc->pid != 0)
574 : : DisplayXidCache();
575 : : #endif
576 : :
577 : : /* See ProcGlobal comment explaining why both locks are held */
578 : 15132 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1849 andres@anarazel.de 579 : 15132 : LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
580 : :
1548 581 : 15132 : myoff = proc->pgxactoff;
582 : :
583 [ + - - + ]: 15132 : Assert(myoff >= 0 && myoff < arrayP->numProcs);
584 [ - + ]: 15132 : Assert(ProcGlobal->allProcs[arrayP->pgprocnos[myoff]].pgxactoff == myoff);
585 : :
6573 tgl@sss.pgh.pa.us 586 [ + + ]: 15132 : if (TransactionIdIsValid(latestXid))
587 : : {
1548 andres@anarazel.de 588 [ - + ]: 295 : Assert(TransactionIdIsValid(ProcGlobal->xids[myoff]));
589 : :
590 : : /* Advance global latestCompletedXid while holding the lock */
1852 591 : 295 : MaintainLatestCompletedXid(latestXid);
592 : :
593 : : /* Same with xactCompletionCount */
638 heikki.linnakangas@i 594 : 295 : TransamVariables->xactCompletionCount++;
595 : :
1548 andres@anarazel.de 596 : 295 : ProcGlobal->xids[myoff] = InvalidTransactionId;
597 : 295 : ProcGlobal->subxidStates[myoff].overflowed = false;
598 : 295 : ProcGlobal->subxidStates[myoff].count = 0;
599 : : }
600 : : else
601 : : {
602 : : /* Shouldn't be trying to remove a live transaction here */
603 [ - + ]: 14837 : Assert(!TransactionIdIsValid(ProcGlobal->xids[myoff]));
604 : : }
605 : :
606 [ - + ]: 15132 : Assert(!TransactionIdIsValid(ProcGlobal->xids[myoff]));
607 [ - + ]: 15132 : Assert(ProcGlobal->subxidStates[myoff].count == 0);
608 [ - + ]: 15132 : Assert(ProcGlobal->subxidStates[myoff].overflowed == false);
609 : :
610 : 15132 : ProcGlobal->statusFlags[myoff] = 0;
611 : :
612 : : /* Keep the PGPROC array sorted. See notes above */
613 : 15132 : movecount = arrayP->numProcs - myoff - 1;
614 : 15132 : memmove(&arrayP->pgprocnos[myoff],
615 : 15132 : &arrayP->pgprocnos[myoff + 1],
616 : : movecount * sizeof(*arrayP->pgprocnos));
617 : 15132 : memmove(&ProcGlobal->xids[myoff],
618 : 15132 : &ProcGlobal->xids[myoff + 1],
619 : : movecount * sizeof(*ProcGlobal->xids));
620 : 15132 : memmove(&ProcGlobal->subxidStates[myoff],
621 : 15132 : &ProcGlobal->subxidStates[myoff + 1],
622 : : movecount * sizeof(*ProcGlobal->subxidStates));
623 : 15132 : memmove(&ProcGlobal->statusFlags[myoff],
624 : 15132 : &ProcGlobal->statusFlags[myoff + 1],
625 : : movecount * sizeof(*ProcGlobal->statusFlags));
626 : :
627 : 15132 : arrayP->pgprocnos[arrayP->numProcs - 1] = -1; /* for debugging */
628 : 15132 : arrayP->numProcs--;
629 : :
630 : : /*
631 : : * Adjust pgxactoff of following procs for removed PGPROC (note that
632 : : * numProcs already has been decremented).
633 : : */
634 [ + + ]: 45150 : for (int index = myoff; index < arrayP->numProcs; index++)
635 : : {
636 : 30018 : int procno = arrayP->pgprocnos[index];
637 : :
638 [ + - - + ]: 30018 : Assert(procno >= 0 && procno < (arrayP->maxProcs + NUM_AUXILIARY_PROCS));
639 [ - + ]: 30018 : Assert(allProcs[procno].pgxactoff - 1 == index);
640 : :
641 : 30018 : allProcs[procno].pgxactoff = index;
642 : : }
643 : :
644 : : /*
645 : : * Release in reversed acquisition order, to reduce frequency of having to
646 : : * wait for XidGenLock while holding ProcArrayLock.
647 : : */
1849 648 : 15132 : LWLockRelease(XidGenLock);
7415 tgl@sss.pgh.pa.us 649 : 15132 : LWLockRelease(ProcArrayLock);
650 : 15132 : }
651 : :
652 : :
653 : : /*
654 : : * ProcArrayEndTransaction -- mark a transaction as no longer running
655 : : *
656 : : * This is used interchangeably for commit and abort cases. The transaction
657 : : * commit/abort must already be reported to WAL and pg_xact.
658 : : *
659 : : * proc is currently always MyProc, but we pass it explicitly for flexibility.
660 : : * latestXid is the latest Xid among the transaction's main XID and
661 : : * subtransactions, or InvalidTransactionId if it has no XID. (We must ask
662 : : * the caller to pass latestXid, instead of computing it from the PGPROC's
663 : : * contents, because the subxid information in the PGPROC might be
664 : : * incomplete.)
665 : : */
666 : : void
6573 667 : 318322 : ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
668 : : {
669 [ + + ]: 318322 : if (TransactionIdIsValid(latestXid))
670 : : {
671 : : /*
672 : : * We must lock ProcArrayLock while clearing our advertised XID, so
673 : : * that we do not exit the set of "running" transactions while someone
674 : : * else is taking a snapshot. See discussion in
675 : : * src/backend/access/transam/README.
676 : : */
1849 andres@anarazel.de 677 [ - + ]: 128712 : Assert(TransactionIdIsValid(proc->xid));
678 : :
679 : : /*
680 : : * If we can immediately acquire ProcArrayLock, we clear our own XID
681 : : * and release the lock. If not, use group XID clearing to improve
682 : : * efficiency.
683 : : */
3684 rhaas@postgresql.org 684 [ + + ]: 128712 : if (LWLockConditionalAcquire(ProcArrayLock, LW_EXCLUSIVE))
685 : : {
1849 andres@anarazel.de 686 : 128583 : ProcArrayEndTransactionInternal(proc, latestXid);
3684 rhaas@postgresql.org 687 : 128583 : LWLockRelease(ProcArrayLock);
688 : : }
689 : : else
690 : 129 : ProcArrayGroupClearXid(proc, latestXid);
691 : : }
692 : : else
693 : : {
694 : : /*
695 : : * If we have no XID, we don't need to lock, since we won't affect
696 : : * anyone else's calculation of a snapshot. We might change their
697 : : * estimate of global xmin, but that's OK.
698 : : */
1849 andres@anarazel.de 699 [ - + ]: 189610 : Assert(!TransactionIdIsValid(proc->xid));
700 [ - + ]: 189610 : Assert(proc->subxidStatus.count == 0);
701 [ - + ]: 189610 : Assert(!proc->subxidStatus.overflowed);
702 : :
552 heikki.linnakangas@i 703 : 189610 : proc->vxid.lxid = InvalidLocalTransactionId;
1850 andres@anarazel.de 704 : 189610 : proc->xmin = InvalidTransactionId;
705 : :
706 : : /* be sure this is cleared in abort */
1247 rhaas@postgresql.org 707 : 189610 : proc->delayChkptFlags = 0;
708 : :
5712 simon@2ndQuadrant.co 709 : 189610 : proc->recoveryConflictPending = false;
710 : :
711 : : /* must be cleared with xid/xmin: */
712 : : /* avoid unnecessarily dirtying shared cachelines */
1755 alvherre@alvh.no-ip. 713 [ + + ]: 189610 : if (proc->statusFlags & PROC_VACUUM_STATE_MASK)
714 : : {
1879 andres@anarazel.de 715 [ - + ]: 13523 : Assert(!LWLockHeldByMe(ProcArrayLock));
1745 alvherre@alvh.no-ip. 716 : 13523 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1755 717 [ - + ]: 13523 : Assert(proc->statusFlags == ProcGlobal->statusFlags[proc->pgxactoff]);
718 : 13523 : proc->statusFlags &= ~PROC_VACUUM_STATE_MASK;
719 : 13523 : ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags;
1879 andres@anarazel.de 720 : 13523 : LWLockRelease(ProcArrayLock);
721 : : }
722 : : }
6573 tgl@sss.pgh.pa.us 723 : 318322 : }
724 : :
725 : : /*
726 : : * Mark a write transaction as no longer running.
727 : : *
728 : : * We don't do any locking here; caller must handle that.
729 : : */
730 : : static inline void
1849 andres@anarazel.de 731 : 128712 : ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid)
732 : : {
1451 fujii@postgresql.org 733 : 128712 : int pgxactoff = proc->pgxactoff;
734 : :
735 : : /*
736 : : * Note: we need exclusive lock here because we're going to change other
737 : : * processes' PGPROC entries.
738 : : */
1753 alvherre@alvh.no-ip. 739 [ - + ]: 128712 : Assert(LWLockHeldByMeInMode(ProcArrayLock, LW_EXCLUSIVE));
1849 andres@anarazel.de 740 [ - + ]: 128712 : Assert(TransactionIdIsValid(ProcGlobal->xids[pgxactoff]));
741 [ - + ]: 128712 : Assert(ProcGlobal->xids[pgxactoff] == proc->xid);
742 : :
743 : 128712 : ProcGlobal->xids[pgxactoff] = InvalidTransactionId;
744 : 128712 : proc->xid = InvalidTransactionId;
552 heikki.linnakangas@i 745 : 128712 : proc->vxid.lxid = InvalidLocalTransactionId;
1850 andres@anarazel.de 746 : 128712 : proc->xmin = InvalidTransactionId;
747 : :
748 : : /* be sure this is cleared in abort */
1247 rhaas@postgresql.org 749 : 128712 : proc->delayChkptFlags = 0;
750 : :
3684 751 : 128712 : proc->recoveryConflictPending = false;
752 : :
753 : : /* must be cleared with xid/xmin: */
754 : : /* avoid unnecessarily dirtying shared cachelines */
1755 alvherre@alvh.no-ip. 755 [ + + ]: 128712 : if (proc->statusFlags & PROC_VACUUM_STATE_MASK)
756 : : {
757 : 642 : proc->statusFlags &= ~PROC_VACUUM_STATE_MASK;
758 : 642 : ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags;
759 : : }
760 : :
761 : : /* Clear the subtransaction-XID cache too while holding the lock */
1849 andres@anarazel.de 762 [ + - - + ]: 128712 : Assert(ProcGlobal->subxidStates[pgxactoff].count == proc->subxidStatus.count &&
763 : : ProcGlobal->subxidStates[pgxactoff].overflowed == proc->subxidStatus.overflowed);
764 [ + + - + ]: 128712 : if (proc->subxidStatus.count > 0 || proc->subxidStatus.overflowed)
765 : : {
766 : 464 : ProcGlobal->subxidStates[pgxactoff].count = 0;
767 : 464 : ProcGlobal->subxidStates[pgxactoff].overflowed = false;
768 : 464 : proc->subxidStatus.count = 0;
769 : 464 : proc->subxidStatus.overflowed = false;
770 : : }
771 : :
772 : : /* Also advance global latestCompletedXid while holding the lock */
1852 773 : 128712 : MaintainLatestCompletedXid(latestXid);
774 : :
775 : : /* Same with xactCompletionCount */
638 heikki.linnakangas@i 776 : 128712 : TransamVariables->xactCompletionCount++;
3684 rhaas@postgresql.org 777 : 128712 : }
778 : :
779 : : /*
780 : : * ProcArrayGroupClearXid -- group XID clearing
781 : : *
782 : : * When we cannot immediately acquire ProcArrayLock in exclusive mode at
783 : : * commit time, add ourselves to a list of processes that need their XIDs
784 : : * cleared. The first process to add itself to the list will acquire
785 : : * ProcArrayLock in exclusive mode and perform ProcArrayEndTransactionInternal
786 : : * on behalf of all group members. This avoids a great deal of contention
787 : : * around ProcArrayLock when many processes are trying to commit at once,
788 : : * since the lock need not be repeatedly handed off from one committing
789 : : * process to the next.
790 : : */
791 : : static void
792 : 129 : ProcArrayGroupClearXid(PGPROC *proc, TransactionId latestXid)
793 : : {
562 heikki.linnakangas@i 794 : 129 : int pgprocno = GetNumberFromPGProc(proc);
2493 andres@anarazel.de 795 : 129 : PROC_HDR *procglobal = ProcGlobal;
796 : : uint32 nextidx;
797 : : uint32 wakeidx;
798 : :
799 : : /* We should definitely have an XID to clear. */
1849 800 [ - + ]: 129 : Assert(TransactionIdIsValid(proc->xid));
801 : :
802 : : /* Add ourselves to the list of processes needing a group XID clear. */
3495 rhaas@postgresql.org 803 : 129 : proc->procArrayGroupMember = true;
804 : 129 : proc->procArrayGroupMemberXid = latestXid;
2150 noah@leadboat.com 805 : 129 : nextidx = pg_atomic_read_u32(&procglobal->procArrayGroupFirst);
806 : : while (true)
807 : : {
3495 rhaas@postgresql.org 808 : 129 : pg_atomic_write_u32(&proc->procArrayGroupNext, nextidx);
809 : :
810 [ + - ]: 129 : if (pg_atomic_compare_exchange_u32(&procglobal->procArrayGroupFirst,
811 : : &nextidx,
812 : : (uint32) pgprocno))
3684 813 : 129 : break;
814 : : }
815 : :
816 : : /*
817 : : * If the list was not empty, the leader will clear our XID. It is
818 : : * impossible to have followers without a leader because the first process
819 : : * that has added itself to the list will always have nextidx as
820 : : * INVALID_PROC_NUMBER.
821 : : */
552 heikki.linnakangas@i 822 [ + + ]: 129 : if (nextidx != INVALID_PROC_NUMBER)
823 : : {
3166 rhaas@postgresql.org 824 : 27 : int extraWaits = 0;
825 : :
826 : : /* Sleep until the leader clears our XID. */
3074 827 : 27 : pgstat_report_wait_start(WAIT_EVENT_PROCARRAY_GROUP_UPDATE);
828 : : for (;;)
829 : : {
830 : : /* acts as a read barrier */
3190 tgl@sss.pgh.pa.us 831 : 27 : PGSemaphoreLock(proc->sem);
3495 rhaas@postgresql.org 832 [ + - ]: 27 : if (!proc->procArrayGroupMember)
3656 833 : 27 : break;
3656 rhaas@postgresql.org 834 :UBC 0 : extraWaits++;
835 : : }
3074 rhaas@postgresql.org 836 :CBC 27 : pgstat_report_wait_end();
837 : :
552 heikki.linnakangas@i 838 [ - + ]: 27 : Assert(pg_atomic_read_u32(&proc->procArrayGroupNext) == INVALID_PROC_NUMBER);
839 : :
840 : : /* Fix semaphore count for any absorbed wakeups */
3684 rhaas@postgresql.org 841 [ - + ]: 27 : while (extraWaits-- > 0)
3190 tgl@sss.pgh.pa.us 842 :UBC 0 : PGSemaphoreUnlock(proc->sem);
3684 rhaas@postgresql.org 843 :CBC 27 : return;
844 : : }
845 : :
846 : : /* We are the leader. Acquire the lock on behalf of everyone. */
847 : 102 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
848 : :
849 : : /*
850 : : * Now that we've got the lock, clear the list of processes waiting for
851 : : * group XID clearing, saving a pointer to the head of the list. Trying
852 : : * to pop elements one at a time could lead to an ABA problem.
853 : : */
2541 akorotkov@postgresql 854 : 102 : nextidx = pg_atomic_exchange_u32(&procglobal->procArrayGroupFirst,
855 : : INVALID_PROC_NUMBER);
856 : :
857 : : /* Remember head of list so we can perform wakeups after dropping lock. */
3684 rhaas@postgresql.org 858 : 102 : wakeidx = nextidx;
859 : :
860 : : /* Walk the list and clear all XIDs. */
552 heikki.linnakangas@i 861 [ + + ]: 231 : while (nextidx != INVALID_PROC_NUMBER)
862 : : {
1451 fujii@postgresql.org 863 : 129 : PGPROC *nextproc = &allProcs[nextidx];
864 : :
865 : 129 : ProcArrayEndTransactionInternal(nextproc, nextproc->procArrayGroupMemberXid);
866 : :
867 : : /* Move to next proc in list. */
868 : 129 : nextidx = pg_atomic_read_u32(&nextproc->procArrayGroupNext);
869 : : }
870 : :
871 : : /* We're done with the lock now. */
3684 rhaas@postgresql.org 872 : 102 : LWLockRelease(ProcArrayLock);
873 : :
874 : : /*
875 : : * Now that we've released the lock, go back and wake everybody up. We
876 : : * don't do this under the lock so as to keep lock hold times to a
877 : : * minimum. The system calls we need to perform to wake other processes
878 : : * up are probably much slower than the simple memory writes we did while
879 : : * holding the lock.
880 : : */
552 heikki.linnakangas@i 881 [ + + ]: 231 : while (wakeidx != INVALID_PROC_NUMBER)
882 : : {
1451 fujii@postgresql.org 883 : 129 : PGPROC *nextproc = &allProcs[wakeidx];
884 : :
885 : 129 : wakeidx = pg_atomic_read_u32(&nextproc->procArrayGroupNext);
552 heikki.linnakangas@i 886 : 129 : pg_atomic_write_u32(&nextproc->procArrayGroupNext, INVALID_PROC_NUMBER);
887 : :
888 : : /* ensure all previous writes are visible before follower continues. */
3656 rhaas@postgresql.org 889 : 129 : pg_write_barrier();
890 : :
1451 fujii@postgresql.org 891 : 129 : nextproc->procArrayGroupMember = false;
892 : :
893 [ + + ]: 129 : if (nextproc != MyProc)
894 : 27 : PGSemaphoreUnlock(nextproc->sem);
895 : : }
896 : : }
897 : :
898 : : /*
899 : : * ProcArrayClearTransaction -- clear the transaction fields
900 : : *
901 : : * This is used after successfully preparing a 2-phase transaction. We are
902 : : * not actually reporting the transaction's XID as no longer running --- it
903 : : * will still appear as running because the 2PC's gxact is in the ProcArray
904 : : * too. We just have to clear out our own PGPROC.
905 : : */
906 : : void
6573 tgl@sss.pgh.pa.us 907 : 288 : ProcArrayClearTransaction(PGPROC *proc)
908 : : {
909 : : int pgxactoff;
910 : :
911 : : /*
912 : : * Currently we need to lock ProcArrayLock exclusively here, as we
913 : : * increment xactCompletionCount below. We also need it at least in shared
914 : : * mode for pgproc->pgxactoff to stay the same below.
915 : : *
916 : : * We could however, as this action does not actually change anyone's view
917 : : * of the set of running XIDs (our entry is duplicate with the gxact that
918 : : * has already been inserted into the ProcArray), lower the lock level to
919 : : * shared if we were to make xactCompletionCount an atomic variable. But
920 : : * that doesn't seem worth it currently, as a 2PC commit is heavyweight
921 : : * enough for this not to be the bottleneck. If it ever becomes a
922 : : * bottleneck it may also be worth considering to combine this with the
923 : : * subsequent ProcArrayRemove()
924 : : */
1844 andres@anarazel.de 925 : 288 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
926 : :
1849 927 : 288 : pgxactoff = proc->pgxactoff;
928 : :
929 : 288 : ProcGlobal->xids[pgxactoff] = InvalidTransactionId;
930 : 288 : proc->xid = InvalidTransactionId;
931 : :
552 heikki.linnakangas@i 932 : 288 : proc->vxid.lxid = InvalidLocalTransactionId;
1850 andres@anarazel.de 933 : 288 : proc->xmin = InvalidTransactionId;
5712 simon@2ndQuadrant.co 934 : 288 : proc->recoveryConflictPending = false;
935 : :
1755 alvherre@alvh.no-ip. 936 [ - + ]: 288 : Assert(!(proc->statusFlags & PROC_VACUUM_STATE_MASK));
1247 rhaas@postgresql.org 937 [ - + ]: 288 : Assert(!proc->delayChkptFlags);
938 : :
939 : : /*
940 : : * Need to increment completion count even though transaction hasn't
941 : : * really committed yet. The reason for that is that GetSnapshotData()
942 : : * omits the xid of the current transaction, thus without the increment we
943 : : * otherwise could end up reusing the snapshot later. Which would be bad,
944 : : * because it might not count the prepared transaction as running.
945 : : */
638 heikki.linnakangas@i 946 : 288 : TransamVariables->xactCompletionCount++;
947 : :
948 : : /* Clear the subtransaction-XID cache too */
1849 andres@anarazel.de 949 [ + - - + ]: 288 : Assert(ProcGlobal->subxidStates[pgxactoff].count == proc->subxidStatus.count &&
950 : : ProcGlobal->subxidStates[pgxactoff].overflowed == proc->subxidStatus.overflowed);
951 [ + + - + ]: 288 : if (proc->subxidStatus.count > 0 || proc->subxidStatus.overflowed)
952 : : {
953 : 103 : ProcGlobal->subxidStates[pgxactoff].count = 0;
954 : 103 : ProcGlobal->subxidStates[pgxactoff].overflowed = false;
955 : 103 : proc->subxidStatus.count = 0;
956 : 103 : proc->subxidStatus.overflowed = false;
957 : : }
958 : :
959 : 288 : LWLockRelease(ProcArrayLock);
6573 tgl@sss.pgh.pa.us 960 : 288 : }
961 : :
962 : : /*
963 : : * Update TransamVariables->latestCompletedXid to point to latestXid if
964 : : * currently older.
965 : : */
966 : : static void
1852 andres@anarazel.de 967 : 129654 : MaintainLatestCompletedXid(TransactionId latestXid)
968 : : {
638 heikki.linnakangas@i 969 : 129654 : FullTransactionId cur_latest = TransamVariables->latestCompletedXid;
970 : :
1852 andres@anarazel.de 971 [ - + ]: 129654 : Assert(FullTransactionIdIsValid(cur_latest));
972 [ - + ]: 129654 : Assert(!RecoveryInProgress());
973 [ - + ]: 129654 : Assert(LWLockHeldByMe(ProcArrayLock));
974 : :
975 [ + + ]: 129654 : if (TransactionIdPrecedes(XidFromFullTransactionId(cur_latest), latestXid))
976 : : {
638 heikki.linnakangas@i 977 : 115436 : TransamVariables->latestCompletedXid =
1852 andres@anarazel.de 978 : 115436 : FullXidRelativeTo(cur_latest, latestXid);
979 : : }
980 : :
981 [ + + - + ]: 129654 : Assert(IsBootstrapProcessingMode() ||
982 : : FullTransactionIdIsNormal(TransamVariables->latestCompletedXid));
983 : 129654 : }
984 : :
985 : : /*
986 : : * Same as MaintainLatestCompletedXid, except for use during WAL replay.
987 : : */
988 : : static void
989 : 22092 : MaintainLatestCompletedXidRecovery(TransactionId latestXid)
990 : : {
638 heikki.linnakangas@i 991 : 22092 : FullTransactionId cur_latest = TransamVariables->latestCompletedXid;
992 : : FullTransactionId rel;
993 : :
1852 andres@anarazel.de 994 [ - + - - ]: 22092 : Assert(AmStartupProcess() || !IsUnderPostmaster);
995 [ - + ]: 22092 : Assert(LWLockHeldByMe(ProcArrayLock));
996 : :
997 : : /*
998 : : * Need a FullTransactionId to compare latestXid with. Can't rely on
999 : : * latestCompletedXid to be initialized in recovery. But in recovery it's
1000 : : * safe to access nextXid without a lock for the startup process.
1001 : : */
638 heikki.linnakangas@i 1002 : 22092 : rel = TransamVariables->nextXid;
1003 [ - + ]: 22092 : Assert(FullTransactionIdIsValid(TransamVariables->nextXid));
1004 : :
1852 andres@anarazel.de 1005 [ + + + + ]: 44084 : if (!FullTransactionIdIsValid(cur_latest) ||
1006 : 21992 : TransactionIdPrecedes(XidFromFullTransactionId(cur_latest), latestXid))
1007 : : {
638 heikki.linnakangas@i 1008 : 16344 : TransamVariables->latestCompletedXid =
1852 andres@anarazel.de 1009 : 16344 : FullXidRelativeTo(rel, latestXid);
1010 : : }
1011 : :
638 heikki.linnakangas@i 1012 [ - + ]: 22092 : Assert(FullTransactionIdIsNormal(TransamVariables->latestCompletedXid));
1852 andres@anarazel.de 1013 : 22092 : }
1014 : :
1015 : : /*
1016 : : * ProcArrayInitRecovery -- initialize recovery xid mgmt environment
1017 : : *
1018 : : * Remember up to where the startup process initialized the CLOG and subtrans
1019 : : * so we can ensure it's initialized gaplessly up to the point where necessary
1020 : : * while in recovery.
1021 : : */
1022 : : void
4458 simon@2ndQuadrant.co 1023 : 100 : ProcArrayInitRecovery(TransactionId initializedUptoXID)
1024 : : {
1025 [ - + ]: 100 : Assert(standbyState == STANDBY_INITIALIZED);
1026 [ - + ]: 100 : Assert(TransactionIdIsNormal(initializedUptoXID));
1027 : :
1028 : : /*
1029 : : * we set latestObservedXid to the xid SUBTRANS has been initialized up
1030 : : * to, so we can extend it from that point onwards in
1031 : : * RecordKnownAssignedTransactionIds, and when we get consistent in
1032 : : * ProcArrayApplyRecoveryInfo().
1033 : : */
1034 : 100 : latestObservedXid = initializedUptoXID;
1035 [ - + ]: 100 : TransactionIdRetreat(latestObservedXid);
1036 : 100 : }
1037 : :
1038 : : /*
1039 : : * ProcArrayApplyRecoveryInfo -- apply recovery info about xids
1040 : : *
1041 : : * Takes us through 3 states: Initialized, Pending and Ready.
1042 : : * Normal case is to go all the way to Ready straight away, though there
1043 : : * are atypical cases where we need to take it in steps.
1044 : : *
1045 : : * Use the data about running transactions on the primary to create the initial
1046 : : * state of KnownAssignedXids. We also use these records to regularly prune
1047 : : * KnownAssignedXids because we know it is possible that some transactions
1048 : : * with FATAL errors fail to write abort records, which could cause eventual
1049 : : * overflow.
1050 : : *
1051 : : * See comments for LogStandbySnapshot().
1052 : : */
1053 : : void
5740 1054 : 735 : ProcArrayApplyRecoveryInfo(RunningTransactions running)
1055 : : {
1056 : : TransactionId *xids;
1057 : : TransactionId advanceNextXid;
1058 : : int nxids;
1059 : : int i;
1060 : :
1061 [ - + ]: 735 : Assert(standbyState >= STANDBY_INITIALIZED);
5594 1062 [ - + ]: 735 : Assert(TransactionIdIsValid(running->nextXid));
1063 [ - + ]: 735 : Assert(TransactionIdIsValid(running->oldestRunningXid));
1064 [ - + ]: 735 : Assert(TransactionIdIsNormal(running->latestCompletedXid));
1065 : :
1066 : : /*
1067 : : * Remove stale transactions, if any.
1068 : : */
5740 1069 : 735 : ExpireOldKnownAssignedTransactionIds(running->oldestRunningXid);
1070 : :
1071 : : /*
1072 : : * Adjust TransamVariables->nextXid before StandbyReleaseOldLocks(),
1073 : : * because we will need it up to date for accessing two-phase transactions
1074 : : * in StandbyReleaseOldLocks().
1075 : : */
596 akorotkov@postgresql 1076 : 735 : advanceNextXid = running->nextXid;
1077 [ - + ]: 735 : TransactionIdRetreat(advanceNextXid);
1078 : 735 : AdvanceNextFullTransactionIdPastXid(advanceNextXid);
1079 [ - + ]: 735 : Assert(FullTransactionIdIsValid(TransamVariables->nextXid));
1080 : :
1081 : : /*
1082 : : * Remove stale locks, if any.
1083 : : */
2639 simon@2ndQuadrant.co 1084 : 735 : StandbyReleaseOldLocks(running->oldestRunningXid);
1085 : :
1086 : : /*
1087 : : * If our snapshot is already valid, nothing else to do...
1088 : : */
5740 1089 [ + + ]: 735 : if (standbyState == STANDBY_SNAPSHOT_READY)
1090 : 635 : return;
1091 : :
1092 : : /*
1093 : : * If our initial RunningTransactionsData had an overflowed snapshot then
1094 : : * we knew we were missing some subxids from our snapshot. If we continue
1095 : : * to see overflowed snapshots then we might never be able to start up, so
1096 : : * we make another test to see if our snapshot is now valid. We know that
1097 : : * the missing subxids are equal to or earlier than nextXid. After we
1098 : : * initialise we continue to apply changes during recovery, so once the
1099 : : * oldestRunningXid is later than the nextXid from the initial snapshot we
1100 : : * know that we no longer have missing information and can mark the
1101 : : * snapshot as valid.
1102 : : */
1103 [ - + ]: 100 : if (standbyState == STANDBY_SNAPSHOT_PENDING)
1104 : : {
1105 : : /*
1106 : : * If the snapshot isn't overflowed or if its empty we can reset our
1107 : : * pending state and use this snapshot instead.
1108 : : */
436 heikki.linnakangas@i 1109 [ # # # # ]:UBC 0 : if (running->subxid_status != SUBXIDS_MISSING || running->xcnt == 0)
1110 : : {
1111 : : /*
1112 : : * If we have already collected known assigned xids, we need to
1113 : : * throw them away before we apply the recovery snapshot.
1114 : : */
4838 simon@2ndQuadrant.co 1115 : 0 : KnownAssignedXidsReset();
5057 1116 : 0 : standbyState = STANDBY_INITIALIZED;
1117 : : }
1118 : : else
1119 : : {
1120 [ # # ]: 0 : if (TransactionIdPrecedes(standbySnapshotPendingXmin,
1121 : : running->oldestRunningXid))
1122 : : {
1123 : 0 : standbyState = STANDBY_SNAPSHOT_READY;
635 michael@paquier.xyz 1124 [ # # ]: 0 : elog(DEBUG1,
1125 : : "recovery snapshots are now enabled");
1126 : : }
1127 : : else
1128 [ # # ]: 0 : elog(DEBUG1,
1129 : : "recovery snapshot waiting for non-overflowed snapshot or "
1130 : : "until oldest active xid on standby is at least %u (now %u)",
1131 : : standbySnapshotPendingXmin,
1132 : : running->oldestRunningXid);
5057 simon@2ndQuadrant.co 1133 : 0 : return;
1134 : : }
1135 : : }
1136 : :
5595 simon@2ndQuadrant.co 1137 [ - + ]:CBC 100 : Assert(standbyState == STANDBY_INITIALIZED);
1138 : :
1139 : : /*
1140 : : * NB: this can be reached at least twice, so make sure new code can deal
1141 : : * with that.
1142 : : */
1143 : :
1144 : : /*
1145 : : * Nobody else is running yet, but take locks anyhow
1146 : : */
1147 : 100 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1148 : :
1149 : : /*
1150 : : * KnownAssignedXids is sorted so we cannot just add the xids, we have to
1151 : : * sort them first.
1152 : : *
1153 : : * Some of the new xids are top-level xids and some are subtransactions.
1154 : : * We don't call SubTransSetParent because it doesn't matter yet. If we
1155 : : * aren't overflowed then all xids will fit in snapshot and so we don't
1156 : : * need subtrans. If we later overflow, an xid assignment record will add
1157 : : * xids to subtrans. If RunningTransactionsData is overflowed then we
1158 : : * don't have enough information to correctly update subtrans anyway.
1159 : : */
1160 : :
1161 : : /*
1162 : : * Allocate a temporary array to avoid modifying the array passed as
1163 : : * argument.
1164 : : */
4661 1165 : 100 : xids = palloc(sizeof(TransactionId) * (running->xcnt + running->subxcnt));
1166 : :
1167 : : /*
1168 : : * Add to the temp array any xids which have not already completed.
1169 : : */
5387 heikki.linnakangas@i 1170 : 100 : nxids = 0;
4661 simon@2ndQuadrant.co 1171 [ + + ]: 104 : for (i = 0; i < running->xcnt + running->subxcnt; i++)
1172 : : {
5595 1173 : 4 : TransactionId xid = running->xids[i];
1174 : :
1175 : : /*
1176 : : * The running-xacts snapshot can contain xids that were still visible
1177 : : * in the procarray when the snapshot was taken, but were already
1178 : : * WAL-logged as completed. They're not running anymore, so ignore
1179 : : * them.
1180 : : */
5740 1181 [ + - - + ]: 4 : if (TransactionIdDidCommit(xid) || TransactionIdDidAbort(xid))
5740 simon@2ndQuadrant.co 1182 :UBC 0 : continue;
1183 : :
5595 simon@2ndQuadrant.co 1184 :CBC 4 : xids[nxids++] = xid;
1185 : : }
1186 : :
1187 [ + + ]: 100 : if (nxids > 0)
1188 : : {
4838 1189 [ - + ]: 4 : if (procArray->numKnownAssignedXids != 0)
1190 : : {
4838 simon@2ndQuadrant.co 1191 :UBC 0 : LWLockRelease(ProcArrayLock);
1192 [ # # ]: 0 : elog(ERROR, "KnownAssignedXids is not empty");
1193 : : }
1194 : :
1195 : : /*
1196 : : * Sort the array so that we can add them safely into
1197 : : * KnownAssignedXids.
1198 : : *
1199 : : * We have to sort them logically, because in KnownAssignedXidsAdd we
1200 : : * call TransactionIdFollowsOrEquals and so on. But we know these XIDs
1201 : : * come from RUNNING_XACTS, which means there are only normal XIDs
1202 : : * from the same epoch, so this is safe.
1203 : : */
1318 tomas.vondra@postgre 1204 :CBC 4 : qsort(xids, nxids, sizeof(TransactionId), xidLogicalComparator);
1205 : :
1206 : : /*
1207 : : * Add the sorted snapshot into KnownAssignedXids. The running-xacts
1208 : : * snapshot may include duplicated xids because of prepared
1209 : : * transactions, so ignore them.
1210 : : */
5595 simon@2ndQuadrant.co 1211 [ + + ]: 8 : for (i = 0; i < nxids; i++)
1212 : : {
2519 michael@paquier.xyz 1213 [ - + - - ]: 4 : if (i > 0 && TransactionIdEquals(xids[i - 1], xids[i]))
1214 : : {
2519 michael@paquier.xyz 1215 [ # # ]:UBC 0 : elog(DEBUG1,
1216 : : "found duplicated transaction %u for KnownAssignedXids insertion",
1217 : : xids[i]);
1218 : 0 : continue;
1219 : : }
5387 heikki.linnakangas@i 1220 :CBC 4 : KnownAssignedXidsAdd(xids[i], xids[i], true);
1221 : : }
1222 : :
635 michael@paquier.xyz 1223 : 4 : KnownAssignedXidsDisplay(DEBUG3);
1224 : : }
1225 : :
5595 simon@2ndQuadrant.co 1226 : 100 : pfree(xids);
1227 : :
1228 : : /*
1229 : : * latestObservedXid is at least set to the point where SUBTRANS was
1230 : : * started up to (cf. ProcArrayInitRecovery()) or to the biggest xid
1231 : : * RecordKnownAssignedTransactionIds() was called for. Initialize
1232 : : * subtrans from thereon, up to nextXid - 1.
1233 : : *
1234 : : * We need to duplicate parts of RecordKnownAssignedTransactionId() here,
1235 : : * because we've just added xids to the known assigned xids machinery that
1236 : : * haven't gone through RecordKnownAssignedTransactionId().
1237 : : */
4458 1238 [ - + ]: 100 : Assert(TransactionIdIsNormal(latestObservedXid));
4306 heikki.linnakangas@i 1239 [ - + ]: 100 : TransactionIdAdvance(latestObservedXid);
4458 simon@2ndQuadrant.co 1240 [ - + ]: 200 : while (TransactionIdPrecedes(latestObservedXid, running->nextXid))
1241 : : {
4458 simon@2ndQuadrant.co 1242 :UBC 0 : ExtendSUBTRANS(latestObservedXid);
1243 [ # # ]: 0 : TransactionIdAdvance(latestObservedXid);
1244 : : }
4141 bruce@momjian.us 1245 [ - + ]:CBC 100 : TransactionIdRetreat(latestObservedXid); /* = running->nextXid - 1 */
1246 : :
1247 : : /* ----------
1248 : : * Now we've got the running xids we need to set the global values that
1249 : : * are used to track snapshots as they evolve further.
1250 : : *
1251 : : * - latestCompletedXid which will be the xmax for snapshots
1252 : : * - lastOverflowedXid which shows whether snapshots overflow
1253 : : * - nextXid
1254 : : *
1255 : : * If the snapshot overflowed, then we still initialise with what we know,
1256 : : * but the recovery snapshot isn't fully valid yet because we know there
1257 : : * are some subxids missing. We don't know the specific subxids that are
1258 : : * missing, so conservatively assume the last one is latestObservedXid.
1259 : : * ----------
1260 : : */
436 heikki.linnakangas@i 1261 [ - + ]: 100 : if (running->subxid_status == SUBXIDS_MISSING)
1262 : : {
5595 simon@2ndQuadrant.co 1263 :UBC 0 : standbyState = STANDBY_SNAPSHOT_PENDING;
1264 : :
1265 : 0 : standbySnapshotPendingXmin = latestObservedXid;
5387 heikki.linnakangas@i 1266 : 0 : procArray->lastOverflowedXid = latestObservedXid;
1267 : : }
1268 : : else
1269 : : {
5595 simon@2ndQuadrant.co 1270 :CBC 100 : standbyState = STANDBY_SNAPSHOT_READY;
1271 : :
1272 : 100 : standbySnapshotPendingXmin = InvalidTransactionId;
1273 : :
1274 : : /*
1275 : : * If the 'xids' array didn't include all subtransactions, we have to
1276 : : * mark any snapshots taken as overflowed.
1277 : : */
436 heikki.linnakangas@i 1278 [ + + ]: 100 : if (running->subxid_status == SUBXIDS_IN_SUBTRANS)
1279 : 26 : procArray->lastOverflowedXid = latestObservedXid;
1280 : : else
1281 : : {
1282 [ - + ]: 74 : Assert(running->subxid_status == SUBXIDS_IN_ARRAY);
1283 : 74 : procArray->lastOverflowedXid = InvalidTransactionId;
1284 : : }
1285 : : }
1286 : :
1287 : : /*
1288 : : * If a transaction wrote a commit record in the gap between taking and
1289 : : * logging the snapshot then latestCompletedXid may already be higher than
1290 : : * the value from the snapshot, so check before we use the incoming value.
1291 : : * It also might not yet be set at all.
1292 : : */
1852 andres@anarazel.de 1293 : 100 : MaintainLatestCompletedXidRecovery(running->latestCompletedXid);
1294 : :
1295 : : /*
1296 : : * NB: No need to increment TransamVariables->xactCompletionCount here,
1297 : : * nobody can see it yet.
1298 : : */
1299 : :
4961 tgl@sss.pgh.pa.us 1300 : 100 : LWLockRelease(ProcArrayLock);
1301 : :
635 michael@paquier.xyz 1302 : 100 : KnownAssignedXidsDisplay(DEBUG3);
5740 simon@2ndQuadrant.co 1303 [ + - ]: 100 : if (standbyState == STANDBY_SNAPSHOT_READY)
635 michael@paquier.xyz 1304 [ + + ]: 100 : elog(DEBUG1, "recovery snapshots are now enabled");
1305 : : else
635 michael@paquier.xyz 1306 [ # # ]:UBC 0 : elog(DEBUG1,
1307 : : "recovery snapshot waiting for non-overflowed snapshot or "
1308 : : "until oldest active xid on standby is at least %u (now %u)",
1309 : : standbySnapshotPendingXmin,
1310 : : running->oldestRunningXid);
1311 : : }
1312 : :
1313 : : /*
1314 : : * ProcArrayApplyXidAssignment
1315 : : * Process an XLOG_XACT_ASSIGNMENT WAL record
1316 : : */
1317 : : void
5740 simon@2ndQuadrant.co 1318 :CBC 21 : ProcArrayApplyXidAssignment(TransactionId topxid,
1319 : : int nsubxids, TransactionId *subxids)
1320 : : {
1321 : : TransactionId max_xid;
1322 : : int i;
1323 : :
5595 1324 [ - + ]: 21 : Assert(standbyState >= STANDBY_INITIALIZED);
1325 : :
5740 1326 : 21 : max_xid = TransactionIdLatest(topxid, nsubxids, subxids);
1327 : :
1328 : : /*
1329 : : * Mark all the subtransactions as observed.
1330 : : *
1331 : : * NOTE: This will fail if the subxid contains too many previously
1332 : : * unobserved xids to fit into known-assigned-xids. That shouldn't happen
1333 : : * as the code stands, because xid-assignment records should never contain
1334 : : * more than PGPROC_MAX_CACHED_SUBXIDS entries.
1335 : : */
1336 : 21 : RecordKnownAssignedTransactionIds(max_xid);
1337 : :
1338 : : /*
1339 : : * Notice that we update pg_subtrans with the top-level xid, rather than
1340 : : * the parent xid. This is a difference between normal processing and
1341 : : * recovery, yet is still correct in all cases. The reason is that
1342 : : * subtransaction commit is not marked in clog until commit processing, so
1343 : : * all aborted subtransactions have already been clearly marked in clog.
1344 : : * As a result we are able to refer directly to the top-level
1345 : : * transaction's state rather than skipping through all the intermediate
1346 : : * states in the subtransaction tree. This should be the first time we
1347 : : * have attempted to SubTransSetParent().
1348 : : */
1349 [ + + ]: 1365 : for (i = 0; i < nsubxids; i++)
3054 1350 : 1344 : SubTransSetParent(subxids[i], topxid);
1351 : :
1352 : : /* KnownAssignedXids isn't maintained yet, so we're done for now */
4306 heikki.linnakangas@i 1353 [ - + ]: 21 : if (standbyState == STANDBY_INITIALIZED)
4306 heikki.linnakangas@i 1354 :UBC 0 : return;
1355 : :
1356 : : /*
1357 : : * Uses same locking as transaction commit
1358 : : */
5740 simon@2ndQuadrant.co 1359 :CBC 21 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1360 : :
1361 : : /*
1362 : : * Remove subxids from known-assigned-xacts.
1363 : : */
5610 tgl@sss.pgh.pa.us 1364 : 21 : KnownAssignedXidsRemoveTree(InvalidTransactionId, nsubxids, subxids);
1365 : :
1366 : : /*
1367 : : * Advance lastOverflowedXid to be at least the last of these subxids.
1368 : : */
5740 simon@2ndQuadrant.co 1369 [ + - ]: 21 : if (TransactionIdPrecedes(procArray->lastOverflowedXid, max_xid))
1370 : 21 : procArray->lastOverflowedXid = max_xid;
1371 : :
1372 : 21 : LWLockRelease(ProcArrayLock);
1373 : : }
1374 : :
1375 : : /*
1376 : : * TransactionIdIsInProgress -- is given transaction running in some backend
1377 : : *
1378 : : * Aside from some shortcuts such as checking RecentXmin and our own Xid,
1379 : : * there are four possibilities for finding a running transaction:
1380 : : *
1381 : : * 1. The given Xid is a main transaction Id. We will find this out cheaply
1382 : : * by looking at ProcGlobal->xids.
1383 : : *
1384 : : * 2. The given Xid is one of the cached subxact Xids in the PGPROC array.
1385 : : * We can find this out cheaply too.
1386 : : *
1387 : : * 3. In Hot Standby mode, we must search the KnownAssignedXids list to see
1388 : : * if the Xid is running on the primary.
1389 : : *
1390 : : * 4. Search the SubTrans tree to find the Xid's topmost parent, and then see
1391 : : * if that is running according to ProcGlobal->xids[] or KnownAssignedXids.
1392 : : * This is the slowest way, but sadly it has to be done always if the others
1393 : : * failed, unless we see that the cached subxact sets are complete (none have
1394 : : * overflowed).
1395 : : *
1396 : : * ProcArrayLock has to be held while we do 1, 2, 3. If we save the top Xids
1397 : : * while doing 1 and 3, we can release the ProcArrayLock while we do 4.
1398 : : * This buys back some concurrency (and we can't retrieve the main Xids from
1399 : : * ProcGlobal->xids[] again anyway; see GetNewTransactionId).
1400 : : */
1401 : : bool
7415 tgl@sss.pgh.pa.us 1402 : 5584779 : TransactionIdIsInProgress(TransactionId xid)
1403 : : {
1404 : : static TransactionId *xids = NULL;
1405 : : static TransactionId *other_xids;
1406 : : XidCacheStatus *other_subxidstates;
6560 1407 : 5584779 : int nxids = 0;
7415 1408 : 5584779 : ProcArrayStruct *arrayP = procArray;
1409 : : TransactionId topxid;
1410 : : TransactionId latestCompletedXid;
1411 : : int mypgxactoff;
1412 : : int numProcs;
1413 : : int j;
1414 : :
1415 : : /*
1416 : : * Don't bother checking a transaction older than RecentXmin; it could not
1417 : : * possibly still be running. (Note: in particular, this guarantees that
1418 : : * we reject InvalidTransactionId, FrozenTransactionId, etc as not
1419 : : * running.)
1420 : : */
1421 [ + + ]: 5584779 : if (TransactionIdPrecedes(xid, RecentXmin))
1422 : : {
1423 : : xc_by_recent_xmin_inc();
1424 : 4439134 : return false;
1425 : : }
1426 : :
1427 : : /*
1428 : : * We may have just checked the status of this transaction, so if it is
1429 : : * already known to be completed, we can fall out without any access to
1430 : : * shared memory.
1431 : : */
1167 heikki.linnakangas@i 1432 [ + + ]: 1145645 : if (TransactionIdEquals(cachedXidIsNotInProgress, xid))
1433 : : {
1434 : : xc_by_known_xact_inc();
6388 tgl@sss.pgh.pa.us 1435 : 925205 : return false;
1436 : : }
1437 : :
1438 : : /*
1439 : : * Also, we can handle our own transaction (and subtransactions) without
1440 : : * any access to shared memory.
1441 : : */
6560 1442 [ + + ]: 220440 : if (TransactionIdIsCurrentTransactionId(xid))
1443 : : {
1444 : : xc_by_my_xact_inc();
1445 : 196529 : return true;
1446 : : }
1447 : :
1448 : : /*
1449 : : * If first time through, get workspace to remember main XIDs in. We
1450 : : * malloc it permanently to avoid repeated palloc/pfree overhead.
1451 : : */
1452 [ + + ]: 23911 : if (xids == NULL)
1453 : : {
1454 : : /*
1455 : : * In hot standby mode, reserve enough space to hold all xids in the
1456 : : * known-assigned list. If we later finish recovery, we no longer need
1457 : : * the bigger array, but we don't bother to shrink it.
1458 : : */
5610 1459 [ + + ]: 756 : int maxxids = RecoveryInProgress() ? TOTAL_MAX_CACHED_SUBXIDS : arrayP->maxProcs;
1460 : :
5740 simon@2ndQuadrant.co 1461 : 756 : xids = (TransactionId *) malloc(maxxids * sizeof(TransactionId));
6560 tgl@sss.pgh.pa.us 1462 [ - + ]: 756 : if (xids == NULL)
6560 tgl@sss.pgh.pa.us 1463 [ # # ]:UBC 0 : ereport(ERROR,
1464 : : (errcode(ERRCODE_OUT_OF_MEMORY),
1465 : : errmsg("out of memory")));
1466 : : }
1467 : :
1849 andres@anarazel.de 1468 :CBC 23911 : other_xids = ProcGlobal->xids;
1469 : 23911 : other_subxidstates = ProcGlobal->subxidStates;
1470 : :
7415 tgl@sss.pgh.pa.us 1471 : 23911 : LWLockAcquire(ProcArrayLock, LW_SHARED);
1472 : :
1473 : : /*
1474 : : * Now that we have the lock, we can check latestCompletedXid; if the
1475 : : * target Xid is after that, it's surely still running.
1476 : : */
1852 andres@anarazel.de 1477 : 23911 : latestCompletedXid =
638 heikki.linnakangas@i 1478 : 23911 : XidFromFullTransactionId(TransamVariables->latestCompletedXid);
1852 andres@anarazel.de 1479 [ + + ]: 23911 : if (TransactionIdPrecedes(latestCompletedXid, xid))
1480 : : {
6558 tgl@sss.pgh.pa.us 1481 : 5926 : LWLockRelease(ProcArrayLock);
1482 : : xc_by_latest_xid_inc();
1483 : 5926 : return true;
1484 : : }
1485 : :
1486 : : /* No shortcuts, gotta grovel through the array */
1849 andres@anarazel.de 1487 : 17985 : mypgxactoff = MyProc->pgxactoff;
1488 : 17985 : numProcs = arrayP->numProcs;
1451 fujii@postgresql.org 1489 [ + + ]: 163401 : for (int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
1490 : : {
1491 : : int pgprocno;
1492 : : PGPROC *proc;
1493 : : TransactionId pxid;
1494 : : int pxids;
1495 : :
1496 : : /* Ignore ourselves --- dealt with it above */
1849 andres@anarazel.de 1497 [ + + ]: 151225 : if (pgxactoff == mypgxactoff)
6560 tgl@sss.pgh.pa.us 1498 : 13858 : continue;
1499 : :
1500 : : /* Fetch xid just once - see GetNewTransactionId */
1849 andres@anarazel.de 1501 : 137367 : pxid = UINT32_ACCESS_ONCE(other_xids[pgxactoff]);
1502 : :
7415 tgl@sss.pgh.pa.us 1503 [ + + ]: 137367 : if (!TransactionIdIsValid(pxid))
1504 : 80213 : continue;
1505 : :
1506 : : /*
1507 : : * Step 1: check the main Xid
1508 : : */
1509 [ + + ]: 57154 : if (TransactionIdEquals(pxid, xid))
1510 : : {
6560 1511 : 5676 : LWLockRelease(ProcArrayLock);
1512 : : xc_by_main_xid_inc();
1513 : 5676 : return true;
1514 : : }
1515 : :
1516 : : /*
1517 : : * We can ignore main Xids that are younger than the target Xid, since
1518 : : * the target could not possibly be their child.
1519 : : */
7415 1520 [ + + ]: 51478 : if (TransactionIdPrecedes(xid, pxid))
1521 : 29387 : continue;
1522 : :
1523 : : /*
1524 : : * Step 2: check the cached child-Xids arrays
1525 : : */
1849 andres@anarazel.de 1526 : 22091 : pxids = other_subxidstates[pgxactoff].count;
2493 1527 : 22091 : pg_read_barrier(); /* pairs with barrier in GetNewTransactionId() */
1849 1528 : 22091 : pgprocno = arrayP->pgprocnos[pgxactoff];
1529 : 22091 : proc = &allProcs[pgprocno];
2493 1530 [ + + ]: 36861 : for (j = pxids - 1; j >= 0; j--)
1531 : : {
1532 : : /* Fetch xid just once - see GetNewTransactionId */
1533 : 14903 : TransactionId cxid = UINT32_ACCESS_ONCE(proc->subxids.xids[j]);
1534 : :
7415 tgl@sss.pgh.pa.us 1535 [ + + ]: 14903 : if (TransactionIdEquals(cxid, xid))
1536 : : {
6560 1537 : 133 : LWLockRelease(ProcArrayLock);
1538 : : xc_by_child_xid_inc();
1539 : 133 : return true;
1540 : : }
1541 : : }
1542 : :
1543 : : /*
1544 : : * Save the main Xid for step 4. We only need to remember main Xids
1545 : : * that have uncached children. (Note: there is no race condition
1546 : : * here because the overflowed flag cannot be cleared, only set, while
1547 : : * we hold ProcArrayLock. So we can't miss an Xid that we need to
1548 : : * worry about.)
1549 : : */
1849 andres@anarazel.de 1550 [ + + ]: 21958 : if (other_subxidstates[pgxactoff].overflowed)
7415 tgl@sss.pgh.pa.us 1551 : 157 : xids[nxids++] = pxid;
1552 : : }
1553 : :
1554 : : /*
1555 : : * Step 3: in hot standby mode, check the known-assigned-xids list. XIDs
1556 : : * in the list must be treated as running.
1557 : : */
5740 simon@2ndQuadrant.co 1558 [ + + ]: 12176 : if (RecoveryInProgress())
1559 : : {
1560 : : /* none of the PGPROC entries should have XIDs in hot standby mode */
1561 [ - + ]: 1 : Assert(nxids == 0);
1562 : :
5610 tgl@sss.pgh.pa.us 1563 [ - + ]: 1 : if (KnownAssignedXidExists(xid))
1564 : : {
5740 simon@2ndQuadrant.co 1565 :UBC 0 : LWLockRelease(ProcArrayLock);
1566 : : xc_by_known_assigned_inc();
1567 : 0 : return true;
1568 : : }
1569 : :
1570 : : /*
1571 : : * If the KnownAssignedXids overflowed, we have to check pg_subtrans
1572 : : * too. Fetch all xids from KnownAssignedXids that are lower than
1573 : : * xid, since if xid is a subtransaction its parent will always have a
1574 : : * lower value. Note we will collect both main and subXIDs here, but
1575 : : * there's no help for it.
1576 : : */
5740 simon@2ndQuadrant.co 1577 [ - + ]:CBC 1 : if (TransactionIdPrecedesOrEquals(xid, procArray->lastOverflowedXid))
5740 simon@2ndQuadrant.co 1578 :UBC 0 : nxids = KnownAssignedXidsGet(xids, xid);
1579 : : }
1580 : :
7415 tgl@sss.pgh.pa.us 1581 :CBC 12176 : LWLockRelease(ProcArrayLock);
1582 : :
1583 : : /*
1584 : : * If none of the relevant caches overflowed, we know the Xid is not
1585 : : * running without even looking at pg_subtrans.
1586 : : */
1587 [ + + ]: 12176 : if (nxids == 0)
1588 : : {
1589 : : xc_no_overflow_inc();
1167 heikki.linnakangas@i 1590 : 12019 : cachedXidIsNotInProgress = xid;
6560 tgl@sss.pgh.pa.us 1591 : 12019 : return false;
1592 : : }
1593 : :
1594 : : /*
1595 : : * Step 4: have to check pg_subtrans.
1596 : : *
1597 : : * At this point, we know it's either a subtransaction of one of the Xids
1598 : : * in xids[], or it's not running. If it's an already-failed
1599 : : * subtransaction, we want to say "not running" even though its parent may
1600 : : * still be running. So first, check pg_xact to see if it's been aborted.
1601 : : */
1602 : : xc_slow_answer_inc();
1603 : :
7415 1604 [ - + ]: 157 : if (TransactionIdDidAbort(xid))
1605 : : {
1167 heikki.linnakangas@i 1606 :UBC 0 : cachedXidIsNotInProgress = xid;
6560 tgl@sss.pgh.pa.us 1607 : 0 : return false;
1608 : : }
1609 : :
1610 : : /*
1611 : : * It isn't aborted, so check whether the transaction tree it belongs to
1612 : : * is still running (or, more precisely, whether it was running when we
1613 : : * held ProcArrayLock).
1614 : : */
7415 tgl@sss.pgh.pa.us 1615 :CBC 157 : topxid = SubTransGetTopmostTransaction(xid);
1616 [ - + ]: 157 : Assert(TransactionIdIsValid(topxid));
1080 michael@paquier.xyz 1617 [ + - + - ]: 314 : if (!TransactionIdEquals(topxid, xid) &&
1618 : 157 : pg_lfind32(topxid, xids, nxids))
1619 : 157 : return true;
1620 : :
1167 heikki.linnakangas@i 1621 :UBC 0 : cachedXidIsNotInProgress = xid;
6560 tgl@sss.pgh.pa.us 1622 : 0 : return false;
1623 : : }
1624 : :
1625 : :
1626 : : /*
1627 : : * Determine XID horizons.
1628 : : *
1629 : : * This is used by wrapper functions like GetOldestNonRemovableTransactionId()
1630 : : * (for VACUUM), GetReplicationHorizons() (for hot_standby_feedback), etc as
1631 : : * well as "internally" by GlobalVisUpdate() (see comment above struct
1632 : : * GlobalVisState).
1633 : : *
1634 : : * See the definition of ComputeXidHorizonsResult for the various computed
1635 : : * horizons.
1636 : : *
1637 : : * For VACUUM separate horizons (used to decide which deleted tuples must
1638 : : * be preserved), for shared and non-shared tables are computed. For shared
1639 : : * relations backends in all databases must be considered, but for non-shared
1640 : : * relations that's not required, since only backends in my own database could
1641 : : * ever see the tuples in them. Also, we can ignore concurrently running lazy
1642 : : * VACUUMs because (a) they must be working on other tables, and (b) they
1643 : : * don't need to do snapshot-based lookups.
1644 : : *
1645 : : * This also computes a horizon used to truncate pg_subtrans. For that
1646 : : * backends in all databases have to be considered, and concurrently running
1647 : : * lazy VACUUMs cannot be ignored, as they still may perform pg_subtrans
1648 : : * accesses.
1649 : : *
1650 : : * Note: we include all currently running xids in the set of considered xids.
1651 : : * This ensures that if a just-started xact has not yet set its snapshot,
1652 : : * when it does set the snapshot it cannot set xmin less than what we compute.
1653 : : * See notes in src/backend/access/transam/README.
1654 : : *
1655 : : * Note: despite the above, it's possible for the calculated values to move
1656 : : * backwards on repeated calls. The calculated values are conservative, so
1657 : : * that anything older is definitely not considered as running by anyone
1658 : : * anymore, but the exact values calculated depend on a number of things. For
1659 : : * example, if there are no transactions running in the current database, the
1660 : : * horizon for normal tables will be latestCompletedXid. If a transaction
1661 : : * begins after that, its xmin will include in-progress transactions in other
1662 : : * databases that started earlier, so another call will return a lower value.
1663 : : * Nonetheless it is safe to vacuum a table in the current database with the
1664 : : * first result. There are also replication-related effects: a walsender
1665 : : * process can set its xmin based on transactions that are no longer running
1666 : : * on the primary but are still being replayed on the standby, thus possibly
1667 : : * making the values go backwards. In this case there is a possibility that
1668 : : * we lose data that the standby would like to have, but unless the standby
1669 : : * uses a replication slot to make its xmin persistent there is little we can
1670 : : * do about that --- data is only protected if the walsender runs continuously
1671 : : * while queries are executed on the standby. (The Hot Standby code deals
1672 : : * with such cases by failing standby queries that needed to access
1673 : : * already-removed data, so there's no integrity bug.)
1674 : : *
1675 : : * Note: the approximate horizons (see definition of GlobalVisState) are
1676 : : * updated by the computations done here. That's currently required for
1677 : : * correctness and a small optimization. Without doing so it's possible that
1678 : : * heap vacuum's call to heap_page_prune_and_freeze() uses a more conservative
1679 : : * horizon than later when deciding which tuples can be removed - which the
1680 : : * code doesn't expect (breaking HOT).
1681 : : */
1682 : : static void
1851 andres@anarazel.de 1683 :CBC 96558 : ComputeXidHorizons(ComputeXidHorizonsResult *h)
1684 : : {
7415 tgl@sss.pgh.pa.us 1685 : 96558 : ProcArrayStruct *arrayP = procArray;
1686 : : TransactionId kaxmin;
1851 andres@anarazel.de 1687 : 96558 : bool in_recovery = RecoveryInProgress();
1849 1688 : 96558 : TransactionId *other_xids = ProcGlobal->xids;
1689 : :
1690 : : /* inferred after ProcArrayLock is released */
1194 alvherre@alvh.no-ip. 1691 : 96558 : h->catalog_oldest_nonremovable = InvalidTransactionId;
1692 : :
6573 tgl@sss.pgh.pa.us 1693 : 96558 : LWLockAcquire(ProcArrayLock, LW_SHARED);
1694 : :
638 heikki.linnakangas@i 1695 : 96558 : h->latest_completed = TransamVariables->latestCompletedXid;
1696 : :
1697 : : /*
1698 : : * We initialize the MIN() calculation with latestCompletedXid + 1. This
1699 : : * is a lower bound for the XIDs that might appear in the ProcArray later,
1700 : : * and so protects us against overestimating the result due to future
1701 : : * additions.
1702 : : */
1703 : : {
1704 : : TransactionId initial;
1705 : :
1851 andres@anarazel.de 1706 : 96558 : initial = XidFromFullTransactionId(h->latest_completed);
1707 [ - + ]: 96558 : Assert(TransactionIdIsValid(initial));
1708 [ - + ]: 96558 : TransactionIdAdvance(initial);
1709 : :
1710 : 96558 : h->oldest_considered_running = initial;
1711 : 96558 : h->shared_oldest_nonremovable = initial;
1712 : 96558 : h->data_oldest_nonremovable = initial;
1713 : :
1714 : : /*
1715 : : * Only modifications made by this backend affect the horizon for
1716 : : * temporary relations. Instead of a check in each iteration of the
1717 : : * loop over all PGPROCs it is cheaper to just initialize to the
1718 : : * current top-level xid any.
1719 : : *
1720 : : * Without an assigned xid we could use a horizon as aggressive as
1721 : : * GetNewTransactionId(), but we can get away with the much cheaper
1722 : : * latestCompletedXid + 1: If this backend has no xid there, by
1723 : : * definition, can't be any newer changes in the temp table than
1724 : : * latestCompletedXid.
1725 : : */
1774 1726 [ + + ]: 96558 : if (TransactionIdIsValid(MyProc->xid))
1727 : 31846 : h->temp_oldest_nonremovable = MyProc->xid;
1728 : : else
1729 : 64712 : h->temp_oldest_nonremovable = initial;
1730 : : }
1731 : :
1732 : : /*
1733 : : * Fetch slot horizons while ProcArrayLock is held - the
1734 : : * LWLockAcquire/LWLockRelease are a barrier, ensuring this happens inside
1735 : : * the lock.
1736 : : */
1851 1737 : 96558 : h->slot_xmin = procArray->replication_slot_xmin;
1738 : 96558 : h->slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
1739 : :
1740 [ + + ]: 533883 : for (int index = 0; index < arrayP->numProcs; index++)
1741 : : {
4836 bruce@momjian.us 1742 : 437325 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 1743 : 437325 : PGPROC *proc = &allProcs[pgprocno];
1755 alvherre@alvh.no-ip. 1744 : 437325 : int8 statusFlags = ProcGlobal->statusFlags[index];
1745 : : TransactionId xid;
1746 : : TransactionId xmin;
1747 : :
1748 : : /* Fetch xid just once - see GetNewTransactionId */
1847 andres@anarazel.de 1749 : 437325 : xid = UINT32_ACCESS_ONCE(other_xids[index]);
1850 1750 : 437325 : xmin = UINT32_ACCESS_ONCE(proc->xmin);
1751 : :
1752 : : /*
1753 : : * Consider both the transaction's Xmin, and its Xid.
1754 : : *
1755 : : * We must check both because a transaction might have an Xmin but not
1756 : : * (yet) an Xid; conversely, if it has an Xid, that could determine
1757 : : * some not-yet-set Xmin.
1758 : : */
1851 1759 : 437325 : xmin = TransactionIdOlder(xmin, xid);
1760 : :
1761 : : /* if neither is set, this proc doesn't influence the horizon */
1812 peter@eisentraut.org 1762 [ + + ]: 437325 : if (!TransactionIdIsValid(xmin))
6978 alvherre@alvh.no-ip. 1763 : 230780 : continue;
1764 : :
1765 : : /*
1766 : : * Don't ignore any procs when determining which transactions might be
1767 : : * considered running. While slots should ensure logical decoding
1768 : : * backends are protected even without this check, it can't hurt to
1769 : : * include them here as well..
1770 : : */
1851 andres@anarazel.de 1771 : 206545 : h->oldest_considered_running =
1772 : 206545 : TransactionIdOlder(h->oldest_considered_running, xmin);
1773 : :
1774 : : /*
1775 : : * Skip over backends either vacuuming (which is ok with rows being
1776 : : * removed, as long as pg_subtrans is not truncated) or doing logical
1777 : : * decoding (which manages xmin separately, check below).
1778 : : */
1755 alvherre@alvh.no-ip. 1779 [ + + ]: 206545 : if (statusFlags & (PROC_IN_VACUUM | PROC_IN_LOGICAL_DECODING))
1851 andres@anarazel.de 1780 : 15302 : continue;
1781 : :
1782 : : /* shared tables need to take backends in all databases into account */
1783 : 191243 : h->shared_oldest_nonremovable =
1784 : 191243 : TransactionIdOlder(h->shared_oldest_nonremovable, xmin);
1785 : :
1786 : : /*
1787 : : * Normally sessions in other databases are ignored for anything but
1788 : : * the shared horizon.
1789 : : *
1790 : : * However, include them when MyDatabaseId is not (yet) set. A
1791 : : * backend in the process of starting up must not compute a "too
1792 : : * aggressive" horizon, otherwise we could end up using it to prune
1793 : : * still-needed data away. If the current backend never connects to a
1794 : : * database this is harmless, because data_oldest_nonremovable will
1795 : : * never be utilized.
1796 : : *
1797 : : * Also, sessions marked with PROC_AFFECTS_ALL_HORIZONS should always
1798 : : * be included. (This flag is used for hot standby feedback, which
1799 : : * can't be tied to a specific database.)
1800 : : *
1801 : : * Also, while in recovery we cannot compute an accurate per-database
1802 : : * horizon, as all xids are managed via the KnownAssignedXids
1803 : : * machinery.
1804 : : */
1240 tgl@sss.pgh.pa.us 1805 [ + + ]: 191243 : if (proc->databaseId == MyDatabaseId ||
1806 [ + + ]: 8041 : MyDatabaseId == InvalidOid ||
1807 [ + + - + ]: 525 : (statusFlags & PROC_AFFECTS_ALL_HORIZONS) ||
1808 : : in_recovery)
1809 : : {
1194 alvherre@alvh.no-ip. 1810 : 190720 : h->data_oldest_nonremovable =
1811 : 190720 : TransactionIdOlder(h->data_oldest_nonremovable, xmin);
1812 : : }
1813 : : }
1814 : :
1815 : : /*
1816 : : * If in recovery fetch oldest xid in KnownAssignedXids, will be applied
1817 : : * after lock is released.
1818 : : */
1851 andres@anarazel.de 1819 [ + + ]: 96558 : if (in_recovery)
1820 : 338 : kaxmin = KnownAssignedXidsGetOldestXmin();
1821 : :
1822 : : /*
1823 : : * No other information from shared state is needed, release the lock
1824 : : * immediately. The rest of the computations can be done without a lock.
1825 : : */
1826 : 96558 : LWLockRelease(ProcArrayLock);
1827 : :
1828 [ + + ]: 96558 : if (in_recovery)
1829 : : {
1830 : 338 : h->oldest_considered_running =
1831 : 338 : TransactionIdOlder(h->oldest_considered_running, kaxmin);
1832 : 338 : h->shared_oldest_nonremovable =
1833 : 338 : TransactionIdOlder(h->shared_oldest_nonremovable, kaxmin);
1834 : 338 : h->data_oldest_nonremovable =
1835 : 338 : TransactionIdOlder(h->data_oldest_nonremovable, kaxmin);
1836 : : /* temp relations cannot be accessed in recovery */
1837 : : }
1838 : :
866 1839 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->oldest_considered_running,
1840 : : h->shared_oldest_nonremovable));
1841 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->shared_oldest_nonremovable,
1842 : : h->data_oldest_nonremovable));
1843 : :
1844 : : /*
1845 : : * Check whether there are replication slots requiring an older xmin.
1846 : : */
1851 1847 : 96558 : h->shared_oldest_nonremovable =
1848 : 96558 : TransactionIdOlder(h->shared_oldest_nonremovable, h->slot_xmin);
1849 : 96558 : h->data_oldest_nonremovable =
1850 : 96558 : TransactionIdOlder(h->data_oldest_nonremovable, h->slot_xmin);
1851 : :
1852 : : /*
1853 : : * The only difference between catalog / data horizons is that the slot's
1854 : : * catalog xmin is applied to the catalog one (so catalogs can be accessed
1855 : : * for logical decoding). Initialize with data horizon, and then back up
1856 : : * further if necessary. Have to back up the shared horizon as well, since
1857 : : * that also can contain catalogs.
1858 : : */
1859 : 96558 : h->shared_oldest_nonremovable_raw = h->shared_oldest_nonremovable;
1860 : 96558 : h->shared_oldest_nonremovable =
1861 : 96558 : TransactionIdOlder(h->shared_oldest_nonremovable,
1862 : : h->slot_catalog_xmin);
1194 alvherre@alvh.no-ip. 1863 : 96558 : h->catalog_oldest_nonremovable = h->data_oldest_nonremovable;
1851 andres@anarazel.de 1864 : 96558 : h->catalog_oldest_nonremovable =
1865 : 96558 : TransactionIdOlder(h->catalog_oldest_nonremovable,
1866 : : h->slot_catalog_xmin);
1867 : :
1868 : : /*
1869 : : * It's possible that slots backed up the horizons further than
1870 : : * oldest_considered_running. Fix.
1871 : : */
1872 : 96558 : h->oldest_considered_running =
1873 : 96558 : TransactionIdOlder(h->oldest_considered_running,
1874 : : h->shared_oldest_nonremovable);
1875 : 96558 : h->oldest_considered_running =
1876 : 96558 : TransactionIdOlder(h->oldest_considered_running,
1877 : : h->catalog_oldest_nonremovable);
1878 : 96558 : h->oldest_considered_running =
1879 : 96558 : TransactionIdOlder(h->oldest_considered_running,
1880 : : h->data_oldest_nonremovable);
1881 : :
1882 : : /*
1883 : : * shared horizons have to be at least as old as the oldest visible in
1884 : : * current db
1885 : : */
1886 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->shared_oldest_nonremovable,
1887 : : h->data_oldest_nonremovable));
1888 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->shared_oldest_nonremovable,
1889 : : h->catalog_oldest_nonremovable));
1890 : :
1891 : : /*
1892 : : * Horizons need to ensure that pg_subtrans access is still possible for
1893 : : * the relevant backends.
1894 : : */
1895 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->oldest_considered_running,
1896 : : h->shared_oldest_nonremovable));
1897 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->oldest_considered_running,
1898 : : h->catalog_oldest_nonremovable));
1899 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->oldest_considered_running,
1900 : : h->data_oldest_nonremovable));
1774 1901 [ - + ]: 96558 : Assert(TransactionIdPrecedesOrEquals(h->oldest_considered_running,
1902 : : h->temp_oldest_nonremovable));
1851 1903 [ + + - + ]: 96558 : Assert(!TransactionIdIsValid(h->slot_xmin) ||
1904 : : TransactionIdPrecedesOrEquals(h->oldest_considered_running,
1905 : : h->slot_xmin));
1906 [ + + - + ]: 96558 : Assert(!TransactionIdIsValid(h->slot_catalog_xmin) ||
1907 : : TransactionIdPrecedesOrEquals(h->oldest_considered_running,
1908 : : h->slot_catalog_xmin));
1909 : :
1910 : : /* update approximate horizons with the computed horizons */
1911 : 96558 : GlobalVisUpdateApply(h);
1912 : 96558 : }
1913 : :
1914 : : /*
1915 : : * Determine what kind of visibility horizon needs to be used for a
1916 : : * relation. If rel is NULL, the most conservative horizon is used.
1917 : : */
1918 : : static inline GlobalVisHorizonKind
1505 1919 : 14093431 : GlobalVisHorizonKindForRel(Relation rel)
1920 : : {
1921 : : /*
1922 : : * Other relkinds currently don't contain xids, nor always the necessary
1923 : : * logical decoding markers.
1924 : : */
1925 [ + + + + : 14093431 : Assert(!rel ||
+ + - + ]
1926 : : rel->rd_rel->relkind == RELKIND_RELATION ||
1927 : : rel->rd_rel->relkind == RELKIND_MATVIEW ||
1928 : : rel->rd_rel->relkind == RELKIND_TOASTVALUE);
1929 : :
1930 [ + + + + : 14093431 : if (rel == NULL || rel->rd_rel->relisshared || RecoveryInProgress())
+ + ]
1931 : 73696 : return VISHORIZON_SHARED;
1932 [ + + ]: 14019735 : else if (IsCatalogRelation(rel) ||
1933 [ + + + + : 11356772 : RelationIsAccessibleInLogicalDecoding(rel))
- + - - -
- + - + +
- + - - +
- ]
1934 : 2662964 : return VISHORIZON_CATALOG;
1935 [ + + + + ]: 11356771 : else if (!RELATION_IS_LOCAL(rel))
1936 : 11302859 : return VISHORIZON_DATA;
1937 : : else
1938 : 53912 : return VISHORIZON_TEMP;
1939 : : }
1940 : :
1941 : : /*
1942 : : * Return the oldest XID for which deleted tuples must be preserved in the
1943 : : * passed table.
1944 : : *
1945 : : * If rel is not NULL the horizon may be considerably more recent than
1946 : : * otherwise (i.e. fewer tuples will be removable). In the NULL case a horizon
1947 : : * that is correct (but not optimal) for all relations will be returned.
1948 : : *
1949 : : * This is used by VACUUM to decide which deleted tuples must be preserved in
1950 : : * the passed in table.
1951 : : */
1952 : : TransactionId
1851 1953 : 42756 : GetOldestNonRemovableTransactionId(Relation rel)
1954 : : {
1955 : : ComputeXidHorizonsResult horizons;
1956 : :
1957 : 42756 : ComputeXidHorizons(&horizons);
1958 : :
1505 1959 [ + + + + : 42756 : switch (GlobalVisHorizonKindForRel(rel))
- ]
1960 : : {
1961 : 4187 : case VISHORIZON_SHARED:
1962 : 4187 : return horizons.shared_oldest_nonremovable;
1963 : 14582 : case VISHORIZON_CATALOG:
1964 : 14582 : return horizons.catalog_oldest_nonremovable;
1965 : 12024 : case VISHORIZON_DATA:
1966 : 12024 : return horizons.data_oldest_nonremovable;
1967 : 11963 : case VISHORIZON_TEMP:
1968 : 11963 : return horizons.temp_oldest_nonremovable;
1969 : : }
1970 : :
1971 : : /* just to prevent compiler warnings */
1505 andres@anarazel.de 1972 :UBC 0 : return InvalidTransactionId;
1973 : : }
1974 : :
1975 : : /*
1976 : : * Return the oldest transaction id any currently running backend might still
1977 : : * consider running. This should not be used for visibility / pruning
1978 : : * determinations (see GetOldestNonRemovableTransactionId()), but for
1979 : : * decisions like up to where pg_subtrans can be truncated.
1980 : : */
1981 : : TransactionId
1851 andres@anarazel.de 1982 :CBC 1649 : GetOldestTransactionIdConsideredRunning(void)
1983 : : {
1984 : : ComputeXidHorizonsResult horizons;
1985 : :
1986 : 1649 : ComputeXidHorizons(&horizons);
1987 : :
1988 : 1649 : return horizons.oldest_considered_running;
1989 : : }
1990 : :
1991 : : /*
1992 : : * Return the visibility horizons for a hot standby feedback message.
1993 : : */
1994 : : void
1995 : 36 : GetReplicationHorizons(TransactionId *xmin, TransactionId *catalog_xmin)
1996 : : {
1997 : : ComputeXidHorizonsResult horizons;
1998 : :
1999 : 36 : ComputeXidHorizons(&horizons);
2000 : :
2001 : : /*
2002 : : * Don't want to use shared_oldest_nonremovable here, as that contains the
2003 : : * effect of replication slot's catalog_xmin. We want to send a separate
2004 : : * feedback for the catalog horizon, so the primary can remove data table
2005 : : * contents more aggressively.
2006 : : */
2007 : 36 : *xmin = horizons.shared_oldest_nonremovable_raw;
2008 : 36 : *catalog_xmin = horizons.slot_catalog_xmin;
7415 tgl@sss.pgh.pa.us 2009 : 36 : }
2010 : :
2011 : : /*
2012 : : * GetMaxSnapshotXidCount -- get max size for snapshot XID array
2013 : : *
2014 : : * We have to export this for use by snapmgr.c.
2015 : : */
2016 : : int
5068 2017 : 30018 : GetMaxSnapshotXidCount(void)
2018 : : {
2019 : 30018 : return procArray->maxProcs;
2020 : : }
2021 : :
2022 : : /*
2023 : : * GetMaxSnapshotSubxidCount -- get max size for snapshot sub-XID array
2024 : : *
2025 : : * We have to export this for use by snapmgr.c.
2026 : : */
2027 : : int
2028 : 29833 : GetMaxSnapshotSubxidCount(void)
2029 : : {
2030 : 29833 : return TOTAL_MAX_CACHED_SUBXIDS;
2031 : : }
2032 : :
2033 : : /*
2034 : : * Helper function for GetSnapshotData() that checks if the bulk of the
2035 : : * visibility information in the snapshot is still valid. If so, it updates
2036 : : * the fields that need to change and returns true. Otherwise it returns
2037 : : * false.
2038 : : *
2039 : : * This very likely can be evolved to not need ProcArrayLock held (at very
2040 : : * least in the case we already hold a snapshot), but that's for another day.
2041 : : */
2042 : : static bool
1846 andres@anarazel.de 2043 : 1792817 : GetSnapshotDataReuse(Snapshot snapshot)
2044 : : {
2045 : : uint64 curXactCompletionCount;
2046 : :
2047 [ - + ]: 1792817 : Assert(LWLockHeldByMe(ProcArrayLock));
2048 : :
2049 [ + + ]: 1792817 : if (unlikely(snapshot->snapXactCompletionCount == 0))
2050 : 28420 : return false;
2051 : :
638 heikki.linnakangas@i 2052 : 1764397 : curXactCompletionCount = TransamVariables->xactCompletionCount;
1846 andres@anarazel.de 2053 [ + + ]: 1764397 : if (curXactCompletionCount != snapshot->snapXactCompletionCount)
2054 : 350090 : return false;
2055 : :
2056 : : /*
2057 : : * If the current xactCompletionCount is still the same as it was at the
2058 : : * time the snapshot was built, we can be sure that rebuilding the
2059 : : * contents of the snapshot the hard way would result in the same snapshot
2060 : : * contents:
2061 : : *
2062 : : * As explained in transam/README, the set of xids considered running by
2063 : : * GetSnapshotData() cannot change while ProcArrayLock is held. Snapshot
2064 : : * contents only depend on transactions with xids and xactCompletionCount
2065 : : * is incremented whenever a transaction with an xid finishes (while
2066 : : * holding ProcArrayLock exclusively). Thus the xactCompletionCount check
2067 : : * ensures we would detect if the snapshot would have changed.
2068 : : *
2069 : : * As the snapshot contents are the same as it was before, it is safe to
2070 : : * re-enter the snapshot's xmin into the PGPROC array. None of the rows
2071 : : * visible under the snapshot could already have been removed (that'd
2072 : : * require the set of running transactions to change) and it fulfills the
2073 : : * requirement that concurrent GetSnapshotData() calls yield the same
2074 : : * xmin.
2075 : : */
2076 [ + + ]: 1414307 : if (!TransactionIdIsValid(MyProc->xmin))
2077 : 476911 : MyProc->xmin = TransactionXmin = snapshot->xmin;
2078 : :
2079 : 1414307 : RecentXmin = snapshot->xmin;
2080 [ - + ]: 1414307 : Assert(TransactionIdPrecedesOrEquals(TransactionXmin, RecentXmin));
2081 : :
2082 : 1414307 : snapshot->curcid = GetCurrentCommandId(false);
2083 : 1414307 : snapshot->active_count = 0;
2084 : 1414307 : snapshot->regd_count = 0;
2085 : 1414307 : snapshot->copied = false;
2086 : :
2087 : 1414307 : return true;
2088 : : }
2089 : :
2090 : : /*
2091 : : * GetSnapshotData -- returns information about running transactions.
2092 : : *
2093 : : * The returned snapshot includes xmin (lowest still-running xact ID),
2094 : : * xmax (highest completed xact ID + 1), and a list of running xact IDs
2095 : : * in the range xmin <= xid < xmax. It is used as follows:
2096 : : * All xact IDs < xmin are considered finished.
2097 : : * All xact IDs >= xmax are considered still running.
2098 : : * For an xact ID xmin <= xid < xmax, consult list to see whether
2099 : : * it is considered running or not.
2100 : : * This ensures that the set of transactions seen as "running" by the
2101 : : * current xact will not change after it takes the snapshot.
2102 : : *
2103 : : * All running top-level XIDs are included in the snapshot, except for lazy
2104 : : * VACUUM processes. We also try to include running subtransaction XIDs,
2105 : : * but since PGPROC has only a limited cache area for subxact XIDs, full
2106 : : * information may not be available. If we find any overflowed subxid arrays,
2107 : : * we have to mark the snapshot's subxid data as overflowed, and extra work
2108 : : * *may* need to be done to determine what's running (see XidInMVCCSnapshot()).
2109 : : *
2110 : : * We also update the following backend-global variables:
2111 : : * TransactionXmin: the oldest xmin of any snapshot in use in the
2112 : : * current transaction (this is the same as MyProc->xmin).
2113 : : * RecentXmin: the xmin computed for the most recent snapshot. XIDs
2114 : : * older than this are known not running any more.
2115 : : *
2116 : : * And try to advance the bounds of GlobalVis{Shared,Catalog,Data,Temp}Rels
2117 : : * for the benefit of the GlobalVisTest* family of functions.
2118 : : *
2119 : : * Note: this function should probably not be called with an argument that's
2120 : : * not statically allocated (see xip allocation below).
2121 : : */
2122 : : Snapshot
6326 alvherre@alvh.no-ip. 2123 : 1792817 : GetSnapshotData(Snapshot snapshot)
2124 : : {
7415 tgl@sss.pgh.pa.us 2125 : 1792817 : ProcArrayStruct *arrayP = procArray;
1849 andres@anarazel.de 2126 : 1792817 : TransactionId *other_xids = ProcGlobal->xids;
2127 : : TransactionId xmin;
2128 : : TransactionId xmax;
1451 fujii@postgresql.org 2129 : 1792817 : int count = 0;
6943 tgl@sss.pgh.pa.us 2130 : 1792817 : int subcount = 0;
5740 simon@2ndQuadrant.co 2131 : 1792817 : bool suboverflowed = false;
2132 : : FullTransactionId latest_completed;
2133 : : TransactionId oldestxid;
2134 : : int mypgxactoff;
2135 : : TransactionId myxid;
2136 : : uint64 curXactCompletionCount;
2137 : :
2493 andres@anarazel.de 2138 : 1792817 : TransactionId replication_slot_xmin = InvalidTransactionId;
2139 : 1792817 : TransactionId replication_slot_catalog_xmin = InvalidTransactionId;
2140 : :
7415 tgl@sss.pgh.pa.us 2141 [ - + ]: 1792817 : Assert(snapshot != NULL);
2142 : :
2143 : : /*
2144 : : * Allocating space for maxProcs xids is usually overkill; numProcs would
2145 : : * be sufficient. But it seems better to do the malloc while not holding
2146 : : * the lock, so we can't look at numProcs. Likewise, we allocate much
2147 : : * more subxip storage than is probably needed.
2148 : : *
2149 : : * This does open a possibility for avoiding repeated malloc/free: since
2150 : : * maxProcs does not change at runtime, we can simply reuse the previous
2151 : : * xip arrays if any. (This relies on the fact that all callers pass
2152 : : * static SnapshotData structs.)
2153 : : */
2154 [ + + ]: 1792817 : if (snapshot->xip == NULL)
2155 : : {
2156 : : /*
2157 : : * First call for this snapshot. Snapshot is same size whether or not
2158 : : * we are in recovery, see later comments.
2159 : : */
2160 : 28215 : snapshot->xip = (TransactionId *)
5068 2161 : 28215 : malloc(GetMaxSnapshotXidCount() * sizeof(TransactionId));
7415 2162 [ - + ]: 28215 : if (snapshot->xip == NULL)
7415 tgl@sss.pgh.pa.us 2163 [ # # ]:UBC 0 : ereport(ERROR,
2164 : : (errcode(ERRCODE_OUT_OF_MEMORY),
2165 : : errmsg("out of memory")));
6943 tgl@sss.pgh.pa.us 2166 [ - + ]:CBC 28215 : Assert(snapshot->subxip == NULL);
2167 : 28215 : snapshot->subxip = (TransactionId *)
5068 2168 : 28215 : malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
6943 2169 [ - + ]: 28215 : if (snapshot->subxip == NULL)
6943 tgl@sss.pgh.pa.us 2170 [ # # ]:UBC 0 : ereport(ERROR,
2171 : : (errcode(ERRCODE_OUT_OF_MEMORY),
2172 : : errmsg("out of memory")));
2173 : : }
2174 : :
2175 : : /*
2176 : : * It is sufficient to get shared lock on ProcArrayLock, even if we are
2177 : : * going to set MyProc->xmin.
2178 : : */
6943 tgl@sss.pgh.pa.us 2179 :CBC 1792817 : LWLockAcquire(ProcArrayLock, LW_SHARED);
2180 : :
1846 andres@anarazel.de 2181 [ + + ]: 1792817 : if (GetSnapshotDataReuse(snapshot))
2182 : : {
2183 : 1414307 : LWLockRelease(ProcArrayLock);
2184 : 1414307 : return snapshot;
2185 : : }
2186 : :
638 heikki.linnakangas@i 2187 : 378510 : latest_completed = TransamVariables->latestCompletedXid;
1849 andres@anarazel.de 2188 : 378510 : mypgxactoff = MyProc->pgxactoff;
2189 : 378510 : myxid = other_xids[mypgxactoff];
2190 [ - + ]: 378510 : Assert(myxid == MyProc->xid);
2191 : :
638 heikki.linnakangas@i 2192 : 378510 : oldestxid = TransamVariables->oldestXid;
2193 : 378510 : curXactCompletionCount = TransamVariables->xactCompletionCount;
2194 : :
2195 : : /* xmax is always latestCompletedXid + 1 */
1852 andres@anarazel.de 2196 : 378510 : xmax = XidFromFullTransactionId(latest_completed);
6573 tgl@sss.pgh.pa.us 2197 [ - + ]: 378510 : TransactionIdAdvance(xmax);
1852 andres@anarazel.de 2198 [ - + ]: 378510 : Assert(TransactionIdIsNormal(xmax));
2199 : :
2200 : : /* initialize xmin calculation with xmax */
1851 2201 : 378510 : xmin = xmax;
2202 : :
2203 : : /* take own xid into account, saves a check inside the loop */
1849 2204 [ + + + - : 378510 : if (TransactionIdIsNormal(myxid) && NormalTransactionIdPrecedes(myxid, xmin))
- + + + ]
2205 : 30907 : xmin = myxid;
2206 : :
5619 simon@2ndQuadrant.co 2207 : 378510 : snapshot->takenDuringRecovery = RecoveryInProgress();
2208 : :
5620 2209 [ + + ]: 378510 : if (!snapshot->takenDuringRecovery)
2210 : : {
1451 fujii@postgresql.org 2211 : 377815 : int numProcs = arrayP->numProcs;
1849 andres@anarazel.de 2212 : 377815 : TransactionId *xip = snapshot->xip;
4836 bruce@momjian.us 2213 : 377815 : int *pgprocnos = arrayP->pgprocnos;
1849 andres@anarazel.de 2214 : 377815 : XidCacheStatus *subxidStates = ProcGlobal->subxidStates;
1755 alvherre@alvh.no-ip. 2215 : 377815 : uint8 *allStatusFlags = ProcGlobal->statusFlags;
2216 : :
2217 : : /*
2218 : : * First collect set of pgxactoff/xids that need to be included in the
2219 : : * snapshot.
2220 : : */
1451 fujii@postgresql.org 2221 [ + + ]: 3221691 : for (int pgxactoff = 0; pgxactoff < numProcs; pgxactoff++)
2222 : : {
2223 : : /* Fetch xid just once - see GetNewTransactionId */
1849 andres@anarazel.de 2224 : 2843876 : TransactionId xid = UINT32_ACCESS_ONCE(other_xids[pgxactoff]);
2225 : : uint8 statusFlags;
2226 : :
2227 [ - + ]: 2843876 : Assert(allProcs[arrayP->pgprocnos[pgxactoff]].pgxactoff == pgxactoff);
2228 : :
2229 : : /*
2230 : : * If the transaction has no XID assigned, we can skip it; it
2231 : : * won't have sub-XIDs either.
2232 : : */
2233 [ + + ]: 2843876 : if (likely(xid == InvalidTransactionId))
6576 tgl@sss.pgh.pa.us 2234 : 2117326 : continue;
2235 : :
2236 : : /*
2237 : : * We don't include our own XIDs (if any) in the snapshot. It
2238 : : * needs to be included in the xmin computation, but we did so
2239 : : * outside the loop.
2240 : : */
1849 andres@anarazel.de 2241 [ + + ]: 726550 : if (pgxactoff == mypgxactoff)
2242 : 53508 : continue;
2243 : :
2244 : : /*
2245 : : * The only way we are able to get here with a non-normal xid is
2246 : : * during bootstrap - with this backend using
2247 : : * BootstrapTransactionId. But the above test should filter that
2248 : : * out.
2249 : : */
2250 [ - + ]: 673042 : Assert(TransactionIdIsNormal(xid));
2251 : :
2252 : : /*
2253 : : * If the XID is >= xmax, we can skip it; such transactions will
2254 : : * be treated as running anyway (and any sub-XIDs will also be >=
2255 : : * xmax).
2256 : : */
2257 [ + - - + : 673042 : if (!NormalTransactionIdPrecedes(xid, xmax))
+ + ]
4836 bruce@momjian.us 2258 : 135243 : continue;
2259 : :
2260 : : /*
2261 : : * Skip over backends doing logical decoding which manages xmin
2262 : : * separately (check below) and ones running LAZY VACUUM.
2263 : : */
1755 alvherre@alvh.no-ip. 2264 : 537799 : statusFlags = allStatusFlags[pgxactoff];
2265 [ + + ]: 537799 : if (statusFlags & (PROC_IN_LOGICAL_DECODING | PROC_IN_VACUUM))
1849 andres@anarazel.de 2266 :GBC 72 : continue;
2267 : :
5013 rhaas@postgresql.org 2268 [ + - - + :CBC 537727 : if (NormalTransactionIdPrecedes(xid, xmin))
+ + ]
2269 : 284552 : xmin = xid;
2270 : :
2271 : : /* Add XID to snapshot. */
1849 andres@anarazel.de 2272 : 537727 : xip[count++] = xid;
2273 : :
2274 : : /*
2275 : : * Save subtransaction XIDs if possible (if we've already
2276 : : * overflowed, there's no point). Note that the subxact XIDs must
2277 : : * be later than their parent, so no need to check them against
2278 : : * xmin. We could filter against xmax, but it seems better not to
2279 : : * do that much work while holding the ProcArrayLock.
2280 : : *
2281 : : * The other backend can add more subxids concurrently, but cannot
2282 : : * remove any. Hence it's important to fetch nxids just once.
2283 : : * Should be safe to use memcpy, though. (We needn't worry about
2284 : : * missing any xids added concurrently, because they must postdate
2285 : : * xmax.)
2286 : : *
2287 : : * Again, our own XIDs are not included in the snapshot.
2288 : : */
5013 rhaas@postgresql.org 2289 [ + - ]: 537727 : if (!suboverflowed)
2290 : : {
2291 : :
1849 andres@anarazel.de 2292 [ + + ]: 537727 : if (subxidStates[pgxactoff].overflowed)
5620 simon@2ndQuadrant.co 2293 : 21 : suboverflowed = true;
2294 : : else
2295 : : {
1849 andres@anarazel.de 2296 : 537706 : int nsubxids = subxidStates[pgxactoff].count;
2297 : :
2298 [ + + ]: 537706 : if (nsubxids > 0)
2299 : : {
2300 : 5758 : int pgprocno = pgprocnos[pgxactoff];
2493 2301 : 5758 : PGPROC *proc = &allProcs[pgprocno];
2302 : :
2303 : 5758 : pg_read_barrier(); /* pairs with GetNewTransactionId */
2304 : :
5620 simon@2ndQuadrant.co 2305 : 5758 : memcpy(snapshot->subxip + subcount,
942 peter@eisentraut.org 2306 : 5758 : proc->subxids.xids,
2307 : : nsubxids * sizeof(TransactionId));
1849 andres@anarazel.de 2308 : 5758 : subcount += nsubxids;
2309 : : }
2310 : : }
2311 : : }
2312 : : }
2313 : : }
2314 : : else
2315 : : {
2316 : : /*
2317 : : * We're in hot standby, so get XIDs from KnownAssignedXids.
2318 : : *
2319 : : * We store all xids directly into subxip[]. Here's why:
2320 : : *
2321 : : * In recovery we don't know which xids are top-level and which are
2322 : : * subxacts, a design choice that greatly simplifies xid processing.
2323 : : *
2324 : : * It seems like we would want to try to put xids into xip[] only, but
2325 : : * that is fairly small. We would either need to make that bigger or
2326 : : * to increase the rate at which we WAL-log xid assignment; neither is
2327 : : * an appealing choice.
2328 : : *
2329 : : * We could try to store xids into xip[] first and then into subxip[]
2330 : : * if there are too many xids. That only works if the snapshot doesn't
2331 : : * overflow because we do not search subxip[] in that case. A simpler
2332 : : * way is to just store all xids in the subxip array because this is
2333 : : * by far the bigger array. We just leave the xip array empty.
2334 : : *
2335 : : * Either way we need to change the way XidInMVCCSnapshot() works
2336 : : * depending upon when the snapshot was taken, or change normal
2337 : : * snapshot processing so it matches.
2338 : : *
2339 : : * Note: It is possible for recovery to end before we finish taking
2340 : : * the snapshot, and for newly assigned transaction ids to be added to
2341 : : * the ProcArray. xmax cannot change while we hold ProcArrayLock, so
2342 : : * those newly added transaction ids would be filtered away, so we
2343 : : * need not be concerned about them.
2344 : : */
5610 tgl@sss.pgh.pa.us 2345 : 695 : subcount = KnownAssignedXidsGetAndSetXmin(snapshot->subxip, &xmin,
2346 : : xmax);
2347 : :
2348 [ + + ]: 695 : if (TransactionIdPrecedesOrEquals(xmin, procArray->lastOverflowedXid))
5740 simon@2ndQuadrant.co 2349 : 6 : suboverflowed = true;
2350 : : }
2351 : :
2352 : :
2353 : : /*
2354 : : * Fetch into local variable while ProcArrayLock is held - the
2355 : : * LWLockRelease below is a barrier, ensuring this happens inside the
2356 : : * lock.
2357 : : */
4236 rhaas@postgresql.org 2358 : 378510 : replication_slot_xmin = procArray->replication_slot_xmin;
4205 2359 : 378510 : replication_slot_catalog_xmin = procArray->replication_slot_catalog_xmin;
2360 : :
1850 andres@anarazel.de 2361 [ + + ]: 378510 : if (!TransactionIdIsValid(MyProc->xmin))
2362 : 203211 : MyProc->xmin = TransactionXmin = xmin;
2363 : :
7415 tgl@sss.pgh.pa.us 2364 : 378510 : LWLockRelease(ProcArrayLock);
2365 : :
2366 : : /* maintain state for GlobalVis* */
2367 : : {
2368 : : TransactionId def_vis_xid;
2369 : : TransactionId def_vis_xid_data;
2370 : : FullTransactionId def_vis_fxid;
2371 : : FullTransactionId def_vis_fxid_data;
2372 : : FullTransactionId oldestfxid;
2373 : :
2374 : : /*
2375 : : * Converting oldestXid is only safe when xid horizon cannot advance,
2376 : : * i.e. holding locks. While we don't hold the lock anymore, all the
2377 : : * necessary data has been gathered with lock held.
2378 : : */
1851 andres@anarazel.de 2379 : 378510 : oldestfxid = FullXidRelativeTo(latest_completed, oldestxid);
2380 : :
2381 : : /* Check whether there's a replication slot requiring an older xmin. */
2382 : : def_vis_xid_data =
866 2383 : 378510 : TransactionIdOlder(xmin, replication_slot_xmin);
2384 : :
2385 : : /*
2386 : : * Rows in non-shared, non-catalog tables possibly could be vacuumed
2387 : : * if older than this xid.
2388 : : */
1851 2389 : 378510 : def_vis_xid = def_vis_xid_data;
2390 : :
2391 : : /*
2392 : : * Check whether there's a replication slot requiring an older catalog
2393 : : * xmin.
2394 : : */
2395 : : def_vis_xid =
2396 : 378510 : TransactionIdOlder(replication_slot_catalog_xmin, def_vis_xid);
2397 : :
2398 : 378510 : def_vis_fxid = FullXidRelativeTo(latest_completed, def_vis_xid);
2399 : 378510 : def_vis_fxid_data = FullXidRelativeTo(latest_completed, def_vis_xid_data);
2400 : :
2401 : : /*
2402 : : * Check if we can increase upper bound. As a previous
2403 : : * GlobalVisUpdate() might have computed more aggressive values, don't
2404 : : * overwrite them if so.
2405 : : */
2406 : : GlobalVisSharedRels.definitely_needed =
2407 : 378510 : FullTransactionIdNewer(def_vis_fxid,
2408 : : GlobalVisSharedRels.definitely_needed);
2409 : : GlobalVisCatalogRels.definitely_needed =
2410 : 378510 : FullTransactionIdNewer(def_vis_fxid,
2411 : : GlobalVisCatalogRels.definitely_needed);
2412 : : GlobalVisDataRels.definitely_needed =
2413 : 378510 : FullTransactionIdNewer(def_vis_fxid_data,
2414 : : GlobalVisDataRels.definitely_needed);
2415 : : /* See temp_oldest_nonremovable computation in ComputeXidHorizons() */
1774 2416 [ + + ]: 378510 : if (TransactionIdIsNormal(myxid))
2417 : : GlobalVisTempRels.definitely_needed =
2418 : 53408 : FullXidRelativeTo(latest_completed, myxid);
2419 : : else
2420 : : {
2421 : 325102 : GlobalVisTempRels.definitely_needed = latest_completed;
2422 : 325102 : FullTransactionIdAdvance(&GlobalVisTempRels.definitely_needed);
2423 : : }
2424 : :
2425 : : /*
2426 : : * Check if we know that we can initialize or increase the lower
2427 : : * bound. Currently the only cheap way to do so is to use
2428 : : * TransamVariables->oldestXid as input.
2429 : : *
2430 : : * We should definitely be able to do better. We could e.g. put a
2431 : : * global lower bound value into TransamVariables.
2432 : : */
2433 : : GlobalVisSharedRels.maybe_needed =
1851 2434 : 378510 : FullTransactionIdNewer(GlobalVisSharedRels.maybe_needed,
2435 : : oldestfxid);
2436 : : GlobalVisCatalogRels.maybe_needed =
2437 : 378510 : FullTransactionIdNewer(GlobalVisCatalogRels.maybe_needed,
2438 : : oldestfxid);
2439 : : GlobalVisDataRels.maybe_needed =
2440 : 378510 : FullTransactionIdNewer(GlobalVisDataRels.maybe_needed,
2441 : : oldestfxid);
2442 : : /* accurate value known */
1774 2443 : 378510 : GlobalVisTempRels.maybe_needed = GlobalVisTempRels.definitely_needed;
2444 : : }
2445 : :
7415 tgl@sss.pgh.pa.us 2446 : 378510 : RecentXmin = xmin;
1849 andres@anarazel.de 2447 [ - + ]: 378510 : Assert(TransactionIdPrecedesOrEquals(TransactionXmin, RecentXmin));
2448 : :
7415 tgl@sss.pgh.pa.us 2449 : 378510 : snapshot->xmin = xmin;
2450 : 378510 : snapshot->xmax = xmax;
2451 : 378510 : snapshot->xcnt = count;
6943 2452 : 378510 : snapshot->subxcnt = subcount;
5740 simon@2ndQuadrant.co 2453 : 378510 : snapshot->suboverflowed = suboverflowed;
1846 andres@anarazel.de 2454 : 378510 : snapshot->snapXactCompletionCount = curXactCompletionCount;
2455 : :
6490 tgl@sss.pgh.pa.us 2456 : 378510 : snapshot->curcid = GetCurrentCommandId(false);
2457 : :
2458 : : /*
2459 : : * This is a new snapshot, so set both refcounts are zero, and mark it as
2460 : : * not copied in persistent memory.
2461 : : */
6326 alvherre@alvh.no-ip. 2462 : 378510 : snapshot->active_count = 0;
2463 : 378510 : snapshot->regd_count = 0;
2464 : 378510 : snapshot->copied = false;
2465 : :
7415 tgl@sss.pgh.pa.us 2466 : 378510 : return snapshot;
2467 : : }
2468 : :
2469 : : /*
2470 : : * ProcArrayInstallImportedXmin -- install imported xmin into MyProc->xmin
2471 : : *
2472 : : * This is called when installing a snapshot imported from another
2473 : : * transaction. To ensure that OldestXmin doesn't go backwards, we must
2474 : : * check that the source transaction is still running, and we'd better do
2475 : : * that atomically with installing the new xmin.
2476 : : *
2477 : : * Returns true if successful, false if source xact is no longer running.
2478 : : */
2479 : : bool
3006 andres@anarazel.de 2480 : 18 : ProcArrayInstallImportedXmin(TransactionId xmin,
2481 : : VirtualTransactionId *sourcevxid)
2482 : : {
5068 tgl@sss.pgh.pa.us 2483 : 18 : bool result = false;
2484 : 18 : ProcArrayStruct *arrayP = procArray;
2485 : : int index;
2486 : :
2487 [ - + ]: 18 : Assert(TransactionIdIsNormal(xmin));
3006 andres@anarazel.de 2488 [ - + ]: 18 : if (!sourcevxid)
5068 tgl@sss.pgh.pa.us 2489 :UBC 0 : return false;
2490 : :
2491 : : /* Get lock so source xact can't end while we're doing this */
5068 tgl@sss.pgh.pa.us 2492 :CBC 18 : LWLockAcquire(ProcArrayLock, LW_SHARED);
2493 : :
2494 : : /*
2495 : : * Find the PGPROC entry of the source transaction. (This could use
2496 : : * GetPGProcByNumber(), unless it's a prepared xact. But this isn't
2497 : : * performance critical.)
2498 : : */
2499 [ + - ]: 18 : for (index = 0; index < arrayP->numProcs; index++)
2500 : : {
4836 bruce@momjian.us 2501 : 18 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 2502 : 18 : PGPROC *proc = &allProcs[pgprocno];
1755 alvherre@alvh.no-ip. 2503 : 18 : int statusFlags = ProcGlobal->statusFlags[index];
2504 : : TransactionId xid;
2505 : :
2506 : : /* Ignore procs running LAZY VACUUM */
2507 [ - + ]: 18 : if (statusFlags & PROC_IN_VACUUM)
5068 tgl@sss.pgh.pa.us 2508 :UBC 0 : continue;
2509 : :
2510 : : /* We are only interested in the specific virtual transaction. */
552 heikki.linnakangas@i 2511 [ - + ]:CBC 18 : if (proc->vxid.procNumber != sourcevxid->procNumber)
3006 andres@anarazel.de 2512 :UBC 0 : continue;
552 heikki.linnakangas@i 2513 [ - + ]:CBC 18 : if (proc->vxid.lxid != sourcevxid->localTransactionId)
5068 tgl@sss.pgh.pa.us 2514 :UBC 0 : continue;
2515 : :
2516 : : /*
2517 : : * We check the transaction's database ID for paranoia's sake: if it's
2518 : : * in another DB then its xmin does not cover us. Caller should have
2519 : : * detected this already, so we just treat any funny cases as
2520 : : * "transaction not found".
2521 : : */
5068 tgl@sss.pgh.pa.us 2522 [ - + ]:CBC 18 : if (proc->databaseId != MyDatabaseId)
5068 tgl@sss.pgh.pa.us 2523 :UBC 0 : continue;
2524 : :
2525 : : /*
2526 : : * Likewise, let's just make real sure its xmin does cover us.
2527 : : */
1850 andres@anarazel.de 2528 :CBC 18 : xid = UINT32_ACCESS_ONCE(proc->xmin);
5068 tgl@sss.pgh.pa.us 2529 [ + - ]: 18 : if (!TransactionIdIsNormal(xid) ||
2530 [ - + ]: 18 : !TransactionIdPrecedesOrEquals(xid, xmin))
5068 tgl@sss.pgh.pa.us 2531 :UBC 0 : continue;
2532 : :
2533 : : /*
2534 : : * We're good. Install the new xmin. As in GetSnapshotData, set
2535 : : * TransactionXmin too. (Note that because snapmgr.c called
2536 : : * GetSnapshotData first, we'll be overwriting a valid xmin here, so
2537 : : * we don't check that.)
2538 : : */
1850 andres@anarazel.de 2539 :CBC 18 : MyProc->xmin = TransactionXmin = xmin;
2540 : :
5068 tgl@sss.pgh.pa.us 2541 : 18 : result = true;
2542 : 18 : break;
2543 : : }
2544 : :
2545 : 18 : LWLockRelease(ProcArrayLock);
2546 : :
2547 : 18 : return result;
2548 : : }
2549 : :
2550 : : /*
2551 : : * ProcArrayInstallRestoredXmin -- install restored xmin into MyProc->xmin
2552 : : *
2553 : : * This is like ProcArrayInstallImportedXmin, but we have a pointer to the
2554 : : * PGPROC of the transaction from which we imported the snapshot, rather than
2555 : : * an XID.
2556 : : *
2557 : : * Note that this function also copies statusFlags from the source `proc` in
2558 : : * order to avoid the case where MyProc's xmin needs to be skipped for
2559 : : * computing xid horizon.
2560 : : *
2561 : : * Returns true if successful, false if source xact is no longer running.
2562 : : */
2563 : : bool
3782 rhaas@postgresql.org 2564 : 1572 : ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc)
2565 : : {
2566 : 1572 : bool result = false;
2567 : : TransactionId xid;
2568 : :
2569 [ - + ]: 1572 : Assert(TransactionIdIsNormal(xmin));
2570 [ - + ]: 1572 : Assert(proc != NULL);
2571 : :
2572 : : /*
2573 : : * Get an exclusive lock so that we can copy statusFlags from source proc.
2574 : : */
1387 akapila@postgresql.o 2575 : 1572 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
2576 : :
2577 : : /*
2578 : : * Be certain that the referenced PGPROC has an advertised xmin which is
2579 : : * no later than the one we're installing, so that the system-wide xmin
2580 : : * can't go backwards. Also, make sure it's running in the same database,
2581 : : * so that the per-database xmin cannot go backwards.
2582 : : */
1850 andres@anarazel.de 2583 : 1572 : xid = UINT32_ACCESS_ONCE(proc->xmin);
3782 rhaas@postgresql.org 2584 [ + - + - ]: 1572 : if (proc->databaseId == MyDatabaseId &&
2585 [ + - ]: 1572 : TransactionIdIsNormal(xid) &&
2586 : 1572 : TransactionIdPrecedesOrEquals(xid, xmin))
2587 : : {
2588 : : /*
2589 : : * Install xmin and propagate the statusFlags that affect how the
2590 : : * value is interpreted by vacuum.
2591 : : */
1850 andres@anarazel.de 2592 : 1572 : MyProc->xmin = TransactionXmin = xmin;
1206 alvherre@alvh.no-ip. 2593 : 1572 : MyProc->statusFlags = (MyProc->statusFlags & ~PROC_XMIN_FLAGS) |
2594 : 1572 : (proc->statusFlags & PROC_XMIN_FLAGS);
2595 : 1572 : ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
2596 : :
3782 rhaas@postgresql.org 2597 : 1572 : result = true;
2598 : : }
2599 : :
2600 : 1572 : LWLockRelease(ProcArrayLock);
2601 : :
2602 : 1572 : return result;
2603 : : }
2604 : :
2605 : : /*
2606 : : * GetRunningTransactionData -- returns information about running transactions.
2607 : : *
2608 : : * Similar to GetSnapshotData but returns more information. We include
2609 : : * all PGPROCs with an assigned TransactionId, even VACUUM processes and
2610 : : * prepared transactions.
2611 : : *
2612 : : * We acquire XidGenLock and ProcArrayLock, but the caller is responsible for
2613 : : * releasing them. Acquiring XidGenLock ensures that no new XIDs enter the proc
2614 : : * array until the caller has WAL-logged this snapshot, and releases the
2615 : : * lock. Acquiring ProcArrayLock ensures that no transactions commit until the
2616 : : * lock is released.
2617 : : *
2618 : : * The returned data structure is statically allocated; caller should not
2619 : : * modify it, and must not assume it is valid past the next call.
2620 : : *
2621 : : * This is never executed during recovery so there is no need to look at
2622 : : * KnownAssignedXids.
2623 : : *
2624 : : * Dummy PGPROCs from prepared transaction are included, meaning that this
2625 : : * may return entries with duplicated TransactionId values coming from
2626 : : * transaction finishing to prepare. Nothing is done about duplicated
2627 : : * entries here to not hold on ProcArrayLock more than necessary.
2628 : : *
2629 : : * We don't worry about updating other counters, we want to keep this as
2630 : : * simple as possible and leave GetSnapshotData() as the primary code for
2631 : : * that bookkeeping.
2632 : : *
2633 : : * Note that if any transaction has overflowed its cached subtransactions
2634 : : * then there is no real need include any subtransactions.
2635 : : */
2636 : : RunningTransactions
5740 simon@2ndQuadrant.co 2637 : 1346 : GetRunningTransactionData(void)
2638 : : {
2639 : : /* result workspace */
2640 : : static RunningTransactionsData CurrentRunningXactsData;
2641 : :
2642 : 1346 : ProcArrayStruct *arrayP = procArray;
1849 andres@anarazel.de 2643 : 1346 : TransactionId *other_xids = ProcGlobal->xids;
5610 tgl@sss.pgh.pa.us 2644 : 1346 : RunningTransactions CurrentRunningXacts = &CurrentRunningXactsData;
2645 : : TransactionId latestCompletedXid;
2646 : : TransactionId oldestRunningXid;
2647 : : TransactionId oldestDatabaseRunningXid;
2648 : : TransactionId *xids;
2649 : : int index;
2650 : : int count;
2651 : : int subcount;
2652 : : bool suboverflowed;
2653 : :
5740 simon@2ndQuadrant.co 2654 [ - + ]: 1346 : Assert(!RecoveryInProgress());
2655 : :
2656 : : /*
2657 : : * Allocating space for maxProcs xids is usually overkill; numProcs would
2658 : : * be sufficient. But it seems better to do the malloc while not holding
2659 : : * the lock, so we can't look at numProcs. Likewise, we allocate much
2660 : : * more subxip storage than is probably needed.
2661 : : *
2662 : : * Should only be allocated in bgwriter, since only ever executed during
2663 : : * checkpoints.
2664 : : */
2665 [ + + ]: 1346 : if (CurrentRunningXacts->xids == NULL)
2666 : : {
2667 : : /*
2668 : : * First call
2669 : : */
2670 : 500 : CurrentRunningXacts->xids = (TransactionId *)
2671 : 500 : malloc(TOTAL_MAX_CACHED_SUBXIDS * sizeof(TransactionId));
2672 [ - + ]: 500 : if (CurrentRunningXacts->xids == NULL)
5740 simon@2ndQuadrant.co 2673 [ # # ]:UBC 0 : ereport(ERROR,
2674 : : (errcode(ERRCODE_OUT_OF_MEMORY),
2675 : : errmsg("out of memory")));
2676 : : }
2677 : :
5740 simon@2ndQuadrant.co 2678 :CBC 1346 : xids = CurrentRunningXacts->xids;
2679 : :
2680 : 1346 : count = subcount = 0;
2681 : 1346 : suboverflowed = false;
2682 : :
2683 : : /*
2684 : : * Ensure that no xids enter or leave the procarray while we obtain
2685 : : * snapshot.
2686 : : */
2687 : 1346 : LWLockAcquire(ProcArrayLock, LW_SHARED);
2688 : 1346 : LWLockAcquire(XidGenLock, LW_SHARED);
2689 : :
1852 andres@anarazel.de 2690 : 1346 : latestCompletedXid =
638 heikki.linnakangas@i 2691 : 1346 : XidFromFullTransactionId(TransamVariables->latestCompletedXid);
541 akorotkov@postgresql 2692 : 1346 : oldestDatabaseRunningXid = oldestRunningXid =
638 heikki.linnakangas@i 2693 : 1346 : XidFromFullTransactionId(TransamVariables->nextXid);
2694 : :
2695 : : /*
2696 : : * Spin over procArray collecting all xids
2697 : : */
5740 simon@2ndQuadrant.co 2698 [ + + ]: 6511 : for (index = 0; index < arrayP->numProcs; index++)
2699 : : {
2700 : : TransactionId xid;
2701 : :
2702 : : /* Fetch xid just once - see GetNewTransactionId */
1849 andres@anarazel.de 2703 : 5165 : xid = UINT32_ACCESS_ONCE(other_xids[index]);
2704 : :
2705 : : /*
2706 : : * We don't need to store transactions that don't have a TransactionId
2707 : : * yet because they will not show as running on a standby server.
2708 : : */
5740 simon@2ndQuadrant.co 2709 [ + + ]: 5165 : if (!TransactionIdIsValid(xid))
2710 : 4312 : continue;
2711 : :
2712 : : /*
2713 : : * Be careful not to exclude any xids before calculating the values of
2714 : : * oldestRunningXid and suboverflowed, since these are used to clean
2715 : : * up transaction information held on standbys.
2716 : : */
2717 [ + + ]: 853 : if (TransactionIdPrecedes(xid, oldestRunningXid))
2718 : 566 : oldestRunningXid = xid;
2719 : :
2720 : : /*
2721 : : * Also, update the oldest running xid within the current database. As
2722 : : * fetching pgprocno and PGPROC could cause cache misses, we do cheap
2723 : : * TransactionId comparison first.
2724 : : */
429 akorotkov@postgresql 2725 [ + - ]: 853 : if (TransactionIdPrecedes(xid, oldestDatabaseRunningXid))
2726 : : {
2727 : 853 : int pgprocno = arrayP->pgprocnos[index];
2728 : 853 : PGPROC *proc = &allProcs[pgprocno];
2729 : :
2730 [ + + ]: 853 : if (proc->databaseId == MyDatabaseId)
2731 : 209 : oldestDatabaseRunningXid = xid;
2732 : : }
2733 : :
1849 andres@anarazel.de 2734 [ + + ]: 853 : if (ProcGlobal->subxidStates[index].overflowed)
4661 simon@2ndQuadrant.co 2735 : 2 : suboverflowed = true;
2736 : :
2737 : : /*
2738 : : * If we wished to exclude xids this would be the right place for it.
2739 : : * Procs with the PROC_IN_VACUUM flag set don't usually assign xids,
2740 : : * but they do during truncation at the end when they get the lock and
2741 : : * truncate, so it is not much of a problem to include them if they
2742 : : * are seen and it is cleaner to include them.
2743 : : */
2744 : :
2641 2745 : 853 : xids[count++] = xid;
2746 : : }
2747 : :
2748 : : /*
2749 : : * Spin over procArray collecting all subxids, but only if there hasn't
2750 : : * been a suboverflow.
2751 : : */
4661 2752 [ + + ]: 1346 : if (!suboverflowed)
2753 : : {
1849 andres@anarazel.de 2754 : 1344 : XidCacheStatus *other_subxidstates = ProcGlobal->subxidStates;
2755 : :
4661 simon@2ndQuadrant.co 2756 [ + + ]: 6502 : for (index = 0; index < arrayP->numProcs; index++)
2757 : : {
2758 : 5158 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 2759 : 5158 : PGPROC *proc = &allProcs[pgprocno];
2760 : : int nsubxids;
2761 : :
2762 : : /*
2763 : : * Save subtransaction XIDs. Other backends can't add or remove
2764 : : * entries while we're holding XidGenLock.
2765 : : */
1849 2766 : 5158 : nsubxids = other_subxidstates[index].count;
2767 [ + + ]: 5158 : if (nsubxids > 0)
2768 : : {
2769 : : /* barrier not really required, as XidGenLock is held, but ... */
2493 2770 : 13 : pg_read_barrier(); /* pairs with GetNewTransactionId */
2771 : :
942 peter@eisentraut.org 2772 : 13 : memcpy(&xids[count], proc->subxids.xids,
2773 : : nsubxids * sizeof(TransactionId));
1849 andres@anarazel.de 2774 : 13 : count += nsubxids;
2775 : 13 : subcount += nsubxids;
2776 : :
2777 : : /*
2778 : : * Top-level XID of a transaction is always less than any of
2779 : : * its subxids, so we don't need to check if any of the
2780 : : * subxids are smaller than oldestRunningXid
2781 : : */
2782 : : }
2783 : : }
2784 : : }
2785 : :
2786 : : /*
2787 : : * It's important *not* to include the limits set by slots here because
2788 : : * snapbuild.c uses oldestRunningXid to manage its xmin horizon. If those
2789 : : * were to be included here the initial value could never increase because
2790 : : * of a circular dependency where slots only increase their limits when
2791 : : * running xacts increases oldestRunningXid and running xacts only
2792 : : * increases if slots do.
2793 : : */
2794 : :
4661 simon@2ndQuadrant.co 2795 : 1346 : CurrentRunningXacts->xcnt = count - subcount;
2796 : 1346 : CurrentRunningXacts->subxcnt = subcount;
436 heikki.linnakangas@i 2797 [ + + ]: 1346 : CurrentRunningXacts->subxid_status = suboverflowed ? SUBXIDS_IN_SUBTRANS : SUBXIDS_IN_ARRAY;
638 2798 : 1346 : CurrentRunningXacts->nextXid = XidFromFullTransactionId(TransamVariables->nextXid);
5740 simon@2ndQuadrant.co 2799 : 1346 : CurrentRunningXacts->oldestRunningXid = oldestRunningXid;
541 akorotkov@postgresql 2800 : 1346 : CurrentRunningXacts->oldestDatabaseRunningXid = oldestDatabaseRunningXid;
5595 simon@2ndQuadrant.co 2801 : 1346 : CurrentRunningXacts->latestCompletedXid = latestCompletedXid;
2802 : :
5594 2803 [ - + ]: 1346 : Assert(TransactionIdIsValid(CurrentRunningXacts->nextXid));
2804 [ - + ]: 1346 : Assert(TransactionIdIsValid(CurrentRunningXacts->oldestRunningXid));
2805 [ - + ]: 1346 : Assert(TransactionIdIsNormal(CurrentRunningXacts->latestCompletedXid));
2806 : :
2807 : : /* We don't release the locks here, the caller is responsible for that */
2808 : :
5740 2809 : 1346 : return CurrentRunningXacts;
2810 : : }
2811 : :
2812 : : /*
2813 : : * GetOldestActiveTransactionId()
2814 : : *
2815 : : * Similar to GetSnapshotData but returns just oldestActiveXid. We include
2816 : : * all PGPROCs with an assigned TransactionId, even VACUUM processes.
2817 : : *
2818 : : * If allDbs is true, we look at all databases, though there is no need to
2819 : : * include WALSender since this has no effect on hot standby conflicts. If
2820 : : * allDbs is false, skip processes attached to other databases.
2821 : : *
2822 : : * This is never executed during recovery so there is no need to look at
2823 : : * KnownAssignedXids.
2824 : : *
2825 : : * We don't worry about updating other counters, we want to keep this as
2826 : : * simple as possible and leave GetSnapshotData() as the primary code for
2827 : : * that bookkeeping.
2828 : : *
2829 : : * inCommitOnly indicates getting the oldestActiveXid among the transactions
2830 : : * in the commit critical section.
2831 : : */
2832 : : TransactionId
45 akapila@postgresql.o 2833 :GNC 1174 : GetOldestActiveTransactionId(bool inCommitOnly, bool allDbs)
2834 : : {
5057 simon@2ndQuadrant.co 2835 :CBC 1174 : ProcArrayStruct *arrayP = procArray;
1849 andres@anarazel.de 2836 : 1174 : TransactionId *other_xids = ProcGlobal->xids;
2837 : : TransactionId oldestRunningXid;
2838 : : int index;
2839 : :
5057 simon@2ndQuadrant.co 2840 [ - + ]: 1174 : Assert(!RecoveryInProgress());
2841 : :
2842 : : /*
2843 : : * Read nextXid, as the upper bound of what's still active.
2844 : : *
2845 : : * Reading a TransactionId is atomic, but we must grab the lock to make
2846 : : * sure that all XIDs < nextXid are already present in the proc array (or
2847 : : * have already completed), when we spin over it.
2848 : : */
2977 heikki.linnakangas@i 2849 : 1174 : LWLockAcquire(XidGenLock, LW_SHARED);
638 2850 : 1174 : oldestRunningXid = XidFromFullTransactionId(TransamVariables->nextXid);
2977 2851 : 1174 : LWLockRelease(XidGenLock);
2852 : :
2853 : : /*
2854 : : * Spin over procArray collecting all xids and subxids.
2855 : : */
2856 : 1174 : LWLockAcquire(ProcArrayLock, LW_SHARED);
5057 simon@2ndQuadrant.co 2857 [ + + ]: 5576 : for (index = 0; index < arrayP->numProcs; index++)
2858 : : {
2859 : : TransactionId xid;
45 akapila@postgresql.o 2860 :GNC 4402 : int pgprocno = arrayP->pgprocnos[index];
2861 : 4402 : PGPROC *proc = &allProcs[pgprocno];
2862 : :
2863 : : /* Fetch xid just once - see GetNewTransactionId */
1849 andres@anarazel.de 2864 :CBC 4402 : xid = UINT32_ACCESS_ONCE(other_xids[index]);
2865 : :
5057 simon@2ndQuadrant.co 2866 [ + + ]: 4402 : if (!TransactionIdIsNormal(xid))
2867 : 3406 : continue;
2868 : :
45 akapila@postgresql.o 2869 [ + + ]:GNC 996 : if (inCommitOnly &&
2870 [ - + ]: 183 : (proc->delayChkptFlags & DELAY_CHKPT_IN_COMMIT) == 0)
45 akapila@postgresql.o 2871 :UNC 0 : continue;
2872 : :
45 akapila@postgresql.o 2873 [ + + - + ]:GNC 996 : if (!allDbs && proc->databaseId != MyDatabaseId)
45 akapila@postgresql.o 2874 :UNC 0 : continue;
2875 : :
5057 simon@2ndQuadrant.co 2876 [ + + ]:CBC 996 : if (TransactionIdPrecedes(xid, oldestRunningXid))
2877 : 732 : oldestRunningXid = xid;
2878 : :
2879 : : /*
2880 : : * Top-level XID of a transaction is always less than any of its
2881 : : * subxids, so we don't need to check if any of the subxids are
2882 : : * smaller than oldestRunningXid
2883 : : */
2884 : : }
2885 : 1174 : LWLockRelease(ProcArrayLock);
2886 : :
2887 : 1174 : return oldestRunningXid;
2888 : : }
2889 : :
2890 : : /*
2891 : : * GetOldestSafeDecodingTransactionId -- lowest xid not affected by vacuum
2892 : : *
2893 : : * Returns the oldest xid that we can guarantee not to have been affected by
2894 : : * vacuum, i.e. no rows >= that xid have been vacuumed away unless the
2895 : : * transaction aborted. Note that the value can (and most of the time will) be
2896 : : * much more conservative than what really has been affected by vacuum, but we
2897 : : * currently don't have better data available.
2898 : : *
2899 : : * This is useful to initialize the cutoff xid after which a new changeset
2900 : : * extraction replication slot can start decoding changes.
2901 : : *
2902 : : * Must be called with ProcArrayLock held either shared or exclusively,
2903 : : * although most callers will want to use exclusive mode since it is expected
2904 : : * that the caller will immediately use the xid to peg the xmin horizon.
2905 : : */
2906 : : TransactionId
3058 andres@anarazel.de 2907 : 634 : GetOldestSafeDecodingTransactionId(bool catalogOnly)
2908 : : {
4205 rhaas@postgresql.org 2909 : 634 : ProcArrayStruct *arrayP = procArray;
2910 : : TransactionId oldestSafeXid;
2911 : : int index;
2912 : 634 : bool recovery_in_progress = RecoveryInProgress();
2913 : :
2914 [ - + ]: 634 : Assert(LWLockHeldByMe(ProcArrayLock));
2915 : :
2916 : : /*
2917 : : * Acquire XidGenLock, so no transactions can acquire an xid while we're
2918 : : * running. If no transaction with xid were running concurrently a new xid
2919 : : * could influence the RecentXmin et al.
2920 : : *
2921 : : * We initialize the computation to nextXid since that's guaranteed to be
2922 : : * a safe, albeit pessimal, value.
2923 : : */
2924 : 634 : LWLockAcquire(XidGenLock, LW_SHARED);
638 heikki.linnakangas@i 2925 : 634 : oldestSafeXid = XidFromFullTransactionId(TransamVariables->nextXid);
2926 : :
2927 : : /*
2928 : : * If there's already a slot pegging the xmin horizon, we can start with
2929 : : * that value, it's guaranteed to be safe since it's computed by this
2930 : : * routine initially and has been enforced since. We can always use the
2931 : : * slot's general xmin horizon, but the catalog horizon is only usable
2932 : : * when only catalog data is going to be looked at.
2933 : : */
3058 andres@anarazel.de 2934 [ + + + + ]: 842 : if (TransactionIdIsValid(procArray->replication_slot_xmin) &&
2935 : 208 : TransactionIdPrecedes(procArray->replication_slot_xmin,
2936 : : oldestSafeXid))
2937 : 12 : oldestSafeXid = procArray->replication_slot_xmin;
2938 : :
2939 [ + + ]: 634 : if (catalogOnly &&
2940 [ + + + + ]: 297 : TransactionIdIsValid(procArray->replication_slot_catalog_xmin) &&
4205 rhaas@postgresql.org 2941 : 56 : TransactionIdPrecedes(procArray->replication_slot_catalog_xmin,
2942 : : oldestSafeXid))
2943 : 25 : oldestSafeXid = procArray->replication_slot_catalog_xmin;
2944 : :
2945 : : /*
2946 : : * If we're not in recovery, we walk over the procarray and collect the
2947 : : * lowest xid. Since we're called with ProcArrayLock held and have
2948 : : * acquired XidGenLock, no entries can vanish concurrently, since
2949 : : * ProcGlobal->xids[i] is only set with XidGenLock held and only cleared
2950 : : * with ProcArrayLock held.
2951 : : *
2952 : : * In recovery we can't lower the safe value besides what we've computed
2953 : : * above, so we'll have to wait a bit longer there. We unfortunately can
2954 : : * *not* use KnownAssignedXidsGetOldestXmin() since the KnownAssignedXids
2955 : : * machinery can miss values and return an older value than is safe.
2956 : : */
2957 [ + + ]: 634 : if (!recovery_in_progress)
2958 : : {
1849 andres@anarazel.de 2959 : 629 : TransactionId *other_xids = ProcGlobal->xids;
2960 : :
2961 : : /*
2962 : : * Spin over procArray collecting min(ProcGlobal->xids[i])
2963 : : */
4205 rhaas@postgresql.org 2964 [ + + ]: 3237 : for (index = 0; index < arrayP->numProcs; index++)
2965 : : {
2966 : : TransactionId xid;
2967 : :
2968 : : /* Fetch xid just once - see GetNewTransactionId */
1849 andres@anarazel.de 2969 : 2608 : xid = UINT32_ACCESS_ONCE(other_xids[index]);
2970 : :
4205 rhaas@postgresql.org 2971 [ + + ]: 2608 : if (!TransactionIdIsNormal(xid))
2972 : 2599 : continue;
2973 : :
2974 [ + + ]: 9 : if (TransactionIdPrecedes(xid, oldestSafeXid))
2975 : 8 : oldestSafeXid = xid;
2976 : : }
2977 : : }
2978 : :
2979 : 634 : LWLockRelease(XidGenLock);
2980 : :
2981 : 634 : return oldestSafeXid;
2982 : : }
2983 : :
2984 : : /*
2985 : : * GetVirtualXIDsDelayingChkpt -- Get the VXIDs of transactions that are
2986 : : * delaying checkpoint because they have critical actions in progress.
2987 : : *
2988 : : * Constructs an array of VXIDs of transactions that are currently in commit
2989 : : * critical sections, as shown by having specified delayChkptFlags bits set
2990 : : * in their PGPROC.
2991 : : *
2992 : : * Returns a palloc'd array that should be freed by the caller.
2993 : : * *nvxids is the number of valid entries.
2994 : : *
2995 : : * Note that because backends set or clear delayChkptFlags without holding any
2996 : : * lock, the result is somewhat indeterminate, but we don't really care. Even
2997 : : * in a multiprocessor with delayed writes to shared memory, it should be
2998 : : * certain that setting of delayChkptFlags will propagate to shared memory
2999 : : * when the backend takes a lock, so we cannot fail to see a virtual xact as
3000 : : * delayChkptFlags if it's already inserted its commit record. Whether it
3001 : : * takes a little while for clearing of delayChkptFlags to propagate is
3002 : : * unimportant for correctness.
3003 : : */
3004 : : VirtualTransactionId *
1262 3005 : 2958 : GetVirtualXIDsDelayingChkpt(int *nvxids, int type)
3006 : : {
3007 : : VirtualTransactionId *vxids;
6731 tgl@sss.pgh.pa.us 3008 : 2958 : ProcArrayStruct *arrayP = procArray;
4660 simon@2ndQuadrant.co 3009 : 2958 : int count = 0;
3010 : : int index;
3011 : :
1262 rhaas@postgresql.org 3012 [ - + ]: 2958 : Assert(type != 0);
3013 : :
3014 : : /* allocate what's certainly enough result space */
3015 : : vxids = (VirtualTransactionId *)
4660 simon@2ndQuadrant.co 3016 : 2958 : palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
3017 : :
6731 tgl@sss.pgh.pa.us 3018 : 2958 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3019 : :
3020 [ + + ]: 9883 : for (index = 0; index < arrayP->numProcs; index++)
3021 : : {
4483 bruce@momjian.us 3022 : 6925 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3023 : 6925 : PGPROC *proc = &allProcs[pgprocno];
3024 : :
1247 rhaas@postgresql.org 3025 [ + + ]: 6925 : if ((proc->delayChkptFlags & type) != 0)
3026 : : {
3027 : : VirtualTransactionId vxid;
3028 : :
4660 simon@2ndQuadrant.co 3029 : 28 : GET_VXID_FROM_PGPROC(vxid, *proc);
3030 [ + - ]: 28 : if (VirtualTransactionIdIsValid(vxid))
3031 : 28 : vxids[count++] = vxid;
3032 : : }
3033 : : }
3034 : :
6731 tgl@sss.pgh.pa.us 3035 : 2958 : LWLockRelease(ProcArrayLock);
3036 : :
4660 simon@2ndQuadrant.co 3037 : 2958 : *nvxids = count;
3038 : 2958 : return vxids;
3039 : : }
3040 : :
3041 : : /*
3042 : : * HaveVirtualXIDsDelayingChkpt -- Are any of the specified VXIDs delaying?
3043 : : *
3044 : : * This is used with the results of GetVirtualXIDsDelayingChkpt to see if any
3045 : : * of the specified VXIDs are still in critical sections of code.
3046 : : *
3047 : : * Note: this is O(N^2) in the number of vxacts that are/were delaying, but
3048 : : * those numbers should be small enough for it not to be a problem.
3049 : : */
3050 : : bool
1262 rhaas@postgresql.org 3051 : 28 : HaveVirtualXIDsDelayingChkpt(VirtualTransactionId *vxids, int nvxids, int type)
3052 : : {
6505 bruce@momjian.us 3053 : 28 : bool result = false;
6731 tgl@sss.pgh.pa.us 3054 : 28 : ProcArrayStruct *arrayP = procArray;
3055 : : int index;
3056 : :
1262 rhaas@postgresql.org 3057 [ - + ]: 28 : Assert(type != 0);
3058 : :
6731 tgl@sss.pgh.pa.us 3059 : 28 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3060 : :
4469 noah@leadboat.com 3061 [ + + ]: 333 : for (index = 0; index < arrayP->numProcs; index++)
3062 : : {
3063 : 310 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3064 : 310 : PGPROC *proc = &allProcs[pgprocno];
3065 : : VirtualTransactionId vxid;
3066 : :
4469 noah@leadboat.com 3067 : 310 : GET_VXID_FROM_PGPROC(vxid, *proc);
3068 : :
1247 rhaas@postgresql.org 3069 [ + + ]: 310 : if ((proc->delayChkptFlags & type) != 0 &&
1262 3070 [ + - ]: 18 : VirtualTransactionIdIsValid(vxid))
3071 : : {
3072 : : int i;
3073 : :
4469 noah@leadboat.com 3074 [ + + ]: 36 : for (i = 0; i < nvxids; i++)
3075 : : {
3076 [ + + + + ]: 23 : if (VirtualTransactionIdEquals(vxid, vxids[i]))
3077 : : {
6731 tgl@sss.pgh.pa.us 3078 :GBC 5 : result = true;
3079 : 5 : break;
3080 : : }
3081 : : }
4469 noah@leadboat.com 3082 [ + + ]:CBC 18 : if (result)
4469 noah@leadboat.com 3083 :GBC 5 : break;
3084 : : }
3085 : : }
3086 : :
6731 tgl@sss.pgh.pa.us 3087 :CBC 28 : LWLockRelease(ProcArrayLock);
3088 : :
3089 : 28 : return result;
3090 : : }
3091 : :
3092 : : /*
3093 : : * ProcNumberGetProc -- get a backend's PGPROC given its proc number
3094 : : *
3095 : : * The result may be out of date arbitrarily quickly, so the caller
3096 : : * must be careful about how this information is used. NULL is
3097 : : * returned if the backend is not active.
3098 : : */
3099 : : PGPROC *
552 heikki.linnakangas@i 3100 : 558 : ProcNumberGetProc(ProcNumber procNumber)
3101 : : {
3102 : : PGPROC *result;
3103 : :
3104 [ + + - + ]: 558 : if (procNumber < 0 || procNumber >= ProcGlobal->allProcCount)
3105 : 1 : return NULL;
3106 : 557 : result = GetPGProcByNumber(procNumber);
3107 : :
3108 [ + + ]: 557 : if (result->pid == 0)
3109 : 5 : return NULL;
3110 : :
3111 : 552 : return result;
3112 : : }
3113 : :
3114 : : /*
3115 : : * ProcNumberGetTransactionIds -- get a backend's transaction status
3116 : : *
3117 : : * Get the xid, xmin, nsubxid and overflow status of the backend. The
3118 : : * result may be out of date arbitrarily quickly, so the caller must be
3119 : : * careful about how this information is used.
3120 : : */
3121 : : void
3122 : 10290 : ProcNumberGetTransactionIds(ProcNumber procNumber, TransactionId *xid,
3123 : : TransactionId *xmin, int *nsubxid, bool *overflowed)
3124 : : {
3125 : : PGPROC *proc;
3126 : :
3127 : 10290 : *xid = InvalidTransactionId;
3128 : 10290 : *xmin = InvalidTransactionId;
3129 : 10290 : *nsubxid = 0;
3130 : 10290 : *overflowed = false;
3131 : :
3132 [ + - - + ]: 10290 : if (procNumber < 0 || procNumber >= ProcGlobal->allProcCount)
552 heikki.linnakangas@i 3133 :UBC 0 : return;
552 heikki.linnakangas@i 3134 :CBC 10290 : proc = GetPGProcByNumber(procNumber);
3135 : :
3136 : : /* Need to lock out additions/removals of backends */
3137 : 10290 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3138 : :
3139 [ + - ]: 10290 : if (proc->pid != 0)
3140 : : {
3141 : 10290 : *xid = proc->xid;
3142 : 10290 : *xmin = proc->xmin;
3143 : 10290 : *nsubxid = proc->subxidStatus.count;
3144 : 10290 : *overflowed = proc->subxidStatus.overflowed;
3145 : : }
3146 : :
3147 : 10290 : LWLockRelease(ProcArrayLock);
3148 : : }
3149 : :
3150 : : /*
3151 : : * BackendPidGetProc -- get a backend's PGPROC given its PID
3152 : : *
3153 : : * Returns NULL if not found. Note that it is up to the caller to be
3154 : : * sure that the question remains meaningful for long enough for the
3155 : : * answer to be used ...
3156 : : */
3157 : : PGPROC *
7415 tgl@sss.pgh.pa.us 3158 : 10736 : BackendPidGetProc(int pid)
3159 : : {
3160 : : PGPROC *result;
3161 : :
3484 3162 [ + + ]: 10736 : if (pid == 0) /* never match dummy PGPROCs */
3163 : 3 : return NULL;
3164 : :
3165 : 10733 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3166 : :
3167 : 10733 : result = BackendPidGetProcWithLock(pid);
3168 : :
3169 : 10733 : LWLockRelease(ProcArrayLock);
3170 : :
3171 : 10733 : return result;
3172 : : }
3173 : :
3174 : : /*
3175 : : * BackendPidGetProcWithLock -- get a backend's PGPROC given its PID
3176 : : *
3177 : : * Same as above, except caller must be holding ProcArrayLock. The found
3178 : : * entry, if any, can be assumed to be valid as long as the lock remains held.
3179 : : */
3180 : : PGPROC *
3181 : 12653 : BackendPidGetProcWithLock(int pid)
3182 : : {
7415 3183 : 12653 : PGPROC *result = NULL;
3184 : 12653 : ProcArrayStruct *arrayP = procArray;
3185 : : int index;
3186 : :
7386 3187 [ - + ]: 12653 : if (pid == 0) /* never match dummy PGPROCs */
7386 tgl@sss.pgh.pa.us 3188 :UBC 0 : return NULL;
3189 : :
7415 tgl@sss.pgh.pa.us 3190 [ + + ]:CBC 48993 : for (index = 0; index < arrayP->numProcs; index++)
3191 : : {
5034 rhaas@postgresql.org 3192 : 43896 : PGPROC *proc = &allProcs[arrayP->pgprocnos[index]];
3193 : :
7415 tgl@sss.pgh.pa.us 3194 [ + + ]: 43896 : if (proc->pid == pid)
3195 : : {
3196 : 7556 : result = proc;
3197 : 7556 : break;
3198 : : }
3199 : : }
3200 : :
3201 : 12653 : return result;
3202 : : }
3203 : :
3204 : : /*
3205 : : * BackendXidGetPid -- get a backend's pid given its XID
3206 : : *
3207 : : * Returns 0 if not found or it's a prepared transaction. Note that
3208 : : * it is up to the caller to be sure that the question remains
3209 : : * meaningful for long enough for the answer to be used ...
3210 : : *
3211 : : * Only main transaction Ids are considered. This function is mainly
3212 : : * useful for determining what backend owns a lock.
3213 : : *
3214 : : * Beware that not every xact has an XID assigned. However, as long as you
3215 : : * only call this using an XID found on disk, you're safe.
3216 : : */
3217 : : int
7322 ishii@postgresql.org 3218 : 30 : BackendXidGetPid(TransactionId xid)
3219 : : {
7266 bruce@momjian.us 3220 : 30 : int result = 0;
7322 ishii@postgresql.org 3221 : 30 : ProcArrayStruct *arrayP = procArray;
1849 andres@anarazel.de 3222 : 30 : TransactionId *other_xids = ProcGlobal->xids;
3223 : : int index;
3224 : :
7322 ishii@postgresql.org 3225 [ - + ]: 30 : if (xid == InvalidTransactionId) /* never match invalid xid */
7322 ishii@postgresql.org 3226 :UBC 0 : return 0;
3227 : :
7322 ishii@postgresql.org 3228 :CBC 30 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3229 : :
3230 [ + + ]: 92 : for (index = 0; index < arrayP->numProcs; index++)
3231 : : {
1849 andres@anarazel.de 3232 [ + + ]: 84 : if (other_xids[index] == xid)
3233 : : {
729 michael@paquier.xyz 3234 : 22 : int pgprocno = arrayP->pgprocnos[index];
3235 : 22 : PGPROC *proc = &allProcs[pgprocno];
3236 : :
7322 ishii@postgresql.org 3237 : 22 : result = proc->pid;
3238 : 22 : break;
3239 : : }
3240 : : }
3241 : :
3242 : 30 : LWLockRelease(ProcArrayLock);
3243 : :
3244 : 30 : return result;
3245 : : }
3246 : :
3247 : : /*
3248 : : * IsBackendPid -- is a given pid a running backend
3249 : : *
3250 : : * This is not called by the backend, but is called by external modules.
3251 : : */
3252 : : bool
7415 tgl@sss.pgh.pa.us 3253 : 2 : IsBackendPid(int pid)
3254 : : {
3255 : 2 : return (BackendPidGetProc(pid) != NULL);
3256 : : }
3257 : :
3258 : :
3259 : : /*
3260 : : * GetCurrentVirtualXIDs -- returns an array of currently active VXIDs.
3261 : : *
3262 : : * The array is palloc'd. The number of valid entries is returned into *nvxids.
3263 : : *
3264 : : * The arguments allow filtering the set of VXIDs returned. Our own process
3265 : : * is always skipped. In addition:
3266 : : * If limitXmin is not InvalidTransactionId, skip processes with
3267 : : * xmin > limitXmin.
3268 : : * If excludeXmin0 is true, skip processes with xmin = 0.
3269 : : * If allDbs is false, skip processes attached to other databases.
3270 : : * If excludeVacuum isn't zero, skip processes for which
3271 : : * (statusFlags & excludeVacuum) is not zero.
3272 : : *
3273 : : * Note: the purpose of the limitXmin and excludeXmin0 parameters is to
3274 : : * allow skipping backends whose oldest live snapshot is no older than
3275 : : * some snapshot we have. Since we examine the procarray with only shared
3276 : : * lock, there are race conditions: a backend could set its xmin just after
3277 : : * we look. Indeed, on multiprocessors with weak memory ordering, the
3278 : : * other backend could have set its xmin *before* we look. We know however
3279 : : * that such a backend must have held shared ProcArrayLock overlapping our
3280 : : * own hold of ProcArrayLock, else we would see its xmin update. Therefore,
3281 : : * any snapshot the other backend is taking concurrently with our scan cannot
3282 : : * consider any transactions as still running that we think are committed
3283 : : * (since backends must hold ProcArrayLock exclusive to commit).
3284 : : */
3285 : : VirtualTransactionId *
5999 3286 : 369 : GetCurrentVirtualXIDs(TransactionId limitXmin, bool excludeXmin0,
3287 : : bool allDbs, int excludeVacuum,
3288 : : int *nvxids)
3289 : : {
3290 : : VirtualTransactionId *vxids;
6576 3291 : 369 : ProcArrayStruct *arrayP = procArray;
3292 : 369 : int count = 0;
3293 : : int index;
3294 : :
3295 : : /* allocate what's certainly enough result space */
3296 : : vxids = (VirtualTransactionId *)
5999 3297 : 369 : palloc(sizeof(VirtualTransactionId) * arrayP->maxProcs);
3298 : :
6576 3299 : 369 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3300 : :
3301 [ + + ]: 2199 : for (index = 0; index < arrayP->numProcs; index++)
3302 : : {
4836 bruce@momjian.us 3303 : 1830 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3304 : 1830 : PGPROC *proc = &allProcs[pgprocno];
1755 alvherre@alvh.no-ip. 3305 : 1830 : uint8 statusFlags = ProcGlobal->statusFlags[index];
3306 : :
6576 tgl@sss.pgh.pa.us 3307 [ + + ]: 1830 : if (proc == MyProc)
3308 : 369 : continue;
3309 : :
1755 alvherre@alvh.no-ip. 3310 [ + + ]: 1461 : if (excludeVacuum & statusFlags)
6450 tgl@sss.pgh.pa.us 3311 : 11 : continue;
3312 : :
6574 3313 [ + - + + ]: 1450 : if (allDbs || proc->databaseId == MyDatabaseId)
3314 : : {
3315 : : /* Fetch xmin just once - might change on us */
1850 andres@anarazel.de 3316 : 642 : TransactionId pxmin = UINT32_ACCESS_ONCE(proc->xmin);
3317 : :
5999 tgl@sss.pgh.pa.us 3318 [ + - + + ]: 642 : if (excludeXmin0 && !TransactionIdIsValid(pxmin))
3319 : 378 : continue;
3320 : :
3321 : : /*
3322 : : * InvalidTransactionId precedes all other XIDs, so a proc that
3323 : : * hasn't set xmin yet will not be rejected by this test.
3324 : : */
6574 3325 [ + - + + ]: 528 : if (!TransactionIdIsValid(limitXmin) ||
5999 3326 : 264 : TransactionIdPrecedesOrEquals(pxmin, limitXmin))
3327 : : {
3328 : : VirtualTransactionId vxid;
3329 : :
6574 3330 : 252 : GET_VXID_FROM_PGPROC(vxid, *proc);
3331 [ + - ]: 252 : if (VirtualTransactionIdIsValid(vxid))
3332 : 252 : vxids[count++] = vxid;
3333 : : }
3334 : : }
3335 : : }
3336 : :
6576 3337 : 369 : LWLockRelease(ProcArrayLock);
3338 : :
5999 3339 : 369 : *nvxids = count;
6576 3340 : 369 : return vxids;
3341 : : }
3342 : :
3343 : : /*
3344 : : * GetConflictingVirtualXIDs -- returns an array of currently active VXIDs.
3345 : : *
3346 : : * Usage is limited to conflict resolution during recovery on standby servers.
3347 : : * limitXmin is supplied as either a cutoff with snapshotConflictHorizon
3348 : : * semantics, or InvalidTransactionId in cases where caller cannot accurately
3349 : : * determine a safe snapshotConflictHorizon value.
3350 : : *
3351 : : * If limitXmin is InvalidTransactionId then we want to kill everybody,
3352 : : * so we're not worried if they have a snapshot or not, nor does it really
3353 : : * matter what type of lock we hold. Caller must avoid calling here with
3354 : : * snapshotConflictHorizon style cutoffs that were set to InvalidTransactionId
3355 : : * during original execution, since that actually indicates that there is
3356 : : * definitely no need for a recovery conflict (the snapshotConflictHorizon
3357 : : * convention for InvalidTransactionId values is the opposite of our own!).
3358 : : *
3359 : : * All callers that are checking xmins always now supply a valid and useful
3360 : : * value for limitXmin. The limitXmin is always lower than the lowest
3361 : : * numbered KnownAssignedXid that is not already a FATAL error. This is
3362 : : * because we only care about cleanup records that are cleaning up tuple
3363 : : * versions from committed transactions. In that case they will only occur
3364 : : * at the point where the record is less than the lowest running xid. That
3365 : : * allows us to say that if any backend takes a snapshot concurrently with
3366 : : * us then the conflict assessment made here would never include the snapshot
3367 : : * that is being derived. So we take LW_SHARED on the ProcArray and allow
3368 : : * concurrent snapshots when limitXmin is valid. We might think about adding
3369 : : * Assert(limitXmin < lowest(KnownAssignedXids))
3370 : : * but that would not be true in the case of FATAL errors lagging in array,
3371 : : * but we already know those are bogus anyway, so we skip that test.
3372 : : *
3373 : : * If dbOid is valid we skip backends attached to other databases.
3374 : : *
3375 : : * Be careful to *not* pfree the result from this function. We reuse
3376 : : * this array sufficiently often that we use malloc for the result.
3377 : : */
3378 : : VirtualTransactionId *
5705 simon@2ndQuadrant.co 3379 : 13583 : GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
3380 : : {
3381 : : static VirtualTransactionId *vxids;
5740 3382 : 13583 : ProcArrayStruct *arrayP = procArray;
3383 : 13583 : int count = 0;
3384 : : int index;
3385 : :
3386 : : /*
3387 : : * If first time through, get workspace to remember main XIDs in. We
3388 : : * malloc it permanently to avoid repeated palloc/pfree overhead. Allow
3389 : : * result space, remembering room for a terminator.
3390 : : */
3391 [ + + ]: 13583 : if (vxids == NULL)
3392 : : {
3393 : 21 : vxids = (VirtualTransactionId *)
3394 : 21 : malloc(sizeof(VirtualTransactionId) * (arrayP->maxProcs + 1));
3395 [ - + ]: 21 : if (vxids == NULL)
5740 simon@2ndQuadrant.co 3396 [ # # ]:UBC 0 : ereport(ERROR,
3397 : : (errcode(ERRCODE_OUT_OF_MEMORY),
3398 : : errmsg("out of memory")));
3399 : : }
3400 : :
5617 simon@2ndQuadrant.co 3401 :CBC 13583 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3402 : :
5740 3403 [ + + ]: 13722 : for (index = 0; index < arrayP->numProcs; index++)
3404 : : {
4836 bruce@momjian.us 3405 : 139 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3406 : 139 : PGPROC *proc = &allProcs[pgprocno];
3407 : :
3408 : : /* Exclude prepared transactions */
5740 simon@2ndQuadrant.co 3409 [ - + ]: 139 : if (proc->pid == 0)
5740 simon@2ndQuadrant.co 3410 :UBC 0 : continue;
3411 : :
5740 simon@2ndQuadrant.co 3412 [ + + ]:CBC 139 : if (!OidIsValid(dbOid) ||
3413 [ + + ]: 133 : proc->databaseId == dbOid)
3414 : : {
3415 : : /* Fetch xmin just once - can't change on us, but good coding */
1850 andres@anarazel.de 3416 : 11 : TransactionId pxmin = UINT32_ACCESS_ONCE(proc->xmin);
3417 : :
3418 : : /*
3419 : : * We ignore an invalid pxmin because this means that backend has
3420 : : * no snapshot currently. We hold a Share lock to avoid contention
3421 : : * with users taking snapshots. That is not a problem because the
3422 : : * current xmin is always at least one higher than the latest
3423 : : * removed xid, so any new snapshot would never conflict with the
3424 : : * test here.
3425 : : */
5617 simon@2ndQuadrant.co 3426 [ + + + + ]: 11 : if (!TransactionIdIsValid(limitXmin) ||
3427 [ + + ]: 4 : (TransactionIdIsValid(pxmin) && !TransactionIdFollows(pxmin, limitXmin)))
3428 : : {
3429 : : VirtualTransactionId vxid;
3430 : :
5740 3431 : 2 : GET_VXID_FROM_PGPROC(vxid, *proc);
3432 [ + - ]: 2 : if (VirtualTransactionIdIsValid(vxid))
3433 : 2 : vxids[count++] = vxid;
3434 : : }
3435 : : }
3436 : : }
3437 : :
3438 : 13583 : LWLockRelease(ProcArrayLock);
3439 : :
3440 : : /* add the terminator */
552 heikki.linnakangas@i 3441 : 13583 : vxids[count].procNumber = INVALID_PROC_NUMBER;
5740 simon@2ndQuadrant.co 3442 : 13583 : vxids[count].localTransactionId = InvalidLocalTransactionId;
3443 : :
3444 : 13583 : return vxids;
3445 : : }
3446 : :
3447 : : /*
3448 : : * CancelVirtualTransaction - used in recovery conflict processing
3449 : : *
3450 : : * Returns pid of the process signaled, or 0 if not found.
3451 : : */
3452 : : pid_t
5712 3453 : 3 : CancelVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode)
3454 : : {
1704 fujii@postgresql.org 3455 : 3 : return SignalVirtualTransaction(vxid, sigmode, true);
3456 : : }
3457 : :
3458 : : pid_t
3459 : 5 : SignalVirtualTransaction(VirtualTransactionId vxid, ProcSignalReason sigmode,
3460 : : bool conflictPending)
3461 : : {
5740 simon@2ndQuadrant.co 3462 : 5 : ProcArrayStruct *arrayP = procArray;
3463 : : int index;
3464 : 5 : pid_t pid = 0;
3465 : :
3466 : 5 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3467 : :
3468 [ + - ]: 5 : for (index = 0; index < arrayP->numProcs; index++)
3469 : : {
4836 bruce@momjian.us 3470 : 5 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3471 : 5 : PGPROC *proc = &allProcs[pgprocno];
3472 : : VirtualTransactionId procvxid;
3473 : :
5740 simon@2ndQuadrant.co 3474 : 5 : GET_VXID_FROM_PGPROC(procvxid, *proc);
3475 : :
552 heikki.linnakangas@i 3476 [ + - ]: 5 : if (procvxid.procNumber == vxid.procNumber &&
5740 simon@2ndQuadrant.co 3477 [ + - ]: 5 : procvxid.localTransactionId == vxid.localTransactionId)
3478 : : {
1704 fujii@postgresql.org 3479 : 5 : proc->recoveryConflictPending = conflictPending;
5740 simon@2ndQuadrant.co 3480 : 5 : pid = proc->pid;
5712 3481 [ + - ]: 5 : if (pid != 0)
3482 : : {
3483 : : /*
3484 : : * Kill the pid if it's still here. If not, that's what we
3485 : : * wanted so ignore any errors.
3486 : : */
552 heikki.linnakangas@i 3487 : 5 : (void) SendProcSignal(pid, sigmode, vxid.procNumber);
3488 : : }
5740 simon@2ndQuadrant.co 3489 : 5 : break;
3490 : : }
3491 : : }
3492 : :
3493 : 5 : LWLockRelease(ProcArrayLock);
3494 : :
3495 : 5 : return pid;
3496 : : }
3497 : :
3498 : : /*
3499 : : * MinimumActiveBackends --- count backends (other than myself) that are
3500 : : * in active transactions. Return true if the count exceeds the
3501 : : * minimum threshold passed. This is used as a heuristic to decide if
3502 : : * a pre-XLOG-flush delay is worthwhile during commit.
3503 : : *
3504 : : * Do not count backends that are blocked waiting for locks, since they are
3505 : : * not going to get to run until someone else commits.
3506 : : */
3507 : : bool
5386 simon@2ndQuadrant.co 3508 :UBC 0 : MinimumActiveBackends(int min)
3509 : : {
7415 tgl@sss.pgh.pa.us 3510 : 0 : ProcArrayStruct *arrayP = procArray;
3511 : 0 : int count = 0;
3512 : : int index;
3513 : :
3514 : : /* Quick short-circuit if no minimum is specified */
5386 simon@2ndQuadrant.co 3515 [ # # ]: 0 : if (min == 0)
3516 : 0 : return true;
3517 : :
3518 : : /*
3519 : : * Note: for speed, we don't acquire ProcArrayLock. This is a little bit
3520 : : * bogus, but since we are only testing fields for zero or nonzero, it
3521 : : * should be OK. The result is only used for heuristic purposes anyway...
3522 : : */
7415 tgl@sss.pgh.pa.us 3523 [ # # ]: 0 : for (index = 0; index < arrayP->numProcs; index++)
3524 : : {
4836 bruce@momjian.us 3525 : 0 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3526 : 0 : PGPROC *proc = &allProcs[pgprocno];
3527 : :
3528 : : /*
3529 : : * Since we're not holding a lock, need to be prepared to deal with
3530 : : * garbage, as someone could have incremented numProcs but not yet
3531 : : * filled the structure.
3532 : : *
3533 : : * If someone just decremented numProcs, 'proc' could also point to a
3534 : : * PGPROC entry that's no longer in the array. It still points to a
3535 : : * PGPROC struct, though, because freed PGPROC entries just go to the
3536 : : * free list and are recycled. Its contents are nonsense in that case,
3537 : : * but that's acceptable for this function.
3538 : : */
3890 sfrost@snowman.net 3539 [ # # ]: 0 : if (pgprocno == -1)
3540 : 0 : continue; /* do not count deleted entries */
7415 tgl@sss.pgh.pa.us 3541 [ # # ]: 0 : if (proc == MyProc)
3542 : 0 : continue; /* do not count myself */
1849 andres@anarazel.de 3543 [ # # ]: 0 : if (proc->xid == InvalidTransactionId)
5034 rhaas@postgresql.org 3544 : 0 : continue; /* do not count if no XID assigned */
7386 tgl@sss.pgh.pa.us 3545 [ # # ]: 0 : if (proc->pid == 0)
3546 : 0 : continue; /* do not count prepared xacts */
7415 3547 [ # # ]: 0 : if (proc->waitLock != NULL)
3548 : 0 : continue; /* do not count if blocked on a lock */
3549 : 0 : count++;
5386 simon@2ndQuadrant.co 3550 [ # # ]: 0 : if (count >= min)
3551 : 0 : break;
3552 : : }
3553 : :
3554 : 0 : return count >= min;
3555 : : }
3556 : :
3557 : : /*
3558 : : * CountDBBackends --- count backends that are using specified database
3559 : : */
3560 : : int
7342 tgl@sss.pgh.pa.us 3561 :CBC 13 : CountDBBackends(Oid databaseid)
3562 : : {
3563 : 13 : ProcArrayStruct *arrayP = procArray;
3564 : 13 : int count = 0;
3565 : : int index;
3566 : :
3567 : 13 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3568 : :
3569 [ + + ]: 20 : for (index = 0; index < arrayP->numProcs; index++)
3570 : : {
4836 bruce@momjian.us 3571 : 7 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3572 : 7 : PGPROC *proc = &allProcs[pgprocno];
3573 : :
7342 tgl@sss.pgh.pa.us 3574 [ - + ]: 7 : if (proc->pid == 0)
7342 tgl@sss.pgh.pa.us 3575 :UBC 0 : continue; /* do not count prepared xacts */
5616 simon@2ndQuadrant.co 3576 [ + - ]:CBC 7 : if (!OidIsValid(databaseid) ||
3577 [ + + ]: 7 : proc->databaseId == databaseid)
7342 tgl@sss.pgh.pa.us 3578 : 1 : count++;
3579 : : }
3580 : :
3581 : 13 : LWLockRelease(ProcArrayLock);
3582 : :
3583 : 13 : return count;
3584 : : }
3585 : :
3586 : : /*
3587 : : * CountDBConnections --- counts database backends (only regular backends)
3588 : : */
3589 : : int
3139 andrew@dunslane.net 3590 :UBC 0 : CountDBConnections(Oid databaseid)
3591 : : {
3592 : 0 : ProcArrayStruct *arrayP = procArray;
3593 : 0 : int count = 0;
3594 : : int index;
3595 : :
3596 : 0 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3597 : :
3598 [ # # ]: 0 : for (index = 0; index < arrayP->numProcs; index++)
3599 : : {
3600 : 0 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3601 : 0 : PGPROC *proc = &allProcs[pgprocno];
3602 : :
3139 andrew@dunslane.net 3603 [ # # ]: 0 : if (proc->pid == 0)
3604 : 0 : continue; /* do not count prepared xacts */
252 tgl@sss.pgh.pa.us 3605 [ # # ]: 0 : if (!proc->isRegularBackend)
3606 : 0 : continue; /* count only regular backend processes */
3139 andrew@dunslane.net 3607 [ # # ]: 0 : if (!OidIsValid(databaseid) ||
3608 [ # # ]: 0 : proc->databaseId == databaseid)
3609 : 0 : count++;
3610 : : }
3611 : :
3612 : 0 : LWLockRelease(ProcArrayLock);
3613 : :
3614 : 0 : return count;
3615 : : }
3616 : :
3617 : : /*
3618 : : * CancelDBBackends --- cancel backends that are using specified database
3619 : : */
3620 : : void
5705 simon@2ndQuadrant.co 3621 :CBC 8 : CancelDBBackends(Oid databaseid, ProcSignalReason sigmode, bool conflictPending)
3622 : : {
5718 3623 : 8 : ProcArrayStruct *arrayP = procArray;
3624 : : int index;
3625 : :
3626 : : /* tell all backends to die */
3627 : 8 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3628 : :
3629 [ + + ]: 16 : for (index = 0; index < arrayP->numProcs; index++)
3630 : : {
4836 bruce@momjian.us 3631 : 8 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3632 : 8 : PGPROC *proc = &allProcs[pgprocno];
3633 : :
5705 simon@2ndQuadrant.co 3634 [ + + + - ]: 8 : if (databaseid == InvalidOid || proc->databaseId == databaseid)
3635 : : {
3636 : : VirtualTransactionId procvxid;
3637 : : pid_t pid;
3638 : :
5712 3639 : 8 : GET_VXID_FROM_PGPROC(procvxid, *proc);
3640 : :
5705 3641 : 8 : proc->recoveryConflictPending = conflictPending;
5712 3642 : 8 : pid = proc->pid;
3643 [ + - ]: 8 : if (pid != 0)
3644 : : {
3645 : : /*
3646 : : * Kill the pid if it's still here. If not, that's what we
3647 : : * wanted so ignore any errors.
3648 : : */
552 heikki.linnakangas@i 3649 : 8 : (void) SendProcSignal(pid, sigmode, procvxid.procNumber);
3650 : : }
3651 : : }
3652 : : }
3653 : :
5718 simon@2ndQuadrant.co 3654 : 8 : LWLockRelease(ProcArrayLock);
3655 : 8 : }
3656 : :
3657 : : /*
3658 : : * CountUserBackends --- count backends that are used by specified user
3659 : : * (only regular backends, not any type of background worker)
3660 : : */
3661 : : int
7342 tgl@sss.pgh.pa.us 3662 :UBC 0 : CountUserBackends(Oid roleid)
3663 : : {
3664 : 0 : ProcArrayStruct *arrayP = procArray;
3665 : 0 : int count = 0;
3666 : : int index;
3667 : :
3668 : 0 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3669 : :
3670 [ # # ]: 0 : for (index = 0; index < arrayP->numProcs; index++)
3671 : : {
4836 bruce@momjian.us 3672 : 0 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3673 : 0 : PGPROC *proc = &allProcs[pgprocno];
3674 : :
7342 tgl@sss.pgh.pa.us 3675 [ # # ]: 0 : if (proc->pid == 0)
3676 : 0 : continue; /* do not count prepared xacts */
252 3677 [ # # ]: 0 : if (!proc->isRegularBackend)
3678 : 0 : continue; /* count only regular backend processes */
7342 3679 [ # # ]: 0 : if (proc->roleId == roleid)
3680 : 0 : count++;
3681 : : }
3682 : :
3683 : 0 : LWLockRelease(ProcArrayLock);
3684 : :
3685 : 0 : return count;
3686 : : }
3687 : :
3688 : : /*
3689 : : * CountOtherDBBackends -- check for other backends running in the given DB
3690 : : *
3691 : : * If there are other backends in the DB, we will wait a maximum of 5 seconds
3692 : : * for them to exit. Autovacuum backends are encouraged to exit early by
3693 : : * sending them SIGTERM, but normal user backends are just waited for.
3694 : : *
3695 : : * The current backend is always ignored; it is caller's responsibility to
3696 : : * check whether the current backend uses the given DB, if it's important.
3697 : : *
3698 : : * Returns true if there are (still) other backends in the DB, false if not.
3699 : : * Also, *nbackends and *nprepared are set to the number of other backends
3700 : : * and prepared transactions in the DB, respectively.
3701 : : *
3702 : : * This function is used to interlock DROP DATABASE and related commands
3703 : : * against there being any active backends in the target DB --- dropping the
3704 : : * DB while active backends remain would be a Bad Thing. Note that we cannot
3705 : : * detect here the possibility of a newly-started backend that is trying to
3706 : : * connect to the doomed database, so additional interlocking is needed during
3707 : : * backend startup. The caller should normally hold an exclusive lock on the
3708 : : * target DB before calling this, which is one reason we mustn't wait
3709 : : * indefinitely.
3710 : : */
3711 : : bool
6242 tgl@sss.pgh.pa.us 3712 :CBC 431 : CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
3713 : : {
6672 3714 : 431 : ProcArrayStruct *arrayP = procArray;
3715 : :
3716 : : #define MAXAUTOVACPIDS 10 /* max autovacs to SIGTERM per iteration */
3717 : : int autovac_pids[MAXAUTOVACPIDS];
3718 : : int tries;
3719 : :
3720 : : /* 50 tries with 100ms sleep between tries makes 5 sec total wait */
3721 [ + - ]: 431 : for (tries = 0; tries < 50; tries++)
3722 : : {
6242 3723 : 431 : int nautovacs = 0;
6672 3724 : 431 : bool found = false;
3725 : : int index;
3726 : :
3727 [ - + ]: 431 : CHECK_FOR_INTERRUPTS();
3728 : :
6242 3729 : 431 : *nbackends = *nprepared = 0;
3730 : :
6672 3731 : 431 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3732 : :
3733 [ + + ]: 1580 : for (index = 0; index < arrayP->numProcs; index++)
3734 : : {
4836 bruce@momjian.us 3735 : 1149 : int pgprocno = arrayP->pgprocnos[index];
2493 andres@anarazel.de 3736 : 1149 : PGPROC *proc = &allProcs[pgprocno];
1755 alvherre@alvh.no-ip. 3737 : 1149 : uint8 statusFlags = ProcGlobal->statusFlags[index];
3738 : :
6672 tgl@sss.pgh.pa.us 3739 [ + + ]: 1149 : if (proc->databaseId != databaseId)
3740 : 1053 : continue;
3741 [ + - ]: 96 : if (proc == MyProc)
3742 : 96 : continue;
3743 : :
6672 tgl@sss.pgh.pa.us 3744 :UBC 0 : found = true;
3745 : :
6242 3746 [ # # ]: 0 : if (proc->pid == 0)
3747 : 0 : (*nprepared)++;
3748 : : else
3749 : : {
3750 : 0 : (*nbackends)++;
1755 alvherre@alvh.no-ip. 3751 [ # # # # ]: 0 : if ((statusFlags & PROC_IS_AUTOVACUUM) &&
3752 : : nautovacs < MAXAUTOVACPIDS)
6242 tgl@sss.pgh.pa.us 3753 : 0 : autovac_pids[nautovacs++] = proc->pid;
3754 : : }
3755 : : }
3756 : :
6242 tgl@sss.pgh.pa.us 3757 :CBC 431 : LWLockRelease(ProcArrayLock);
3758 : :
6672 3759 [ + - ]: 431 : if (!found)
6505 bruce@momjian.us 3760 : 431 : return false; /* no conflicting backends, so done */
3761 : :
3762 : : /*
3763 : : * Send SIGTERM to any conflicting autovacuums before sleeping. We
3764 : : * postpone this step until after the loop because we don't want to
3765 : : * hold ProcArrayLock while issuing kill(). We have no idea what might
3766 : : * block kill() inside the kernel...
3767 : : */
6242 tgl@sss.pgh.pa.us 3768 [ # # ]:UBC 0 : for (index = 0; index < nautovacs; index++)
3769 : 0 : (void) kill(autovac_pids[index], SIGTERM); /* ignore any error */
3770 : :
3771 : : /* sleep, then try again */
6505 bruce@momjian.us 3772 : 0 : pg_usleep(100 * 1000L); /* 100ms */
3773 : : }
3774 : :
3775 : 0 : return true; /* timed out, still conflicts */
3776 : : }
3777 : :
3778 : : /*
3779 : : * Terminate existing connections to the specified database. This routine
3780 : : * is used by the DROP DATABASE command when user has asked to forcefully
3781 : : * drop the database.
3782 : : *
3783 : : * The current backend is always ignored; it is caller's responsibility to
3784 : : * check whether the current backend uses the given DB, if it's important.
3785 : : *
3786 : : * If the target database has a prepared transaction or permissions checks
3787 : : * fail for a connection, this fails without terminating anything.
3788 : : */
3789 : : void
2125 akapila@postgresql.o 3790 :CBC 1 : TerminateOtherDBBackends(Oid databaseId)
3791 : : {
3792 : 1 : ProcArrayStruct *arrayP = procArray;
3793 : 1 : List *pids = NIL;
3794 : 1 : int nprepared = 0;
3795 : : int i;
3796 : :
3797 : 1 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3798 : :
3799 [ + + ]: 4 : for (i = 0; i < procArray->numProcs; i++)
3800 : : {
3801 : 3 : int pgprocno = arrayP->pgprocnos[i];
3802 : 3 : PGPROC *proc = &allProcs[pgprocno];
3803 : :
3804 [ + - ]: 3 : if (proc->databaseId != databaseId)
3805 : 3 : continue;
2125 akapila@postgresql.o 3806 [ # # ]:UBC 0 : if (proc == MyProc)
3807 : 0 : continue;
3808 : :
3809 [ # # ]: 0 : if (proc->pid != 0)
3810 : 0 : pids = lappend_int(pids, proc->pid);
3811 : : else
3812 : 0 : nprepared++;
3813 : : }
3814 : :
2125 akapila@postgresql.o 3815 :CBC 1 : LWLockRelease(ProcArrayLock);
3816 : :
3817 [ - + ]: 1 : if (nprepared > 0)
2125 akapila@postgresql.o 3818 [ # # ]:UBC 0 : ereport(ERROR,
3819 : : (errcode(ERRCODE_OBJECT_IN_USE),
3820 : : errmsg("database \"%s\" is being used by prepared transactions",
3821 : : get_database_name(databaseId)),
3822 : : errdetail_plural("There is %d prepared transaction using the database.",
3823 : : "There are %d prepared transactions using the database.",
3824 : : nprepared,
3825 : : nprepared)));
3826 : :
2125 akapila@postgresql.o 3827 [ - + ]:CBC 1 : if (pids)
3828 : : {
3829 : : ListCell *lc;
3830 : :
3831 : : /*
3832 : : * Permissions checks relax the pg_terminate_backend checks in two
3833 : : * ways, both by omitting the !OidIsValid(proc->roleId) check:
3834 : : *
3835 : : * - Accept terminating autovacuum workers, since DROP DATABASE
3836 : : * without FORCE terminates them.
3837 : : *
3838 : : * - Accept terminating bgworkers. For bgworker authors, it's
3839 : : * convenient to be able to recommend FORCE if a worker is blocking
3840 : : * DROP DATABASE unexpectedly.
3841 : : *
3842 : : * Unlike pg_terminate_backend, we don't raise some warnings - like
3843 : : * "PID %d is not a PostgreSQL server process", because for us already
3844 : : * finished session is not a problem.
3845 : : */
2125 akapila@postgresql.o 3846 [ # # # # :UBC 0 : foreach(lc, pids)
# # ]
3847 : : {
3848 : 0 : int pid = lfirst_int(lc);
3849 : 0 : PGPROC *proc = BackendPidGetProc(pid);
3850 : :
3851 [ # # ]: 0 : if (proc != NULL)
3852 : : {
3853 [ # # # # ]: 0 : if (superuser_arg(proc->roleId) && !superuser())
3854 [ # # ]: 0 : ereport(ERROR,
3855 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3856 : : errmsg("permission denied to terminate process"),
3857 : : errdetail("Only roles with the %s attribute may terminate processes of roles with the %s attribute.",
3858 : : "SUPERUSER", "SUPERUSER")));
3859 : :
3860 [ # # ]: 0 : if (!has_privs_of_role(GetUserId(), proc->roleId) &&
1619 sfrost@snowman.net 3861 [ # # ]: 0 : !has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_BACKEND))
2125 akapila@postgresql.o 3862 [ # # ]: 0 : ereport(ERROR,
3863 : : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3864 : : errmsg("permission denied to terminate process"),
3865 : : errdetail("Only roles with privileges of the role whose process is being terminated or with privileges of the \"%s\" role may terminate this process.",
3866 : : "pg_signal_backend")));
3867 : : }
3868 : : }
3869 : :
3870 : : /*
3871 : : * There's a race condition here: once we release the ProcArrayLock,
3872 : : * it's possible for the session to exit before we issue kill. That
3873 : : * race condition possibility seems too unlikely to worry about. See
3874 : : * pg_signal_backend.
3875 : : */
3876 [ # # # # : 0 : foreach(lc, pids)
# # ]
3877 : : {
3878 : 0 : int pid = lfirst_int(lc);
3879 : 0 : PGPROC *proc = BackendPidGetProc(pid);
3880 : :
3881 [ # # ]: 0 : if (proc != NULL)
3882 : : {
3883 : : /*
3884 : : * If we have setsid(), signal the backend's whole process
3885 : : * group
3886 : : */
3887 : : #ifdef HAVE_SETSID
3888 : 0 : (void) kill(-pid, SIGTERM);
3889 : : #else
3890 : : (void) kill(pid, SIGTERM);
3891 : : #endif
3892 : : }
3893 : : }
3894 : : }
2125 akapila@postgresql.o 3895 :CBC 1 : }
3896 : :
3897 : : /*
3898 : : * ProcArraySetReplicationSlotXmin
3899 : : *
3900 : : * Install limits to future computations of the xmin horizon to prevent vacuum
3901 : : * and HOT pruning from removing affected rows still needed by clients with
3902 : : * replication slots.
3903 : : */
3904 : : void
4205 rhaas@postgresql.org 3905 : 2189 : ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin,
3906 : : bool already_locked)
3907 : : {
3908 [ + + - + ]: 2189 : Assert(!already_locked || LWLockHeldByMe(ProcArrayLock));
3909 : :
3910 [ + + ]: 2189 : if (!already_locked)
3911 : 1750 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3912 : :
4236 3913 : 2189 : procArray->replication_slot_xmin = xmin;
4205 3914 : 2189 : procArray->replication_slot_catalog_xmin = catalog_xmin;
3915 : :
3916 [ + + ]: 2189 : if (!already_locked)
3917 : 1750 : LWLockRelease(ProcArrayLock);
3918 : :
1019 alvherre@alvh.no-ip. 3919 [ + + ]: 2189 : elog(DEBUG1, "xmin required by slots: data %u, catalog %u",
3920 : : xmin, catalog_xmin);
4205 rhaas@postgresql.org 3921 : 2189 : }
3922 : :
3923 : : /*
3924 : : * ProcArrayGetReplicationSlotXmin
3925 : : *
3926 : : * Return the current slot xmin limits. That's useful to be able to remove
3927 : : * data that's older than those limits.
3928 : : */
3929 : : void
3930 : 21 : ProcArrayGetReplicationSlotXmin(TransactionId *xmin,
3931 : : TransactionId *catalog_xmin)
3932 : : {
3933 : 21 : LWLockAcquire(ProcArrayLock, LW_SHARED);
3934 : :
3935 [ - + ]: 21 : if (xmin != NULL)
4205 rhaas@postgresql.org 3936 :UBC 0 : *xmin = procArray->replication_slot_xmin;
3937 : :
4205 rhaas@postgresql.org 3938 [ + - ]:CBC 21 : if (catalog_xmin != NULL)
3939 : 21 : *catalog_xmin = procArray->replication_slot_catalog_xmin;
3940 : :
4236 3941 : 21 : LWLockRelease(ProcArrayLock);
3942 : 21 : }
3943 : :
3944 : : /*
3945 : : * XidCacheRemoveRunningXids
3946 : : *
3947 : : * Remove a bunch of TransactionIds from the list of known-running
3948 : : * subtransactions for my backend. Both the specified xid and those in
3949 : : * the xids[] array (of length nxids) are removed from the subxids cache.
3950 : : * latestXid must be the latest XID among the group.
3951 : : */
3952 : : void
6573 tgl@sss.pgh.pa.us 3953 : 647 : XidCacheRemoveRunningXids(TransactionId xid,
3954 : : int nxids, const TransactionId *xids,
3955 : : TransactionId latestXid)
3956 : : {
3957 : : int i,
3958 : : j;
3959 : : XidCacheStatus *mysubxidstat;
3960 : :
6943 3961 [ - + ]: 647 : Assert(TransactionIdIsValid(xid));
3962 : :
3963 : : /*
3964 : : * We must hold ProcArrayLock exclusively in order to remove transactions
3965 : : * from the PGPROC array. (See src/backend/access/transam/README.) It's
3966 : : * possible this could be relaxed since we know this routine is only used
3967 : : * to abort subtransactions, but pending closer analysis we'd best be
3968 : : * conservative.
3969 : : *
3970 : : * Note that we do not have to be careful about memory ordering of our own
3971 : : * reads wrt. GetNewTransactionId() here - only this process can modify
3972 : : * relevant fields of MyProc/ProcGlobal->xids[]. But we do have to be
3973 : : * careful about our own writes being well ordered.
3974 : : */
7415 3975 : 647 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
3976 : :
1849 andres@anarazel.de 3977 : 647 : mysubxidstat = &ProcGlobal->subxidStates[MyProc->pgxactoff];
3978 : :
3979 : : /*
3980 : : * Under normal circumstances xid and xids[] will be in increasing order,
3981 : : * as will be the entries in subxids. Scan backwards to avoid O(N^2)
3982 : : * behavior when removing a lot of xids.
3983 : : */
7415 tgl@sss.pgh.pa.us 3984 [ + + ]: 677 : for (i = nxids - 1; i >= 0; i--)
3985 : : {
3986 : 30 : TransactionId anxid = xids[i];
3987 : :
1849 andres@anarazel.de 3988 [ + - ]: 30 : for (j = MyProc->subxidStatus.count - 1; j >= 0; j--)
3989 : : {
7415 tgl@sss.pgh.pa.us 3990 [ + - ]: 30 : if (TransactionIdEquals(MyProc->subxids.xids[j], anxid))
3991 : : {
1849 andres@anarazel.de 3992 : 30 : MyProc->subxids.xids[j] = MyProc->subxids.xids[MyProc->subxidStatus.count - 1];
3993 : 30 : pg_write_barrier();
3994 : 30 : mysubxidstat->count--;
3995 : 30 : MyProc->subxidStatus.count--;
7415 tgl@sss.pgh.pa.us 3996 : 30 : break;
3997 : : }
3998 : : }
3999 : :
4000 : : /*
4001 : : * Ordinarily we should have found it, unless the cache has
4002 : : * overflowed. However it's also possible for this routine to be
4003 : : * invoked multiple times for the same subtransaction, in case of an
4004 : : * error during AbortSubTransaction. So instead of Assert, emit a
4005 : : * debug warning.
4006 : : */
1849 andres@anarazel.de 4007 [ - + - - ]: 30 : if (j < 0 && !MyProc->subxidStatus.overflowed)
7415 tgl@sss.pgh.pa.us 4008 [ # # ]:UBC 0 : elog(WARNING, "did not find subXID %u in MyProc", anxid);
4009 : : }
4010 : :
1849 andres@anarazel.de 4011 [ + - ]:CBC 647 : for (j = MyProc->subxidStatus.count - 1; j >= 0; j--)
4012 : : {
7415 tgl@sss.pgh.pa.us 4013 [ + - ]: 647 : if (TransactionIdEquals(MyProc->subxids.xids[j], xid))
4014 : : {
1849 andres@anarazel.de 4015 : 647 : MyProc->subxids.xids[j] = MyProc->subxids.xids[MyProc->subxidStatus.count - 1];
4016 : 647 : pg_write_barrier();
4017 : 647 : mysubxidstat->count--;
4018 : 647 : MyProc->subxidStatus.count--;
7415 tgl@sss.pgh.pa.us 4019 : 647 : break;
4020 : : }
4021 : : }
4022 : : /* Ordinarily we should have found it, unless the cache has overflowed */
1849 andres@anarazel.de 4023 [ - + - - ]: 647 : if (j < 0 && !MyProc->subxidStatus.overflowed)
7415 tgl@sss.pgh.pa.us 4024 [ # # ]:UBC 0 : elog(WARNING, "did not find subXID %u in MyProc", xid);
4025 : :
4026 : : /* Also advance global latestCompletedXid while holding the lock */
1852 andres@anarazel.de 4027 :CBC 647 : MaintainLatestCompletedXid(latestXid);
4028 : :
4029 : : /* ... and xactCompletionCount */
638 heikki.linnakangas@i 4030 : 647 : TransamVariables->xactCompletionCount++;
4031 : :
7415 tgl@sss.pgh.pa.us 4032 : 647 : LWLockRelease(ProcArrayLock);
4033 : 647 : }
4034 : :
4035 : : #ifdef XIDCACHE_DEBUG
4036 : :
4037 : : /*
4038 : : * Print stats about effectiveness of XID cache
4039 : : */
4040 : : static void
4041 : : DisplayXidCache(void)
4042 : : {
4043 : : fprintf(stderr,
4044 : : "XidCache: xmin: %ld, known: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, knownassigned: %ld, nooflo: %ld, slow: %ld\n",
4045 : : xc_by_recent_xmin,
4046 : : xc_by_known_xact,
4047 : : xc_by_my_xact,
4048 : : xc_by_latest_xid,
4049 : : xc_by_main_xid,
4050 : : xc_by_child_xid,
4051 : : xc_by_known_assigned,
4052 : : xc_no_overflow,
4053 : : xc_slow_answer);
4054 : : }
4055 : : #endif /* XIDCACHE_DEBUG */
4056 : :
4057 : : /*
4058 : : * If rel != NULL, return test state appropriate for relation, otherwise
4059 : : * return state usable for all relations. The latter may consider XIDs as
4060 : : * not-yet-visible-to-everyone that a state for a specific relation would
4061 : : * already consider visible-to-everyone.
4062 : : *
4063 : : * This needs to be called while a snapshot is active or registered, otherwise
4064 : : * there are wraparound and other dangers.
4065 : : *
4066 : : * See comment for GlobalVisState for details.
4067 : : */
4068 : : GlobalVisState *
1851 andres@anarazel.de 4069 : 14050675 : GlobalVisTestFor(Relation rel)
4070 : : {
1505 4071 : 14050675 : GlobalVisState *state = NULL;
4072 : :
4073 : : /* XXX: we should assert that a snapshot is pushed or registered */
1851 4074 [ - + ]: 14050675 : Assert(RecentXmin);
4075 : :
1505 4076 [ + + + + : 14050675 : switch (GlobalVisHorizonKindForRel(rel))
- ]
4077 : : {
4078 : 69509 : case VISHORIZON_SHARED:
4079 : 69509 : state = &GlobalVisSharedRels;
4080 : 69509 : break;
4081 : 2648382 : case VISHORIZON_CATALOG:
4082 : 2648382 : state = &GlobalVisCatalogRels;
4083 : 2648382 : break;
4084 : 11290835 : case VISHORIZON_DATA:
4085 : 11290835 : state = &GlobalVisDataRels;
4086 : 11290835 : break;
4087 : 41949 : case VISHORIZON_TEMP:
4088 : 41949 : state = &GlobalVisTempRels;
4089 : 41949 : break;
4090 : : }
4091 : :
1851 4092 [ + - - + ]: 14050675 : Assert(FullTransactionIdIsValid(state->definitely_needed) &&
4093 : : FullTransactionIdIsValid(state->maybe_needed));
4094 : :
4095 : 14050675 : return state;
4096 : : }
4097 : :
4098 : : /*
4099 : : * Return true if it's worth updating the accurate maybe_needed boundary.
4100 : : *
4101 : : * As it is somewhat expensive to determine xmin horizons, we don't want to
4102 : : * repeatedly do so when there is a low likelihood of it being beneficial.
4103 : : *
4104 : : * The current heuristic is that we update only if RecentXmin has changed
4105 : : * since the last update. If the oldest currently running transaction has not
4106 : : * finished, it is unlikely that recomputing the horizon would be useful.
4107 : : */
4108 : : static bool
4109 : 381272 : GlobalVisTestShouldUpdate(GlobalVisState *state)
4110 : : {
4111 : : /* hasn't been updated yet */
4112 [ + + ]: 381272 : if (!TransactionIdIsValid(ComputeXidHorizonsResultLastXmin))
4113 : 8303 : return true;
4114 : :
4115 : : /*
4116 : : * If the maybe_needed/definitely_needed boundaries are the same, it's
4117 : : * unlikely to be beneficial to refresh boundaries.
4118 : : */
4119 [ - + ]: 372969 : if (FullTransactionIdFollowsOrEquals(state->maybe_needed,
4120 : : state->definitely_needed))
1851 andres@anarazel.de 4121 :UBC 0 : return false;
4122 : :
4123 : : /* does the last snapshot built have a different xmin? */
1851 andres@anarazel.de 4124 :CBC 372969 : return RecentXmin != ComputeXidHorizonsResultLastXmin;
4125 : : }
4126 : :
4127 : : static void
4128 : 96558 : GlobalVisUpdateApply(ComputeXidHorizonsResult *horizons)
4129 : : {
4130 : : GlobalVisSharedRels.maybe_needed =
4131 : 96558 : FullXidRelativeTo(horizons->latest_completed,
4132 : : horizons->shared_oldest_nonremovable);
4133 : : GlobalVisCatalogRels.maybe_needed =
4134 : 96558 : FullXidRelativeTo(horizons->latest_completed,
4135 : : horizons->catalog_oldest_nonremovable);
4136 : : GlobalVisDataRels.maybe_needed =
4137 : 96558 : FullXidRelativeTo(horizons->latest_completed,
4138 : : horizons->data_oldest_nonremovable);
4139 : : GlobalVisTempRels.maybe_needed =
1774 4140 : 96558 : FullXidRelativeTo(horizons->latest_completed,
4141 : : horizons->temp_oldest_nonremovable);
4142 : :
4143 : : /*
4144 : : * In longer running transactions it's possible that transactions we
4145 : : * previously needed to treat as running aren't around anymore. So update
4146 : : * definitely_needed to not be earlier than maybe_needed.
4147 : : */
4148 : : GlobalVisSharedRels.definitely_needed =
1851 4149 : 96558 : FullTransactionIdNewer(GlobalVisSharedRels.maybe_needed,
4150 : : GlobalVisSharedRels.definitely_needed);
4151 : : GlobalVisCatalogRels.definitely_needed =
4152 : 96558 : FullTransactionIdNewer(GlobalVisCatalogRels.maybe_needed,
4153 : : GlobalVisCatalogRels.definitely_needed);
4154 : : GlobalVisDataRels.definitely_needed =
4155 : 96558 : FullTransactionIdNewer(GlobalVisDataRels.maybe_needed,
4156 : : GlobalVisDataRels.definitely_needed);
1774 4157 : 96558 : GlobalVisTempRels.definitely_needed = GlobalVisTempRels.maybe_needed;
4158 : :
1851 4159 : 96558 : ComputeXidHorizonsResultLastXmin = RecentXmin;
4160 : 96558 : }
4161 : :
4162 : : /*
4163 : : * Update boundaries in GlobalVis{Shared,Catalog, Data}Rels
4164 : : * using ComputeXidHorizons().
4165 : : */
4166 : : static void
4167 : 52117 : GlobalVisUpdate(void)
4168 : : {
4169 : : ComputeXidHorizonsResult horizons;
4170 : :
4171 : : /* updates the horizons as a side-effect */
4172 : 52117 : ComputeXidHorizons(&horizons);
4173 : 52117 : }
4174 : :
4175 : : /*
4176 : : * Return true if no snapshot still considers fxid to be running.
4177 : : *
4178 : : * The state passed needs to have been initialized for the relation fxid is
4179 : : * from (NULL is also OK), otherwise the result may not be correct.
4180 : : *
4181 : : * See comment for GlobalVisState for details.
4182 : : */
4183 : : bool
4184 : 9324031 : GlobalVisTestIsRemovableFullXid(GlobalVisState *state,
4185 : : FullTransactionId fxid)
4186 : : {
4187 : : /*
4188 : : * If fxid is older than maybe_needed bound, it definitely is visible to
4189 : : * everyone.
4190 : : */
4191 [ + + ]: 9324031 : if (FullTransactionIdPrecedes(fxid, state->maybe_needed))
4192 : 2273460 : return true;
4193 : :
4194 : : /*
4195 : : * If fxid is >= definitely_needed bound, it is very likely to still be
4196 : : * considered running.
4197 : : */
4198 [ + + ]: 7050571 : if (FullTransactionIdFollowsOrEquals(fxid, state->definitely_needed))
4199 : 6669299 : return false;
4200 : :
4201 : : /*
4202 : : * fxid is between maybe_needed and definitely_needed, i.e. there might or
4203 : : * might not exist a snapshot considering fxid running. If it makes sense,
4204 : : * update boundaries and recheck.
4205 : : */
4206 [ + + ]: 381272 : if (GlobalVisTestShouldUpdate(state))
4207 : : {
4208 : 52117 : GlobalVisUpdate();
4209 : :
4210 [ - + ]: 52117 : Assert(FullTransactionIdPrecedes(fxid, state->definitely_needed));
4211 : :
4212 : 52117 : return FullTransactionIdPrecedes(fxid, state->maybe_needed);
4213 : : }
4214 : : else
4215 : 329155 : return false;
4216 : : }
4217 : :
4218 : : /*
4219 : : * Wrapper around GlobalVisTestIsRemovableFullXid() for 32bit xids.
4220 : : *
4221 : : * It is crucial that this only gets called for xids from a source that
4222 : : * protects against xid wraparounds (e.g. from a table and thus protected by
4223 : : * relfrozenxid).
4224 : : */
4225 : : bool
4226 : 9321811 : GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid)
4227 : : {
4228 : : FullTransactionId fxid;
4229 : :
4230 : : /*
4231 : : * Convert 32 bit argument to FullTransactionId. We can do so safely
4232 : : * because we know the xid has to, at the very least, be between
4233 : : * [oldestXid, nextXid), i.e. within 2 billion of xid. To avoid taking a
4234 : : * lock to determine either, we can just compare with
4235 : : * state->definitely_needed, which was based on those value at the time
4236 : : * the current snapshot was built.
4237 : : */
4238 : 9321811 : fxid = FullXidRelativeTo(state->definitely_needed, xid);
4239 : :
4240 : 9321811 : return GlobalVisTestIsRemovableFullXid(state, fxid);
4241 : : }
4242 : :
4243 : : /*
4244 : : * Convenience wrapper around GlobalVisTestFor() and
4245 : : * GlobalVisTestIsRemovableFullXid(), see their comments.
4246 : : */
4247 : : bool
1672 pg@bowt.ie 4248 : 2220 : GlobalVisCheckRemovableFullXid(Relation rel, FullTransactionId fxid)
4249 : : {
4250 : : GlobalVisState *state;
4251 : :
1851 andres@anarazel.de 4252 : 2220 : state = GlobalVisTestFor(rel);
4253 : :
4254 : 2220 : return GlobalVisTestIsRemovableFullXid(state, fxid);
4255 : : }
4256 : :
4257 : : /*
4258 : : * Convenience wrapper around GlobalVisTestFor() and
4259 : : * GlobalVisTestIsRemovableXid(), see their comments.
4260 : : */
4261 : : bool
4262 : 6 : GlobalVisCheckRemovableXid(Relation rel, TransactionId xid)
4263 : : {
4264 : : GlobalVisState *state;
4265 : :
4266 : 6 : state = GlobalVisTestFor(rel);
4267 : :
4268 : 6 : return GlobalVisTestIsRemovableXid(state, xid);
4269 : : }
4270 : :
4271 : : /*
4272 : : * Convert a 32 bit transaction id into 64 bit transaction id, by assuming it
4273 : : * is within MaxTransactionId / 2 of XidFromFullTransactionId(rel).
4274 : : *
4275 : : * Be very careful about when to use this function. It can only safely be used
4276 : : * when there is a guarantee that xid is within MaxTransactionId / 2 xids of
4277 : : * rel. That e.g. can be guaranteed if the caller assures a snapshot is
4278 : : * held by the backend and xid is from a table (where vacuum/freezing ensures
4279 : : * the xid has to be within that range), or if xid is from the procarray and
4280 : : * prevents xid wraparound that way.
4281 : : */
4282 : : static inline FullTransactionId
1852 4283 : 11028761 : FullXidRelativeTo(FullTransactionId rel, TransactionId xid)
4284 : : {
4285 : 11028761 : TransactionId rel_xid = XidFromFullTransactionId(rel);
4286 : :
4287 [ - + ]: 11028761 : Assert(TransactionIdIsValid(xid));
4288 [ - + ]: 11028761 : Assert(TransactionIdIsValid(rel_xid));
4289 : :
4290 : : /* not guaranteed to find issues, but likely to catch mistakes */
4291 : 11028761 : AssertTransactionIdInAllowableRange(xid);
4292 : :
4293 : 22057522 : return FullTransactionIdFromU64(U64FromFullTransactionId(rel)
4294 : 11028761 : + (int32) (xid - rel_xid));
4295 : : }
4296 : :
4297 : :
4298 : : /* ----------------------------------------------
4299 : : * KnownAssignedTransactionIds sub-module
4300 : : * ----------------------------------------------
4301 : : */
4302 : :
4303 : : /*
4304 : : * In Hot Standby mode, we maintain a list of transactions that are (or were)
4305 : : * running on the primary at the current point in WAL. These XIDs must be
4306 : : * treated as running by standby transactions, even though they are not in
4307 : : * the standby server's PGPROC array.
4308 : : *
4309 : : * We record all XIDs that we know have been assigned. That includes all the
4310 : : * XIDs seen in WAL records, plus all unobserved XIDs that we can deduce have
4311 : : * been assigned. We can deduce the existence of unobserved XIDs because we
4312 : : * know XIDs are assigned in sequence, with no gaps. The KnownAssignedXids
4313 : : * list expands as new XIDs are observed or inferred, and contracts when
4314 : : * transaction completion records arrive.
4315 : : *
4316 : : * During hot standby we do not fret too much about the distinction between
4317 : : * top-level XIDs and subtransaction XIDs. We store both together in the
4318 : : * KnownAssignedXids list. In backends, this is copied into snapshots in
4319 : : * GetSnapshotData(), taking advantage of the fact that XidInMVCCSnapshot()
4320 : : * doesn't care about the distinction either. Subtransaction XIDs are
4321 : : * effectively treated as top-level XIDs and in the typical case pg_subtrans
4322 : : * links are *not* maintained (which does not affect visibility).
4323 : : *
4324 : : * We have room in KnownAssignedXids and in snapshots to hold maxProcs *
4325 : : * (1 + PGPROC_MAX_CACHED_SUBXIDS) XIDs, so every primary transaction must
4326 : : * report its subtransaction XIDs in a WAL XLOG_XACT_ASSIGNMENT record at
4327 : : * least every PGPROC_MAX_CACHED_SUBXIDS. When we receive one of these
4328 : : * records, we mark the subXIDs as children of the top XID in pg_subtrans,
4329 : : * and then remove them from KnownAssignedXids. This prevents overflow of
4330 : : * KnownAssignedXids and snapshots, at the cost that status checks for these
4331 : : * subXIDs will take a slower path through TransactionIdIsInProgress().
4332 : : * This means that KnownAssignedXids is not necessarily complete for subXIDs,
4333 : : * though it should be complete for top-level XIDs; this is the same situation
4334 : : * that holds with respect to the PGPROC entries in normal running.
4335 : : *
4336 : : * When we throw away subXIDs from KnownAssignedXids, we need to keep track of
4337 : : * that, similarly to tracking overflow of a PGPROC's subxids array. We do
4338 : : * that by remembering the lastOverflowedXid, ie the last thrown-away subXID.
4339 : : * As long as that is within the range of interesting XIDs, we have to assume
4340 : : * that subXIDs are missing from snapshots. (Note that subXID overflow occurs
4341 : : * on primary when 65th subXID arrives, whereas on standby it occurs when 64th
4342 : : * subXID arrives - that is not an error.)
4343 : : *
4344 : : * Should a backend on primary somehow disappear before it can write an abort
4345 : : * record, then we just leave those XIDs in KnownAssignedXids. They actually
4346 : : * aborted but we think they were running; the distinction is irrelevant
4347 : : * because either way any changes done by the transaction are not visible to
4348 : : * backends in the standby. We prune KnownAssignedXids when
4349 : : * XLOG_RUNNING_XACTS arrives, to forestall possible overflow of the
4350 : : * array due to such dead XIDs.
4351 : : */
4352 : :
4353 : : /*
4354 : : * RecordKnownAssignedTransactionIds
4355 : : * Record the given XID in KnownAssignedXids, as well as any preceding
4356 : : * unobserved XIDs.
4357 : : *
4358 : : * RecordKnownAssignedTransactionIds() should be run for *every* WAL record
4359 : : * associated with a transaction. Must be called for each record after we
4360 : : * have executed StartupCLOG() et al, since we must ExtendCLOG() etc..
4361 : : *
4362 : : * Called during recovery in analogy with and in place of GetNewTransactionId()
4363 : : */
4364 : : void
5740 simon@2ndQuadrant.co 4365 : 2457328 : RecordKnownAssignedTransactionIds(TransactionId xid)
4366 : : {
5595 4367 [ - + ]: 2457328 : Assert(standbyState >= STANDBY_INITIALIZED);
5594 4368 [ - + ]: 2457328 : Assert(TransactionIdIsValid(xid));
4306 heikki.linnakangas@i 4369 [ - + ]: 2457328 : Assert(TransactionIdIsValid(latestObservedXid));
4370 : :
635 michael@paquier.xyz 4371 [ - + ]: 2457328 : elog(DEBUG4, "record known xact %u latestObservedXid %u",
4372 : : xid, latestObservedXid);
4373 : :
4374 : : /*
4375 : : * When a newly observed xid arrives, it is frequently the case that it is
4376 : : * *not* the next xid in sequence. When this occurs, we must treat the
4377 : : * intervening xids as running also.
4378 : : */
5740 simon@2ndQuadrant.co 4379 [ + + ]: 2457328 : if (TransactionIdFollows(xid, latestObservedXid))
4380 : : {
4381 : : TransactionId next_expected_xid;
4382 : :
4383 : : /*
4384 : : * Extend subtrans like we do in GetNewTransactionId() during normal
4385 : : * operation using individual extend steps. Note that we do not need
4386 : : * to extend clog since its extensions are WAL logged.
4387 : : *
4388 : : * This part has to be done regardless of standbyState since we
4389 : : * immediately start assigning subtransactions to their toplevel
4390 : : * transactions.
4391 : : */
5610 tgl@sss.pgh.pa.us 4392 : 21838 : next_expected_xid = latestObservedXid;
4306 heikki.linnakangas@i 4393 [ + + ]: 44627 : while (TransactionIdPrecedes(next_expected_xid, xid))
4394 : : {
4395 [ - + ]: 22789 : TransactionIdAdvance(next_expected_xid);
5740 simon@2ndQuadrant.co 4396 : 22789 : ExtendSUBTRANS(next_expected_xid);
4397 : : }
4306 heikki.linnakangas@i 4398 [ - + ]: 21838 : Assert(next_expected_xid == xid);
4399 : :
4400 : : /*
4401 : : * If the KnownAssignedXids machinery isn't up yet, there's nothing
4402 : : * more to do since we don't track assigned xids yet.
4403 : : */
4404 [ - + ]: 21838 : if (standbyState <= STANDBY_INITIALIZED)
4405 : : {
4306 heikki.linnakangas@i 4406 :UBC 0 : latestObservedXid = xid;
4407 : 0 : return;
4408 : : }
4409 : :
4410 : : /*
4411 : : * Add (latestObservedXid, xid] onto the KnownAssignedXids array.
4412 : : */
5610 tgl@sss.pgh.pa.us 4413 :CBC 21838 : next_expected_xid = latestObservedXid;
4414 [ - + ]: 21838 : TransactionIdAdvance(next_expected_xid);
4415 : 21838 : KnownAssignedXidsAdd(next_expected_xid, xid, false);
4416 : :
4417 : : /*
4418 : : * Now we can advance latestObservedXid
4419 : : */
5740 simon@2ndQuadrant.co 4420 : 21838 : latestObservedXid = xid;
4421 : :
4422 : : /* TransamVariables->nextXid must be beyond any observed xid */
2354 tmunro@postgresql.or 4423 : 21838 : AdvanceNextFullTransactionIdPastXid(latestObservedXid);
4424 : : }
4425 : : }
4426 : :
4427 : : /*
4428 : : * ExpireTreeKnownAssignedTransactionIds
4429 : : * Remove the given XIDs from KnownAssignedXids.
4430 : : *
4431 : : * Called during recovery in analogy with and in place of ProcArrayEndTransaction()
4432 : : */
4433 : : void
5740 simon@2ndQuadrant.co 4434 : 21257 : ExpireTreeKnownAssignedTransactionIds(TransactionId xid, int nsubxids,
4435 : : TransactionId *subxids, TransactionId max_xid)
4436 : : {
5595 4437 [ - + ]: 21257 : Assert(standbyState >= STANDBY_INITIALIZED);
4438 : :
4439 : : /*
4440 : : * Uses same locking as transaction commit
4441 : : */
5740 4442 : 21257 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4443 : :
5610 tgl@sss.pgh.pa.us 4444 : 21257 : KnownAssignedXidsRemoveTree(xid, nsubxids, subxids);
4445 : :
4446 : : /* As in ProcArrayEndTransaction, advance latestCompletedXid */
1852 andres@anarazel.de 4447 : 21257 : MaintainLatestCompletedXidRecovery(max_xid);
4448 : :
4449 : : /* ... and xactCompletionCount */
638 heikki.linnakangas@i 4450 : 21257 : TransamVariables->xactCompletionCount++;
4451 : :
5740 simon@2ndQuadrant.co 4452 : 21257 : LWLockRelease(ProcArrayLock);
4453 : 21257 : }
4454 : :
4455 : : /*
4456 : : * ExpireAllKnownAssignedTransactionIds
4457 : : * Remove all entries in KnownAssignedXids and reset lastOverflowedXid.
4458 : : */
4459 : : void
4460 : 100 : ExpireAllKnownAssignedTransactionIds(void)
4461 : : {
4462 : : FullTransactionId latestXid;
4463 : :
4464 : 100 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
5610 tgl@sss.pgh.pa.us 4465 : 100 : KnownAssignedXidsRemovePreceding(InvalidTransactionId);
4466 : :
4467 : : /* Reset latestCompletedXid to nextXid - 1 */
167 heikki.linnakangas@i 4468 [ - + ]: 100 : Assert(FullTransactionIdIsValid(TransamVariables->nextXid));
4469 : 100 : latestXid = TransamVariables->nextXid;
4470 : 100 : FullTransactionIdRetreat(&latestXid);
4471 : 100 : TransamVariables->latestCompletedXid = latestXid;
4472 : :
4473 : : /*
4474 : : * Any transactions that were in-progress were effectively aborted, so
4475 : : * advance xactCompletionCount.
4476 : : */
4477 : 100 : TransamVariables->xactCompletionCount++;
4478 : :
4479 : : /*
4480 : : * Reset lastOverflowedXid. Currently, lastOverflowedXid has no use after
4481 : : * the call of this function. But do this for unification with what
4482 : : * ExpireOldKnownAssignedTransactionIds() do.
4483 : : */
1400 akorotkov@postgresql 4484 : 100 : procArray->lastOverflowedXid = InvalidTransactionId;
5740 simon@2ndQuadrant.co 4485 : 100 : LWLockRelease(ProcArrayLock);
4486 : 100 : }
4487 : :
4488 : : /*
4489 : : * ExpireOldKnownAssignedTransactionIds
4490 : : * Remove KnownAssignedXids entries preceding the given XID and
4491 : : * potentially reset lastOverflowedXid.
4492 : : */
4493 : : void
4494 : 735 : ExpireOldKnownAssignedTransactionIds(TransactionId xid)
4495 : : {
4496 : : TransactionId latestXid;
4497 : :
4498 : 735 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4499 : :
4500 : : /* As in ProcArrayEndTransaction, advance latestCompletedXid */
167 heikki.linnakangas@i 4501 : 735 : latestXid = xid;
4502 [ - + ]: 735 : TransactionIdRetreat(latestXid);
4503 : 735 : MaintainLatestCompletedXidRecovery(latestXid);
4504 : :
4505 : : /* ... and xactCompletionCount */
4506 : 735 : TransamVariables->xactCompletionCount++;
4507 : :
4508 : : /*
4509 : : * Reset lastOverflowedXid if we know all transactions that have been
4510 : : * possibly running are being gone. Not doing so could cause an incorrect
4511 : : * lastOverflowedXid value, which makes extra snapshots be marked as
4512 : : * suboverflowed.
4513 : : */
1400 akorotkov@postgresql 4514 [ + + ]: 735 : if (TransactionIdPrecedes(procArray->lastOverflowedXid, xid))
4515 : 729 : procArray->lastOverflowedXid = InvalidTransactionId;
5610 tgl@sss.pgh.pa.us 4516 : 735 : KnownAssignedXidsRemovePreceding(xid);
5740 simon@2ndQuadrant.co 4517 : 735 : LWLockRelease(ProcArrayLock);
4518 : 735 : }
4519 : :
4520 : : /*
4521 : : * KnownAssignedTransactionIdsIdleMaintenance
4522 : : * Opportunistically do maintenance work when the startup process
4523 : : * is about to go idle.
4524 : : */
4525 : : void
1012 tgl@sss.pgh.pa.us 4526 : 11031 : KnownAssignedTransactionIdsIdleMaintenance(void)
4527 : : {
4528 : 11031 : KnownAssignedXidsCompress(KAX_STARTUP_PROCESS_IDLE, false);
4529 : 11031 : }
4530 : :
4531 : :
4532 : : /*
4533 : : * Private module functions to manipulate KnownAssignedXids
4534 : : *
4535 : : * There are 5 main uses of the KnownAssignedXids data structure:
4536 : : *
4537 : : * * backends taking snapshots - all valid XIDs need to be copied out
4538 : : * * backends seeking to determine presence of a specific XID
4539 : : * * startup process adding new known-assigned XIDs
4540 : : * * startup process removing specific XIDs as transactions end
4541 : : * * startup process pruning array when special WAL records arrive
4542 : : *
4543 : : * This data structure is known to be a hot spot during Hot Standby, so we
4544 : : * go to some lengths to make these operations as efficient and as concurrent
4545 : : * as possible.
4546 : : *
4547 : : * The XIDs are stored in an array in sorted order --- TransactionIdPrecedes
4548 : : * order, to be exact --- to allow binary search for specific XIDs. Note:
4549 : : * in general TransactionIdPrecedes would not provide a total order, but
4550 : : * we know that the entries present at any instant should not extend across
4551 : : * a large enough fraction of XID space to wrap around (the primary would
4552 : : * shut down for fear of XID wrap long before that happens). So it's OK to
4553 : : * use TransactionIdPrecedes as a binary-search comparator.
4554 : : *
4555 : : * It's cheap to maintain the sortedness during insertions, since new known
4556 : : * XIDs are always reported in XID order; we just append them at the right.
4557 : : *
4558 : : * To keep individual deletions cheap, we need to allow gaps in the array.
4559 : : * This is implemented by marking array elements as valid or invalid using
4560 : : * the parallel boolean array KnownAssignedXidsValid[]. A deletion is done
4561 : : * by setting KnownAssignedXidsValid[i] to false, *without* clearing the
4562 : : * XID entry itself. This preserves the property that the XID entries are
4563 : : * sorted, so we can do binary searches easily. Periodically we compress
4564 : : * out the unused entries; that's much cheaper than having to compress the
4565 : : * array immediately on every deletion.
4566 : : *
4567 : : * The actually valid items in KnownAssignedXids[] and KnownAssignedXidsValid[]
4568 : : * are those with indexes tail <= i < head; items outside this subscript range
4569 : : * have unspecified contents. When head reaches the end of the array, we
4570 : : * force compression of unused entries rather than wrapping around, since
4571 : : * allowing wraparound would greatly complicate the search logic. We maintain
4572 : : * an explicit tail pointer so that pruning of old XIDs can be done without
4573 : : * immediately moving the array contents. In most cases only a small fraction
4574 : : * of the array contains valid entries at any instant.
4575 : : *
4576 : : * Although only the startup process can ever change the KnownAssignedXids
4577 : : * data structure, we still need interlocking so that standby backends will
4578 : : * not observe invalid intermediate states. The convention is that backends
4579 : : * must hold shared ProcArrayLock to examine the array. To remove XIDs from
4580 : : * the array, the startup process must hold ProcArrayLock exclusively, for
4581 : : * the usual transactional reasons (compare commit/abort of a transaction
4582 : : * during normal running). Compressing unused entries out of the array
4583 : : * likewise requires exclusive lock. To add XIDs to the array, we just insert
4584 : : * them into slots to the right of the head pointer and then advance the head
4585 : : * pointer. This doesn't require any lock at all, but on machines with weak
4586 : : * memory ordering, we need to be careful that other processors see the array
4587 : : * element changes before they see the head pointer change. We handle this by
4588 : : * using memory barriers when reading or writing the head/tail pointers (unless
4589 : : * the caller holds ProcArrayLock exclusively).
4590 : : *
4591 : : * Algorithmic analysis:
4592 : : *
4593 : : * If we have a maximum of M slots, with N XIDs currently spread across
4594 : : * S elements then we have N <= S <= M always.
4595 : : *
4596 : : * * Adding a new XID is O(1) and needs no lock (unless compression must
4597 : : * happen)
4598 : : * * Compressing the array is O(S) and requires exclusive lock
4599 : : * * Removing an XID is O(logS) and requires exclusive lock
4600 : : * * Taking a snapshot is O(S) and requires shared lock
4601 : : * * Checking for an XID is O(logS) and requires shared lock
4602 : : *
4603 : : * In comparison, using a hash table for KnownAssignedXids would mean that
4604 : : * taking snapshots would be O(M). If we can maintain S << M then the
4605 : : * sorted array technique will deliver significantly faster snapshots.
4606 : : * If we try to keep S too small then we will spend too much time compressing,
4607 : : * so there is an optimal point for any workload mix. We use a heuristic to
4608 : : * decide when to compress the array, though trimming also helps reduce
4609 : : * frequency of compressing. The heuristic requires us to track the number of
4610 : : * currently valid XIDs in the array (N). Except in special cases, we'll
4611 : : * compress when S >= 2N. Bounding S at 2N in turn bounds the time for
4612 : : * taking a snapshot to be O(N), which it would have to be anyway.
4613 : : */
4614 : :
4615 : :
4616 : : /*
4617 : : * Compress KnownAssignedXids by shifting valid data down to the start of the
4618 : : * array, removing any gaps.
4619 : : *
4620 : : * A compression step is forced if "reason" is KAX_NO_SPACE, otherwise
4621 : : * we do it only if a heuristic indicates it's a good time to do it.
4622 : : *
4623 : : * Compression requires holding ProcArrayLock in exclusive mode.
4624 : : * Caller must pass haveLock = true if it already holds the lock.
4625 : : */
4626 : : static void
4627 : 33044 : KnownAssignedXidsCompress(KAXCompressReason reason, bool haveLock)
4628 : : {
2493 andres@anarazel.de 4629 : 33044 : ProcArrayStruct *pArray = procArray;
4630 : : int head,
4631 : : tail,
4632 : : nelements;
4633 : : int compress_index;
4634 : : int i;
4635 : :
4636 : : /* Counters for compression heuristics */
4637 : : static unsigned int transactionEndsCounter;
4638 : : static TimestampTz lastCompressTs;
4639 : :
4640 : : /* Tuning constants */
4641 : : #define KAX_COMPRESS_FREQUENCY 128 /* in transactions */
4642 : : #define KAX_COMPRESS_IDLE_INTERVAL 1000 /* in ms */
4643 : :
4644 : : /*
4645 : : * Since only the startup process modifies the head/tail pointers, we
4646 : : * don't need a lock to read them here.
4647 : : */
5610 tgl@sss.pgh.pa.us 4648 : 33044 : head = pArray->headKnownAssignedXids;
4649 : 33044 : tail = pArray->tailKnownAssignedXids;
1012 4650 : 33044 : nelements = head - tail;
4651 : :
4652 : : /*
4653 : : * If we can choose whether to compress, use a heuristic to avoid
4654 : : * compressing too often or not often enough. "Compress" here simply
4655 : : * means moving the values to the beginning of the array, so it is not as
4656 : : * complex or costly as typical data compression algorithms.
4657 : : */
4658 [ + + ]: 33044 : if (nelements == pArray->numKnownAssignedXids)
4659 : : {
4660 : : /*
4661 : : * When there are no gaps between head and tail, don't bother to
4662 : : * compress, except in the KAX_NO_SPACE case where we must compress to
4663 : : * create some space after the head.
4664 : : */
4665 [ + - ]: 16090 : if (reason != KAX_NO_SPACE)
4666 : 16090 : return;
4667 : : }
4668 [ + + ]: 16954 : else if (reason == KAX_TRANSACTION_END)
4669 : : {
4670 : : /*
4671 : : * Consider compressing only once every so many commits. Frequency
4672 : : * determined by benchmarks.
4673 : : */
4674 [ + + ]: 13477 : if ((transactionEndsCounter++) % KAX_COMPRESS_FREQUENCY != 0)
4675 : 13362 : return;
4676 : :
4677 : : /*
4678 : : * Furthermore, compress only if the used part of the array is less
4679 : : * than 50% full (see comments above).
4680 : : */
4681 [ + + ]: 115 : if (nelements < 2 * pArray->numKnownAssignedXids)
5610 4682 : 13 : return;
4683 : : }
1012 4684 [ + + ]: 3477 : else if (reason == KAX_STARTUP_PROCESS_IDLE)
4685 : : {
4686 : : /*
4687 : : * We're about to go idle for lack of new WAL, so we might as well
4688 : : * compress. But not too often, to avoid ProcArray lock contention
4689 : : * with readers.
4690 : : */
4691 [ + + ]: 3358 : if (lastCompressTs != 0)
4692 : : {
4693 : : TimestampTz compress_after;
4694 : :
4695 : 3357 : compress_after = TimestampTzPlusMilliseconds(lastCompressTs,
4696 : : KAX_COMPRESS_IDLE_INTERVAL);
4697 [ + + ]: 3357 : if (GetCurrentTimestamp() < compress_after)
4698 : 3326 : return;
4699 : : }
4700 : : }
4701 : :
4702 : : /* Need to compress, so get the lock if we don't have it. */
4703 [ + + ]: 253 : if (!haveLock)
4704 : 32 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
4705 : :
4706 : : /*
4707 : : * We compress the array by reading the valid values from tail to head,
4708 : : * re-aligning data to 0th element.
4709 : : */
5610 4710 : 253 : compress_index = 0;
4711 [ + + ]: 9705 : for (i = tail; i < head; i++)
4712 : : {
4713 [ + + ]: 9452 : if (KnownAssignedXidsValid[i])
4714 : : {
4715 : 1012 : KnownAssignedXids[compress_index] = KnownAssignedXids[i];
4716 : 1012 : KnownAssignedXidsValid[compress_index] = true;
4717 : 1012 : compress_index++;
4718 : : }
4719 : : }
1012 4720 [ - + ]: 253 : Assert(compress_index == pArray->numKnownAssignedXids);
4721 : :
5610 4722 : 253 : pArray->tailKnownAssignedXids = 0;
4723 : 253 : pArray->headKnownAssignedXids = compress_index;
4724 : :
1012 4725 [ + + ]: 253 : if (!haveLock)
4726 : 32 : LWLockRelease(ProcArrayLock);
4727 : :
4728 : : /* Update timestamp for maintenance. No need to hold lock for this. */
4729 : 253 : lastCompressTs = GetCurrentTimestamp();
4730 : : }
4731 : :
4732 : : /*
4733 : : * Add xids into KnownAssignedXids at the head of the array.
4734 : : *
4735 : : * xids from from_xid to to_xid, inclusive, are added to the array.
4736 : : *
4737 : : * If exclusive_lock is true then caller already holds ProcArrayLock in
4738 : : * exclusive mode, so we need no extra locking here. Else caller holds no
4739 : : * lock, so we need to be sure we maintain sufficient interlocks against
4740 : : * concurrent readers. (Only the startup process ever calls this, so no need
4741 : : * to worry about concurrent writers.)
4742 : : */
4743 : : static void
5610 4744 : 21842 : KnownAssignedXidsAdd(TransactionId from_xid, TransactionId to_xid,
4745 : : bool exclusive_lock)
4746 : : {
2493 andres@anarazel.de 4747 : 21842 : ProcArrayStruct *pArray = procArray;
4748 : : TransactionId next_xid;
4749 : : int head,
4750 : : tail;
4751 : : int nxids;
4752 : : int i;
4753 : :
5610 tgl@sss.pgh.pa.us 4754 [ - + ]: 21842 : Assert(TransactionIdPrecedesOrEquals(from_xid, to_xid));
4755 : :
4756 : : /*
4757 : : * Calculate how many array slots we'll need. Normally this is cheap; in
4758 : : * the unusual case where the XIDs cross the wrap point, we do it the hard
4759 : : * way.
4760 : : */
4761 [ + - ]: 21842 : if (to_xid >= from_xid)
4762 : 21842 : nxids = to_xid - from_xid + 1;
4763 : : else
4764 : : {
5610 tgl@sss.pgh.pa.us 4765 :UBC 0 : nxids = 1;
4766 : 0 : next_xid = from_xid;
4767 [ # # ]: 0 : while (TransactionIdPrecedes(next_xid, to_xid))
4768 : : {
4769 : 0 : nxids++;
4770 [ # # ]: 0 : TransactionIdAdvance(next_xid);
4771 : : }
4772 : : }
4773 : :
4774 : : /*
4775 : : * Since only the startup process modifies the head/tail pointers, we
4776 : : * don't need a lock to read them here.
4777 : : */
5610 tgl@sss.pgh.pa.us 4778 :CBC 21842 : head = pArray->headKnownAssignedXids;
4779 : 21842 : tail = pArray->tailKnownAssignedXids;
4780 : :
4781 [ + - - + ]: 21842 : Assert(head >= 0 && head <= pArray->maxKnownAssignedXids);
4782 [ + - - + ]: 21842 : Assert(tail >= 0 && tail < pArray->maxKnownAssignedXids);
4783 : :
4784 : : /*
4785 : : * Verify that insertions occur in TransactionId sequence. Note that even
4786 : : * if the last existing element is marked invalid, it must still have a
4787 : : * correctly sequenced XID value.
4788 : : */
4789 [ + + - + ]: 36453 : if (head > tail &&
4790 : 14611 : TransactionIdFollowsOrEquals(KnownAssignedXids[head - 1], from_xid))
4791 : : {
5610 tgl@sss.pgh.pa.us 4792 :UBC 0 : KnownAssignedXidsDisplay(LOG);
4793 [ # # ]: 0 : elog(ERROR, "out-of-order XID insertion in KnownAssignedXids");
4794 : : }
4795 : :
4796 : : /*
4797 : : * If our xids won't fit in the remaining space, compress out free space
4798 : : */
5610 tgl@sss.pgh.pa.us 4799 [ - + ]:CBC 21842 : if (head + nxids > pArray->maxKnownAssignedXids)
4800 : : {
1012 tgl@sss.pgh.pa.us 4801 :UBC 0 : KnownAssignedXidsCompress(KAX_NO_SPACE, exclusive_lock);
4802 : :
5610 4803 : 0 : head = pArray->headKnownAssignedXids;
4804 : : /* note: we no longer care about the tail pointer */
4805 : :
4806 : : /*
4807 : : * If it still won't fit then we're out of memory
4808 : : */
4809 [ # # ]: 0 : if (head + nxids > pArray->maxKnownAssignedXids)
1981 peter@eisentraut.org 4810 [ # # ]: 0 : elog(ERROR, "too many KnownAssignedXids");
4811 : : }
4812 : :
4813 : : /* Now we can insert the xids into the space starting at head */
5610 tgl@sss.pgh.pa.us 4814 :CBC 21842 : next_xid = from_xid;
4815 [ + + ]: 44635 : for (i = 0; i < nxids; i++)
4816 : : {
4817 : 22793 : KnownAssignedXids[head] = next_xid;
4818 : 22793 : KnownAssignedXidsValid[head] = true;
4819 [ - + ]: 22793 : TransactionIdAdvance(next_xid);
4820 : 22793 : head++;
4821 : : }
4822 : :
4823 : : /* Adjust count of number of valid entries */
4824 : 21842 : pArray->numKnownAssignedXids += nxids;
4825 : :
4826 : : /*
4827 : : * Now update the head pointer. We use a write barrier to ensure that
4828 : : * other processors see the above array updates before they see the head
4829 : : * pointer change. The barrier isn't required if we're holding
4830 : : * ProcArrayLock exclusively.
4831 : : */
732 nathan@postgresql.or 4832 [ + + ]: 21842 : if (!exclusive_lock)
4833 : 21838 : pg_write_barrier();
4834 : :
4835 : 21842 : pArray->headKnownAssignedXids = head;
5610 tgl@sss.pgh.pa.us 4836 : 21842 : }
4837 : :
4838 : : /*
4839 : : * KnownAssignedXidsSearch
4840 : : *
4841 : : * Searches KnownAssignedXids for a specific xid and optionally removes it.
4842 : : * Returns true if it was found, false if not.
4843 : : *
4844 : : * Caller must hold ProcArrayLock in shared or exclusive mode.
4845 : : * Exclusive lock must be held for remove = true.
4846 : : */
4847 : : static bool
4848 : 23927 : KnownAssignedXidsSearch(TransactionId xid, bool remove)
4849 : : {
2493 andres@anarazel.de 4850 : 23927 : ProcArrayStruct *pArray = procArray;
4851 : : int first,
4852 : : last;
4853 : : int head;
4854 : : int tail;
5541 bruce@momjian.us 4855 : 23927 : int result_index = -1;
4856 : :
732 nathan@postgresql.or 4857 : 23927 : tail = pArray->tailKnownAssignedXids;
4858 : 23927 : head = pArray->headKnownAssignedXids;
4859 : :
4860 : : /*
4861 : : * Only the startup process removes entries, so we don't need the read
4862 : : * barrier in that case.
4863 : : */
4864 [ + + ]: 23927 : if (!remove)
4865 : 1 : pg_read_barrier(); /* pairs with KnownAssignedXidsAdd */
4866 : :
4867 : : /*
4868 : : * Standard binary search. Note we can ignore the KnownAssignedXidsValid
4869 : : * array here, since even invalid entries will contain sorted XIDs.
4870 : : */
5610 tgl@sss.pgh.pa.us 4871 : 23927 : first = tail;
4872 : 23927 : last = head - 1;
4873 [ + + ]: 84526 : while (first <= last)
4874 : : {
4875 : : int mid_index;
4876 : : TransactionId mid_xid;
4877 : :
4878 : 83328 : mid_index = (first + last) / 2;
4879 : 83328 : mid_xid = KnownAssignedXids[mid_index];
4880 : :
4881 [ + + ]: 83328 : if (xid == mid_xid)
4882 : : {
4883 : 22729 : result_index = mid_index;
4884 : 22729 : break;
4885 : : }
4886 [ + + ]: 60599 : else if (TransactionIdPrecedes(xid, mid_xid))
4887 : 12999 : last = mid_index - 1;
4888 : : else
4889 : 47600 : first = mid_index + 1;
4890 : : }
4891 : :
4892 [ + + ]: 23927 : if (result_index < 0)
4893 : 1198 : return false; /* not in array */
4894 : :
4895 [ + + ]: 22729 : if (!KnownAssignedXidsValid[result_index])
4896 : 22 : return false; /* in array, but invalid */
4897 : :
4898 [ + - ]: 22707 : if (remove)
4899 : : {
4900 : 22707 : KnownAssignedXidsValid[result_index] = false;
4901 : :
4902 : 22707 : pArray->numKnownAssignedXids--;
4903 [ - + ]: 22707 : Assert(pArray->numKnownAssignedXids >= 0);
4904 : :
4905 : : /*
4906 : : * If we're removing the tail element then advance tail pointer over
4907 : : * any invalid elements. This will speed future searches.
4908 : : */
4909 [ + + ]: 22707 : if (result_index == tail)
4910 : : {
4911 : 8271 : tail++;
4912 [ + + + + ]: 14267 : while (tail < head && !KnownAssignedXidsValid[tail])
4913 : 5996 : tail++;
4914 [ + + ]: 8271 : if (tail >= head)
4915 : : {
4916 : : /* Array is empty, so we can reset both pointers */
4917 : 7221 : pArray->headKnownAssignedXids = 0;
4918 : 7221 : pArray->tailKnownAssignedXids = 0;
4919 : : }
4920 : : else
4921 : : {
4922 : 1050 : pArray->tailKnownAssignedXids = tail;
4923 : : }
4924 : : }
4925 : : }
4926 : :
4927 : 22707 : return true;
4928 : : }
4929 : :
4930 : : /*
4931 : : * Is the specified XID present in KnownAssignedXids[]?
4932 : : *
4933 : : * Caller must hold ProcArrayLock in shared or exclusive mode.
4934 : : */
4935 : : static bool
4936 : 1 : KnownAssignedXidExists(TransactionId xid)
4937 : : {
4938 [ - + ]: 1 : Assert(TransactionIdIsValid(xid));
4939 : :
4940 : 1 : return KnownAssignedXidsSearch(xid, false);
4941 : : }
4942 : :
4943 : : /*
4944 : : * Remove the specified XID from KnownAssignedXids[].
4945 : : *
4946 : : * Caller must hold ProcArrayLock in exclusive mode.
4947 : : */
4948 : : static void
5740 simon@2ndQuadrant.co 4949 : 23926 : KnownAssignedXidsRemove(TransactionId xid)
4950 : : {
4951 [ - + ]: 23926 : Assert(TransactionIdIsValid(xid));
4952 : :
635 michael@paquier.xyz 4953 [ - + ]: 23926 : elog(DEBUG4, "remove KnownAssignedXid %u", xid);
4954 : :
4955 : : /*
4956 : : * Note: we cannot consider it an error to remove an XID that's not
4957 : : * present. We intentionally remove subxact IDs while processing
4958 : : * XLOG_XACT_ASSIGNMENT, to avoid array overflow. Then those XIDs will be
4959 : : * removed again when the top-level xact commits or aborts.
4960 : : *
4961 : : * It might be possible to track such XIDs to distinguish this case from
4962 : : * actual errors, but it would be complicated and probably not worth it.
4963 : : * So, just ignore the search result.
4964 : : */
5610 tgl@sss.pgh.pa.us 4965 : 23926 : (void) KnownAssignedXidsSearch(xid, true);
5740 simon@2ndQuadrant.co 4966 : 23926 : }
4967 : :
4968 : : /*
4969 : : * KnownAssignedXidsRemoveTree
4970 : : * Remove xid (if it's not InvalidTransactionId) and all the subxids.
4971 : : *
4972 : : * Caller must hold ProcArrayLock in exclusive mode.
4973 : : */
4974 : : static void
5610 tgl@sss.pgh.pa.us 4975 : 21278 : KnownAssignedXidsRemoveTree(TransactionId xid, int nsubxids,
4976 : : TransactionId *subxids)
4977 : : {
4978 : : int i;
4979 : :
4980 [ + + ]: 21278 : if (TransactionIdIsValid(xid))
4981 : 21257 : KnownAssignedXidsRemove(xid);
4982 : :
4983 [ + + ]: 23947 : for (i = 0; i < nsubxids; i++)
4984 : 2669 : KnownAssignedXidsRemove(subxids[i]);
4985 : :
4986 : : /* Opportunistically compress the array */
1012 4987 : 21278 : KnownAssignedXidsCompress(KAX_TRANSACTION_END, true);
5740 simon@2ndQuadrant.co 4988 : 21278 : }
4989 : :
4990 : : /*
4991 : : * Prune KnownAssignedXids up to, but *not* including xid. If xid is invalid
4992 : : * then clear the whole table.
4993 : : *
4994 : : * Caller must hold ProcArrayLock in exclusive mode.
4995 : : */
4996 : : static void
5610 tgl@sss.pgh.pa.us 4997 : 835 : KnownAssignedXidsRemovePreceding(TransactionId removeXid)
4998 : : {
2493 andres@anarazel.de 4999 : 835 : ProcArrayStruct *pArray = procArray;
5541 bruce@momjian.us 5000 : 835 : int count = 0;
5001 : : int head,
5002 : : tail,
5003 : : i;
5004 : :
5610 tgl@sss.pgh.pa.us 5005 [ + + ]: 835 : if (!TransactionIdIsValid(removeXid))
5006 : : {
635 michael@paquier.xyz 5007 [ - + ]: 100 : elog(DEBUG4, "removing all KnownAssignedXids");
5610 tgl@sss.pgh.pa.us 5008 : 100 : pArray->numKnownAssignedXids = 0;
5009 : 100 : pArray->headKnownAssignedXids = pArray->tailKnownAssignedXids = 0;
5010 : 100 : return;
5011 : : }
5012 : :
635 michael@paquier.xyz 5013 [ - + ]: 735 : elog(DEBUG4, "prune KnownAssignedXids to %u", removeXid);
5014 : :
5015 : : /*
5016 : : * Mark entries invalid starting at the tail. Since array is sorted, we
5017 : : * can stop as soon as we reach an entry >= removeXid.
5018 : : */
5610 tgl@sss.pgh.pa.us 5019 : 735 : tail = pArray->tailKnownAssignedXids;
5020 : 735 : head = pArray->headKnownAssignedXids;
5021 : :
5022 [ + + ]: 735 : for (i = tail; i < head; i++)
5023 : : {
5024 [ + - ]: 201 : if (KnownAssignedXidsValid[i])
5025 : : {
5541 bruce@momjian.us 5026 : 201 : TransactionId knownXid = KnownAssignedXids[i];
5027 : :
5610 tgl@sss.pgh.pa.us 5028 [ + - ]: 201 : if (TransactionIdFollowsOrEquals(knownXid, removeXid))
5029 : 201 : break;
5030 : :
5610 tgl@sss.pgh.pa.us 5031 [ # # ]:UBC 0 : if (!StandbyTransactionIdIsPrepared(knownXid))
5032 : : {
5033 : 0 : KnownAssignedXidsValid[i] = false;
5034 : 0 : count++;
5035 : : }
5036 : : }
5037 : : }
5038 : :
5610 tgl@sss.pgh.pa.us 5039 :CBC 735 : pArray->numKnownAssignedXids -= count;
5040 [ - + ]: 735 : Assert(pArray->numKnownAssignedXids >= 0);
5041 : :
5042 : : /*
5043 : : * Advance the tail pointer if we've marked the tail item invalid.
5044 : : */
5045 [ + + ]: 735 : for (i = tail; i < head; i++)
5046 : : {
5047 [ + - ]: 201 : if (KnownAssignedXidsValid[i])
5048 : 201 : break;
5049 : : }
5050 [ + + ]: 735 : if (i >= head)
5051 : : {
5052 : : /* Array is empty, so we can reset both pointers */
5053 : 534 : pArray->headKnownAssignedXids = 0;
5054 : 534 : pArray->tailKnownAssignedXids = 0;
5055 : : }
5056 : : else
5057 : : {
5058 : 201 : pArray->tailKnownAssignedXids = i;
5059 : : }
5060 : :
5061 : : /* Opportunistically compress the array */
1012 5062 : 735 : KnownAssignedXidsCompress(KAX_PRUNE, true);
5063 : : }
5064 : :
5065 : : /*
5066 : : * KnownAssignedXidsGet - Get an array of xids by scanning KnownAssignedXids.
5067 : : * We filter out anything >= xmax.
5068 : : *
5069 : : * Returns the number of XIDs stored into xarray[]. Caller is responsible
5070 : : * that array is large enough.
5071 : : *
5072 : : * Caller must hold ProcArrayLock in (at least) shared mode.
5073 : : */
5074 : : static int
5610 tgl@sss.pgh.pa.us 5075 :UBC 0 : KnownAssignedXidsGet(TransactionId *xarray, TransactionId xmax)
5076 : : {
5077 : 0 : TransactionId xtmp = InvalidTransactionId;
5078 : :
5079 : 0 : return KnownAssignedXidsGetAndSetXmin(xarray, &xtmp, xmax);
5080 : : }
5081 : :
5082 : : /*
5083 : : * KnownAssignedXidsGetAndSetXmin - as KnownAssignedXidsGet, plus
5084 : : * we reduce *xmin to the lowest xid value seen if not already lower.
5085 : : *
5086 : : * Caller must hold ProcArrayLock in (at least) shared mode.
5087 : : */
5088 : : static int
5610 tgl@sss.pgh.pa.us 5089 :CBC 695 : KnownAssignedXidsGetAndSetXmin(TransactionId *xarray, TransactionId *xmin,
5090 : : TransactionId xmax)
5091 : : {
5092 : 695 : int count = 0;
5093 : : int head,
5094 : : tail;
5095 : : int i;
5096 : :
5097 : : /*
5098 : : * Fetch head just once, since it may change while we loop. We can stop
5099 : : * once we reach the initially seen head, since we are certain that an xid
5100 : : * cannot enter and then leave the array while we hold ProcArrayLock. We
5101 : : * might miss newly-added xids, but they should be >= xmax so irrelevant
5102 : : * anyway.
5103 : : */
3613 rhaas@postgresql.org 5104 : 695 : tail = procArray->tailKnownAssignedXids;
5105 : 695 : head = procArray->headKnownAssignedXids;
5106 : :
732 nathan@postgresql.or 5107 : 695 : pg_read_barrier(); /* pairs with KnownAssignedXidsAdd */
5108 : :
5610 tgl@sss.pgh.pa.us 5109 [ + + ]: 719 : for (i = tail; i < head; i++)
5110 : : {
5111 : : /* Skip any gaps in the array */
5112 [ + + ]: 90 : if (KnownAssignedXidsValid[i])
5113 : : {
5114 : 80 : TransactionId knownXid = KnownAssignedXids[i];
5115 : :
5116 : : /*
5117 : : * Update xmin if required. Only the first XID need be checked,
5118 : : * since the array is sorted.
5119 : : */
5120 [ + - + + ]: 160 : if (count == 0 &&
5121 : 80 : TransactionIdPrecedes(knownXid, *xmin))
5122 : 14 : *xmin = knownXid;
5123 : :
5124 : : /*
5125 : : * Filter out anything >= xmax, again relying on sorted property
5126 : : * of array.
5127 : : */
5595 simon@2ndQuadrant.co 5128 [ + - + + ]: 160 : if (TransactionIdIsValid(xmax) &&
5129 : 80 : TransactionIdFollowsOrEquals(knownXid, xmax))
5610 tgl@sss.pgh.pa.us 5130 : 66 : break;
5131 : :
5132 : : /* Add knownXid into output array */
5133 : 14 : xarray[count++] = knownXid;
5134 : : }
5135 : : }
5136 : :
5137 : 695 : return count;
5138 : : }
5139 : :
5140 : : /*
5141 : : * Get oldest XID in the KnownAssignedXids array, or InvalidTransactionId
5142 : : * if nothing there.
5143 : : */
5144 : : static TransactionId
5486 simon@2ndQuadrant.co 5145 : 338 : KnownAssignedXidsGetOldestXmin(void)
5146 : : {
5147 : : int head,
5148 : : tail;
5149 : : int i;
5150 : :
5151 : : /*
5152 : : * Fetch head just once, since it may change while we loop.
5153 : : */
3613 rhaas@postgresql.org 5154 : 338 : tail = procArray->tailKnownAssignedXids;
5155 : 338 : head = procArray->headKnownAssignedXids;
5156 : :
732 nathan@postgresql.or 5157 : 338 : pg_read_barrier(); /* pairs with KnownAssignedXidsAdd */
5158 : :
5486 simon@2ndQuadrant.co 5159 [ + + ]: 338 : for (i = tail; i < head; i++)
5160 : : {
5161 : : /* Skip any gaps in the array */
5162 [ + - ]: 146 : if (KnownAssignedXidsValid[i])
5163 : 146 : return KnownAssignedXids[i];
5164 : : }
5165 : :
5166 : 192 : return InvalidTransactionId;
5167 : : }
5168 : :
5169 : : /*
5170 : : * Display KnownAssignedXids to provide debug trail
5171 : : *
5172 : : * Currently this is only called within startup process, so we need no
5173 : : * special locking.
5174 : : *
5175 : : * Note this is pretty expensive, and much of the expense will be incurred
5176 : : * even if the elog message will get discarded. It's not currently called
5177 : : * in any performance-critical places, however, so no need to be tenser.
5178 : : */
5179 : : static void
5740 5180 : 104 : KnownAssignedXidsDisplay(int trace_level)
5181 : : {
2493 andres@anarazel.de 5182 : 104 : ProcArrayStruct *pArray = procArray;
5183 : : StringInfoData buf;
5184 : : int head,
5185 : : tail,
5186 : : i;
5541 bruce@momjian.us 5187 : 104 : int nxids = 0;
5188 : :
5610 tgl@sss.pgh.pa.us 5189 : 104 : tail = pArray->tailKnownAssignedXids;
5190 : 104 : head = pArray->headKnownAssignedXids;
5191 : :
5740 simon@2ndQuadrant.co 5192 : 104 : initStringInfo(&buf);
5193 : :
5610 tgl@sss.pgh.pa.us 5194 [ + + ]: 112 : for (i = tail; i < head; i++)
5195 : : {
5196 [ + - ]: 8 : if (KnownAssignedXidsValid[i])
5197 : : {
5198 : 8 : nxids++;
5504 rhaas@postgresql.org 5199 : 8 : appendStringInfo(&buf, "[%d]=%u ", i, KnownAssignedXids[i]);
5200 : : }
5201 : : }
5202 : :
5203 [ - + ]: 104 : elog(trace_level, "%d KnownAssignedXids (num=%d tail=%d head=%d) %s",
5204 : : nxids,
5205 : : pArray->numKnownAssignedXids,
5206 : : pArray->tailKnownAssignedXids,
5207 : : pArray->headKnownAssignedXids,
5208 : : buf.data);
5209 : :
5740 simon@2ndQuadrant.co 5210 : 104 : pfree(buf.data);
5211 : 104 : }
5212 : :
5213 : : /*
5214 : : * KnownAssignedXidsReset
5215 : : * Resets KnownAssignedXids to be empty
5216 : : */
5217 : : static void
4838 simon@2ndQuadrant.co 5218 :UBC 0 : KnownAssignedXidsReset(void)
5219 : : {
2493 andres@anarazel.de 5220 : 0 : ProcArrayStruct *pArray = procArray;
5221 : :
4838 simon@2ndQuadrant.co 5222 : 0 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
5223 : :
5224 : 0 : pArray->numKnownAssignedXids = 0;
5225 : 0 : pArray->tailKnownAssignedXids = 0;
5226 : 0 : pArray->headKnownAssignedXids = 0;
5227 : :
5228 : 0 : LWLockRelease(ProcArrayLock);
5229 : 0 : }
|