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