PostgreSQL Source Code  git master
multixact.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * multixact.c
4  * PostgreSQL multi-transaction-log manager
5  *
6  * The pg_multixact manager is a pg_xact-like manager that stores an array of
7  * MultiXactMember for each MultiXactId. It is a fundamental part of the
8  * shared-row-lock implementation. Each MultiXactMember is comprised of a
9  * TransactionId and a set of flag bits. The name is a bit historical:
10  * originally, a MultiXactId consisted of more than one TransactionId (except
11  * in rare corner cases), hence "multi". Nowadays, however, it's perfectly
12  * legitimate to have MultiXactIds that only include a single Xid.
13  *
14  * The meaning of the flag bits is opaque to this module, but they are mostly
15  * used in heapam.c to identify lock modes that each of the member transactions
16  * is holding on any given tuple. This module just contains support to store
17  * and retrieve the arrays.
18  *
19  * We use two SLRU areas, one for storing the offsets at which the data
20  * starts for each MultiXactId in the other one. This trick allows us to
21  * store variable length arrays of TransactionIds. (We could alternatively
22  * use one area containing counts and TransactionIds, with valid MultiXactId
23  * values pointing at slots containing counts; but that way seems less robust
24  * since it would get completely confused if someone inquired about a bogus
25  * MultiXactId that pointed to an intermediate slot containing an XID.)
26  *
27  * XLOG interactions: this module generates a record whenever a new OFFSETs or
28  * MEMBERs page is initialized to zeroes, as well as an
29  * XLOG_MULTIXACT_CREATE_ID record whenever a new MultiXactId is defined.
30  * This module ignores the WAL rule "write xlog before data," because it
31  * suffices that actions recording a MultiXactId in a heap xmax do follow that
32  * rule. The only way for the MXID to be referenced from any data page is for
33  * heap_lock_tuple() or heap_update() to have put it there, and each generates
34  * an XLOG record that must follow ours. The normal LSN interlock between the
35  * data page and that XLOG record will ensure that our XLOG record reaches
36  * disk first. If the SLRU members/offsets data reaches disk sooner than the
37  * XLOG records, we do not care; after recovery, no xmax will refer to it. On
38  * the flip side, to ensure that all referenced entries _do_ reach disk, this
39  * module's XLOG records completely rebuild the data entered since the last
40  * checkpoint. We flush and sync all dirty OFFSETs and MEMBERs pages to disk
41  * before each checkpoint is considered complete.
42  *
43  * Like clog.c, and unlike subtrans.c, we have to preserve state across
44  * crashes and ensure that MXID and offset numbering increases monotonically
45  * across a crash. We do this in the same way as it's done for transaction
46  * IDs: the WAL record is guaranteed to contain evidence of every MXID we
47  * could need to worry about, and we just make sure that at the end of
48  * replay, the next-MXID and next-offset counters are at least as large as
49  * anything we saw during replay.
50  *
51  * We are able to remove segments no longer necessary by carefully tracking
52  * each table's used values: during vacuum, any multixact older than a certain
53  * value is removed; the cutoff value is stored in pg_class. The minimum value
54  * across all tables in each database is stored in pg_database, and the global
55  * minimum across all databases is part of pg_control and is kept in shared
56  * memory. Whenever that minimum is advanced, the SLRUs are truncated.
57  *
58  * When new multixactid values are to be created, care is taken that the
59  * counter does not fall within the wraparound horizon considering the global
60  * minimum value.
61  *
62  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
63  * Portions Copyright (c) 1994, Regents of the University of California
64  *
65  * src/backend/access/transam/multixact.c
66  *
67  *-------------------------------------------------------------------------
68  */
69 #include "postgres.h"
70 
71 #include "access/multixact.h"
72 #include "access/slru.h"
73 #include "access/transam.h"
74 #include "access/twophase.h"
75 #include "access/twophase_rmgr.h"
76 #include "access/xact.h"
77 #include "access/xloginsert.h"
78 #include "access/xlogutils.h"
79 #include "catalog/pg_type.h"
80 #include "commands/dbcommands.h"
81 #include "funcapi.h"
82 #include "lib/ilist.h"
83 #include "miscadmin.h"
84 #include "pg_trace.h"
85 #include "postmaster/autovacuum.h"
86 #include "storage/lmgr.h"
87 #include "storage/pmsignal.h"
88 #include "storage/proc.h"
89 #include "storage/procarray.h"
90 #include "utils/builtins.h"
91 #include "utils/memutils.h"
92 #include "utils/snapmgr.h"
93 
94 
95 /*
96  * Defines for MultiXactOffset page sizes. A page is the same BLCKSZ as is
97  * used everywhere else in Postgres.
98  *
99  * Note: because MultiXactOffsets are 32 bits and wrap around at 0xFFFFFFFF,
100  * MultiXact page numbering also wraps around at
101  * 0xFFFFFFFF/MULTIXACT_OFFSETS_PER_PAGE, and segment numbering at
102  * 0xFFFFFFFF/MULTIXACT_OFFSETS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need
103  * take no explicit notice of that fact in this module, except when comparing
104  * segment and page numbers in TruncateMultiXact (see
105  * MultiXactOffsetPagePrecedes).
106  */
107 
108 /* We need four bytes per offset */
109 #define MULTIXACT_OFFSETS_PER_PAGE (BLCKSZ / sizeof(MultiXactOffset))
110 
111 #define MultiXactIdToOffsetPage(xid) \
112  ((xid) / (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
113 #define MultiXactIdToOffsetEntry(xid) \
114  ((xid) % (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
115 #define MultiXactIdToOffsetSegment(xid) (MultiXactIdToOffsetPage(xid) / SLRU_PAGES_PER_SEGMENT)
116 
117 /*
118  * The situation for members is a bit more complex: we store one byte of
119  * additional flag bits for each TransactionId. To do this without getting
120  * into alignment issues, we store four bytes of flags, and then the
121  * corresponding 4 Xids. Each such 5-word (20-byte) set we call a "group", and
122  * are stored as a whole in pages. Thus, with 8kB BLCKSZ, we keep 409 groups
123  * per page. This wastes 12 bytes per page, but that's OK -- simplicity (and
124  * performance) trumps space efficiency here.
125  *
126  * Note that the "offset" macros work with byte offset, not array indexes, so
127  * arithmetic must be done using "char *" pointers.
128  */
129 /* We need eight bits per xact, so one xact fits in a byte */
130 #define MXACT_MEMBER_BITS_PER_XACT 8
131 #define MXACT_MEMBER_FLAGS_PER_BYTE 1
132 #define MXACT_MEMBER_XACT_BITMASK ((1 << MXACT_MEMBER_BITS_PER_XACT) - 1)
133 
134 /* how many full bytes of flags are there in a group? */
135 #define MULTIXACT_FLAGBYTES_PER_GROUP 4
136 #define MULTIXACT_MEMBERS_PER_MEMBERGROUP \
137  (MULTIXACT_FLAGBYTES_PER_GROUP * MXACT_MEMBER_FLAGS_PER_BYTE)
138 /* size in bytes of a complete group */
139 #define MULTIXACT_MEMBERGROUP_SIZE \
140  (sizeof(TransactionId) * MULTIXACT_MEMBERS_PER_MEMBERGROUP + MULTIXACT_FLAGBYTES_PER_GROUP)
141 #define MULTIXACT_MEMBERGROUPS_PER_PAGE (BLCKSZ / MULTIXACT_MEMBERGROUP_SIZE)
142 #define MULTIXACT_MEMBERS_PER_PAGE \
143  (MULTIXACT_MEMBERGROUPS_PER_PAGE * MULTIXACT_MEMBERS_PER_MEMBERGROUP)
144 
145 /*
146  * Because the number of items per page is not a divisor of the last item
147  * number (member 0xFFFFFFFF), the last segment does not use the maximum number
148  * of pages, and moreover the last used page therein does not use the same
149  * number of items as previous pages. (Another way to say it is that the
150  * 0xFFFFFFFF member is somewhere in the middle of the last page, so the page
151  * has some empty space after that item.)
152  *
153  * This constant is the number of members in the last page of the last segment.
154  */
155 #define MAX_MEMBERS_IN_LAST_MEMBERS_PAGE \
156  ((uint32) ((0xFFFFFFFF % MULTIXACT_MEMBERS_PER_PAGE) + 1))
157 
158 /* page in which a member is to be found */
159 #define MXOffsetToMemberPage(xid) ((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
160 #define MXOffsetToMemberSegment(xid) (MXOffsetToMemberPage(xid) / SLRU_PAGES_PER_SEGMENT)
161 
162 /* Location (byte offset within page) of flag word for a given member */
163 #define MXOffsetToFlagsOffset(xid) \
164  ((((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_MEMBERGROUP) % \
165  (TransactionId) MULTIXACT_MEMBERGROUPS_PER_PAGE) * \
166  (TransactionId) MULTIXACT_MEMBERGROUP_SIZE)
167 #define MXOffsetToFlagsBitShift(xid) \
168  (((xid) % (TransactionId) MULTIXACT_MEMBERS_PER_MEMBERGROUP) * \
169  MXACT_MEMBER_BITS_PER_XACT)
170 
171 /* Location (byte offset within page) of TransactionId of given member */
172 #define MXOffsetToMemberOffset(xid) \
173  (MXOffsetToFlagsOffset(xid) + MULTIXACT_FLAGBYTES_PER_GROUP + \
174  ((xid) % MULTIXACT_MEMBERS_PER_MEMBERGROUP) * sizeof(TransactionId))
175 
176 /* Multixact members wraparound thresholds. */
177 #define MULTIXACT_MEMBER_SAFE_THRESHOLD (MaxMultiXactOffset / 2)
178 #define MULTIXACT_MEMBER_DANGER_THRESHOLD \
179  (MaxMultiXactOffset - MaxMultiXactOffset / 4)
180 
181 #define PreviousMultiXactId(xid) \
182  ((xid) == FirstMultiXactId ? MaxMultiXactId : (xid) - 1)
183 
184 /*
185  * Links to shared-memory data structures for MultiXact control
186  */
189 
190 #define MultiXactOffsetCtl (&MultiXactOffsetCtlData)
191 #define MultiXactMemberCtl (&MultiXactMemberCtlData)
192 
193 /*
194  * MultiXact state shared across all backends. All this state is protected
195  * by MultiXactGenLock. (We also use MultiXactOffsetSLRULock and
196  * MultiXactMemberSLRULock to guard accesses to the two sets of SLRU
197  * buffers. For concurrency's sake, we avoid holding more than one of these
198  * locks at a time.)
199  */
200 typedef struct MultiXactStateData
201 {
202  /* next-to-be-assigned MultiXactId */
204 
205  /* next-to-be-assigned offset */
207 
208  /* Have we completed multixact startup? */
210 
211  /*
212  * Oldest multixact that is still potentially referenced by a relation.
213  * Anything older than this should not be consulted. These values are
214  * updated by vacuum.
215  */
218 
219  /*
220  * Oldest multixact offset that is potentially referenced by a multixact
221  * referenced by a relation. We don't always know this value, so there's
222  * a flag here to indicate whether or not we currently do.
223  */
226 
227  /* support for anti-wraparound measures */
232 
233  /* support for members anti-wraparound measures */
234  MultiXactOffset offsetStopLimit; /* known if oldestOffsetKnown */
235 
236  /*
237  * Per-backend data starts here. We have two arrays stored in the area
238  * immediately following the MultiXactStateData struct. Each is indexed by
239  * BackendId.
240  *
241  * In both arrays, there's a slot for all normal backends (1..MaxBackends)
242  * followed by a slot for max_prepared_xacts prepared transactions. Valid
243  * BackendIds start from 1; element zero of each array is never used.
244  *
245  * OldestMemberMXactId[k] is the oldest MultiXactId each backend's current
246  * transaction(s) could possibly be a member of, or InvalidMultiXactId
247  * when the backend has no live transaction that could possibly be a
248  * member of a MultiXact. Each backend sets its entry to the current
249  * nextMXact counter just before first acquiring a shared lock in a given
250  * transaction, and clears it at transaction end. (This works because only
251  * during or after acquiring a shared lock could an XID possibly become a
252  * member of a MultiXact, and that MultiXact would have to be created
253  * during or after the lock acquisition.)
254  *
255  * OldestVisibleMXactId[k] is the oldest MultiXactId each backend's
256  * current transaction(s) think is potentially live, or InvalidMultiXactId
257  * when not in a transaction or not in a transaction that's paid any
258  * attention to MultiXacts yet. This is computed when first needed in a
259  * given transaction, and cleared at transaction end. We can compute it
260  * as the minimum of the valid OldestMemberMXactId[] entries at the time
261  * we compute it (using nextMXact if none are valid). Each backend is
262  * required not to attempt to access any SLRU data for MultiXactIds older
263  * than its own OldestVisibleMXactId[] setting; this is necessary because
264  * the relevant SLRU data can be concurrently truncated away.
265  *
266  * The oldest valid value among all of the OldestMemberMXactId[] and
267  * OldestVisibleMXactId[] entries is considered by vacuum as the earliest
268  * possible value still having any live member transaction -- OldestMxact.
269  * Any value older than that is typically removed from tuple headers, or
270  * "frozen" via being replaced with a new xmax. VACUUM can sometimes even
271  * remove an individual MultiXact xmax whose value is >= its OldestMxact
272  * cutoff, though typically only when no individual member XID is still
273  * running. See FreezeMultiXactId for full details.
274  *
275  * Whenever VACUUM advances relminmxid, then either its OldestMxact cutoff
276  * or the oldest extant Multi remaining in the table is used as the new
277  * pg_class.relminmxid value (whichever is earlier). The minimum of all
278  * relminmxid values in each database is stored in pg_database.datminmxid.
279  * In turn, the minimum of all of those values is stored in pg_control.
280  * This is used as the truncation point for pg_multixact when unneeded
281  * segments get removed by vac_truncate_clog() during vacuuming.
282  */
285 
286 /*
287  * Last element of OldestMemberMXactId and OldestVisibleMXactId arrays.
288  * Valid elements are (1..MaxOldestSlot); element 0 is never used.
289  */
290 #define MaxOldestSlot (MaxBackends + max_prepared_xacts)
291 
292 /* Pointers to the state data in shared memory */
296 
297 
298 /*
299  * Definitions for the backend-local MultiXactId cache.
300  *
301  * We use this cache to store known MultiXacts, so we don't need to go to
302  * SLRU areas every time.
303  *
304  * The cache lasts for the duration of a single transaction, the rationale
305  * for this being that most entries will contain our own TransactionId and
306  * so they will be uninteresting by the time our next transaction starts.
307  * (XXX not clear that this is correct --- other members of the MultiXact
308  * could hang around longer than we did. However, it's not clear what a
309  * better policy for flushing old cache entries would be.) FIXME actually
310  * this is plain wrong now that multixact's may contain update Xids.
311  *
312  * We allocate the cache entries in a memory context that is deleted at
313  * transaction end, so we don't need to do retail freeing of entries.
314  */
315 typedef struct mXactCacheEnt
316 {
318  int nmembers;
322 
323 #define MAX_CACHE_ENTRIES 256
326 
327 #ifdef MULTIXACT_DEBUG
328 #define debug_elog2(a,b) elog(a,b)
329 #define debug_elog3(a,b,c) elog(a,b,c)
330 #define debug_elog4(a,b,c,d) elog(a,b,c,d)
331 #define debug_elog5(a,b,c,d,e) elog(a,b,c,d,e)
332 #define debug_elog6(a,b,c,d,e,f) elog(a,b,c,d,e,f)
333 #else
334 #define debug_elog2(a,b)
335 #define debug_elog3(a,b,c)
336 #define debug_elog4(a,b,c,d)
337 #define debug_elog5(a,b,c,d,e)
338 #define debug_elog6(a,b,c,d,e,f)
339 #endif
340 
341 /* internal MultiXactId management */
342 static void MultiXactIdSetOldestVisible(void);
343 static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
344  int nmembers, MultiXactMember *members);
345 static MultiXactId GetNewMultiXactId(int nmembers, MultiXactOffset *offset);
346 
347 /* MultiXact cache management */
348 static int mxactMemberComparator(const void *arg1, const void *arg2);
349 static MultiXactId mXactCacheGetBySet(int nmembers, MultiXactMember *members);
350 static int mXactCacheGetById(MultiXactId multi, MultiXactMember **members);
351 static void mXactCachePut(MultiXactId multi, int nmembers,
352  MultiXactMember *members);
353 
354 static char *mxstatus_to_string(MultiXactStatus status);
355 
356 /* management of SLRU infrastructure */
357 static int ZeroMultiXactOffsetPage(int64 pageno, bool writeXlog);
358 static int ZeroMultiXactMemberPage(int64 pageno, bool writeXlog);
359 static bool MultiXactOffsetPagePrecedes(int64 page1, int64 page2);
360 static bool MultiXactMemberPagePrecedes(int64 page1, int64 page2);
361 static bool MultiXactOffsetPrecedes(MultiXactOffset offset1,
362  MultiXactOffset offset2);
363 static void ExtendMultiXactOffset(MultiXactId multi);
364 static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers);
365 static bool MultiXactOffsetWouldWrap(MultiXactOffset boundary,
366  MultiXactOffset start, uint32 distance);
367 static bool SetOffsetVacuumLimit(bool is_startup);
368 static bool find_multixact_start(MultiXactId multi, MultiXactOffset *result);
369 static void WriteMZeroPageXlogRec(int64 pageno, uint8 info);
370 static void WriteMTruncateXlogRec(Oid oldestMultiDB,
371  MultiXactId startTruncOff,
372  MultiXactId endTruncOff,
373  MultiXactOffset startTruncMemb,
374  MultiXactOffset endTruncMemb);
375 
376 
377 /*
378  * MultiXactIdCreate
379  * Construct a MultiXactId representing two TransactionIds.
380  *
381  * The two XIDs must be different, or be requesting different statuses.
382  *
383  * NB - we don't worry about our local MultiXactId cache here, because that
384  * is handled by the lower-level routines.
385  */
388  TransactionId xid2, MultiXactStatus status2)
389 {
390  MultiXactId newMulti;
391  MultiXactMember members[2];
392 
395 
396  Assert(!TransactionIdEquals(xid1, xid2) || (status1 != status2));
397 
398  /* MultiXactIdSetOldestMember() must have been called already. */
400 
401  /*
402  * Note: unlike MultiXactIdExpand, we don't bother to check that both XIDs
403  * are still running. In typical usage, xid2 will be our own XID and the
404  * caller just did a check on xid1, so it'd be wasted effort.
405  */
406 
407  members[0].xid = xid1;
408  members[0].status = status1;
409  members[1].xid = xid2;
410  members[1].status = status2;
411 
412  newMulti = MultiXactIdCreateFromMembers(2, members);
413 
414  debug_elog3(DEBUG2, "Create: %s",
415  mxid_to_string(newMulti, 2, members));
416 
417  return newMulti;
418 }
419 
420 /*
421  * MultiXactIdExpand
422  * Add a TransactionId to a pre-existing MultiXactId.
423  *
424  * If the TransactionId is already a member of the passed MultiXactId with the
425  * same status, just return it as-is.
426  *
427  * Note that we do NOT actually modify the membership of a pre-existing
428  * MultiXactId; instead we create a new one. This is necessary to avoid
429  * a race condition against code trying to wait for one MultiXactId to finish;
430  * see notes in heapam.c.
431  *
432  * NB - we don't worry about our local MultiXactId cache here, because that
433  * is handled by the lower-level routines.
434  *
435  * Note: It is critical that MultiXactIds that come from an old cluster (i.e.
436  * one upgraded by pg_upgrade from a cluster older than this feature) are not
437  * passed in.
438  */
441 {
442  MultiXactId newMulti;
443  MultiXactMember *members;
444  MultiXactMember *newMembers;
445  int nmembers;
446  int i;
447  int j;
448 
449  Assert(MultiXactIdIsValid(multi));
451 
452  /* MultiXactIdSetOldestMember() must have been called already. */
454 
455  debug_elog5(DEBUG2, "Expand: received multi %u, xid %u status %s",
456  multi, xid, mxstatus_to_string(status));
457 
458  /*
459  * Note: we don't allow for old multis here. The reason is that the only
460  * caller of this function does a check that the multixact is no longer
461  * running.
462  */
463  nmembers = GetMultiXactIdMembers(multi, &members, false, false);
464 
465  if (nmembers < 0)
466  {
467  MultiXactMember member;
468 
469  /*
470  * The MultiXactId is obsolete. This can only happen if all the
471  * MultiXactId members stop running between the caller checking and
472  * passing it to us. It would be better to return that fact to the
473  * caller, but it would complicate the API and it's unlikely to happen
474  * too often, so just deal with it by creating a singleton MultiXact.
475  */
476  member.xid = xid;
477  member.status = status;
478  newMulti = MultiXactIdCreateFromMembers(1, &member);
479 
480  debug_elog4(DEBUG2, "Expand: %u has no members, create singleton %u",
481  multi, newMulti);
482  return newMulti;
483  }
484 
485  /*
486  * If the TransactionId is already a member of the MultiXactId with the
487  * same status, just return the existing MultiXactId.
488  */
489  for (i = 0; i < nmembers; i++)
490  {
491  if (TransactionIdEquals(members[i].xid, xid) &&
492  (members[i].status == status))
493  {
494  debug_elog4(DEBUG2, "Expand: %u is already a member of %u",
495  xid, multi);
496  pfree(members);
497  return multi;
498  }
499  }
500 
501  /*
502  * Determine which of the members of the MultiXactId are still of
503  * interest. This is any running transaction, and also any transaction
504  * that grabbed something stronger than just a lock and was committed. (An
505  * update that aborted is of no interest here; and having more than one
506  * update Xid in a multixact would cause errors elsewhere.)
507  *
508  * Removing dead members is not just an optimization: freezing of tuples
509  * whose Xmax are multis depends on this behavior.
510  *
511  * Note we have the same race condition here as above: j could be 0 at the
512  * end of the loop.
513  */
514  newMembers = (MultiXactMember *)
515  palloc(sizeof(MultiXactMember) * (nmembers + 1));
516 
517  for (i = 0, j = 0; i < nmembers; i++)
518  {
519  if (TransactionIdIsInProgress(members[i].xid) ||
520  (ISUPDATE_from_mxstatus(members[i].status) &&
521  TransactionIdDidCommit(members[i].xid)))
522  {
523  newMembers[j].xid = members[i].xid;
524  newMembers[j++].status = members[i].status;
525  }
526  }
527 
528  newMembers[j].xid = xid;
529  newMembers[j++].status = status;
530  newMulti = MultiXactIdCreateFromMembers(j, newMembers);
531 
532  pfree(members);
533  pfree(newMembers);
534 
535  debug_elog3(DEBUG2, "Expand: returning new multi %u", newMulti);
536 
537  return newMulti;
538 }
539 
540 /*
541  * MultiXactIdIsRunning
542  * Returns whether a MultiXactId is "running".
543  *
544  * We return true if at least one member of the given MultiXactId is still
545  * running. Note that a "false" result is certain not to change,
546  * because it is not legal to add members to an existing MultiXactId.
547  *
548  * Caller is expected to have verified that the multixact does not come from
549  * a pg_upgraded share-locked tuple.
550  */
551 bool
552 MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly)
553 {
554  MultiXactMember *members;
555  int nmembers;
556  int i;
557 
558  debug_elog3(DEBUG2, "IsRunning %u?", multi);
559 
560  /*
561  * "false" here means we assume our callers have checked that the given
562  * multi cannot possibly come from a pg_upgraded database.
563  */
564  nmembers = GetMultiXactIdMembers(multi, &members, false, isLockOnly);
565 
566  if (nmembers <= 0)
567  {
568  debug_elog2(DEBUG2, "IsRunning: no members");
569  return false;
570  }
571 
572  /*
573  * Checking for myself is cheap compared to looking in shared memory;
574  * return true if any live subtransaction of the current top-level
575  * transaction is a member.
576  *
577  * This is not needed for correctness, it's just a fast path.
578  */
579  for (i = 0; i < nmembers; i++)
580  {
581  if (TransactionIdIsCurrentTransactionId(members[i].xid))
582  {
583  debug_elog3(DEBUG2, "IsRunning: I (%d) am running!", i);
584  pfree(members);
585  return true;
586  }
587  }
588 
589  /*
590  * This could be made faster by having another entry point in procarray.c,
591  * walking the PGPROC array only once for all the members. But in most
592  * cases nmembers should be small enough that it doesn't much matter.
593  */
594  for (i = 0; i < nmembers; i++)
595  {
596  if (TransactionIdIsInProgress(members[i].xid))
597  {
598  debug_elog4(DEBUG2, "IsRunning: member %d (%u) is running",
599  i, members[i].xid);
600  pfree(members);
601  return true;
602  }
603  }
604 
605  pfree(members);
606 
607  debug_elog3(DEBUG2, "IsRunning: %u is not running", multi);
608 
609  return false;
610 }
611 
612 /*
613  * MultiXactIdSetOldestMember
614  * Save the oldest MultiXactId this transaction could be a member of.
615  *
616  * We set the OldestMemberMXactId for a given transaction the first time it's
617  * going to do some operation that might require a MultiXactId (tuple lock,
618  * update or delete). We need to do this even if we end up using a
619  * TransactionId instead of a MultiXactId, because there is a chance that
620  * another transaction would add our XID to a MultiXactId.
621  *
622  * The value to set is the next-to-be-assigned MultiXactId, so this is meant to
623  * be called just before doing any such possibly-MultiXactId-able operation.
624  */
625 void
627 {
629  {
630  MultiXactId nextMXact;
631 
632  /*
633  * You might think we don't need to acquire a lock here, since
634  * fetching and storing of TransactionIds is probably atomic, but in
635  * fact we do: suppose we pick up nextMXact and then lose the CPU for
636  * a long time. Someone else could advance nextMXact, and then
637  * another someone else could compute an OldestVisibleMXactId that
638  * would be after the value we are going to store when we get control
639  * back. Which would be wrong.
640  *
641  * Note that a shared lock is sufficient, because it's enough to stop
642  * someone from advancing nextMXact; and nobody else could be trying
643  * to write to our OldestMember entry, only reading (and we assume
644  * storing it is atomic.)
645  */
646  LWLockAcquire(MultiXactGenLock, LW_SHARED);
647 
648  /*
649  * We have to beware of the possibility that nextMXact is in the
650  * wrapped-around state. We don't fix the counter itself here, but we
651  * must be sure to store a valid value in our array entry.
652  */
653  nextMXact = MultiXactState->nextMXact;
654  if (nextMXact < FirstMultiXactId)
655  nextMXact = FirstMultiXactId;
656 
657  OldestMemberMXactId[MyBackendId] = nextMXact;
658 
659  LWLockRelease(MultiXactGenLock);
660 
661  debug_elog4(DEBUG2, "MultiXact: setting OldestMember[%d] = %u",
662  MyBackendId, nextMXact);
663  }
664 }
665 
666 /*
667  * MultiXactIdSetOldestVisible
668  * Save the oldest MultiXactId this transaction considers possibly live.
669  *
670  * We set the OldestVisibleMXactId for a given transaction the first time
671  * it's going to inspect any MultiXactId. Once we have set this, we are
672  * guaranteed that SLRU data for MultiXactIds >= our own OldestVisibleMXactId
673  * won't be truncated away.
674  *
675  * The value to set is the oldest of nextMXact and all the valid per-backend
676  * OldestMemberMXactId[] entries. Because of the locking we do, we can be
677  * certain that no subsequent call to MultiXactIdSetOldestMember can set
678  * an OldestMemberMXactId[] entry older than what we compute here. Therefore
679  * there is no live transaction, now or later, that can be a member of any
680  * MultiXactId older than the OldestVisibleMXactId we compute here.
681  */
682 static void
684 {
686  {
687  MultiXactId oldestMXact;
688  int i;
689 
690  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
691 
692  /*
693  * We have to beware of the possibility that nextMXact is in the
694  * wrapped-around state. We don't fix the counter itself here, but we
695  * must be sure to store a valid value in our array entry.
696  */
697  oldestMXact = MultiXactState->nextMXact;
698  if (oldestMXact < FirstMultiXactId)
699  oldestMXact = FirstMultiXactId;
700 
701  for (i = 1; i <= MaxOldestSlot; i++)
702  {
703  MultiXactId thisoldest = OldestMemberMXactId[i];
704 
705  if (MultiXactIdIsValid(thisoldest) &&
706  MultiXactIdPrecedes(thisoldest, oldestMXact))
707  oldestMXact = thisoldest;
708  }
709 
710  OldestVisibleMXactId[MyBackendId] = oldestMXact;
711 
712  LWLockRelease(MultiXactGenLock);
713 
714  debug_elog4(DEBUG2, "MultiXact: setting OldestVisible[%d] = %u",
715  MyBackendId, oldestMXact);
716  }
717 }
718 
719 /*
720  * ReadNextMultiXactId
721  * Return the next MultiXactId to be assigned, but don't allocate it
722  */
725 {
726  MultiXactId mxid;
727 
728  /* XXX we could presumably do this without a lock. */
729  LWLockAcquire(MultiXactGenLock, LW_SHARED);
730  mxid = MultiXactState->nextMXact;
731  LWLockRelease(MultiXactGenLock);
732 
733  if (mxid < FirstMultiXactId)
734  mxid = FirstMultiXactId;
735 
736  return mxid;
737 }
738 
739 /*
740  * ReadMultiXactIdRange
741  * Get the range of IDs that may still be referenced by a relation.
742  */
743 void
745 {
746  LWLockAcquire(MultiXactGenLock, LW_SHARED);
749  LWLockRelease(MultiXactGenLock);
750 
751  if (*oldest < FirstMultiXactId)
752  *oldest = FirstMultiXactId;
753  if (*next < FirstMultiXactId)
755 }
756 
757 
758 /*
759  * MultiXactIdCreateFromMembers
760  * Make a new MultiXactId from the specified set of members
761  *
762  * Make XLOG, SLRU and cache entries for a new MultiXactId, recording the
763  * given TransactionIds as members. Returns the newly created MultiXactId.
764  *
765  * NB: the passed members[] array will be sorted in-place.
766  */
769 {
770  MultiXactId multi;
771  MultiXactOffset offset;
772  xl_multixact_create xlrec;
773 
774  debug_elog3(DEBUG2, "Create: %s",
775  mxid_to_string(InvalidMultiXactId, nmembers, members));
776 
777  /*
778  * See if the same set of members already exists in our cache; if so, just
779  * re-use that MultiXactId. (Note: it might seem that looking in our
780  * cache is insufficient, and we ought to search disk to see if a
781  * duplicate definition already exists. But since we only ever create
782  * MultiXacts containing our own XID, in most cases any such MultiXacts
783  * were in fact created by us, and so will be in our cache. There are
784  * corner cases where someone else added us to a MultiXact without our
785  * knowledge, but it's not worth checking for.)
786  */
787  multi = mXactCacheGetBySet(nmembers, members);
788  if (MultiXactIdIsValid(multi))
789  {
790  debug_elog2(DEBUG2, "Create: in cache!");
791  return multi;
792  }
793 
794  /* Verify that there is a single update Xid among the given members. */
795  {
796  int i;
797  bool has_update = false;
798 
799  for (i = 0; i < nmembers; i++)
800  {
801  if (ISUPDATE_from_mxstatus(members[i].status))
802  {
803  if (has_update)
804  elog(ERROR, "new multixact has more than one updating member: %s",
805  mxid_to_string(InvalidMultiXactId, nmembers, members));
806  has_update = true;
807  }
808  }
809  }
810 
811  /*
812  * Assign the MXID and offsets range to use, and make sure there is space
813  * in the OFFSETs and MEMBERs files. NB: this routine does
814  * START_CRIT_SECTION().
815  *
816  * Note: unlike MultiXactIdCreate and MultiXactIdExpand, we do not check
817  * that we've called MultiXactIdSetOldestMember here. This is because
818  * this routine is used in some places to create new MultiXactIds of which
819  * the current backend is not a member, notably during freezing of multis
820  * in vacuum. During vacuum, in particular, it would be unacceptable to
821  * keep OldestMulti set, in case it runs for long.
822  */
823  multi = GetNewMultiXactId(nmembers, &offset);
824 
825  /* Make an XLOG entry describing the new MXID. */
826  xlrec.mid = multi;
827  xlrec.moff = offset;
828  xlrec.nmembers = nmembers;
829 
830  /*
831  * XXX Note: there's a lot of padding space in MultiXactMember. We could
832  * find a more compact representation of this Xlog record -- perhaps all
833  * the status flags in one XLogRecData, then all the xids in another one?
834  * Not clear that it's worth the trouble though.
835  */
836  XLogBeginInsert();
837  XLogRegisterData((char *) (&xlrec), SizeOfMultiXactCreate);
838  XLogRegisterData((char *) members, nmembers * sizeof(MultiXactMember));
839 
840  (void) XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_CREATE_ID);
841 
842  /* Now enter the information into the OFFSETs and MEMBERs logs */
843  RecordNewMultiXact(multi, offset, nmembers, members);
844 
845  /* Done with critical section */
847 
848  /* Store the new MultiXactId in the local cache, too */
849  mXactCachePut(multi, nmembers, members);
850 
851  debug_elog2(DEBUG2, "Create: all done");
852 
853  return multi;
854 }
855 
856 /*
857  * RecordNewMultiXact
858  * Write info about a new multixact into the offsets and members files
859  *
860  * This is broken out of MultiXactIdCreateFromMembers so that xlog replay can
861  * use it.
862  */
863 static void
865  int nmembers, MultiXactMember *members)
866 {
867  int64 pageno;
868  int64 prev_pageno;
869  int entryno;
870  int slotno;
871  MultiXactOffset *offptr;
872  int i;
873 
874  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
875 
876  pageno = MultiXactIdToOffsetPage(multi);
877  entryno = MultiXactIdToOffsetEntry(multi);
878 
879  /*
880  * Note: we pass the MultiXactId to SimpleLruReadPage as the "transaction"
881  * to complain about if there's any I/O error. This is kinda bogus, but
882  * since the errors will always give the full pathname, it should be clear
883  * enough that a MultiXactId is really involved. Perhaps someday we'll
884  * take the trouble to generalize the slru.c error reporting code.
885  */
886  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
887  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
888  offptr += entryno;
889 
890  *offptr = offset;
891 
892  MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
893 
894  /* Exchange our lock */
895  LWLockRelease(MultiXactOffsetSLRULock);
896 
897  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
898 
899  prev_pageno = -1;
900 
901  for (i = 0; i < nmembers; i++, offset++)
902  {
903  TransactionId *memberptr;
904  uint32 *flagsptr;
905  uint32 flagsval;
906  int bshift;
907  int flagsoff;
908  int memberoff;
909 
910  Assert(members[i].status <= MultiXactStatusUpdate);
911 
912  pageno = MXOffsetToMemberPage(offset);
913  memberoff = MXOffsetToMemberOffset(offset);
914  flagsoff = MXOffsetToFlagsOffset(offset);
915  bshift = MXOffsetToFlagsBitShift(offset);
916 
917  if (pageno != prev_pageno)
918  {
919  slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
920  prev_pageno = pageno;
921  }
922 
923  memberptr = (TransactionId *)
924  (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
925 
926  *memberptr = members[i].xid;
927 
928  flagsptr = (uint32 *)
929  (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
930 
931  flagsval = *flagsptr;
932  flagsval &= ~(((1 << MXACT_MEMBER_BITS_PER_XACT) - 1) << bshift);
933  flagsval |= (members[i].status << bshift);
934  *flagsptr = flagsval;
935 
936  MultiXactMemberCtl->shared->page_dirty[slotno] = true;
937  }
938 
939  LWLockRelease(MultiXactMemberSLRULock);
940 }
941 
942 /*
943  * GetNewMultiXactId
944  * Get the next MultiXactId.
945  *
946  * Also, reserve the needed amount of space in the "members" area. The
947  * starting offset of the reserved space is returned in *offset.
948  *
949  * This may generate XLOG records for expansion of the offsets and/or members
950  * files. Unfortunately, we have to do that while holding MultiXactGenLock
951  * to avoid race conditions --- the XLOG record for zeroing a page must appear
952  * before any backend can possibly try to store data in that page!
953  *
954  * We start a critical section before advancing the shared counters. The
955  * caller must end the critical section after writing SLRU data.
956  */
957 static MultiXactId
958 GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
959 {
960  MultiXactId result;
961  MultiXactOffset nextOffset;
962 
963  debug_elog3(DEBUG2, "GetNew: for %d xids", nmembers);
964 
965  /* safety check, we should never get this far in a HS standby */
966  if (RecoveryInProgress())
967  elog(ERROR, "cannot assign MultiXactIds during recovery");
968 
969  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
970 
971  /* Handle wraparound of the nextMXact counter */
974 
975  /* Assign the MXID */
976  result = MultiXactState->nextMXact;
977 
978  /*----------
979  * Check to see if it's safe to assign another MultiXactId. This protects
980  * against catastrophic data loss due to multixact wraparound. The basic
981  * rules are:
982  *
983  * If we're past multiVacLimit or the safe threshold for member storage
984  * space, or we don't know what the safe threshold for member storage is,
985  * start trying to force autovacuum cycles.
986  * If we're past multiWarnLimit, start issuing warnings.
987  * If we're past multiStopLimit, refuse to create new MultiXactIds.
988  *
989  * Note these are pretty much the same protections in GetNewTransactionId.
990  *----------
991  */
993  {
994  /*
995  * For safety's sake, we release MultiXactGenLock while sending
996  * signals, warnings, etc. This is not so much because we care about
997  * preserving concurrency in this situation, as to avoid any
998  * possibility of deadlock while doing get_database_name(). First,
999  * copy all the shared values we'll need in this path.
1000  */
1001  MultiXactId multiWarnLimit = MultiXactState->multiWarnLimit;
1002  MultiXactId multiStopLimit = MultiXactState->multiStopLimit;
1003  MultiXactId multiWrapLimit = MultiXactState->multiWrapLimit;
1004  Oid oldest_datoid = MultiXactState->oldestMultiXactDB;
1005 
1006  LWLockRelease(MultiXactGenLock);
1007 
1008  if (IsUnderPostmaster &&
1009  !MultiXactIdPrecedes(result, multiStopLimit))
1010  {
1011  char *oldest_datname = get_database_name(oldest_datoid);
1012 
1013  /*
1014  * Immediately kick autovacuum into action as we're already in
1015  * ERROR territory.
1016  */
1018 
1019  /* complain even if that DB has disappeared */
1020  if (oldest_datname)
1021  ereport(ERROR,
1022  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1023  errmsg("database is not accepting commands that assign new MultiXactIds to avoid wraparound data loss in database \"%s\"",
1024  oldest_datname),
1025  errhint("Execute a database-wide VACUUM in that database.\n"
1026  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1027  else
1028  ereport(ERROR,
1029  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1030  errmsg("database is not accepting commands that assign new MultiXactIds to avoid wraparound data loss in database with OID %u",
1031  oldest_datoid),
1032  errhint("Execute a database-wide VACUUM in that database.\n"
1033  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1034  }
1035 
1036  /*
1037  * To avoid swamping the postmaster with signals, we issue the autovac
1038  * request only once per 64K multis generated. This still gives
1039  * plenty of chances before we get into real trouble.
1040  */
1041  if (IsUnderPostmaster && (result % 65536) == 0)
1043 
1044  if (!MultiXactIdPrecedes(result, multiWarnLimit))
1045  {
1046  char *oldest_datname = get_database_name(oldest_datoid);
1047 
1048  /* complain even if that DB has disappeared */
1049  if (oldest_datname)
1050  ereport(WARNING,
1051  (errmsg_plural("database \"%s\" must be vacuumed before %u more MultiXactId is used",
1052  "database \"%s\" must be vacuumed before %u more MultiXactIds are used",
1053  multiWrapLimit - result,
1054  oldest_datname,
1055  multiWrapLimit - result),
1056  errhint("Execute a database-wide VACUUM in that database.\n"
1057  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1058  else
1059  ereport(WARNING,
1060  (errmsg_plural("database with OID %u must be vacuumed before %u more MultiXactId is used",
1061  "database with OID %u must be vacuumed before %u more MultiXactIds are used",
1062  multiWrapLimit - result,
1063  oldest_datoid,
1064  multiWrapLimit - result),
1065  errhint("Execute a database-wide VACUUM in that database.\n"
1066  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
1067  }
1068 
1069  /* Re-acquire lock and start over */
1070  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1071  result = MultiXactState->nextMXact;
1072  if (result < FirstMultiXactId)
1073  result = FirstMultiXactId;
1074  }
1075 
1076  /* Make sure there is room for the MXID in the file. */
1077  ExtendMultiXactOffset(result);
1078 
1079  /*
1080  * Reserve the members space, similarly to above. Also, be careful not to
1081  * return zero as the starting offset for any multixact. See
1082  * GetMultiXactIdMembers() for motivation.
1083  */
1084  nextOffset = MultiXactState->nextOffset;
1085  if (nextOffset == 0)
1086  {
1087  *offset = 1;
1088  nmembers++; /* allocate member slot 0 too */
1089  }
1090  else
1091  *offset = nextOffset;
1092 
1093  /*----------
1094  * Protect against overrun of the members space as well, with the
1095  * following rules:
1096  *
1097  * If we're past offsetStopLimit, refuse to generate more multis.
1098  * If we're close to offsetStopLimit, emit a warning.
1099  *
1100  * Arbitrarily, we start emitting warnings when we're 20 segments or less
1101  * from offsetStopLimit.
1102  *
1103  * Note we haven't updated the shared state yet, so if we fail at this
1104  * point, the multixact ID we grabbed can still be used by the next guy.
1105  *
1106  * Note that there is no point in forcing autovacuum runs here: the
1107  * multixact freeze settings would have to be reduced for that to have any
1108  * effect.
1109  *----------
1110  */
1111 #define OFFSET_WARN_SEGMENTS 20
1114  nmembers))
1115  {
1116  /* see comment in the corresponding offsets wraparound case */
1118 
1119  ereport(ERROR,
1120  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1121  errmsg("multixact \"members\" limit exceeded"),
1122  errdetail_plural("This command would create a multixact with %u members, but the remaining space is only enough for %u member.",
1123  "This command would create a multixact with %u members, but the remaining space is only enough for %u members.",
1124  MultiXactState->offsetStopLimit - nextOffset - 1,
1125  nmembers,
1126  MultiXactState->offsetStopLimit - nextOffset - 1),
1127  errhint("Execute a database-wide VACUUM in database with OID %u with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.",
1129  }
1130 
1131  /*
1132  * Check whether we should kick autovacuum into action, to prevent members
1133  * wraparound. NB we use a much larger window to trigger autovacuum than
1134  * just the warning limit. The warning is just a measure of last resort -
1135  * this is in line with GetNewTransactionId's behaviour.
1136  */
1140  {
1141  /*
1142  * To avoid swamping the postmaster with signals, we issue the autovac
1143  * request only when crossing a segment boundary. With default
1144  * compilation settings that's roughly after 50k members. This still
1145  * gives plenty of chances before we get into real trouble.
1146  */
1147  if ((MXOffsetToMemberPage(nextOffset) / SLRU_PAGES_PER_SEGMENT) !=
1148  (MXOffsetToMemberPage(nextOffset + nmembers) / SLRU_PAGES_PER_SEGMENT))
1150  }
1151 
1154  nextOffset,
1156  ereport(WARNING,
1157  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1158  errmsg_plural("database with OID %u must be vacuumed before %d more multixact member is used",
1159  "database with OID %u must be vacuumed before %d more multixact members are used",
1160  MultiXactState->offsetStopLimit - nextOffset + nmembers,
1162  MultiXactState->offsetStopLimit - nextOffset + nmembers),
1163  errhint("Execute a database-wide VACUUM in that database with reduced vacuum_multixact_freeze_min_age and vacuum_multixact_freeze_table_age settings.")));
1164 
1165  ExtendMultiXactMember(nextOffset, nmembers);
1166 
1167  /*
1168  * Critical section from here until caller has written the data into the
1169  * just-reserved SLRU space; we don't want to error out with a partly
1170  * written MultiXact structure. (In particular, failing to write our
1171  * start offset after advancing nextMXact would effectively corrupt the
1172  * previous MultiXact.)
1173  */
1175 
1176  /*
1177  * Advance counters. As in GetNewTransactionId(), this must not happen
1178  * until after file extension has succeeded!
1179  *
1180  * We don't care about MultiXactId wraparound here; it will be handled by
1181  * the next iteration. But note that nextMXact may be InvalidMultiXactId
1182  * or the first value on a segment-beginning page after this routine
1183  * exits, so anyone else looking at the variable must be prepared to deal
1184  * with either case. Similarly, nextOffset may be zero, but we won't use
1185  * that as the actual start offset of the next multixact.
1186  */
1188 
1189  MultiXactState->nextOffset += nmembers;
1190 
1191  LWLockRelease(MultiXactGenLock);
1192 
1193  debug_elog4(DEBUG2, "GetNew: returning %u offset %u", result, *offset);
1194  return result;
1195 }
1196 
1197 /*
1198  * GetMultiXactIdMembers
1199  * Return the set of MultiXactMembers that make up a MultiXactId
1200  *
1201  * Return value is the number of members found, or -1 if there are none,
1202  * and *members is set to a newly palloc'ed array of members. It's the
1203  * caller's responsibility to free it when done with it.
1204  *
1205  * from_pgupgrade must be passed as true if and only if only the multixact
1206  * corresponds to a value from a tuple that was locked in a 9.2-or-older
1207  * installation and later pg_upgrade'd (that is, the infomask is
1208  * HEAP_LOCKED_UPGRADED). In this case, we know for certain that no members
1209  * can still be running, so we return -1 just like for an empty multixact
1210  * without any further checking. It would be wrong to try to resolve such a
1211  * multixact: either the multixact is within the current valid multixact
1212  * range, in which case the returned result would be bogus, or outside that
1213  * range, in which case an error would be raised.
1214  *
1215  * In all other cases, the passed multixact must be within the known valid
1216  * range, that is, greater to or equal than oldestMultiXactId, and less than
1217  * nextMXact. Otherwise, an error is raised.
1218  *
1219  * isLockOnly must be set to true if caller is certain that the given multi
1220  * is used only to lock tuples; can be false without loss of correctness,
1221  * but passing a true means we can return quickly without checking for
1222  * old updates.
1223  */
1224 int
1226  bool from_pgupgrade, bool isLockOnly)
1227 {
1228  int64 pageno;
1229  int64 prev_pageno;
1230  int entryno;
1231  int slotno;
1232  MultiXactOffset *offptr;
1233  MultiXactOffset offset;
1234  int length;
1235  int truelength;
1236  int i;
1237  MultiXactId oldestMXact;
1238  MultiXactId nextMXact;
1239  MultiXactId tmpMXact;
1240  MultiXactOffset nextOffset;
1241  MultiXactMember *ptr;
1242 
1243  debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
1244 
1245  if (!MultiXactIdIsValid(multi) || from_pgupgrade)
1246  {
1247  *members = NULL;
1248  return -1;
1249  }
1250 
1251  /* See if the MultiXactId is in the local cache */
1252  length = mXactCacheGetById(multi, members);
1253  if (length >= 0)
1254  {
1255  debug_elog3(DEBUG2, "GetMembers: found %s in the cache",
1256  mxid_to_string(multi, length, *members));
1257  return length;
1258  }
1259 
1260  /* Set our OldestVisibleMXactId[] entry if we didn't already */
1262 
1263  /*
1264  * If we know the multi is used only for locking and not for updates, then
1265  * we can skip checking if the value is older than our oldest visible
1266  * multi. It cannot possibly still be running.
1267  */
1268  if (isLockOnly &&
1270  {
1271  debug_elog2(DEBUG2, "GetMembers: a locker-only multi is too old");
1272  *members = NULL;
1273  return -1;
1274  }
1275 
1276  /*
1277  * We check known limits on MultiXact before resorting to the SLRU area.
1278  *
1279  * An ID older than MultiXactState->oldestMultiXactId cannot possibly be
1280  * useful; it has already been removed, or will be removed shortly, by
1281  * truncation. If one is passed, an error is raised.
1282  *
1283  * Also, an ID >= nextMXact shouldn't ever be seen here; if it is seen, it
1284  * implies undetected ID wraparound has occurred. This raises a hard
1285  * error.
1286  *
1287  * Shared lock is enough here since we aren't modifying any global state.
1288  * Acquire it just long enough to grab the current counter values. We may
1289  * need both nextMXact and nextOffset; see below.
1290  */
1291  LWLockAcquire(MultiXactGenLock, LW_SHARED);
1292 
1293  oldestMXact = MultiXactState->oldestMultiXactId;
1294  nextMXact = MultiXactState->nextMXact;
1295  nextOffset = MultiXactState->nextOffset;
1296 
1297  LWLockRelease(MultiXactGenLock);
1298 
1299  if (MultiXactIdPrecedes(multi, oldestMXact))
1300  ereport(ERROR,
1301  (errcode(ERRCODE_INTERNAL_ERROR),
1302  errmsg("MultiXactId %u does no longer exist -- apparent wraparound",
1303  multi)));
1304 
1305  if (!MultiXactIdPrecedes(multi, nextMXact))
1306  ereport(ERROR,
1307  (errcode(ERRCODE_INTERNAL_ERROR),
1308  errmsg("MultiXactId %u has not been created yet -- apparent wraparound",
1309  multi)));
1310 
1311  /*
1312  * Find out the offset at which we need to start reading MultiXactMembers
1313  * and the number of members in the multixact. We determine the latter as
1314  * the difference between this multixact's starting offset and the next
1315  * one's. However, there are some corner cases to worry about:
1316  *
1317  * 1. This multixact may be the latest one created, in which case there is
1318  * no next one to look at. In this case the nextOffset value we just
1319  * saved is the correct endpoint.
1320  *
1321  * 2. The next multixact may still be in process of being filled in: that
1322  * is, another process may have done GetNewMultiXactId but not yet written
1323  * the offset entry for that ID. In that scenario, it is guaranteed that
1324  * the offset entry for that multixact exists (because GetNewMultiXactId
1325  * won't release MultiXactGenLock until it does) but contains zero
1326  * (because we are careful to pre-zero offset pages). Because
1327  * GetNewMultiXactId will never return zero as the starting offset for a
1328  * multixact, when we read zero as the next multixact's offset, we know we
1329  * have this case. We sleep for a bit and try again.
1330  *
1331  * 3. Because GetNewMultiXactId increments offset zero to offset one to
1332  * handle case #2, there is an ambiguity near the point of offset
1333  * wraparound. If we see next multixact's offset is one, is that our
1334  * multixact's actual endpoint, or did it end at zero with a subsequent
1335  * increment? We handle this using the knowledge that if the zero'th
1336  * member slot wasn't filled, it'll contain zero, and zero isn't a valid
1337  * transaction ID so it can't be a multixact member. Therefore, if we
1338  * read a zero from the members array, just ignore it.
1339  *
1340  * This is all pretty messy, but the mess occurs only in infrequent corner
1341  * cases, so it seems better than holding the MultiXactGenLock for a long
1342  * time on every multixact creation.
1343  */
1344 retry:
1345  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
1346 
1347  pageno = MultiXactIdToOffsetPage(multi);
1348  entryno = MultiXactIdToOffsetEntry(multi);
1349 
1350  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
1351  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1352  offptr += entryno;
1353  offset = *offptr;
1354 
1355  Assert(offset != 0);
1356 
1357  /*
1358  * Use the same increment rule as GetNewMultiXactId(), that is, don't
1359  * handle wraparound explicitly until needed.
1360  */
1361  tmpMXact = multi + 1;
1362 
1363  if (nextMXact == tmpMXact)
1364  {
1365  /* Corner case 1: there is no next multixact */
1366  length = nextOffset - offset;
1367  }
1368  else
1369  {
1370  MultiXactOffset nextMXOffset;
1371 
1372  /* handle wraparound if needed */
1373  if (tmpMXact < FirstMultiXactId)
1374  tmpMXact = FirstMultiXactId;
1375 
1376  prev_pageno = pageno;
1377 
1378  pageno = MultiXactIdToOffsetPage(tmpMXact);
1379  entryno = MultiXactIdToOffsetEntry(tmpMXact);
1380 
1381  if (pageno != prev_pageno)
1382  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, tmpMXact);
1383 
1384  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1385  offptr += entryno;
1386  nextMXOffset = *offptr;
1387 
1388  if (nextMXOffset == 0)
1389  {
1390  /* Corner case 2: next multixact is still being filled in */
1391  LWLockRelease(MultiXactOffsetSLRULock);
1393  pg_usleep(1000L);
1394  goto retry;
1395  }
1396 
1397  length = nextMXOffset - offset;
1398  }
1399 
1400  LWLockRelease(MultiXactOffsetSLRULock);
1401 
1402  ptr = (MultiXactMember *) palloc(length * sizeof(MultiXactMember));
1403 
1404  /* Now get the members themselves. */
1405  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
1406 
1407  truelength = 0;
1408  prev_pageno = -1;
1409  for (i = 0; i < length; i++, offset++)
1410  {
1411  TransactionId *xactptr;
1412  uint32 *flagsptr;
1413  int flagsoff;
1414  int bshift;
1415  int memberoff;
1416 
1417  pageno = MXOffsetToMemberPage(offset);
1418  memberoff = MXOffsetToMemberOffset(offset);
1419 
1420  if (pageno != prev_pageno)
1421  {
1422  slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
1423  prev_pageno = pageno;
1424  }
1425 
1426  xactptr = (TransactionId *)
1427  (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
1428 
1429  if (!TransactionIdIsValid(*xactptr))
1430  {
1431  /* Corner case 3: we must be looking at unused slot zero */
1432  Assert(offset == 0);
1433  continue;
1434  }
1435 
1436  flagsoff = MXOffsetToFlagsOffset(offset);
1437  bshift = MXOffsetToFlagsBitShift(offset);
1438  flagsptr = (uint32 *) (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
1439 
1440  ptr[truelength].xid = *xactptr;
1441  ptr[truelength].status = (*flagsptr >> bshift) & MXACT_MEMBER_XACT_BITMASK;
1442  truelength++;
1443  }
1444 
1445  LWLockRelease(MultiXactMemberSLRULock);
1446 
1447  /* A multixid with zero members should not happen */
1448  Assert(truelength > 0);
1449 
1450  /*
1451  * Copy the result into the local cache.
1452  */
1453  mXactCachePut(multi, truelength, ptr);
1454 
1455  debug_elog3(DEBUG2, "GetMembers: no cache for %s",
1456  mxid_to_string(multi, truelength, ptr));
1457  *members = ptr;
1458  return truelength;
1459 }
1460 
1461 /*
1462  * mxactMemberComparator
1463  * qsort comparison function for MultiXactMember
1464  *
1465  * We can't use wraparound comparison for XIDs because that does not respect
1466  * the triangle inequality! Any old sort order will do.
1467  */
1468 static int
1469 mxactMemberComparator(const void *arg1, const void *arg2)
1470 {
1471  MultiXactMember member1 = *(const MultiXactMember *) arg1;
1472  MultiXactMember member2 = *(const MultiXactMember *) arg2;
1473 
1474  if (member1.xid > member2.xid)
1475  return 1;
1476  if (member1.xid < member2.xid)
1477  return -1;
1478  if (member1.status > member2.status)
1479  return 1;
1480  if (member1.status < member2.status)
1481  return -1;
1482  return 0;
1483 }
1484 
1485 /*
1486  * mXactCacheGetBySet
1487  * returns a MultiXactId from the cache based on the set of
1488  * TransactionIds that compose it, or InvalidMultiXactId if
1489  * none matches.
1490  *
1491  * This is helpful, for example, if two transactions want to lock a huge
1492  * table. By using the cache, the second will use the same MultiXactId
1493  * for the majority of tuples, thus keeping MultiXactId usage low (saving
1494  * both I/O and wraparound issues).
1495  *
1496  * NB: the passed members array will be sorted in-place.
1497  */
1498 static MultiXactId
1499 mXactCacheGetBySet(int nmembers, MultiXactMember *members)
1500 {
1501  dlist_iter iter;
1502 
1503  debug_elog3(DEBUG2, "CacheGet: looking for %s",
1504  mxid_to_string(InvalidMultiXactId, nmembers, members));
1505 
1506  /* sort the array so comparison is easy */
1507  qsort(members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
1508 
1509  dclist_foreach(iter, &MXactCache)
1510  {
1512  iter.cur);
1513 
1514  if (entry->nmembers != nmembers)
1515  continue;
1516 
1517  /*
1518  * We assume the cache entries are sorted, and that the unused bits in
1519  * "status" are zeroed.
1520  */
1521  if (memcmp(members, entry->members, nmembers * sizeof(MultiXactMember)) == 0)
1522  {
1523  debug_elog3(DEBUG2, "CacheGet: found %u", entry->multi);
1525  return entry->multi;
1526  }
1527  }
1528 
1529  debug_elog2(DEBUG2, "CacheGet: not found :-(");
1530  return InvalidMultiXactId;
1531 }
1532 
1533 /*
1534  * mXactCacheGetById
1535  * returns the composing MultiXactMember set from the cache for a
1536  * given MultiXactId, if present.
1537  *
1538  * If successful, *xids is set to the address of a palloc'd copy of the
1539  * MultiXactMember set. Return value is number of members, or -1 on failure.
1540  */
1541 static int
1543 {
1544  dlist_iter iter;
1545 
1546  debug_elog3(DEBUG2, "CacheGet: looking for %u", multi);
1547 
1548  dclist_foreach(iter, &MXactCache)
1549  {
1551  iter.cur);
1552 
1553  if (entry->multi == multi)
1554  {
1555  MultiXactMember *ptr;
1556  Size size;
1557 
1558  size = sizeof(MultiXactMember) * entry->nmembers;
1559  ptr = (MultiXactMember *) palloc(size);
1560 
1561  memcpy(ptr, entry->members, size);
1562 
1563  debug_elog3(DEBUG2, "CacheGet: found %s",
1564  mxid_to_string(multi,
1565  entry->nmembers,
1566  entry->members));
1567 
1568  /*
1569  * Note we modify the list while not using a modifiable iterator.
1570  * This is acceptable only because we exit the iteration
1571  * immediately afterwards.
1572  */
1574 
1575  *members = ptr;
1576  return entry->nmembers;
1577  }
1578  }
1579 
1580  debug_elog2(DEBUG2, "CacheGet: not found");
1581  return -1;
1582 }
1583 
1584 /*
1585  * mXactCachePut
1586  * Add a new MultiXactId and its composing set into the local cache.
1587  */
1588 static void
1589 mXactCachePut(MultiXactId multi, int nmembers, MultiXactMember *members)
1590 {
1591  mXactCacheEnt *entry;
1592 
1593  debug_elog3(DEBUG2, "CachePut: storing %s",
1594  mxid_to_string(multi, nmembers, members));
1595 
1596  if (MXactContext == NULL)
1597  {
1598  /* The cache only lives as long as the current transaction */
1599  debug_elog2(DEBUG2, "CachePut: initializing memory context");
1601  "MultiXact cache context",
1603  }
1604 
1605  entry = (mXactCacheEnt *)
1607  offsetof(mXactCacheEnt, members) +
1608  nmembers * sizeof(MultiXactMember));
1609 
1610  entry->multi = multi;
1611  entry->nmembers = nmembers;
1612  memcpy(entry->members, members, nmembers * sizeof(MultiXactMember));
1613 
1614  /* mXactCacheGetBySet assumes the entries are sorted, so sort them */
1615  qsort(entry->members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
1616 
1617  dclist_push_head(&MXactCache, &entry->node);
1619  {
1620  dlist_node *node;
1621 
1622  node = dclist_tail_node(&MXactCache);
1624 
1625  entry = dclist_container(mXactCacheEnt, node, node);
1626  debug_elog3(DEBUG2, "CachePut: pruning cached multi %u",
1627  entry->multi);
1628 
1629  pfree(entry);
1630  }
1631 }
1632 
1633 static char *
1635 {
1636  switch (status)
1637  {
1639  return "keysh";
1641  return "sh";
1643  return "fornokeyupd";
1645  return "forupd";
1647  return "nokeyupd";
1648  case MultiXactStatusUpdate:
1649  return "upd";
1650  default:
1651  elog(ERROR, "unrecognized multixact status %d", status);
1652  return "";
1653  }
1654 }
1655 
1656 char *
1657 mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
1658 {
1659  static char *str = NULL;
1661  int i;
1662 
1663  if (str != NULL)
1664  pfree(str);
1665 
1666  initStringInfo(&buf);
1667 
1668  appendStringInfo(&buf, "%u %d[%u (%s)", multi, nmembers, members[0].xid,
1669  mxstatus_to_string(members[0].status));
1670 
1671  for (i = 1; i < nmembers; i++)
1672  appendStringInfo(&buf, ", %u (%s)", members[i].xid,
1673  mxstatus_to_string(members[i].status));
1674 
1675  appendStringInfoChar(&buf, ']');
1677  pfree(buf.data);
1678  return str;
1679 }
1680 
1681 /*
1682  * AtEOXact_MultiXact
1683  * Handle transaction end for MultiXact
1684  *
1685  * This is called at top transaction commit or abort (we don't care which).
1686  */
1687 void
1689 {
1690  /*
1691  * Reset our OldestMemberMXactId and OldestVisibleMXactId values, both of
1692  * which should only be valid while within a transaction.
1693  *
1694  * We assume that storing a MultiXactId is atomic and so we need not take
1695  * MultiXactGenLock to do this.
1696  */
1699 
1700  /*
1701  * Discard the local MultiXactId cache. Since MXactContext was created as
1702  * a child of TopTransactionContext, we needn't delete it explicitly.
1703  */
1704  MXactContext = NULL;
1706 }
1707 
1708 /*
1709  * AtPrepare_MultiXact
1710  * Save multixact state at 2PC transaction prepare
1711  *
1712  * In this phase, we only store our OldestMemberMXactId value in the two-phase
1713  * state file.
1714  */
1715 void
1717 {
1718  MultiXactId myOldestMember = OldestMemberMXactId[MyBackendId];
1719 
1720  if (MultiXactIdIsValid(myOldestMember))
1722  &myOldestMember, sizeof(MultiXactId));
1723 }
1724 
1725 /*
1726  * PostPrepare_MultiXact
1727  * Clean up after successful PREPARE TRANSACTION
1728  */
1729 void
1731 {
1732  MultiXactId myOldestMember;
1733 
1734  /*
1735  * Transfer our OldestMemberMXactId value to the slot reserved for the
1736  * prepared transaction.
1737  */
1738  myOldestMember = OldestMemberMXactId[MyBackendId];
1739  if (MultiXactIdIsValid(myOldestMember))
1740  {
1741  BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid, false);
1742 
1743  /*
1744  * Even though storing MultiXactId is atomic, acquire lock to make
1745  * sure others see both changes, not just the reset of the slot of the
1746  * current backend. Using a volatile pointer might suffice, but this
1747  * isn't a hot spot.
1748  */
1749  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1750 
1751  OldestMemberMXactId[dummyBackendId] = myOldestMember;
1753 
1754  LWLockRelease(MultiXactGenLock);
1755  }
1756 
1757  /*
1758  * We don't need to transfer OldestVisibleMXactId value, because the
1759  * transaction is not going to be looking at any more multixacts once it's
1760  * prepared.
1761  *
1762  * We assume that storing a MultiXactId is atomic and so we need not take
1763  * MultiXactGenLock to do this.
1764  */
1766 
1767  /*
1768  * Discard the local MultiXactId cache like in AtEOXact_MultiXact.
1769  */
1770  MXactContext = NULL;
1772 }
1773 
1774 /*
1775  * multixact_twophase_recover
1776  * Recover the state of a prepared transaction at startup
1777  */
1778 void
1780  void *recdata, uint32 len)
1781 {
1782  BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid, false);
1783  MultiXactId oldestMember;
1784 
1785  /*
1786  * Get the oldest member XID from the state file record, and set it in the
1787  * OldestMemberMXactId slot reserved for this prepared transaction.
1788  */
1789  Assert(len == sizeof(MultiXactId));
1790  oldestMember = *((MultiXactId *) recdata);
1791 
1792  OldestMemberMXactId[dummyBackendId] = oldestMember;
1793 }
1794 
1795 /*
1796  * multixact_twophase_postcommit
1797  * Similar to AtEOXact_MultiXact but for COMMIT PREPARED
1798  */
1799 void
1801  void *recdata, uint32 len)
1802 {
1803  BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid, true);
1804 
1805  Assert(len == sizeof(MultiXactId));
1806 
1807  OldestMemberMXactId[dummyBackendId] = InvalidMultiXactId;
1808 }
1809 
1810 /*
1811  * multixact_twophase_postabort
1812  * This is actually just the same as the COMMIT case.
1813  */
1814 void
1816  void *recdata, uint32 len)
1817 {
1818  multixact_twophase_postcommit(xid, info, recdata, len);
1819 }
1820 
1821 /*
1822  * Initialization of shared memory for MultiXact. We use two SLRU areas,
1823  * thus double memory. Also, reserve space for the shared MultiXactState
1824  * struct and the per-backend MultiXactId arrays (two of those, too).
1825  */
1826 Size
1828 {
1829  Size size;
1830 
1831  /* We need 2*MaxOldestSlot + 1 perBackendXactIds[] entries */
1832 #define SHARED_MULTIXACT_STATE_SIZE \
1833  add_size(offsetof(MultiXactStateData, perBackendXactIds) + sizeof(MultiXactId), \
1834  mul_size(sizeof(MultiXactId) * 2, MaxOldestSlot))
1835 
1839 
1840  return size;
1841 }
1842 
1843 void
1845 {
1846  bool found;
1847 
1848  debug_elog2(DEBUG2, "Shared Memory Init for MultiXact");
1849 
1852 
1854  "MultiXactOffset", NUM_MULTIXACTOFFSET_BUFFERS, 0,
1855  MultiXactOffsetSLRULock, "pg_multixact/offsets",
1858  false);
1861  "MultiXactMember", NUM_MULTIXACTMEMBER_BUFFERS, 0,
1862  MultiXactMemberSLRULock, "pg_multixact/members",
1865  false);
1866  /* doesn't call SimpleLruTruncate() or meet criteria for unit tests */
1867 
1868  /* Initialize our shared state struct */
1869  MultiXactState = ShmemInitStruct("Shared MultiXact State",
1871  &found);
1872  if (!IsUnderPostmaster)
1873  {
1874  Assert(!found);
1875 
1876  /* Make sure we zero out the per-backend state */
1878  }
1879  else
1880  Assert(found);
1881 
1882  /*
1883  * Set up array pointers. Note that perBackendXactIds[0] is wasted space
1884  * since we only use indexes 1..MaxOldestSlot in each array.
1885  */
1888 }
1889 
1890 /*
1891  * This func must be called ONCE on system install. It creates the initial
1892  * MultiXact segments. (The MultiXacts directories are assumed to have been
1893  * created by initdb, and MultiXactShmemInit must have been called already.)
1894  */
1895 void
1897 {
1898  int slotno;
1899 
1900  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
1901 
1902  /* Create and zero the first page of the offsets log */
1903  slotno = ZeroMultiXactOffsetPage(0, false);
1904 
1905  /* Make sure it's written out */
1907  Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
1908 
1909  LWLockRelease(MultiXactOffsetSLRULock);
1910 
1911  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
1912 
1913  /* Create and zero the first page of the members log */
1914  slotno = ZeroMultiXactMemberPage(0, false);
1915 
1916  /* Make sure it's written out */
1918  Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
1919 
1920  LWLockRelease(MultiXactMemberSLRULock);
1921 }
1922 
1923 /*
1924  * Initialize (or reinitialize) a page of MultiXactOffset to zeroes.
1925  * If writeXlog is true, also emit an XLOG record saying we did this.
1926  *
1927  * The page is not actually written, just set up in shared memory.
1928  * The slot number of the new page is returned.
1929  *
1930  * Control lock must be held at entry, and will be held at exit.
1931  */
1932 static int
1933 ZeroMultiXactOffsetPage(int64 pageno, bool writeXlog)
1934 {
1935  int slotno;
1936 
1937  slotno = SimpleLruZeroPage(MultiXactOffsetCtl, pageno);
1938 
1939  if (writeXlog)
1941 
1942  return slotno;
1943 }
1944 
1945 /*
1946  * Ditto, for MultiXactMember
1947  */
1948 static int
1949 ZeroMultiXactMemberPage(int64 pageno, bool writeXlog)
1950 {
1951  int slotno;
1952 
1953  slotno = SimpleLruZeroPage(MultiXactMemberCtl, pageno);
1954 
1955  if (writeXlog)
1957 
1958  return slotno;
1959 }
1960 
1961 /*
1962  * MaybeExtendOffsetSlru
1963  * Extend the offsets SLRU area, if necessary
1964  *
1965  * After a binary upgrade from <= 9.2, the pg_multixact/offsets SLRU area might
1966  * contain files that are shorter than necessary; this would occur if the old
1967  * installation had used multixacts beyond the first page (files cannot be
1968  * copied, because the on-disk representation is different). pg_upgrade would
1969  * update pg_control to set the next offset value to be at that position, so
1970  * that tuples marked as locked by such MultiXacts would be seen as visible
1971  * without having to consult multixact. However, trying to create and use a
1972  * new MultiXactId would result in an error because the page on which the new
1973  * value would reside does not exist. This routine is in charge of creating
1974  * such pages.
1975  */
1976 static void
1978 {
1979  int64 pageno;
1980 
1982 
1983  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
1984 
1986  {
1987  int slotno;
1988 
1989  /*
1990  * Fortunately for us, SimpleLruWritePage is already prepared to deal
1991  * with creating a new segment file even if the page we're writing is
1992  * not the first in it, so this is enough.
1993  */
1994  slotno = ZeroMultiXactOffsetPage(pageno, false);
1996  }
1997 
1998  LWLockRelease(MultiXactOffsetSLRULock);
1999 }
2000 
2001 /*
2002  * This must be called ONCE during postmaster or standalone-backend startup.
2003  *
2004  * StartupXLOG has already established nextMXact/nextOffset by calling
2005  * MultiXactSetNextMXact and/or MultiXactAdvanceNextMXact, and the oldestMulti
2006  * info from pg_control and/or MultiXactAdvanceOldest, but we haven't yet
2007  * replayed WAL.
2008  */
2009 void
2011 {
2014  int64 pageno;
2015 
2016  /*
2017  * Initialize offset's idea of the latest page number.
2018  */
2019  pageno = MultiXactIdToOffsetPage(multi);
2020  pg_atomic_write_u64(&MultiXactOffsetCtl->shared->latest_page_number,
2021  pageno);
2022 
2023  /*
2024  * Initialize member's idea of the latest page number.
2025  */
2026  pageno = MXOffsetToMemberPage(offset);
2027  pg_atomic_write_u64(&MultiXactMemberCtl->shared->latest_page_number,
2028  pageno);
2029 }
2030 
2031 /*
2032  * This must be called ONCE at the end of startup/recovery.
2033  */
2034 void
2036 {
2037  MultiXactId nextMXact;
2038  MultiXactOffset offset;
2039  MultiXactId oldestMXact;
2040  Oid oldestMXactDB;
2041  int64 pageno;
2042  int entryno;
2043  int flagsoff;
2044 
2045  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2046  nextMXact = MultiXactState->nextMXact;
2047  offset = MultiXactState->nextOffset;
2048  oldestMXact = MultiXactState->oldestMultiXactId;
2049  oldestMXactDB = MultiXactState->oldestMultiXactDB;
2050  LWLockRelease(MultiXactGenLock);
2051 
2052  /*
2053  * (Re-)Initialize our idea of the latest page number for offsets.
2054  */
2055  pageno = MultiXactIdToOffsetPage(nextMXact);
2056  pg_atomic_write_u64(&MultiXactOffsetCtl->shared->latest_page_number,
2057  pageno);
2058 
2059  /* Clean up offsets state */
2060  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
2061 
2062  /*
2063  * Zero out the remainder of the current offsets page. See notes in
2064  * TrimCLOG() for background. Unlike CLOG, some WAL record covers every
2065  * pg_multixact SLRU mutation. Since, also unlike CLOG, we ignore the WAL
2066  * rule "write xlog before data," nextMXact successors may carry obsolete,
2067  * nonzero offset values. Zero those so case 2 of GetMultiXactIdMembers()
2068  * operates normally.
2069  */
2070  entryno = MultiXactIdToOffsetEntry(nextMXact);
2071  if (entryno != 0)
2072  {
2073  int slotno;
2074  MultiXactOffset *offptr;
2075 
2076  slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, nextMXact);
2077  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
2078  offptr += entryno;
2079 
2080  MemSet(offptr, 0, BLCKSZ - (entryno * sizeof(MultiXactOffset)));
2081 
2082  MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
2083  }
2084 
2085  LWLockRelease(MultiXactOffsetSLRULock);
2086 
2087  /*
2088  * And the same for members.
2089  *
2090  * (Re-)Initialize our idea of the latest page number for members.
2091  */
2092  pageno = MXOffsetToMemberPage(offset);
2093  pg_atomic_write_u64(&MultiXactMemberCtl->shared->latest_page_number,
2094  pageno);
2095 
2096  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
2097 
2098  /*
2099  * Zero out the remainder of the current members page. See notes in
2100  * TrimCLOG() for motivation.
2101  */
2102  flagsoff = MXOffsetToFlagsOffset(offset);
2103  if (flagsoff != 0)
2104  {
2105  int slotno;
2106  TransactionId *xidptr;
2107  int memberoff;
2108 
2109  memberoff = MXOffsetToMemberOffset(offset);
2110  slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, offset);
2111  xidptr = (TransactionId *)
2112  (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
2113 
2114  MemSet(xidptr, 0, BLCKSZ - memberoff);
2115 
2116  /*
2117  * Note: we don't need to zero out the flag bits in the remaining
2118  * members of the current group, because they are always reset before
2119  * writing.
2120  */
2121 
2122  MultiXactMemberCtl->shared->page_dirty[slotno] = true;
2123  }
2124 
2125  LWLockRelease(MultiXactMemberSLRULock);
2126 
2127  /* signal that we're officially up */
2128  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2130  LWLockRelease(MultiXactGenLock);
2131 
2132  /* Now compute how far away the next members wraparound is. */
2133  SetMultiXactIdLimit(oldestMXact, oldestMXactDB, true);
2134 }
2135 
2136 /*
2137  * Get the MultiXact data to save in a checkpoint record
2138  */
2139 void
2140 MultiXactGetCheckptMulti(bool is_shutdown,
2141  MultiXactId *nextMulti,
2142  MultiXactOffset *nextMultiOffset,
2143  MultiXactId *oldestMulti,
2144  Oid *oldestMultiDB)
2145 {
2146  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2147  *nextMulti = MultiXactState->nextMXact;
2148  *nextMultiOffset = MultiXactState->nextOffset;
2149  *oldestMulti = MultiXactState->oldestMultiXactId;
2150  *oldestMultiDB = MultiXactState->oldestMultiXactDB;
2151  LWLockRelease(MultiXactGenLock);
2152 
2154  "MultiXact: checkpoint is nextMulti %u, nextOffset %u, oldestMulti %u in DB %u",
2155  *nextMulti, *nextMultiOffset, *oldestMulti, *oldestMultiDB);
2156 }
2157 
2158 /*
2159  * Perform a checkpoint --- either during shutdown, or on-the-fly
2160  */
2161 void
2163 {
2164  TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_START(true);
2165 
2166  /*
2167  * Write dirty MultiXact pages to disk. This may result in sync requests
2168  * queued for later handling by ProcessSyncRequests(), as part of the
2169  * checkpoint.
2170  */
2173 
2174  TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_DONE(true);
2175 }
2176 
2177 /*
2178  * Set the next-to-be-assigned MultiXactId and offset
2179  *
2180  * This is used when we can determine the correct next ID/offset exactly
2181  * from a checkpoint record. Although this is only called during bootstrap
2182  * and XLog replay, we take the lock in case any hot-standby backends are
2183  * examining the values.
2184  */
2185 void
2187  MultiXactOffset nextMultiOffset)
2188 {
2189  debug_elog4(DEBUG2, "MultiXact: setting next multi to %u offset %u",
2190  nextMulti, nextMultiOffset);
2191  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2192  MultiXactState->nextMXact = nextMulti;
2193  MultiXactState->nextOffset = nextMultiOffset;
2194  LWLockRelease(MultiXactGenLock);
2195 
2196  /*
2197  * During a binary upgrade, make sure that the offsets SLRU is large
2198  * enough to contain the next value that would be created.
2199  *
2200  * We need to do this pretty early during the first startup in binary
2201  * upgrade mode: before StartupMultiXact() in fact, because this routine
2202  * is called even before that by StartupXLOG(). And we can't do it
2203  * earlier than at this point, because during that first call of this
2204  * routine we determine the MultiXactState->nextMXact value that
2205  * MaybeExtendOffsetSlru needs.
2206  */
2207  if (IsBinaryUpgrade)
2209 }
2210 
2211 /*
2212  * Determine the last safe MultiXactId to allocate given the currently oldest
2213  * datminmxid (ie, the oldest MultiXactId that might exist in any database
2214  * of our cluster), and the OID of the (or a) database with that value.
2215  *
2216  * is_startup is true when we are just starting the cluster, false when we
2217  * are updating state in a running cluster. This only affects log messages.
2218  */
2219 void
2220 SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid,
2221  bool is_startup)
2222 {
2223  MultiXactId multiVacLimit;
2224  MultiXactId multiWarnLimit;
2225  MultiXactId multiStopLimit;
2226  MultiXactId multiWrapLimit;
2227  MultiXactId curMulti;
2228  bool needs_offset_vacuum;
2229 
2230  Assert(MultiXactIdIsValid(oldest_datminmxid));
2231 
2232  /*
2233  * We pretend that a wrap will happen halfway through the multixact ID
2234  * space, but that's not really true, because multixacts wrap differently
2235  * from transaction IDs. Note that, separately from any concern about
2236  * multixact IDs wrapping, we must ensure that multixact members do not
2237  * wrap. Limits for that are set in SetOffsetVacuumLimit, not here.
2238  */
2239  multiWrapLimit = oldest_datminmxid + (MaxMultiXactId >> 1);
2240  if (multiWrapLimit < FirstMultiXactId)
2241  multiWrapLimit += FirstMultiXactId;
2242 
2243  /*
2244  * We'll refuse to continue assigning MultiXactIds once we get within 3M
2245  * multi of data loss. See SetTransactionIdLimit.
2246  */
2247  multiStopLimit = multiWrapLimit - 3000000;
2248  if (multiStopLimit < FirstMultiXactId)
2249  multiStopLimit -= FirstMultiXactId;
2250 
2251  /*
2252  * We'll start complaining loudly when we get within 40M multis of data
2253  * loss. This is kind of arbitrary, but if you let your gas gauge get
2254  * down to 2% of full, would you be looking for the next gas station? We
2255  * need to be fairly liberal about this number because there are lots of
2256  * scenarios where most transactions are done by automatic clients that
2257  * won't pay attention to warnings. (No, we're not gonna make this
2258  * configurable. If you know enough to configure it, you know enough to
2259  * not get in this kind of trouble in the first place.)
2260  */
2261  multiWarnLimit = multiWrapLimit - 40000000;
2262  if (multiWarnLimit < FirstMultiXactId)
2263  multiWarnLimit -= FirstMultiXactId;
2264 
2265  /*
2266  * We'll start trying to force autovacuums when oldest_datminmxid gets to
2267  * be more than autovacuum_multixact_freeze_max_age mxids old.
2268  *
2269  * Note: autovacuum_multixact_freeze_max_age is a PGC_POSTMASTER parameter
2270  * so that we don't have to worry about dealing with on-the-fly changes in
2271  * its value. See SetTransactionIdLimit.
2272  */
2273  multiVacLimit = oldest_datminmxid + autovacuum_multixact_freeze_max_age;
2274  if (multiVacLimit < FirstMultiXactId)
2275  multiVacLimit += FirstMultiXactId;
2276 
2277  /* Grab lock for just long enough to set the new limit values */
2278  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2279  MultiXactState->oldestMultiXactId = oldest_datminmxid;
2280  MultiXactState->oldestMultiXactDB = oldest_datoid;
2281  MultiXactState->multiVacLimit = multiVacLimit;
2282  MultiXactState->multiWarnLimit = multiWarnLimit;
2283  MultiXactState->multiStopLimit = multiStopLimit;
2284  MultiXactState->multiWrapLimit = multiWrapLimit;
2285  curMulti = MultiXactState->nextMXact;
2286  LWLockRelease(MultiXactGenLock);
2287 
2288  /* Log the info */
2289  ereport(DEBUG1,
2290  (errmsg_internal("MultiXactId wrap limit is %u, limited by database with OID %u",
2291  multiWrapLimit, oldest_datoid)));
2292 
2293  /*
2294  * Computing the actual limits is only possible once the data directory is
2295  * in a consistent state. There's no need to compute the limits while
2296  * still replaying WAL - no decisions about new multis are made even
2297  * though multixact creations might be replayed. So we'll only do further
2298  * checks after TrimMultiXact() has been called.
2299  */
2301  return;
2302 
2303  Assert(!InRecovery);
2304 
2305  /* Set limits for offset vacuum. */
2306  needs_offset_vacuum = SetOffsetVacuumLimit(is_startup);
2307 
2308  /*
2309  * If past the autovacuum force point, immediately signal an autovac
2310  * request. The reason for this is that autovac only processes one
2311  * database per invocation. Once it's finished cleaning up the oldest
2312  * database, it'll call here, and we'll signal the postmaster to start
2313  * another iteration immediately if there are still any old databases.
2314  */
2315  if ((MultiXactIdPrecedes(multiVacLimit, curMulti) ||
2316  needs_offset_vacuum) && IsUnderPostmaster)
2318 
2319  /* Give an immediate warning if past the wrap warn point */
2320  if (MultiXactIdPrecedes(multiWarnLimit, curMulti))
2321  {
2322  char *oldest_datname;
2323 
2324  /*
2325  * We can be called when not inside a transaction, for example during
2326  * StartupXLOG(). In such a case we cannot do database access, so we
2327  * must just report the oldest DB's OID.
2328  *
2329  * Note: it's also possible that get_database_name fails and returns
2330  * NULL, for example because the database just got dropped. We'll
2331  * still warn, even though the warning might now be unnecessary.
2332  */
2333  if (IsTransactionState())
2334  oldest_datname = get_database_name(oldest_datoid);
2335  else
2336  oldest_datname = NULL;
2337 
2338  if (oldest_datname)
2339  ereport(WARNING,
2340  (errmsg_plural("database \"%s\" must be vacuumed before %u more MultiXactId is used",
2341  "database \"%s\" must be vacuumed before %u more MultiXactIds are used",
2342  multiWrapLimit - curMulti,
2343  oldest_datname,
2344  multiWrapLimit - curMulti),
2345  errhint("To avoid MultiXactId assignment failures, execute a database-wide VACUUM in that database.\n"
2346  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
2347  else
2348  ereport(WARNING,
2349  (errmsg_plural("database with OID %u must be vacuumed before %u more MultiXactId is used",
2350  "database with OID %u must be vacuumed before %u more MultiXactIds are used",
2351  multiWrapLimit - curMulti,
2352  oldest_datoid,
2353  multiWrapLimit - curMulti),
2354  errhint("To avoid MultiXactId assignment failures, execute a database-wide VACUUM in that database.\n"
2355  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
2356  }
2357 }
2358 
2359 /*
2360  * Ensure the next-to-be-assigned MultiXactId is at least minMulti,
2361  * and similarly nextOffset is at least minMultiOffset.
2362  *
2363  * This is used when we can determine minimum safe values from an XLog
2364  * record (either an on-line checkpoint or an mxact creation log entry).
2365  * Although this is only called during XLog replay, we take the lock in case
2366  * any hot-standby backends are examining the values.
2367  */
2368 void
2370  MultiXactOffset minMultiOffset)
2371 {
2372  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2374  {
2375  debug_elog3(DEBUG2, "MultiXact: setting next multi to %u", minMulti);
2376  MultiXactState->nextMXact = minMulti;
2377  }
2378  if (MultiXactOffsetPrecedes(MultiXactState->nextOffset, minMultiOffset))
2379  {
2380  debug_elog3(DEBUG2, "MultiXact: setting next offset to %u",
2381  minMultiOffset);
2382  MultiXactState->nextOffset = minMultiOffset;
2383  }
2384  LWLockRelease(MultiXactGenLock);
2385 }
2386 
2387 /*
2388  * Update our oldestMultiXactId value, but only if it's more recent than what
2389  * we had.
2390  *
2391  * This may only be called during WAL replay.
2392  */
2393 void
2394 MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB)
2395 {
2396  Assert(InRecovery);
2397 
2399  SetMultiXactIdLimit(oldestMulti, oldestMultiDB, false);
2400 }
2401 
2402 /*
2403  * Make sure that MultiXactOffset has room for a newly-allocated MultiXactId.
2404  *
2405  * NB: this is called while holding MultiXactGenLock. We want it to be very
2406  * fast most of the time; even when it's not so fast, no actual I/O need
2407  * happen unless we're forced to write out a dirty log or xlog page to make
2408  * room in shared memory.
2409  */
2410 static void
2412 {
2413  int64 pageno;
2414 
2415  /*
2416  * No work except at first MultiXactId of a page. But beware: just after
2417  * wraparound, the first MultiXactId of page zero is FirstMultiXactId.
2418  */
2419  if (MultiXactIdToOffsetEntry(multi) != 0 &&
2420  multi != FirstMultiXactId)
2421  return;
2422 
2423  pageno = MultiXactIdToOffsetPage(multi);
2424 
2425  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
2426 
2427  /* Zero the page and make an XLOG entry about it */
2428  ZeroMultiXactOffsetPage(pageno, true);
2429 
2430  LWLockRelease(MultiXactOffsetSLRULock);
2431 }
2432 
2433 /*
2434  * Make sure that MultiXactMember has room for the members of a newly-
2435  * allocated MultiXactId.
2436  *
2437  * Like the above routine, this is called while holding MultiXactGenLock;
2438  * same comments apply.
2439  */
2440 static void
2442 {
2443  /*
2444  * It's possible that the members span more than one page of the members
2445  * file, so we loop to ensure we consider each page. The coding is not
2446  * optimal if the members span several pages, but that seems unusual
2447  * enough to not worry much about.
2448  */
2449  while (nmembers > 0)
2450  {
2451  int flagsoff;
2452  int flagsbit;
2454 
2455  /*
2456  * Only zero when at first entry of a page.
2457  */
2458  flagsoff = MXOffsetToFlagsOffset(offset);
2459  flagsbit = MXOffsetToFlagsBitShift(offset);
2460  if (flagsoff == 0 && flagsbit == 0)
2461  {
2462  int64 pageno;
2463 
2464  pageno = MXOffsetToMemberPage(offset);
2465 
2466  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
2467 
2468  /* Zero the page and make an XLOG entry about it */
2469  ZeroMultiXactMemberPage(pageno, true);
2470 
2471  LWLockRelease(MultiXactMemberSLRULock);
2472  }
2473 
2474  /*
2475  * Compute the number of items till end of current page. Careful: if
2476  * addition of unsigned ints wraps around, we're at the last page of
2477  * the last segment; since that page holds a different number of items
2478  * than other pages, we need to do it differently.
2479  */
2480  if (offset + MAX_MEMBERS_IN_LAST_MEMBERS_PAGE < offset)
2481  {
2482  /*
2483  * This is the last page of the last segment; we can compute the
2484  * number of items left to allocate in it without modulo
2485  * arithmetic.
2486  */
2487  difference = MaxMultiXactOffset - offset + 1;
2488  }
2489  else
2491 
2492  /*
2493  * Advance to next page, taking care to properly handle the wraparound
2494  * case. OK if nmembers goes negative.
2495  */
2496  nmembers -= difference;
2497  offset += difference;
2498  }
2499 }
2500 
2501 /*
2502  * GetOldestMultiXactId
2503  *
2504  * Return the oldest MultiXactId that's still possibly still seen as live by
2505  * any running transaction. Older ones might still exist on disk, but they no
2506  * longer have any running member transaction.
2507  *
2508  * It's not safe to truncate MultiXact SLRU segments on the value returned by
2509  * this function; however, it can be set as the new relminmxid for any table
2510  * that VACUUM knows has no remaining MXIDs < the same value. It is only safe
2511  * to truncate SLRUs when no table can possibly still have a referencing MXID.
2512  */
2515 {
2516  MultiXactId oldestMXact;
2517  MultiXactId nextMXact;
2518  int i;
2519 
2520  /*
2521  * This is the oldest valid value among all the OldestMemberMXactId[] and
2522  * OldestVisibleMXactId[] entries, or nextMXact if none are valid.
2523  */
2524  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2525 
2526  /*
2527  * We have to beware of the possibility that nextMXact is in the
2528  * wrapped-around state. We don't fix the counter itself here, but we
2529  * must be sure to use a valid value in our calculation.
2530  */
2531  nextMXact = MultiXactState->nextMXact;
2532  if (nextMXact < FirstMultiXactId)
2533  nextMXact = FirstMultiXactId;
2534 
2535  oldestMXact = nextMXact;
2536  for (i = 1; i <= MaxOldestSlot; i++)
2537  {
2538  MultiXactId thisoldest;
2539 
2540  thisoldest = OldestMemberMXactId[i];
2541  if (MultiXactIdIsValid(thisoldest) &&
2542  MultiXactIdPrecedes(thisoldest, oldestMXact))
2543  oldestMXact = thisoldest;
2544  thisoldest = OldestVisibleMXactId[i];
2545  if (MultiXactIdIsValid(thisoldest) &&
2546  MultiXactIdPrecedes(thisoldest, oldestMXact))
2547  oldestMXact = thisoldest;
2548  }
2549 
2550  LWLockRelease(MultiXactGenLock);
2551 
2552  return oldestMXact;
2553 }
2554 
2555 /*
2556  * Determine how aggressively we need to vacuum in order to prevent member
2557  * wraparound.
2558  *
2559  * To do so determine what's the oldest member offset and install the limit
2560  * info in MultiXactState, where it can be used to prevent overrun of old data
2561  * in the members SLRU area.
2562  *
2563  * The return value is true if emergency autovacuum is required and false
2564  * otherwise.
2565  */
2566 static bool
2567 SetOffsetVacuumLimit(bool is_startup)
2568 {
2569  MultiXactId oldestMultiXactId;
2570  MultiXactId nextMXact;
2571  MultiXactOffset oldestOffset = 0; /* placate compiler */
2572  MultiXactOffset prevOldestOffset;
2573  MultiXactOffset nextOffset;
2574  bool oldestOffsetKnown = false;
2575  bool prevOldestOffsetKnown;
2576  MultiXactOffset offsetStopLimit = 0;
2577  MultiXactOffset prevOffsetStopLimit;
2578 
2579  /*
2580  * NB: Have to prevent concurrent truncation, we might otherwise try to
2581  * lookup an oldestMulti that's concurrently getting truncated away.
2582  */
2583  LWLockAcquire(MultiXactTruncationLock, LW_SHARED);
2584 
2585  /* Read relevant fields from shared memory. */
2586  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2587  oldestMultiXactId = MultiXactState->oldestMultiXactId;
2588  nextMXact = MultiXactState->nextMXact;
2589  nextOffset = MultiXactState->nextOffset;
2590  prevOldestOffsetKnown = MultiXactState->oldestOffsetKnown;
2591  prevOldestOffset = MultiXactState->oldestOffset;
2592  prevOffsetStopLimit = MultiXactState->offsetStopLimit;
2594  LWLockRelease(MultiXactGenLock);
2595 
2596  /*
2597  * Determine the offset of the oldest multixact. Normally, we can read
2598  * the offset from the multixact itself, but there's an important special
2599  * case: if there are no multixacts in existence at all, oldestMXact
2600  * obviously can't point to one. It will instead point to the multixact
2601  * ID that will be assigned the next time one is needed.
2602  */
2603  if (oldestMultiXactId == nextMXact)
2604  {
2605  /*
2606  * When the next multixact gets created, it will be stored at the next
2607  * offset.
2608  */
2609  oldestOffset = nextOffset;
2610  oldestOffsetKnown = true;
2611  }
2612  else
2613  {
2614  /*
2615  * Figure out where the oldest existing multixact's offsets are
2616  * stored. Due to bugs in early release of PostgreSQL 9.3.X and 9.4.X,
2617  * the supposedly-earliest multixact might not really exist. We are
2618  * careful not to fail in that case.
2619  */
2620  oldestOffsetKnown =
2621  find_multixact_start(oldestMultiXactId, &oldestOffset);
2622 
2623  if (oldestOffsetKnown)
2624  ereport(DEBUG1,
2625  (errmsg_internal("oldest MultiXactId member is at offset %u",
2626  oldestOffset)));
2627  else
2628  ereport(LOG,
2629  (errmsg("MultiXact member wraparound protections are disabled because oldest checkpointed MultiXact %u does not exist on disk",
2630  oldestMultiXactId)));
2631  }
2632 
2633  LWLockRelease(MultiXactTruncationLock);
2634 
2635  /*
2636  * If we can, compute limits (and install them MultiXactState) to prevent
2637  * overrun of old data in the members SLRU area. We can only do so if the
2638  * oldest offset is known though.
2639  */
2640  if (oldestOffsetKnown)
2641  {
2642  /* move back to start of the corresponding segment */
2643  offsetStopLimit = oldestOffset - (oldestOffset %
2645 
2646  /* always leave one segment before the wraparound point */
2647  offsetStopLimit -= (MULTIXACT_MEMBERS_PER_PAGE * SLRU_PAGES_PER_SEGMENT);
2648 
2649  if (!prevOldestOffsetKnown && !is_startup)
2650  ereport(LOG,
2651  (errmsg("MultiXact member wraparound protections are now enabled")));
2652 
2653  ereport(DEBUG1,
2654  (errmsg_internal("MultiXact member stop limit is now %u based on MultiXact %u",
2655  offsetStopLimit, oldestMultiXactId)));
2656  }
2657  else if (prevOldestOffsetKnown)
2658  {
2659  /*
2660  * If we failed to get the oldest offset this time, but we have a
2661  * value from a previous pass through this function, use the old
2662  * values rather than automatically forcing an emergency autovacuum
2663  * cycle again.
2664  */
2665  oldestOffset = prevOldestOffset;
2666  oldestOffsetKnown = true;
2667  offsetStopLimit = prevOffsetStopLimit;
2668  }
2669 
2670  /* Install the computed values */
2671  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2672  MultiXactState->oldestOffset = oldestOffset;
2673  MultiXactState->oldestOffsetKnown = oldestOffsetKnown;
2674  MultiXactState->offsetStopLimit = offsetStopLimit;
2675  LWLockRelease(MultiXactGenLock);
2676 
2677  /*
2678  * Do we need an emergency autovacuum? If we're not sure, assume yes.
2679  */
2680  return !oldestOffsetKnown ||
2681  (nextOffset - oldestOffset > MULTIXACT_MEMBER_SAFE_THRESHOLD);
2682 }
2683 
2684 /*
2685  * Return whether adding "distance" to "start" would move past "boundary".
2686  *
2687  * We use this to determine whether the addition is "wrapping around" the
2688  * boundary point, hence the name. The reason we don't want to use the regular
2689  * 2^31-modulo arithmetic here is that we want to be able to use the whole of
2690  * the 2^32-1 space here, allowing for more multixacts than would fit
2691  * otherwise.
2692  */
2693 static bool
2695  uint32 distance)
2696 {
2697  MultiXactOffset finish;
2698 
2699  /*
2700  * Note that offset number 0 is not used (see GetMultiXactIdMembers), so
2701  * if the addition wraps around the UINT_MAX boundary, skip that value.
2702  */
2703  finish = start + distance;
2704  if (finish < start)
2705  finish++;
2706 
2707  /*-----------------------------------------------------------------------
2708  * When the boundary is numerically greater than the starting point, any
2709  * value numerically between the two is not wrapped:
2710  *
2711  * <----S----B---->
2712  * [---) = F wrapped past B (and UINT_MAX)
2713  * [---) = F not wrapped
2714  * [----] = F wrapped past B
2715  *
2716  * When the boundary is numerically less than the starting point (i.e. the
2717  * UINT_MAX wraparound occurs somewhere in between) then all values in
2718  * between are wrapped:
2719  *
2720  * <----B----S---->
2721  * [---) = F not wrapped past B (but wrapped past UINT_MAX)
2722  * [---) = F wrapped past B (and UINT_MAX)
2723  * [----] = F not wrapped
2724  *-----------------------------------------------------------------------
2725  */
2726  if (start < boundary)
2727  return finish >= boundary || finish < start;
2728  else
2729  return finish >= boundary && finish < start;
2730 }
2731 
2732 /*
2733  * Find the starting offset of the given MultiXactId.
2734  *
2735  * Returns false if the file containing the multi does not exist on disk.
2736  * Otherwise, returns true and sets *result to the starting member offset.
2737  *
2738  * This function does not prevent concurrent truncation, so if that's
2739  * required, the caller has to protect against that.
2740  */
2741 static bool
2743 {
2744  MultiXactOffset offset;
2745  int64 pageno;
2746  int entryno;
2747  int slotno;
2748  MultiXactOffset *offptr;
2749 
2751 
2752  pageno = MultiXactIdToOffsetPage(multi);
2753  entryno = MultiXactIdToOffsetEntry(multi);
2754 
2755  /*
2756  * Write out dirty data, so PhysicalPageExists can work correctly.
2757  */
2760 
2762  return false;
2763 
2764  /* lock is acquired by SimpleLruReadPage_ReadOnly */
2765  slotno = SimpleLruReadPage_ReadOnly(MultiXactOffsetCtl, pageno, multi);
2766  offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
2767  offptr += entryno;
2768  offset = *offptr;
2769  LWLockRelease(MultiXactOffsetSLRULock);
2770 
2771  *result = offset;
2772  return true;
2773 }
2774 
2775 /*
2776  * Determine how many multixacts, and how many multixact members, currently
2777  * exist. Return false if unable to determine.
2778  */
2779 static bool
2781 {
2782  MultiXactOffset nextOffset;
2783  MultiXactOffset oldestOffset;
2784  MultiXactId oldestMultiXactId;
2785  MultiXactId nextMultiXactId;
2786  bool oldestOffsetKnown;
2787 
2788  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2789  nextOffset = MultiXactState->nextOffset;
2790  oldestMultiXactId = MultiXactState->oldestMultiXactId;
2791  nextMultiXactId = MultiXactState->nextMXact;
2792  oldestOffset = MultiXactState->oldestOffset;
2793  oldestOffsetKnown = MultiXactState->oldestOffsetKnown;
2794  LWLockRelease(MultiXactGenLock);
2795 
2796  if (!oldestOffsetKnown)
2797  return false;
2798 
2799  *members = nextOffset - oldestOffset;
2800  *multixacts = nextMultiXactId - oldestMultiXactId;
2801  return true;
2802 }
2803 
2804 /*
2805  * Multixact members can be removed once the multixacts that refer to them
2806  * are older than every datminmxid. autovacuum_multixact_freeze_max_age and
2807  * vacuum_multixact_freeze_table_age work together to make sure we never have
2808  * too many multixacts; we hope that, at least under normal circumstances,
2809  * this will also be sufficient to keep us from using too many offsets.
2810  * However, if the average multixact has many members, we might exhaust the
2811  * members space while still using few enough members that these limits fail
2812  * to trigger relminmxid advancement by VACUUM. At that point, we'd have no
2813  * choice but to start failing multixact-creating operations with an error.
2814  *
2815  * To prevent that, if more than a threshold portion of the members space is
2816  * used, we effectively reduce autovacuum_multixact_freeze_max_age and
2817  * to a value just less than the number of multixacts in use. We hope that
2818  * this will quickly trigger autovacuuming on the table or tables with the
2819  * oldest relminmxid, thus allowing datminmxid values to advance and removing
2820  * some members.
2821  *
2822  * As the fraction of the member space currently in use grows, we become
2823  * more aggressive in clamping this value. That not only causes autovacuum
2824  * to ramp up, but also makes any manual vacuums the user issues more
2825  * aggressive. This happens because vacuum_get_cutoffs() will clamp the
2826  * freeze table and the minimum freeze age cutoffs based on the effective
2827  * autovacuum_multixact_freeze_max_age this function returns. In the worst
2828  * case, we'll claim the freeze_max_age to zero, and every vacuum of any
2829  * table will freeze every multixact.
2830  */
2831 int
2833 {
2834  MultiXactOffset members;
2835  uint32 multixacts;
2836  uint32 victim_multixacts;
2837  double fraction;
2838 
2839  /* If we can't determine member space utilization, assume the worst. */
2840  if (!ReadMultiXactCounts(&multixacts, &members))
2841  return 0;
2842 
2843  /* If member space utilization is low, no special action is required. */
2844  if (members <= MULTIXACT_MEMBER_SAFE_THRESHOLD)
2846 
2847  /*
2848  * Compute a target for relminmxid advancement. The number of multixacts
2849  * we try to eliminate from the system is based on how far we are past
2850  * MULTIXACT_MEMBER_SAFE_THRESHOLD.
2851  */
2852  fraction = (double) (members - MULTIXACT_MEMBER_SAFE_THRESHOLD) /
2854  victim_multixacts = multixacts * fraction;
2855 
2856  /* fraction could be > 1.0, but lowest possible freeze age is zero */
2857  if (victim_multixacts > multixacts)
2858  return 0;
2859  return multixacts - victim_multixacts;
2860 }
2861 
2862 typedef struct mxtruncinfo
2863 {
2866 
2867 /*
2868  * SlruScanDirectory callback
2869  * This callback determines the earliest existing page number.
2870  */
2871 static bool
2872 SlruScanDirCbFindEarliest(SlruCtl ctl, char *filename, int64 segpage, void *data)
2873 {
2874  mxtruncinfo *trunc = (mxtruncinfo *) data;
2875 
2876  if (trunc->earliestExistingPage == -1 ||
2877  ctl->PagePrecedes(segpage, trunc->earliestExistingPage))
2878  {
2879  trunc->earliestExistingPage = segpage;
2880  }
2881 
2882  return false; /* keep going */
2883 }
2884 
2885 
2886 /*
2887  * Delete members segments [oldest, newOldest)
2888  *
2889  * The members SLRU can, in contrast to the offsets one, be filled to almost
2890  * the full range at once. This means SimpleLruTruncate() can't trivially be
2891  * used - instead the to-be-deleted range is computed using the offsets
2892  * SLRU. C.f. TruncateMultiXact().
2893  */
2894 static void
2896 {
2897  const int maxsegment = MXOffsetToMemberSegment(MaxMultiXactOffset);
2898  int startsegment = MXOffsetToMemberSegment(oldestOffset);
2899  int endsegment = MXOffsetToMemberSegment(newOldestOffset);
2900  int segment = startsegment;
2901 
2902  /*
2903  * Delete all the segments but the last one. The last segment can still
2904  * contain, possibly partially, valid data.
2905  */
2906  while (segment != endsegment)
2907  {
2908  elog(DEBUG2, "truncating multixact members segment %x", segment);
2910 
2911  /* move to next segment, handling wraparound correctly */
2912  if (segment == maxsegment)
2913  segment = 0;
2914  else
2915  segment += 1;
2916  }
2917 }
2918 
2919 /*
2920  * Delete offsets segments [oldest, newOldest)
2921  */
2922 static void
2924 {
2925  /*
2926  * We step back one multixact to avoid passing a cutoff page that hasn't
2927  * been created yet in the rare case that oldestMulti would be the first
2928  * item on a page and oldestMulti == nextMulti. In that case, if we
2929  * didn't subtract one, we'd trigger SimpleLruTruncate's wraparound
2930  * detection.
2931  */
2933  MultiXactIdToOffsetPage(PreviousMultiXactId(newOldestMulti)));
2934 }
2935 
2936 /*
2937  * Remove all MultiXactOffset and MultiXactMember segments before the oldest
2938  * ones still of interest.
2939  *
2940  * This is only called on a primary as part of vacuum (via
2941  * vac_truncate_clog()). During recovery truncation is done by replaying
2942  * truncation WAL records logged here.
2943  *
2944  * newOldestMulti is the oldest currently required multixact, newOldestMultiDB
2945  * is one of the databases preventing newOldestMulti from increasing.
2946  */
2947 void
2948 TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
2949 {
2950  MultiXactId oldestMulti;
2951  MultiXactId nextMulti;
2952  MultiXactOffset newOldestOffset;
2953  MultiXactOffset oldestOffset;
2954  MultiXactOffset nextOffset;
2955  mxtruncinfo trunc;
2956  MultiXactId earliest;
2957 
2960 
2961  /*
2962  * We can only allow one truncation to happen at once. Otherwise parts of
2963  * members might vanish while we're doing lookups or similar. There's no
2964  * need to have an interlock with creating new multis or such, since those
2965  * are constrained by the limits (which only grow, never shrink).
2966  */
2967  LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
2968 
2969  LWLockAcquire(MultiXactGenLock, LW_SHARED);
2970  nextMulti = MultiXactState->nextMXact;
2971  nextOffset = MultiXactState->nextOffset;
2972  oldestMulti = MultiXactState->oldestMultiXactId;
2973  LWLockRelease(MultiXactGenLock);
2974  Assert(MultiXactIdIsValid(oldestMulti));
2975 
2976  /*
2977  * Make sure to only attempt truncation if there's values to truncate
2978  * away. In normal processing values shouldn't go backwards, but there's
2979  * some corner cases (due to bugs) where that's possible.
2980  */
2981  if (MultiXactIdPrecedesOrEquals(newOldestMulti, oldestMulti))
2982  {
2983  LWLockRelease(MultiXactTruncationLock);
2984  return;
2985  }
2986 
2987  /*
2988  * Note we can't just plow ahead with the truncation; it's possible that
2989  * there are no segments to truncate, which is a problem because we are
2990  * going to attempt to read the offsets page to determine where to
2991  * truncate the members SLRU. So we first scan the directory to determine
2992  * the earliest offsets page number that we can read without error.
2993  *
2994  * When nextMXact is less than one segment away from multiWrapLimit,
2995  * SlruScanDirCbFindEarliest can find some early segment other than the
2996  * actual earliest. (MultiXactOffsetPagePrecedes(EARLIEST, LATEST)
2997  * returns false, because not all pairs of entries have the same answer.)
2998  * That can also arise when an earlier truncation attempt failed unlink()
2999  * or returned early from this function. The only consequence is
3000  * returning early, which wastes space that we could have liberated.
3001  *
3002  * NB: It's also possible that the page that oldestMulti is on has already
3003  * been truncated away, and we crashed before updating oldestMulti.
3004  */
3005  trunc.earliestExistingPage = -1;
3008  if (earliest < FirstMultiXactId)
3009  earliest = FirstMultiXactId;
3010 
3011  /* If there's nothing to remove, we can bail out early. */
3012  if (MultiXactIdPrecedes(oldestMulti, earliest))
3013  {
3014  LWLockRelease(MultiXactTruncationLock);
3015  return;
3016  }
3017 
3018  /*
3019  * First, compute the safe truncation point for MultiXactMember. This is
3020  * the starting offset of the oldest multixact.
3021  *
3022  * Hopefully, find_multixact_start will always work here, because we've
3023  * already checked that it doesn't precede the earliest MultiXact on disk.
3024  * But if it fails, don't truncate anything, and log a message.
3025  */
3026  if (oldestMulti == nextMulti)
3027  {
3028  /* there are NO MultiXacts */
3029  oldestOffset = nextOffset;
3030  }
3031  else if (!find_multixact_start(oldestMulti, &oldestOffset))
3032  {
3033  ereport(LOG,
3034  (errmsg("oldest MultiXact %u not found, earliest MultiXact %u, skipping truncation",
3035  oldestMulti, earliest)));
3036  LWLockRelease(MultiXactTruncationLock);
3037  return;
3038  }
3039 
3040  /*
3041  * Secondly compute up to where to truncate. Lookup the corresponding
3042  * member offset for newOldestMulti for that.
3043  */
3044  if (newOldestMulti == nextMulti)
3045  {
3046  /* there are NO MultiXacts */
3047  newOldestOffset = nextOffset;
3048  }
3049  else if (!find_multixact_start(newOldestMulti, &newOldestOffset))
3050  {
3051  ereport(LOG,
3052  (errmsg("cannot truncate up to MultiXact %u because it does not exist on disk, skipping truncation",
3053  newOldestMulti)));
3054  LWLockRelease(MultiXactTruncationLock);
3055  return;
3056  }
3057 
3058  elog(DEBUG1, "performing multixact truncation: "
3059  "offsets [%u, %u), offsets segments [%x, %x), "
3060  "members [%u, %u), members segments [%x, %x)",
3061  oldestMulti, newOldestMulti,
3062  MultiXactIdToOffsetSegment(oldestMulti),
3063  MultiXactIdToOffsetSegment(newOldestMulti),
3064  oldestOffset, newOldestOffset,
3065  MXOffsetToMemberSegment(oldestOffset),
3066  MXOffsetToMemberSegment(newOldestOffset));
3067 
3068  /*
3069  * Do truncation, and the WAL logging of the truncation, in a critical
3070  * section. That way offsets/members cannot get out of sync anymore, i.e.
3071  * once consistent the newOldestMulti will always exist in members, even
3072  * if we crashed in the wrong moment.
3073  */
3075 
3076  /*
3077  * Prevent checkpoints from being scheduled concurrently. This is critical
3078  * because otherwise a truncation record might not be replayed after a
3079  * crash/basebackup, even though the state of the data directory would
3080  * require it.
3081  */
3084 
3085  /* WAL log truncation */
3086  WriteMTruncateXlogRec(newOldestMultiDB,
3087  oldestMulti, newOldestMulti,
3088  oldestOffset, newOldestOffset);
3089 
3090  /*
3091  * Update in-memory limits before performing the truncation, while inside
3092  * the critical section: Have to do it before truncation, to prevent
3093  * concurrent lookups of those values. Has to be inside the critical
3094  * section as otherwise a future call to this function would error out,
3095  * while looking up the oldest member in offsets, if our caller crashes
3096  * before updating the limits.
3097  */
3098  LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
3099  MultiXactState->oldestMultiXactId = newOldestMulti;
3100  MultiXactState->oldestMultiXactDB = newOldestMultiDB;
3101  LWLockRelease(MultiXactGenLock);
3102 
3103  /* First truncate members */
3104  PerformMembersTruncation(oldestOffset, newOldestOffset);
3105 
3106  /* Then offsets */
3107  PerformOffsetsTruncation(oldestMulti, newOldestMulti);
3108 
3110 
3111  END_CRIT_SECTION();
3112  LWLockRelease(MultiXactTruncationLock);
3113 }
3114 
3115 /*
3116  * Decide whether a MultiXactOffset page number is "older" for truncation
3117  * purposes. Analogous to CLOGPagePrecedes().
3118  *
3119  * Offsetting the values is optional, because MultiXactIdPrecedes() has
3120  * translational symmetry.
3121  */
3122 static bool
3123 MultiXactOffsetPagePrecedes(int64 page1, int64 page2)
3124 {
3125  MultiXactId multi1;
3126  MultiXactId multi2;
3127 
3128  multi1 = ((MultiXactId) page1) * MULTIXACT_OFFSETS_PER_PAGE;
3129  multi1 += FirstMultiXactId + 1;
3130  multi2 = ((MultiXactId) page2) * MULTIXACT_OFFSETS_PER_PAGE;
3131  multi2 += FirstMultiXactId + 1;
3132 
3133  return (MultiXactIdPrecedes(multi1, multi2) &&
3134  MultiXactIdPrecedes(multi1,
3135  multi2 + MULTIXACT_OFFSETS_PER_PAGE - 1));
3136 }
3137 
3138 /*
3139  * Decide whether a MultiXactMember page number is "older" for truncation
3140  * purposes. There is no "invalid offset number" so use the numbers verbatim.
3141  */
3142 static bool
3143 MultiXactMemberPagePrecedes(int64 page1, int64 page2)
3144 {
3145  MultiXactOffset offset1;
3146  MultiXactOffset offset2;
3147 
3148  offset1 = ((MultiXactOffset) page1) * MULTIXACT_MEMBERS_PER_PAGE;
3149  offset2 = ((MultiXactOffset) page2) * MULTIXACT_MEMBERS_PER_PAGE;
3150 
3151  return (MultiXactOffsetPrecedes(offset1, offset2) &&
3152  MultiXactOffsetPrecedes(offset1,
3153  offset2 + MULTIXACT_MEMBERS_PER_PAGE - 1));
3154 }
3155 
3156 /*
3157  * Decide which of two MultiXactIds is earlier.
3158  *
3159  * XXX do we need to do something special for InvalidMultiXactId?
3160  * (Doesn't look like it.)
3161  */
3162 bool
3164 {
3165  int32 diff = (int32) (multi1 - multi2);
3166 
3167  return (diff < 0);
3168 }
3169 
3170 /*
3171  * MultiXactIdPrecedesOrEquals -- is multi1 logically <= multi2?
3172  *
3173  * XXX do we need to do something special for InvalidMultiXactId?
3174  * (Doesn't look like it.)
3175  */
3176 bool
3178 {
3179  int32 diff = (int32) (multi1 - multi2);
3180 
3181  return (diff <= 0);
3182 }
3183 
3184 
3185 /*
3186  * Decide which of two offsets is earlier.
3187  */
3188 static bool
3190 {
3191  int32 diff = (int32) (offset1 - offset2);
3192 
3193  return (diff < 0);
3194 }
3195 
3196 /*
3197  * Write an xlog record reflecting the zeroing of either a MEMBERs or
3198  * OFFSETs page (info shows which)
3199  */
3200 static void
3201 WriteMZeroPageXlogRec(int64 pageno, uint8 info)
3202 {
3203  XLogBeginInsert();
3204  XLogRegisterData((char *) (&pageno), sizeof(pageno));
3205  (void) XLogInsert(RM_MULTIXACT_ID, info);
3206 }
3207 
3208 /*
3209  * Write a TRUNCATE xlog record
3210  *
3211  * We must flush the xlog record to disk before returning --- see notes in
3212  * TruncateCLOG().
3213  */
3214 static void
3216  MultiXactId startTruncOff, MultiXactId endTruncOff,
3217  MultiXactOffset startTruncMemb, MultiXactOffset endTruncMemb)
3218 {
3219  XLogRecPtr recptr;
3220  xl_multixact_truncate xlrec;
3221 
3222  xlrec.oldestMultiDB = oldestMultiDB;
3223 
3224  xlrec.startTruncOff = startTruncOff;
3225  xlrec.endTruncOff = endTruncOff;
3226 
3227  xlrec.startTruncMemb = startTruncMemb;
3228  xlrec.endTruncMemb = endTruncMemb;
3229 
3230  XLogBeginInsert();
3231  XLogRegisterData((char *) (&xlrec), SizeOfMultiXactTruncate);
3232  recptr = XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_TRUNCATE_ID);
3233  XLogFlush(recptr);
3234 }
3235 
3236 /*
3237  * MULTIXACT resource manager's routines
3238  */
3239 void
3241 {
3242  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
3243 
3244  /* Backup blocks are not used in multixact records */
3245  Assert(!XLogRecHasAnyBlockRefs(record));
3246 
3247  if (info == XLOG_MULTIXACT_ZERO_OFF_PAGE)
3248  {
3249  int64 pageno;
3250  int slotno;
3251 
3252  memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
3253 
3254  LWLockAcquire(MultiXactOffsetSLRULock, LW_EXCLUSIVE);
3255 
3256  slotno = ZeroMultiXactOffsetPage(pageno, false);
3258  Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
3259 
3260  LWLockRelease(MultiXactOffsetSLRULock);
3261  }
3262  else if (info == XLOG_MULTIXACT_ZERO_MEM_PAGE)
3263  {
3264  int64 pageno;
3265  int slotno;
3266 
3267  memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
3268 
3269  LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
3270 
3271  slotno = ZeroMultiXactMemberPage(pageno, false);
3273  Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
3274 
3275  LWLockRelease(MultiXactMemberSLRULock);
3276  }
3277  else if (info == XLOG_MULTIXACT_CREATE_ID)
3278  {
3279  xl_multixact_create *xlrec =
3280  (xl_multixact_create *) XLogRecGetData(record);
3281  TransactionId max_xid;
3282  int i;
3283 
3284  /* Store the data back into the SLRU files */
3285  RecordNewMultiXact(xlrec->mid, xlrec->moff, xlrec->nmembers,
3286  xlrec->members);
3287 
3288  /* Make sure nextMXact/nextOffset are beyond what this record has */
3289  MultiXactAdvanceNextMXact(xlrec->mid + 1,
3290  xlrec->moff + xlrec->nmembers);
3291 
3292  /*
3293  * Make sure nextXid is beyond any XID mentioned in the record. This
3294  * should be unnecessary, since any XID found here ought to have other
3295  * evidence in the XLOG, but let's be safe.
3296  */
3297  max_xid = XLogRecGetXid(record);
3298  for (i = 0; i < xlrec->nmembers; i++)
3299  {
3300  if (TransactionIdPrecedes(max_xid, xlrec->members[i].xid))
3301  max_xid = xlrec->members[i].xid;
3302  }
3303 
3305  }
3306  else if (info == XLOG_MULTIXACT_TRUNCATE_ID)
3307  {
3308  xl_multixact_truncate xlrec;
3309  int64 pageno;
3310 
3311  memcpy(&xlrec, XLogRecGetData(record),
3313 
3314  elog(DEBUG1, "replaying multixact truncation: "
3315  "offsets [%u, %u), offsets segments [%x, %x), "
3316  "members [%u, %u), members segments [%x, %x)",
3317  xlrec.startTruncOff, xlrec.endTruncOff,
3320  xlrec.startTruncMemb, xlrec.endTruncMemb,
3323 
3324  /* should not be required, but more than cheap enough */
3325  LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
3326 
3327  /*
3328  * Advance the horizon values, so they're current at the end of
3329  * recovery.
3330  */
3331  SetMultiXactIdLimit(xlrec.endTruncOff, xlrec.oldestMultiDB, false);
3332 
3334 
3335  /*
3336  * During XLOG replay, latest_page_number isn't necessarily set up
3337  * yet; insert a suitable value to bypass the sanity test in
3338  * SimpleLruTruncate.
3339  */
3340  pageno = MultiXactIdToOffsetPage(xlrec.endTruncOff);
3341  pg_atomic_write_u64(&MultiXactOffsetCtl->shared->latest_page_number,
3342  pageno);
3344 
3345  LWLockRelease(MultiXactTruncationLock);
3346  }
3347  else
3348  elog(PANIC, "multixact_redo: unknown op code %u", info);
3349 }
3350 
3351 Datum
3353 {
3354  typedef struct
3355  {
3356  MultiXactMember *members;
3357  int nmembers;
3358  int iter;
3359  } mxact;
3361  mxact *multi;
3362  FuncCallContext *funccxt;
3363 
3364  if (mxid < FirstMultiXactId)
3365  ereport(ERROR,
3366  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3367  errmsg("invalid MultiXactId: %u", mxid)));
3368 
3369  if (SRF_IS_FIRSTCALL())
3370  {
3371  MemoryContext oldcxt;
3372  TupleDesc tupdesc;
3373 
3374  funccxt = SRF_FIRSTCALL_INIT();
3375  oldcxt = MemoryContextSwitchTo(funccxt->multi_call_memory_ctx);
3376 
3377  multi = palloc(sizeof(mxact));
3378  /* no need to allow for old values here */
3379  multi->nmembers = GetMultiXactIdMembers(mxid, &multi->members, false,
3380  false);
3381  multi->iter = 0;
3382 
3383  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
3384  elog(ERROR, "return type must be a row type");
3385  funccxt->tuple_desc = tupdesc;
3386  funccxt->attinmeta = TupleDescGetAttInMetadata(tupdesc);
3387  funccxt->user_fctx = multi;
3388 
3389  MemoryContextSwitchTo(oldcxt);
3390  }
3391 
3392  funccxt = SRF_PERCALL_SETUP();
3393  multi = (mxact *) funccxt->user_fctx;
3394 
3395  while (multi->iter < multi->nmembers)
3396  {
3397  HeapTuple tuple;
3398  char *values[2];
3399 
3400  values[0] = psprintf("%u", multi->members[multi->iter].xid);
3401  values[1] = mxstatus_to_string(multi->members[multi->iter].status);
3402 
3403  tuple = BuildTupleFromCStrings(funccxt->attinmeta, values);
3404 
3405  multi->iter++;
3406  pfree(values[0]);
3407  SRF_RETURN_NEXT(funccxt, HeapTupleGetDatum(tuple));
3408  }
3409 
3410  SRF_RETURN_DONE(funccxt);
3411 }
3412 
3413 /*
3414  * Entrypoint for sync.c to sync offsets files.
3415  */
3416 int
3417 multixactoffsetssyncfiletag(const FileTag *ftag, char *path)
3418 {
3419  return SlruSyncFileTag(MultiXactOffsetCtl, ftag, path);
3420 }
3421 
3422 /*
3423  * Entrypoint for sync.c to sync members files.
3424  */
3425 int
3426 multixactmemberssyncfiletag(const FileTag *ftag, char *path)
3427 {
3428  return SlruSyncFileTag(MultiXactMemberCtl, ftag, path);
3429 }
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:433
int autovacuum_multixact_freeze_max_age
Definition: autovacuum.c:128
int BackendId
Definition: backendid.h:21
static int32 next
Definition: blutils.c:221
static Datum values[MAXATTR]
Definition: bootstrap.c:156
unsigned short uint16
Definition: c.h:494
unsigned int uint32
Definition: c.h:495
signed int int32
Definition: c.h:483
uint32 MultiXactOffset
Definition: c.h:653
TransactionId MultiXactId
Definition: c.h:651
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:387
unsigned char uint8
Definition: c.h:493
#define MemSet(start, val, len)
Definition: c.h:1009
uint32 TransactionId
Definition: c.h:641
size_t Size
Definition: c.h:594
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3089
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1185
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1162
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1300
int errhint(const char *fmt,...)
Definition: elog.c:1322
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define LOG
Definition: elog.h:31
#define WARNING
Definition: elog.h:36
#define DEBUG2
Definition: elog.h:29
#define PANIC
Definition: elog.h:42
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2134
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2085
#define PG_GETARG_TRANSACTIONID(n)
Definition: fmgr.h:279
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
Datum difference(PG_FUNCTION_ARGS)
bool IsBinaryUpgrade
Definition: globals.c:117
bool IsUnderPostmaster
Definition: globals.c:116
BackendId MyBackendId
Definition: globals.c:86
#define dclist_container(type, membername, ptr)
Definition: ilist.h:947
static dlist_node * dclist_tail_node(dclist_head *head)
Definition: ilist.h:920
static uint32 dclist_count(const dclist_head *head)
Definition: ilist.h:932
static void dclist_move_head(dclist_head *head, dlist_node *node)
Definition: ilist.h:808
static void dclist_delete_from(dclist_head *head, dlist_node *node)
Definition: ilist.h:763
#define DCLIST_STATIC_INIT(name)
Definition: ilist.h:282
static void dclist_push_head(dclist_head *head, dlist_node *node)
Definition: ilist.h:693
static void dclist_init(dclist_head *head)
Definition: ilist.h:671
#define dclist_foreach(iter, lhead)
Definition: ilist.h:970
int j
Definition: isn.c:74
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LWTRANCHE_MULTIXACTMEMBER_BUFFER
Definition: lwlock.h:185
@ LWTRANCHE_MULTIXACTOFFSET_BUFFER
Definition: lwlock.h:184
@ LW_SHARED
Definition: lwlock.h:117
@ LW_EXCLUSIVE
Definition: lwlock.h:116
MemoryContext TopTransactionContext
Definition: mcxt.c:146
void pfree(void *pointer)
Definition: mcxt.c:1431
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1034
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1606
void * palloc(Size size)
Definition: mcxt.c:1201
#define AllocSetContextCreate
Definition: memutils.h:128
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:162
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
#define MXOffsetToMemberPage(xid)
Definition: multixact.c:159
static void WriteMTruncateXlogRec(Oid oldestMultiDB, MultiXactId startTruncOff, MultiXactId endTruncOff, MultiXactOffset startTruncMemb, MultiXactOffset endTruncMemb)
Definition: multixact.c:3215
static SlruCtlData MultiXactOffsetCtlData
Definition: multixact.c:187
void MultiXactShmemInit(void)
Definition: multixact.c:1844
#define MULTIXACT_MEMBER_SAFE_THRESHOLD
Definition: multixact.c:177
static bool MultiXactMemberPagePrecedes(int64 page1, int64 page2)
Definition: multixact.c:3143
static MultiXactId GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
Definition: multixact.c:958
static int mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
Definition: multixact.c:1542
MultiXactId MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status)
Definition: multixact.c:440
static int ZeroMultiXactMemberPage(int64 pageno, bool writeXlog)
Definition: multixact.c:1949
#define MXACT_MEMBER_BITS_PER_XACT
Definition: multixact.c:130
static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers)
Definition: multixact.c:2441
void ReadMultiXactIdRange(MultiXactId *oldest, MultiXactId *next)
Definition: multixact.c:744
static void PerformOffsetsTruncation(MultiXactId oldestMulti, MultiXactId newOldestMulti)
Definition: multixact.c:2923
#define MXACT_MEMBER_XACT_BITMASK
Definition: multixact.c:132
#define MultiXactIdToOffsetEntry(xid)
Definition: multixact.c:113
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3163
void multixact_redo(XLogReaderState *record)
Definition: multixact.c:3240
#define MULTIXACT_OFFSETS_PER_PAGE
Definition: multixact.c:109
#define debug_elog5(a, b, c, d, e)
Definition: multixact.c:337
static void MultiXactIdSetOldestVisible(void)
Definition: multixact.c:683
int multixactoffsetssyncfiletag(const FileTag *ftag, char *path)
Definition: multixact.c:3417
void multixact_twophase_postcommit(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: multixact.c:1800
static bool find_multixact_start(MultiXactId multi, MultiXactOffset *result)
Definition: multixact.c:2742
void MultiXactSetNextMXact(MultiXactId nextMulti, MultiXactOffset nextMultiOffset)
Definition: multixact.c:2186
void multixact_twophase_recover(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: multixact.c:1779
#define MultiXactMemberCtl
Definition: multixact.c:191
static bool SlruScanDirCbFindEarliest(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition: multixact.c:2872
void AtPrepare_MultiXact(void)
Definition: multixact.c:1716
static bool MultiXactOffsetWouldWrap(MultiXactOffset boundary, MultiXactOffset start, uint32 distance)
Definition: multixact.c:2694
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3177
#define MXOffsetToFlagsBitShift(xid)
Definition: multixact.c:167
void MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB)
Definition: multixact.c:2394
static void mXactCachePut(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1589
#define MXOffsetToFlagsOffset(xid)
Definition: multixact.c:163
static void MaybeExtendOffsetSlru(void)
Definition: multixact.c:1977
bool MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly)
Definition: multixact.c:552
void MultiXactIdSetOldestMember(void)
Definition: multixact.c:626
static void PerformMembersTruncation(MultiXactOffset oldestOffset, MultiXactOffset newOldestOffset)
Definition: multixact.c:2895
static MemoryContext MXactContext
Definition: multixact.c:325
#define SHARED_MULTIXACT_STATE_SIZE
static MultiXactId * OldestVisibleMXactId
Definition: multixact.c:295
struct mxtruncinfo mxtruncinfo
static int mxactMemberComparator(const void *arg1, const void *arg2)
Definition: multixact.c:1469
struct MultiXactStateData MultiXactStateData
static void ExtendMultiXactOffset(MultiXactId multi)
Definition: multixact.c:2411
void PostPrepare_MultiXact(TransactionId xid)
Definition: multixact.c:1730
Size MultiXactShmemSize(void)
Definition: multixact.c:1827
#define MultiXactOffsetCtl
Definition: multixact.c:190
void multixact_twophase_postabort(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: multixact.c:1815
#define PreviousMultiXactId(xid)
Definition: multixact.c:181
void MultiXactGetCheckptMulti(bool is_shutdown, MultiXactId *nextMulti, MultiXactOffset *nextMultiOffset, MultiXactId *oldestMulti, Oid *oldestMultiDB)
Definition: multixact.c:2140
static void WriteMZeroPageXlogRec(int64 pageno, uint8 info)
Definition: multixact.c:3201
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid, bool is_startup)
Definition: multixact.c:2220
static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset, int nmembers, MultiXactMember *members)
Definition: multixact.c:864
int multixactmemberssyncfiletag(const FileTag *ftag, char *path)
Definition: multixact.c:3426
#define MAX_CACHE_ENTRIES
Definition: multixact.c:323
#define MXOffsetToMemberOffset(xid)
Definition: multixact.c:172
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2514
void CheckPointMultiXact(void)
Definition: multixact.c:2162
#define MaxOldestSlot
Definition: multixact.c:290
MultiXactId MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
Definition: multixact.c:768
static bool ReadMultiXactCounts(uint32 *multixacts, MultiXactOffset *members)
Definition: multixact.c:2780
struct mXactCacheEnt mXactCacheEnt
static MultiXactId mXactCacheGetBySet(int nmembers, MultiXactMember *members)
Definition: multixact.c:1499
static dclist_head MXactCache
Definition: multixact.c:324
#define MXOffsetToMemberSegment(xid)
Definition: multixact.c:160
void TrimMultiXact(void)
Definition: multixact.c:2035
char * mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
Definition: multixact.c:1657
#define debug_elog3(a, b, c)
Definition: multixact.c:335
#define debug_elog4(a, b, c, d)
Definition: multixact.c:336
static bool MultiXactOffsetPagePrecedes(int64 page1, int64 page2)
Definition: multixact.c:3123
static bool SetOffsetVacuumLimit(bool is_startup)
Definition: multixact.c:2567
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2832
void MultiXactAdvanceNextMXact(MultiXactId minMulti, MultiXactOffset minMultiOffset)
Definition: multixact.c:2369
static MultiXactId * OldestMemberMXactId
Definition: multixact.c:294
#define MAX_MEMBERS_IN_LAST_MEMBERS_PAGE
Definition: multixact.c:155
static MultiXactStateData * MultiXactState
Definition: multixact.c:293
static int ZeroMultiXactOffsetPage(int64 pageno, bool writeXlog)
Definition: multixact.c:1933
static char * mxstatus_to_string(MultiXactStatus status)
Definition: multixact.c:1634
#define OFFSET_WARN_SEGMENTS
Datum pg_get_multixact_members(PG_FUNCTION_ARGS)
Definition: multixact.c:3352
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:724
void BootStrapMultiXact(void)
Definition: multixact.c:1896
#define debug_elog6(a, b, c, d, e, f)
Definition: multixact.c:338
#define MULTIXACT_MEMBERS_PER_PAGE
Definition: multixact.c:142
MultiXactId MultiXactIdCreate(TransactionId xid1, MultiXactStatus status1, TransactionId xid2, MultiXactStatus status2)
Definition: multixact.c:387
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition: multixact.c:2948
#define MULTIXACT_MEMBER_DANGER_THRESHOLD
Definition: multixact.c:178
static bool MultiXactOffsetPrecedes(MultiXactOffset offset1, MultiXactOffset offset2)
Definition: multixact.c:3189
#define MultiXactIdToOffsetSegment(xid)
Definition: multixact.c:115
void AtEOXact_MultiXact(void)
Definition: multixact.c:1688
#define MultiXactIdToOffsetPage(xid)
Definition: multixact.c:111
static SlruCtlData MultiXactMemberCtlData
Definition: multixact.c:188
#define debug_elog2(a, b)
Definition: multixact.c:334
void StartupMultiXact(void)
Definition: multixact.c:2010
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool isLockOnly)
Definition: multixact.c:1225
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define XLOG_MULTIXACT_ZERO_MEM_PAGE
Definition: multixact.h:73
#define XLOG_MULTIXACT_ZERO_OFF_PAGE
Definition: multixact.h:72
#define FirstMultiXactId
Definition: multixact.h:25
#define NUM_MULTIXACTOFFSET_BUFFERS
Definition: multixact.h:33
MultiXactStatus
Definition: multixact.h:42
@ MultiXactStatusForShare
Definition: multixact.h:44
@ MultiXactStatusForNoKeyUpdate
Definition: multixact.h:45
@ MultiXactStatusNoKeyUpdate
Definition: multixact.h:48
@ MultiXactStatusUpdate
Definition: multixact.h:50
@ MultiXactStatusForUpdate
Definition: multixact.h:46
@ MultiXactStatusForKeyShare
Definition: multixact.h:43
#define ISUPDATE_from_mxstatus(status)
Definition: multixact.h:56
#define InvalidMultiXactId
Definition: multixact.h:24
#define XLOG_MULTIXACT_TRUNCATE_ID
Definition: multixact.h:75
#define SizeOfMultiXactCreate
Definition: multixact.h:85
#define SizeOfMultiXactTruncate
Definition: multixact.h:100
#define XLOG_MULTIXACT_CREATE_ID
Definition: multixact.h:74
#define MaxMultiXactOffset
Definition: multixact.h:30
#define MaxMultiXactId
Definition: multixact.h:26
#define NUM_MULTIXACTMEMBER_BUFFERS
Definition: multixact.h:34
struct MultiXactMember MultiXactMember
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
const void size_t len
const void * data
while(p+4<=pend)
static char * filename
Definition: pg_dumpall.c:121
static char * buf
Definition: pg_test_fsync.c:73
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:181
@ PMSIGNAL_START_AUTOVAC_LAUNCHER
Definition: pmsignal.h:38
#define qsort(a, b, c, d)
Definition: port.h:449
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
#define DELAY_CHKPT_START
Definition: proc.h:119
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1391
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:388
void pg_usleep(long microsec)
Definition: signal.c:53
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
Definition: slru.c:529
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:645
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1191
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
Definition: slru.c:215
bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int64 pageno)
Definition: slru.c:657
void SlruDeleteSegment(SlruCtl ctl, int64 segno)
Definition: slru.c:1362
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1613
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition: slru.c:429
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1653
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition: slru.c:307
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition: slru.c:1259
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:183
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition: slru.h:168
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:34
PGPROC * MyProc
Definition: proc.c:68
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Definition: sync.h:51
void * user_fctx
Definition: funcapi.h:82
AttInMetadata * attinmeta
Definition: funcapi.h:91
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
TupleDesc tuple_desc
Definition: funcapi.h:112
TransactionId xid
Definition: multixact.h:62
MultiXactStatus status
Definition: multixact.h:63
MultiXactId multiWrapLimit
Definition: multixact.c:231
MultiXactId multiStopLimit
Definition: multixact.c:230
MultiXactId multiWarnLimit
Definition: multixact.c:229
MultiXactId multiVacLimit
Definition: multixact.c:228
MultiXactOffset offsetStopLimit
Definition: multixact.c:234
MultiXactOffset nextOffset
Definition: multixact.c:206
MultiXactId nextMXact
Definition: multixact.c:203
MultiXactId oldestMultiXactId
Definition: multixact.c:216
MultiXactId perBackendXactIds[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.c:283
MultiXactOffset oldestOffset
Definition: multixact.c:224
int delayChkptFlags
Definition: proc.h:226
bool(* PagePrecedes)(int64, int64)
Definition: slru.h:133
dlist_node * cur
Definition: ilist.h:179
MultiXactId multi
Definition: multixact.c:317
dlist_node node
Definition: multixact.c:319
MultiXactMember members[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.c:320
int64 earliestExistingPage
Definition: multixact.c:2864
MultiXactId mid
Definition: multixact.h:79
MultiXactMember members[FLEXIBLE_ARRAY_MEMBER]
Definition: multixact.h:82
MultiXactOffset moff
Definition: multixact.h:80
MultiXactId endTruncOff
Definition: multixact.h:93
MultiXactOffset startTruncMemb
Definition: multixact.h:96
MultiXactOffset endTruncMemb
Definition: multixact.h:97
MultiXactId startTruncOff
Definition: multixact.h:92
@ SYNC_HANDLER_MULTIXACT_MEMBER
Definition: sync.h:41
@ SYNC_HANDLER_MULTIXACT_OFFSET
Definition: sync.h:40
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:126
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void RegisterTwoPhaseRecord(TwoPhaseRmgrId rmid, uint16 info, const void *data, uint32 len)
Definition: twophase.c:1295
BackendId TwoPhaseGetDummyBackendId(TransactionId xid, bool lock_held)
Definition: twophase.c:918
#define TWOPHASE_RM_MULTIXACT_ID
Definition: twophase_rmgr.h:27
void AdvanceNextFullTransactionIdPastXid(TransactionId xid)
Definition: varsup.c:304
bool IsTransactionState(void)
Definition: xact.c:378
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:926
bool RecoveryInProgress(void)
Definition: xlog.c:6211
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2733
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:365
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:475
void XLogBeginInsert(void)
Definition: xloginsert.c:150
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:412
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:417
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
bool InRecovery
Definition: xlogutils.c:53