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