Age Owner Branch data TLA Line data Source code
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-2026, 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/multixact_internal.h"
73 : : #include "access/slru.h"
74 : : #include "access/twophase.h"
75 : : #include "access/twophase_rmgr.h"
76 : : #include "access/xlog.h"
77 : : #include "access/xloginsert.h"
78 : : #include "access/xlogutils.h"
79 : : #include "miscadmin.h"
80 : : #include "pg_trace.h"
81 : : #include "pgstat.h"
82 : : #include "postmaster/autovacuum.h"
83 : : #include "storage/pmsignal.h"
84 : : #include "storage/proc.h"
85 : : #include "storage/procarray.h"
86 : : #include "storage/subsystems.h"
87 : : #include "utils/guc_hooks.h"
88 : : #include "utils/injection_point.h"
89 : : #include "utils/lsyscache.h"
90 : : #include "utils/memutils.h"
91 : :
92 : :
93 : : /*
94 : : * Thresholds used to keep members disk usage in check when multixids have a
95 : : * lot of members. When MULTIXACT_MEMBER_LOW_THRESHOLD is reached, vacuum
96 : : * starts freezing multixids more aggressively, even if the normal multixid
97 : : * age limits haven't been reached yet.
98 : : */
99 : : #define MULTIXACT_MEMBER_LOW_THRESHOLD UINT64CONST(2000000000)
100 : : #define MULTIXACT_MEMBER_HIGH_THRESHOLD UINT64CONST(4000000000)
101 : :
102 : : static inline MultiXactId
328 heikki.linnakangas@i 103 :CBC 107717 : NextMultiXactId(MultiXactId multi)
104 : : {
105 [ + + ]: 107717 : return multi == MaxMultiXactId ? FirstMultiXactId : multi + 1;
106 : : }
107 : :
108 : : static inline MultiXactId
872 heikki.linnakangas@i 109 :UBC 0 : PreviousMultiXactId(MultiXactId multi)
110 : : {
111 [ # # ]: 0 : return multi == FirstMultiXactId ? MaxMultiXactId : multi - 1;
112 : : }
113 : :
114 : : /*
115 : : * Links to shared-memory data structures for MultiXact control
116 : : */
117 : : static bool MultiXactOffsetPagePrecedes(int64 page1, int64 page2);
118 : : static int MultiXactOffsetIoErrorDetail(const void *opaque_data);
119 : : static bool MultiXactMemberPagePrecedes(int64 page1, int64 page2);
120 : : static int MultiXactMemberIoErrorDetail(const void *opaque_data);
121 : :
122 : : static SlruDesc MultiXactOffsetSlruDesc;
123 : : static SlruDesc MultiXactMemberSlruDesc;
124 : :
125 : : #define MultiXactOffsetCtl (&MultiXactOffsetSlruDesc)
126 : : #define MultiXactMemberCtl (&MultiXactMemberSlruDesc)
127 : :
128 : : /*
129 : : * MultiXact state shared across all backends. All this state is protected
130 : : * by MultiXactGenLock. (We also use SLRU bank's lock of MultiXactOffset and
131 : : * MultiXactMember to guard accesses to the two sets of SLRU buffers. For
132 : : * concurrency's sake, we avoid holding more than one of these locks at a
133 : : * time.)
134 : : */
135 : : typedef struct MultiXactStateData
136 : : {
137 : : /* next-to-be-assigned MultiXactId */
138 : : MultiXactId nextMXact;
139 : :
140 : : /* next-to-be-assigned offset */
141 : : MultiXactOffset nextOffset;
142 : :
143 : : /* Have we completed multixact startup? */
144 : : bool finishedStartup;
145 : :
146 : : /*
147 : : * Oldest multixact that is still potentially referenced by a relation.
148 : : * Anything older than this should not be consulted. These values are
149 : : * updated by vacuum.
150 : : */
151 : : MultiXactId oldestMultiXactId;
152 : : Oid oldestMultiXactDB;
153 : :
154 : : /*
155 : : * Oldest multixact offset that is potentially referenced by a multixact
156 : : * referenced by a relation.
157 : : */
158 : : MultiXactOffset oldestOffset;
159 : :
160 : : /* support for anti-wraparound measures */
161 : : MultiXactId multiVacLimit;
162 : : MultiXactId multiWarnLimit;
163 : : MultiXactId multiStopLimit;
164 : : MultiXactId multiWrapLimit;
165 : :
166 : : /*
167 : : * Per-backend data starts here. We have two arrays stored in the area
168 : : * immediately following the MultiXactStateData struct:
169 : : *
170 : : * OldestMemberMXactId[k] is the oldest MultiXactId each backend's current
171 : : * transaction(s) could possibly be a member of, or InvalidMultiXactId
172 : : * when the backend has no live transaction that could possibly be a
173 : : * member of a MultiXact. Each backend sets its entry to the current
174 : : * nextMXact counter just before first acquiring a shared lock in a given
175 : : * transaction, and clears it at transaction end. (This works because only
176 : : * during or after acquiring a shared lock could an XID possibly become a
177 : : * member of a MultiXact, and that MultiXact would have to be created
178 : : * during or after the lock acquisition.)
179 : : *
180 : : * In the OldestMemberMXactId array, there's a slot for all normal
181 : : * backends (0..MaxBackends-1) followed by a slot for max_prepared_xacts
182 : : * prepared transactions.
183 : : *
184 : : * OldestVisibleMXactId[k] is the oldest MultiXactId each backend's
185 : : * current transaction(s) think is potentially live, or InvalidMultiXactId
186 : : * when not in a transaction or not in a transaction that's paid any
187 : : * attention to MultiXacts yet. This is computed when first needed in a
188 : : * given transaction, and cleared at transaction end. We can compute it
189 : : * as the minimum of the valid OldestMemberMXactId[] entries at the time
190 : : * we compute it (using nextMXact if none are valid). Each backend is
191 : : * required not to attempt to access any SLRU data for MultiXactIds older
192 : : * than its own OldestVisibleMXactId[] setting; this is necessary because
193 : : * the relevant SLRU data can be concurrently truncated away.
194 : : *
195 : : * In the OldestVisibleMXactId array, there's a slot for all normal
196 : : * backends (0..MaxBackends-1) only. No slots for prepared transactions.
197 : : *
198 : : * The oldest valid value among all of the OldestMemberMXactId[] and
199 : : * OldestVisibleMXactId[] entries is considered by vacuum as the earliest
200 : : * possible value still having any live member transaction -- OldestMxact.
201 : : * Any value older than that is typically removed from tuple headers, or
202 : : * "frozen" via being replaced with a new xmax. VACUUM can sometimes even
203 : : * remove an individual MultiXact xmax whose value is >= its OldestMxact
204 : : * cutoff, though typically only when no individual member XID is still
205 : : * running. See FreezeMultiXactId for full details.
206 : : *
207 : : * Whenever VACUUM advances relminmxid, then either its OldestMxact cutoff
208 : : * or the oldest extant Multi remaining in the table is used as the new
209 : : * pg_class.relminmxid value (whichever is earlier). The minimum of all
210 : : * relminmxid values in each database is stored in pg_database.datminmxid.
211 : : * In turn, the minimum of all of those values is stored in pg_control.
212 : : * This is used as the truncation point for pg_multixact when unneeded
213 : : * segments get removed by vac_truncate_clog() during vacuuming.
214 : : */
215 : : MultiXactId perBackendXactIds[FLEXIBLE_ARRAY_MEMBER];
216 : : } MultiXactStateData;
217 : :
218 : : /*
219 : : * Sizes of OldestMemberMXactId and OldestVisibleMXactId arrays.
220 : : */
221 : : #define NumMemberSlots (MaxBackends + max_prepared_xacts)
222 : : #define NumVisibleSlots MaxBackends
223 : :
224 : : /* Pointers to the state data in shared memory */
225 : : static MultiXactStateData *MultiXactState;
226 : : static MultiXactId *OldestMemberMXactId;
227 : : static MultiXactId *OldestVisibleMXactId;
228 : :
229 : : static void MultiXactShmemRequest(void *arg);
230 : : static void MultiXactShmemInit(void *arg);
231 : : static void MultiXactShmemAttach(void *arg);
232 : :
233 : : const ShmemCallbacks MultiXactShmemCallbacks = {
234 : : .request_fn = MultiXactShmemRequest,
235 : : .init_fn = MultiXactShmemInit,
236 : : .attach_fn = MultiXactShmemAttach,
237 : : };
238 : :
239 : : static inline MultiXactId *
248 heikki.linnakangas@i 240 :CBC 5001889 : MyOldestMemberMXactIdSlot(void)
241 : : {
242 : : /*
243 : : * The first MaxBackends entries in the OldestMemberMXactId array are
244 : : * reserved for regular backends. MyProcNumber should index into one of
245 : : * them.
246 : : */
247 [ + - - + ]: 5001889 : Assert(MyProcNumber >= 0 && MyProcNumber < MaxBackends);
248 : 5001889 : return &OldestMemberMXactId[MyProcNumber];
249 : : }
250 : :
251 : : static inline MultiXactId *
252 : 156 : PreparedXactOldestMemberMXactIdSlot(ProcNumber procno)
253 : : {
254 : : int prepared_xact_idx;
255 : :
256 [ - + ]: 156 : Assert(procno >= FIRST_PREPARED_XACT_PROC_NUMBER);
257 : 156 : prepared_xact_idx = procno - FIRST_PREPARED_XACT_PROC_NUMBER;
258 : :
259 : : /*
260 : : * The first MaxBackends entries in the OldestMemberMXactId array are
261 : : * reserved for regular backends. Prepared xacts come after them.
262 : : */
263 [ - + ]: 156 : Assert(MaxBackends + prepared_xact_idx < NumMemberSlots);
264 : 156 : return &OldestMemberMXactId[MaxBackends + prepared_xact_idx];
265 : : }
266 : :
267 : : static inline MultiXactId *
268 : 527866 : MyOldestVisibleMXactIdSlot(void)
269 : : {
270 [ + - - + ]: 527866 : Assert(MyProcNumber >= 0 && MyProcNumber < NumVisibleSlots);
271 : 527866 : return &OldestVisibleMXactId[MyProcNumber];
272 : : }
273 : :
274 : : /*
275 : : * Definitions for the backend-local MultiXactId cache.
276 : : *
277 : : * We use this cache to store known MultiXacts, so we don't need to go to
278 : : * SLRU areas every time.
279 : : *
280 : : * The cache lasts for the duration of a single transaction, the rationale
281 : : * for this being that most entries will contain our own TransactionId and
282 : : * so they will be uninteresting by the time our next transaction starts.
283 : : * (XXX not clear that this is correct --- other members of the MultiXact
284 : : * could hang around longer than we did. However, it's not clear what a
285 : : * better policy for flushing old cache entries would be.) FIXME actually
286 : : * this is plain wrong now that multixact's may contain update Xids.
287 : : *
288 : : * We allocate the cache entries in a memory context that is deleted at
289 : : * transaction end, so we don't need to do retail freeing of entries.
290 : : */
291 : : typedef struct mXactCacheEnt
292 : : {
293 : : MultiXactId multi;
294 : : int nmembers;
295 : : dlist_node node;
296 : : pg_attribute_counted_by(nmembers) MultiXactMember members[FLEXIBLE_ARRAY_MEMBER];
297 : : } mXactCacheEnt;
298 : :
299 : : #define MAX_CACHE_ENTRIES 256
300 : : static dclist_head MXactCache = DCLIST_STATIC_INIT(MXactCache);
301 : : static MemoryContext MXactContext = NULL;
302 : :
303 : : #ifdef MULTIXACT_DEBUG
304 : : #define debug_elog2(a,b) elog(a,b)
305 : : #define debug_elog3(a,b,c) elog(a,b,c)
306 : : #define debug_elog4(a,b,c,d) elog(a,b,c,d)
307 : : #define debug_elog5(a,b,c,d,e) elog(a,b,c,d,e)
308 : : #define debug_elog6(a,b,c,d,e,f) elog(a,b,c,d,e,f)
309 : : #else
310 : : #define debug_elog2(a,b)
311 : : #define debug_elog3(a,b,c)
312 : : #define debug_elog4(a,b,c,d)
313 : : #define debug_elog5(a,b,c,d,e)
314 : : #define debug_elog6(a,b,c,d,e,f)
315 : : #endif
316 : :
317 : : /* internal MultiXactId management */
318 : : static void MultiXactIdSetOldestVisible(void);
319 : : static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
320 : : int nmembers, MultiXactMember *members);
321 : : static MultiXactId GetNewMultiXactId(int nmembers, MultiXactOffset *offset);
322 : :
323 : : /* MultiXact cache management */
324 : : static int mxactMemberComparator(const void *arg1, const void *arg2);
325 : : static MultiXactId mXactCacheGetBySet(int nmembers, MultiXactMember *members);
326 : : static int mXactCacheGetById(MultiXactId multi, MultiXactMember **members);
327 : : static void mXactCachePut(MultiXactId multi, int nmembers,
328 : : MultiXactMember *members);
329 : :
330 : : /* management of SLRU infrastructure */
331 : :
332 : : /* opaque_data type for MultiXactMemberIoErrorDetail */
333 : : typedef struct MultiXactMemberSlruReadContext
334 : : {
335 : : MultiXactId multi;
336 : : MultiXactOffset offset;
337 : : } MultiXactMemberSlruReadContext;
338 : :
339 : : static void ExtendMultiXactOffset(MultiXactId multi);
340 : : static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers);
341 : : static void SetOldestOffset(void);
342 : : static bool find_multixact_start(MultiXactId multi, MultiXactOffset *result);
343 : : static void WriteMTruncateXlogRec(Oid oldestMultiDB,
344 : : MultiXactId oldestMulti,
345 : : MultiXactOffset oldestOffset);
346 : :
347 : :
348 : : /*
349 : : * MultiXactIdCreate
350 : : * Construct a MultiXactId representing two TransactionIds.
351 : : *
352 : : * The two XIDs must be different, or be requesting different statuses.
353 : : *
354 : : * NB - we don't worry about our local MultiXactId cache here, because that
355 : : * is handled by the lower-level routines.
356 : : */
357 : : MultiXactId
5034 alvherre@alvh.no-ip. 358 : 1129 : MultiXactIdCreate(TransactionId xid1, MultiXactStatus status1,
359 : : TransactionId xid2, MultiXactStatus status2)
360 : : {
361 : : MultiXactId newMulti;
362 : : MultiXactMember members[2];
363 : :
1469 peter@eisentraut.org 364 [ - + ]: 1129 : Assert(TransactionIdIsValid(xid1));
365 [ - + ]: 1129 : Assert(TransactionIdIsValid(xid2));
366 : :
5034 alvherre@alvh.no-ip. 367 [ + + - + ]: 1129 : Assert(!TransactionIdEquals(xid1, xid2) || (status1 != status2));
368 : :
369 : : /* MultiXactIdSetOldestMember() must have been called already. */
248 heikki.linnakangas@i 370 [ - + ]: 1129 : Assert(MultiXactIdIsValid(*MyOldestMemberMXactIdSlot()));
371 : :
372 : : /*
373 : : * Note: unlike MultiXactIdExpand, we don't bother to check that both XIDs
374 : : * are still running. In typical usage, xid2 will be our own XID and the
375 : : * caller just did a check on xid1, so it'd be wasted effort.
376 : : */
377 : :
5034 alvherre@alvh.no-ip. 378 : 1129 : members[0].xid = xid1;
379 : 1129 : members[0].status = status1;
380 : 1129 : members[1].xid = xid2;
381 : 1129 : members[1].status = status2;
382 : :
4707 383 : 1129 : newMulti = MultiXactIdCreateFromMembers(2, members);
384 : :
385 : : debug_elog3(DEBUG2, "Create: %s",
386 : : mxid_to_string(newMulti, 2, members));
387 : :
7856 tgl@sss.pgh.pa.us 388 : 1129 : return newMulti;
389 : : }
390 : :
391 : : /*
392 : : * MultiXactIdExpand
393 : : * Add a TransactionId to a pre-existing MultiXactId.
394 : : *
395 : : * If the TransactionId is already a member of the passed MultiXactId with the
396 : : * same status, just return it as-is.
397 : : *
398 : : * Note that we do NOT actually modify the membership of a pre-existing
399 : : * MultiXactId; instead we create a new one. This is necessary to avoid
400 : : * a race condition against code trying to wait for one MultiXactId to finish;
401 : : * see notes in heapam.c.
402 : : *
403 : : * NB - we don't worry about our local MultiXactId cache here, because that
404 : : * is handled by the lower-level routines.
405 : : *
406 : : * Note: It is critical that MultiXactIds that come from an old cluster (i.e.
407 : : * one upgraded by pg_upgrade from a cluster older than this feature) are not
408 : : * passed in.
409 : : */
410 : : MultiXactId
5034 alvherre@alvh.no-ip. 411 : 75539 : MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status)
412 : : {
413 : : MultiXactId newMulti;
414 : : MultiXactMember *members;
415 : : MultiXactMember *newMembers;
416 : : int nmembers;
417 : : int i;
418 : : int j;
419 : :
1469 peter@eisentraut.org 420 [ - + ]: 75539 : Assert(MultiXactIdIsValid(multi));
421 [ - + ]: 75539 : Assert(TransactionIdIsValid(xid));
422 : :
423 : : /* MultiXactIdSetOldestMember() must have been called already. */
248 heikki.linnakangas@i 424 [ - + ]: 75539 : Assert(MultiXactIdIsValid(*MyOldestMemberMXactIdSlot()));
425 : :
426 : : debug_elog5(DEBUG2, "Expand: received multi %u, xid %u status %s",
427 : : multi, xid, mxstatus_to_string(status));
428 : :
429 : : /*
430 : : * Note: we don't allow for old multis here. The reason is that the only
431 : : * caller of this function does a check that the multixact is no longer
432 : : * running.
433 : : */
4482 alvherre@alvh.no-ip. 434 : 75539 : nmembers = GetMultiXactIdMembers(multi, &members, false, false);
435 : :
7861 tgl@sss.pgh.pa.us 436 [ - + ]: 75539 : if (nmembers < 0)
437 : : {
438 : : MultiXactMember member;
439 : :
440 : : /*
441 : : * The MultiXactId is obsolete. This can only happen if all the
442 : : * MultiXactId members stop running between the caller checking and
443 : : * passing it to us. It would be better to return that fact to the
444 : : * caller, but it would complicate the API and it's unlikely to happen
445 : : * too often, so just deal with it by creating a singleton MultiXact.
446 : : */
5034 alvherre@alvh.no-ip. 447 :UBC 0 : member.xid = xid;
448 : 0 : member.status = status;
4707 449 : 0 : newMulti = MultiXactIdCreateFromMembers(1, &member);
450 : :
451 : : debug_elog4(DEBUG2, "Expand: %u has no members, create singleton %u",
452 : : multi, newMulti);
7861 tgl@sss.pgh.pa.us 453 : 0 : return newMulti;
454 : : }
455 : :
456 : : /*
457 : : * If the TransactionId is already a member of the MultiXactId with the
458 : : * same status, just return the existing MultiXactId.
459 : : */
7861 tgl@sss.pgh.pa.us 460 [ + + ]:CBC 1465939 : for (i = 0; i < nmembers; i++)
461 : : {
5034 alvherre@alvh.no-ip. 462 [ + + ]: 1390400 : if (TransactionIdEquals(members[i].xid, xid) &&
463 [ - + ]: 54 : (members[i].status == status))
464 : : {
465 : : debug_elog4(DEBUG2, "Expand: %u is already a member of %u",
466 : : xid, multi);
7852 tgl@sss.pgh.pa.us 467 :UBC 0 : pfree(members);
7861 468 : 0 : return multi;
469 : : }
470 : : }
471 : :
472 : : /*
473 : : * Determine which of the members of the MultiXactId are still of
474 : : * interest. This is any running transaction, and also any transaction
475 : : * that grabbed something stronger than just a lock and was committed. (An
476 : : * update that aborted is of no interest here; and having more than one
477 : : * update Xid in a multixact would cause errors elsewhere.)
478 : : *
479 : : * Removing dead members is not just an optimization: freezing of tuples
480 : : * whose Xmax are multis depends on this behavior.
481 : : *
482 : : * Note we have the same race condition here as above: j could be 0 at the
483 : : * end of the loop.
484 : : */
330 michael@paquier.xyz 485 :CBC 75539 : newMembers = palloc_array(MultiXactMember, nmembers + 1);
486 : :
7861 tgl@sss.pgh.pa.us 487 [ + + ]: 1465939 : for (i = 0, j = 0; i < nmembers; i++)
488 : : {
5034 alvherre@alvh.no-ip. 489 [ + + ]: 1390400 : if (TransactionIdIsInProgress(members[i].xid) ||
4578 490 [ + + - + ]: 74691 : (ISUPDATE_from_mxstatus(members[i].status) &&
5034 491 : 19 : TransactionIdDidCommit(members[i].xid)))
492 : : {
493 : 1315728 : newMembers[j].xid = members[i].xid;
494 : 1315728 : newMembers[j++].status = members[i].status;
495 : : }
496 : : }
497 : :
498 : 75539 : newMembers[j].xid = xid;
499 : 75539 : newMembers[j++].status = status;
4707 500 : 75539 : newMulti = MultiXactIdCreateFromMembers(j, newMembers);
501 : :
7861 tgl@sss.pgh.pa.us 502 : 75539 : pfree(members);
503 : 75539 : pfree(newMembers);
504 : :
505 : : debug_elog3(DEBUG2, "Expand: returning new multi %u", newMulti);
506 : :
507 : 75539 : return newMulti;
508 : : }
509 : :
510 : : /*
511 : : * MultiXactIdIsRunning
512 : : * Returns whether a MultiXactId is "running".
513 : : *
514 : : * We return true if at least one member of the given MultiXactId is still
515 : : * running. Note that a "false" result is certain not to change,
516 : : * because it is not legal to add members to an existing MultiXactId.
517 : : *
518 : : * Caller is expected to have verified that the multixact does not come from
519 : : * a pg_upgraded share-locked tuple.
520 : : */
521 : : bool
4482 alvherre@alvh.no-ip. 522 : 149855 : MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly)
523 : : {
524 : : MultiXactMember *members;
525 : : int nmembers;
526 : : int i;
527 : :
528 : : debug_elog3(DEBUG2, "IsRunning %u?", multi);
529 : :
530 : : /*
531 : : * "false" here means we assume our callers have checked that the given
532 : : * multi cannot possibly come from a pg_upgraded database.
533 : : */
534 : 149855 : nmembers = GetMultiXactIdMembers(multi, &members, false, isLockOnly);
535 : :
4227 536 [ + + ]: 149855 : if (nmembers <= 0)
537 : : {
538 : : debug_elog2(DEBUG2, "IsRunning: no members");
7861 tgl@sss.pgh.pa.us 539 : 738 : return false;
540 : : }
541 : :
542 : : /*
543 : : * Checking for myself is cheap compared to looking in shared memory;
544 : : * return true if any live subtransaction of the current top-level
545 : : * transaction is a member.
546 : : *
547 : : * This is not needed for correctness, it's just a fast path.
548 : : */
549 [ + + ]: 2891514 : for (i = 0; i < nmembers; i++)
550 : : {
5034 alvherre@alvh.no-ip. 551 [ + + ]: 2742555 : if (TransactionIdIsCurrentTransactionId(members[i].xid))
552 : : {
553 : : debug_elog3(DEBUG2, "IsRunning: I (%d) am running!", i);
7852 tgl@sss.pgh.pa.us 554 : 158 : pfree(members);
7861 555 : 158 : return true;
556 : : }
557 : : }
558 : :
559 : : /*
560 : : * This could be made faster by having another entry point in procarray.c,
561 : : * walking the PGPROC array only once for all the members. But in most
562 : : * cases nmembers should be small enough that it doesn't much matter.
563 : : */
564 [ + + ]: 296168 : for (i = 0; i < nmembers; i++)
565 : : {
5034 alvherre@alvh.no-ip. 566 [ + + ]: 296124 : if (TransactionIdIsInProgress(members[i].xid))
567 : : {
568 : : debug_elog4(DEBUG2, "IsRunning: member %d (%u) is running",
569 : : i, members[i].xid);
7852 tgl@sss.pgh.pa.us 570 : 148915 : pfree(members);
7861 571 : 148915 : return true;
572 : : }
573 : : }
574 : :
575 : 44 : pfree(members);
576 : :
577 : : debug_elog3(DEBUG2, "IsRunning: %u is not running", multi);
578 : :
579 : 44 : return false;
580 : : }
581 : :
582 : : /*
583 : : * MultiXactIdSetOldestMember
584 : : * Save the oldest MultiXactId this transaction could be a member of.
585 : : *
586 : : * We set the OldestMemberMXactId for a given transaction the first time it's
587 : : * going to do some operation that might require a MultiXactId (tuple lock,
588 : : * update or delete). We need to do this even if we end up using a
589 : : * TransactionId instead of a MultiXactId, because there is a chance that
590 : : * another transaction would add our XID to a MultiXactId.
591 : : *
592 : : * The value to set is the next-to-be-assigned MultiXactId, so this is meant to
593 : : * be called just before doing any such possibly-MultiXactId-able operation.
594 : : */
595 : : void
596 : 4408291 : MultiXactIdSetOldestMember(void)
597 : : {
248 heikki.linnakangas@i 598 [ + + ]: 4408291 : if (!MultiXactIdIsValid(*MyOldestMemberMXactIdSlot()))
599 : : {
600 : : MultiXactId nextMXact;
601 : :
602 : : /*
603 : : * You might think we don't need to acquire a lock here, since
604 : : * fetching and storing of TransactionIds is probably atomic, but in
605 : : * fact we do: suppose we pick up nextMXact and then lose the CPU for
606 : : * a long time. Someone else could advance nextMXact, and then
607 : : * another someone else could compute an OldestVisibleMXactId that
608 : : * would be after the value we are going to store when we get control
609 : : * back. Which would be wrong.
610 : : *
611 : : * Note that a shared lock is sufficient, because it's enough to stop
612 : : * someone from advancing nextMXact; and nobody else could be trying
613 : : * to write to our OldestMember entry, only reading (and we assume
614 : : * storing it is atomic.)
615 : : */
4690 alvherre@alvh.no-ip. 616 : 88123 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
617 : :
7861 tgl@sss.pgh.pa.us 618 : 88123 : nextMXact = MultiXactState->nextMXact;
619 : :
248 heikki.linnakangas@i 620 : 88123 : *MyOldestMemberMXactIdSlot() = nextMXact;
621 : :
7861 tgl@sss.pgh.pa.us 622 : 88123 : LWLockRelease(MultiXactGenLock);
623 : :
624 : : debug_elog4(DEBUG2, "MultiXact: setting OldestMember[%d] = %u",
625 : : MyProcNumber, nextMXact);
626 : : }
627 : 4408291 : }
628 : :
629 : : /*
630 : : * MultiXactIdSetOldestVisible
631 : : * Save the oldest MultiXactId this transaction considers possibly live.
632 : : *
633 : : * We set the OldestVisibleMXactId for a given transaction the first time
634 : : * it's going to inspect any MultiXactId. Once we have set this, we are
635 : : * guaranteed that SLRU data for MultiXactIds >= our own OldestVisibleMXactId
636 : : * won't be truncated away.
637 : : *
638 : : * The value to set is the oldest of nextMXact and all the valid per-backend
639 : : * OldestMemberMXactId[] entries. Because of the locking we do, we can be
640 : : * certain that no subsequent call to MultiXactIdSetOldestMember can set
641 : : * an OldestMemberMXactId[] entry older than what we compute here. Therefore
642 : : * there is no live transaction, now or later, that can be a member of any
643 : : * MultiXactId older than the OldestVisibleMXactId we compute here.
644 : : */
645 : : static void
646 : 92499 : MultiXactIdSetOldestVisible(void)
647 : : {
248 heikki.linnakangas@i 648 [ + + ]: 92499 : if (!MultiXactIdIsValid(*MyOldestVisibleMXactIdSlot()))
649 : : {
650 : : MultiXactId oldestMXact;
651 : : int i;
652 : :
7861 tgl@sss.pgh.pa.us 653 : 3226 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
654 : :
655 : 3226 : oldestMXact = MultiXactState->nextMXact;
248 heikki.linnakangas@i 656 [ + + ]: 411848 : for (i = 0; i < NumMemberSlots; i++)
657 : : {
7861 tgl@sss.pgh.pa.us 658 : 408622 : MultiXactId thisoldest = OldestMemberMXactId[i];
659 : :
660 [ + + + + ]: 464555 : if (MultiXactIdIsValid(thisoldest) &&
661 : 55933 : MultiXactIdPrecedes(thisoldest, oldestMXact))
662 : 5684 : oldestMXact = thisoldest;
663 : : }
664 : :
248 heikki.linnakangas@i 665 : 3226 : *MyOldestVisibleMXactIdSlot() = oldestMXact;
666 : :
7861 tgl@sss.pgh.pa.us 667 : 3226 : LWLockRelease(MultiXactGenLock);
668 : :
669 : : debug_elog4(DEBUG2, "MultiXact: setting OldestVisible[%d] = %u",
670 : : MyProcNumber, oldestMXact);
671 : : }
672 : 92499 : }
673 : :
674 : : /*
675 : : * ReadNextMultiXactId
676 : : * Return the next MultiXactId to be assigned, but don't allocate it
677 : : */
678 : : MultiXactId
5034 alvherre@alvh.no-ip. 679 : 37772 : ReadNextMultiXactId(void)
680 : : {
681 : : MultiXactId mxid;
682 : :
683 : : /* XXX we could presumably do this without a lock. */
684 : 37772 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
685 : 37772 : mxid = MultiXactState->nextMXact;
686 : 37772 : LWLockRelease(MultiXactGenLock);
687 : :
688 : 37772 : return mxid;
689 : : }
690 : :
691 : : /*
692 : : * ReadMultiXactIdRange
693 : : * Get the range of IDs that may still be referenced by a relation.
694 : : */
695 : : void
2205 rhaas@postgresql.org 696 : 1478 : ReadMultiXactIdRange(MultiXactId *oldest, MultiXactId *next)
697 : : {
698 : 1478 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
699 : 1478 : *oldest = MultiXactState->oldestMultiXactId;
700 : 1478 : *next = MultiXactState->nextMXact;
701 : 1478 : LWLockRelease(MultiXactGenLock);
702 : 1478 : }
703 : :
704 : :
705 : : /*
706 : : * MultiXactIdCreateFromMembers
707 : : * Make a new MultiXactId from the specified set of members
708 : : *
709 : : * Make XLOG, SLRU and cache entries for a new MultiXactId, recording the
710 : : * given TransactionIds as members. Returns the newly created MultiXactId.
711 : : *
712 : : * NB: the passed members[] array will be sorted in-place.
713 : : */
714 : : MultiXactId
4707 alvherre@alvh.no-ip. 715 : 76669 : MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
716 : : {
717 : : MultiXactId multi;
718 : : MultiXactOffset offset;
719 : : xl_multixact_create xlrec;
720 : :
721 : : debug_elog3(DEBUG2, "Create: %s",
722 : : mxid_to_string(InvalidMultiXactId, nmembers, members));
723 : :
724 : : /*
725 : : * See if the same set of members already exists in our cache; if so, just
726 : : * re-use that MultiXactId. (Note: it might seem that looking in our
727 : : * cache is insufficient, and we ought to search disk to see if a
728 : : * duplicate definition already exists. But since we only ever create
729 : : * MultiXacts containing our own XID, in most cases any such MultiXacts
730 : : * were in fact created by us, and so will be in our cache. There are
731 : : * corner cases where someone else added us to a MultiXact without our
732 : : * knowledge, but it's not worth checking for.)
733 : : */
5034 734 : 76669 : multi = mXactCacheGetBySet(nmembers, members);
7861 tgl@sss.pgh.pa.us 735 [ + + ]: 76669 : if (MultiXactIdIsValid(multi))
736 : : {
737 : : debug_elog2(DEBUG2, "Create: in cache!");
738 : 71354 : return multi;
739 : : }
740 : :
741 : : /* Verify that there is a single update Xid among the given members. */
742 : : {
743 : : int i;
4578 alvherre@alvh.no-ip. 744 : 5315 : bool has_update = false;
745 : :
746 [ + + ]: 100060 : for (i = 0; i < nmembers; i++)
747 : : {
748 [ + + ]: 94745 : if (ISUPDATE_from_mxstatus(members[i].status))
749 : : {
750 [ - + ]: 2383 : if (has_update)
1442 alvherre@alvh.no-ip. 751 [ # # ]:UBC 0 : elog(ERROR, "new multixact has more than one updating member: %s",
752 : : mxid_to_string(InvalidMultiXactId, nmembers, members));
4578 alvherre@alvh.no-ip. 753 :CBC 2383 : has_update = true;
754 : : }
755 : : }
756 : : }
757 : :
758 : : /* Load the injection point before entering the critical section */
804 michael@paquier.xyz 759 : 5315 : INJECTION_POINT_LOAD("multixact-create-from-members");
760 : :
761 : : /*
762 : : * Assign the MXID and offsets range to use, and make sure there is space
763 : : * in the OFFSETs and MEMBERs files. NB: this routine does
764 : : * START_CRIT_SECTION().
765 : : *
766 : : * Note: unlike MultiXactIdCreate and MultiXactIdExpand, we do not check
767 : : * that we've called MultiXactIdSetOldestMember here. This is because
768 : : * this routine is used in some places to create new MultiXactIds of which
769 : : * the current backend is not a member, notably during freezing of multis
770 : : * in vacuum. During vacuum, in particular, it would be unacceptable to
771 : : * keep OldestMulti set, in case it runs for long.
772 : : */
5034 alvherre@alvh.no-ip. 773 : 5315 : multi = GetNewMultiXactId(nmembers, &offset);
774 : :
544 michael@paquier.xyz 775 : 5315 : INJECTION_POINT_CACHED("multixact-create-from-members", NULL);
776 : :
777 : : /*
778 : : * Prevent checkpoints from starting between the WAL record and the update
779 : : * of the OFFSETs and MEMBERs pages. Otherwise the checkpoint could flush
780 : : * the pages before they hold the new multixact, while recovery after a
781 : : * crash would begin after the WAL record, and the multixact would be lost
782 : : * even though data pages might already refer to it.
783 : : */
2 heikki.linnakangas@i 784 [ - + ]: 5315 : Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0);
785 : 5315 : MyProc->delayChkptFlags |= DELAY_CHKPT_START;
786 : :
787 : : /* Make an XLOG entry describing the new MXID. */
7820 tgl@sss.pgh.pa.us 788 : 5315 : xlrec.mid = multi;
789 : 5315 : xlrec.moff = offset;
5034 alvherre@alvh.no-ip. 790 : 5315 : xlrec.nmembers = nmembers;
791 : :
792 : : /*
793 : : * XXX Note: there's a lot of padding space in MultiXactMember. We could
794 : : * find a more compact representation of this Xlog record -- perhaps all
795 : : * the status flags in one XLogRecData, then all the xids in another one?
796 : : * Not clear that it's worth the trouble though.
797 : : */
4368 heikki.linnakangas@i 798 : 5315 : XLogBeginInsert();
632 peter@eisentraut.org 799 : 5315 : XLogRegisterData(&xlrec, SizeOfMultiXactCreate);
800 : 5315 : XLogRegisterData(members, nmembers * sizeof(MultiXactMember));
801 : :
4368 heikki.linnakangas@i 802 : 5315 : (void) XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_CREATE_ID);
803 : :
804 : : /* Now enter the information into the OFFSETs and MEMBERs logs */
5034 alvherre@alvh.no-ip. 805 : 5315 : RecordNewMultiXact(multi, offset, nmembers, members);
806 : :
2 heikki.linnakangas@i 807 : 5315 : MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
808 : :
809 : : /* Done with critical section */
7678 tgl@sss.pgh.pa.us 810 [ - + ]: 5315 : END_CRIT_SECTION();
811 : :
812 : : /* Store the new MultiXactId in the local cache, too */
5034 alvherre@alvh.no-ip. 813 : 5315 : mXactCachePut(multi, nmembers, members);
814 : :
815 : : debug_elog2(DEBUG2, "Create: all done");
816 : :
7820 tgl@sss.pgh.pa.us 817 : 5315 : return multi;
818 : : }
819 : :
820 : : /*
821 : : * RecordNewMultiXact
822 : : * Write info about a new multixact into the offsets and members files
823 : : *
824 : : * This is broken out of MultiXactIdCreateFromMembers so that xlog replay can
825 : : * use it.
826 : : */
827 : : static void
828 : 5321 : RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
829 : : int nmembers, MultiXactMember *members)
830 : : {
831 : : int64 pageno;
832 : : int64 prev_pageno;
833 : : int entryno;
834 : : int slotno;
835 : : MultiXactOffset *offptr;
836 : : MultiXactId next;
837 : : int64 next_pageno;
838 : : int next_entryno;
839 : : MultiXactOffset *next_offptr;
840 : : MultiXactOffset next_offset;
841 : : LWLock *lock;
981 alvherre@alvh.no-ip. 842 : 5321 : LWLock *prevlock = NULL;
843 : :
844 : : /* position of this multixid in the offsets SLRU area */
7861 tgl@sss.pgh.pa.us 845 : 5321 : pageno = MultiXactIdToOffsetPage(multi);
846 : 5321 : entryno = MultiXactIdToOffsetEntry(multi);
847 : :
848 : : /* position of the next multixid */
328 heikki.linnakangas@i 849 : 5321 : next = NextMultiXactId(multi);
337 850 : 5321 : next_pageno = MultiXactIdToOffsetPage(next);
851 : 5321 : next_entryno = MultiXactIdToOffsetEntry(next);
852 : :
853 : : /*
854 : : * Set the starting offset of this multixid's members.
855 : : *
856 : : * In the common case, it was already set by the previous
857 : : * RecordNewMultiXact call, as this was the next multixid of the previous
858 : : * multixid. But if multiple backends are generating multixids
859 : : * concurrently, we might race ahead and get called before the previous
860 : : * multixid.
861 : : */
981 alvherre@alvh.no-ip. 862 : 5321 : lock = SimpleLruGetBankLock(MultiXactOffsetCtl, pageno);
863 : 5321 : LWLockAcquire(lock, LW_EXCLUSIVE);
864 : :
237 heikki.linnakangas@i 865 : 5321 : slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, &multi);
7820 tgl@sss.pgh.pa.us 866 : 5321 : offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
7861 867 : 5321 : offptr += entryno;
868 : :
337 heikki.linnakangas@i 869 [ + + ]: 5321 : if (*offptr != offset)
870 : : {
871 : : /* should already be set to the correct value, or not at all */
872 [ - + ]: 1 : Assert(*offptr == 0);
873 : 1 : *offptr = offset;
874 : 1 : MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
875 : : }
876 : :
877 : : /*
878 : : * Set the next multixid's offset to the end of this multixid's members.
879 : : */
880 [ + + ]: 5321 : if (next_pageno == pageno)
881 : : {
882 : 5316 : next_offptr = offptr + 1;
883 : : }
884 : : else
885 : : {
886 : : /* must be the first entry on the page */
887 [ + + - + ]: 5 : Assert(next_entryno == 0 || next == FirstMultiXactId);
888 : :
889 : : /* Swap the lock for a lock on the next page */
890 : 5 : LWLockRelease(lock);
891 : 5 : lock = SimpleLruGetBankLock(MultiXactOffsetCtl, next_pageno);
892 : 5 : LWLockAcquire(lock, LW_EXCLUSIVE);
893 : :
237 894 : 5 : slotno = SimpleLruReadPage(MultiXactOffsetCtl, next_pageno, true, &next);
337 895 : 5 : next_offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
896 : 5 : next_offptr += next_entryno;
897 : : }
898 : :
899 : : /* Like in GetNewMultiXactId(), skip over offset 0 */
335 900 : 5321 : next_offset = offset + nmembers;
901 [ - + ]: 5321 : if (next_offset == 0)
335 heikki.linnakangas@i 902 :UBC 0 : next_offset = 1;
335 heikki.linnakangas@i 903 [ + - ]:CBC 5321 : if (*next_offptr != next_offset)
904 : : {
905 : : /* should already be set to the correct value, or not at all */
337 906 [ - + ]: 5321 : Assert(*next_offptr == 0);
335 907 : 5321 : *next_offptr = next_offset;
337 908 : 5321 : MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
909 : : }
910 : :
911 : : /* Release MultiXactOffset SLRU lock. */
981 alvherre@alvh.no-ip. 912 : 5321 : LWLockRelease(lock);
913 : :
7861 tgl@sss.pgh.pa.us 914 : 5321 : prev_pageno = -1;
915 : :
337 heikki.linnakangas@i 916 [ + + ]: 100078 : for (int i = 0; i < nmembers; i++, offset++)
917 : : {
918 : : TransactionId *memberptr;
919 : : uint32 *flagsptr;
920 : : uint32 flagsval;
921 : : int bshift;
922 : : int flagsoff;
923 : : int memberoff;
924 : :
5034 alvherre@alvh.no-ip. 925 [ - + ]: 94757 : Assert(members[i].status <= MultiXactStatusUpdate);
926 : :
7861 tgl@sss.pgh.pa.us 927 : 94757 : pageno = MXOffsetToMemberPage(offset);
5034 alvherre@alvh.no-ip. 928 : 94757 : memberoff = MXOffsetToMemberOffset(offset);
929 : 94757 : flagsoff = MXOffsetToFlagsOffset(offset);
930 : 94757 : bshift = MXOffsetToFlagsBitShift(offset);
931 : :
7861 tgl@sss.pgh.pa.us 932 [ + + ]: 94757 : if (pageno != prev_pageno)
933 : : {
237 heikki.linnakangas@i 934 : 5375 : MultiXactMemberSlruReadContext slru_read_context = {multi, offset};
935 : :
936 : : /*
937 : : * MultiXactMember SLRU page is changed so check if this new page
938 : : * fall into the different SLRU bank then release the old bank's
939 : : * lock and acquire lock on the new bank.
940 : : */
981 alvherre@alvh.no-ip. 941 : 5375 : lock = SimpleLruGetBankLock(MultiXactMemberCtl, pageno);
942 [ + - ]: 5375 : if (lock != prevlock)
943 : : {
944 [ + + ]: 5375 : if (prevlock != NULL)
945 : 54 : LWLockRelease(prevlock);
946 : :
947 : 5375 : LWLockAcquire(lock, LW_EXCLUSIVE);
948 : 5375 : prevlock = lock;
949 : : }
237 heikki.linnakangas@i 950 : 5375 : slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true,
951 : : &slru_read_context);
7861 tgl@sss.pgh.pa.us 952 : 5375 : prev_pageno = pageno;
953 : : }
954 : :
955 : 94757 : memberptr = (TransactionId *)
5034 alvherre@alvh.no-ip. 956 : 94757 : (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
957 : :
958 : 94757 : *memberptr = members[i].xid;
959 : :
960 : 94757 : flagsptr = (uint32 *)
961 : 94757 : (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
962 : :
963 : 94757 : flagsval = *flagsptr;
964 : 94757 : flagsval &= ~(((1 << MXACT_MEMBER_BITS_PER_XACT) - 1) << bshift);
965 : 94757 : flagsval |= (members[i].status << bshift);
966 : 94757 : *flagsptr = flagsval;
967 : :
7670 tgl@sss.pgh.pa.us 968 : 94757 : MultiXactMemberCtl->shared->page_dirty[slotno] = true;
969 : : }
970 : :
981 alvherre@alvh.no-ip. 971 [ + - ]: 5321 : if (prevlock != NULL)
972 : 5321 : LWLockRelease(prevlock);
7861 tgl@sss.pgh.pa.us 973 : 5321 : }
974 : :
975 : : /*
976 : : * GetNewMultiXactId
977 : : * Get the next MultiXactId.
978 : : *
979 : : * Also, reserve the needed amount of space in the "members" area. The
980 : : * starting offset of the reserved space is returned in *offset.
981 : : *
982 : : * This may generate XLOG records for expansion of the offsets and/or members
983 : : * files. Unfortunately, we have to do that while holding MultiXactGenLock
984 : : * to avoid race conditions --- the XLOG record for zeroing a page must appear
985 : : * before any backend can possibly try to store data in that page!
986 : : *
987 : : * We start a critical section before advancing the shared counters. The
988 : : * caller must end the critical section after writing SLRU data.
989 : : */
990 : : static MultiXactId
5034 alvherre@alvh.no-ip. 991 : 5315 : GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
992 : : {
993 : : MultiXactId result;
994 : : MultiXactOffset nextOffset;
995 : :
996 : : debug_elog3(DEBUG2, "GetNew: for %d xids", nmembers);
997 : :
998 : : /* safety check, we should never get this far in a HS standby */
999 [ - + ]: 5315 : if (RecoveryInProgress())
5034 alvherre@alvh.no-ip. 1000 [ # # ]:UBC 0 : elog(ERROR, "cannot assign MultiXactIds during recovery");
1001 : :
7861 tgl@sss.pgh.pa.us 1002 :CBC 5315 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1003 : :
1004 : : /* Assign the MXID */
1005 : 5315 : result = MultiXactState->nextMXact;
1006 : :
1007 : : /*----------
1008 : : * Check to see if it's safe to assign another MultiXactId. This protects
1009 : : * against catastrophic data loss due to multixact wraparound. The basic
1010 : : * rules are:
1011 : : *
1012 : : * If we're past multiVacLimit, start trying to force autovacuum cycles.
1013 : : * If we're past multiWarnLimit, start issuing warnings.
1014 : : * If we're past multiStopLimit, refuse to create new MultiXactIds.
1015 : : *
1016 : : * Note these are pretty much the same protections in GetNewTransactionId.
1017 : : *----------
1018 : : */
4155 andres@anarazel.de 1019 [ - + ]: 5315 : if (!MultiXactIdPrecedes(result, MultiXactState->multiVacLimit))
1020 : : {
1021 : : /*
1022 : : * For safety's sake, we release MultiXactGenLock while sending
1023 : : * signals, warnings, etc. This is not so much because we care about
1024 : : * preserving concurrency in this situation, as to avoid any
1025 : : * possibility of deadlock while doing get_database_name(). First,
1026 : : * copy all the shared values we'll need in this path.
1027 : : */
5034 alvherre@alvh.no-ip. 1028 :UBC 0 : MultiXactId multiWarnLimit = MultiXactState->multiWarnLimit;
1029 : 0 : MultiXactId multiStopLimit = MultiXactState->multiStopLimit;
1030 : 0 : MultiXactId multiWrapLimit = MultiXactState->multiWrapLimit;
1031 : 0 : Oid oldest_datoid = MultiXactState->oldestMultiXactDB;
1032 : :
1033 : 0 : LWLockRelease(MultiXactGenLock);
1034 : :
1035 [ # # ]: 0 : if (IsUnderPostmaster &&
1036 [ # # ]: 0 : !MultiXactIdPrecedes(result, multiStopLimit))
1037 : : {
1038 : 0 : char *oldest_datname = get_database_name(oldest_datoid);
1039 : :
1040 : : /*
1041 : : * Immediately kick autovacuum into action as we're already in
1042 : : * ERROR territory.
1043 : : */
4155 andres@anarazel.de 1044 : 0 : SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
1045 : :
1046 : : /* complain even if that DB has disappeared */
5034 alvherre@alvh.no-ip. 1047 [ # # ]: 0 : if (oldest_datname)
1048 [ # # ]: 0 : ereport(ERROR,
1049 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1050 : : errmsg("database is not accepting commands that assign new MultiXactIds to avoid wraparound data loss in database \"%s\"",
1051 : : oldest_datname),
1052 : : errhint("Execute a database-wide VACUUM in that database.\n"
1053 : : "You might also need to commit or roll back old prepared transactions.")));
1054 : : else
1055 [ # # ]: 0 : ereport(ERROR,
1056 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1057 : : errmsg("database is not accepting commands that assign new MultiXactIds to avoid wraparound data loss in database with OID %u",
1058 : : oldest_datoid),
1059 : : errhint("Execute a database-wide VACUUM in that database.\n"
1060 : : "You might also need to commit or roll back old prepared transactions.")));
1061 : : }
1062 : :
1063 : : /*
1064 : : * To avoid swamping the postmaster with signals, we issue the autovac
1065 : : * request only once per 64K multis generated. This still gives
1066 : : * plenty of chances before we get into real trouble.
1067 : : */
328 heikki.linnakangas@i 1068 [ # # # # : 0 : if (IsUnderPostmaster && ((result % 65536) == 0 || result == FirstMultiXactId))
# # ]
4155 andres@anarazel.de 1069 : 0 : SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
1070 : :
1071 [ # # ]: 0 : if (!MultiXactIdPrecedes(result, multiWarnLimit))
1072 : : {
5034 alvherre@alvh.no-ip. 1073 : 0 : char *oldest_datname = get_database_name(oldest_datoid);
1074 : :
1075 : : /* complain even if that DB has disappeared */
1076 [ # # ]: 0 : if (oldest_datname)
1077 [ # # ]: 0 : ereport(WARNING,
1078 : : (errmsg_plural("database \"%s\" must be vacuumed before %u more MultiXactId is used",
1079 : : "database \"%s\" must be vacuumed before %u more MultiXactIds are used",
1080 : : multiWrapLimit - result,
1081 : : oldest_datname,
1082 : : multiWrapLimit - result),
1083 : : errdetail("Approximately %.2f%% of MultiXactId space remains before wraparound.",
1084 : : (double) (multiWrapLimit - result) / (MaxMultiXactId / 2) * 100),
1085 : : errhint("Execute a database-wide VACUUM in that database.\n"
1086 : : "You might also need to commit or roll back old prepared transactions.")));
1087 : : else
1088 [ # # ]: 0 : ereport(WARNING,
1089 : : (errmsg_plural("database with OID %u must be vacuumed before %u more MultiXactId is used",
1090 : : "database with OID %u must be vacuumed before %u more MultiXactIds are used",
1091 : : multiWrapLimit - result,
1092 : : oldest_datoid,
1093 : : multiWrapLimit - result),
1094 : : errdetail("Approximately %.2f%% of MultiXactId space remains before wraparound.",
1095 : : (double) (multiWrapLimit - result) / (MaxMultiXactId / 2) * 100),
1096 : : errhint("Execute a database-wide VACUUM in that database.\n"
1097 : : "You might also need to commit or roll back old prepared transactions.")));
1098 : : }
1099 : :
1100 : : /* Re-acquire lock and start over */
1101 : 0 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1102 : 0 : result = MultiXactState->nextMXact;
1103 : : }
1104 : :
1105 : : /*
1106 : : * Make sure there is room for the next MXID in the file. Assigning this
1107 : : * MXID sets the next MXID's offset already.
1108 : : */
328 heikki.linnakangas@i 1109 :CBC 5315 : ExtendMultiXactOffset(NextMultiXactId(result));
1110 : :
1111 : : /*
1112 : : * Reserve the members space, similarly to above.
1113 : : */
7678 tgl@sss.pgh.pa.us 1114 : 5315 : nextOffset = MultiXactState->nextOffset;
1115 : :
1116 : : /*
1117 : : * Offsets are 64-bit integers and will never wrap around. Firstly, it
1118 : : * would take an unrealistic amount of time and resources to consume 2^64
1119 : : * offsets. Secondly, multixid creation is WAL-logged, so you would run
1120 : : * out of LSNs before reaching offset wraparound. Nevertheless, check for
1121 : : * wraparound as a sanity check.
1122 : : */
331 heikki.linnakangas@i 1123 [ - + ]: 5315 : if (nextOffset + nmembers < nextOffset)
331 heikki.linnakangas@i 1124 [ # # ]:UBC 0 : ereport(ERROR,
1125 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1126 : : errmsg("MultiXact members would wrap around")));
331 heikki.linnakangas@i 1127 :CBC 5315 : *offset = nextOffset;
1128 : :
5034 alvherre@alvh.no-ip. 1129 : 5315 : ExtendMultiXactMember(nextOffset, nmembers);
1130 : :
1131 : : /*
1132 : : * Critical section from here until caller has written the data into the
1133 : : * just-reserved SLRU space; we don't want to error out with a partly
1134 : : * written MultiXact structure. (In particular, failing to write our
1135 : : * start offset after advancing nextMXact would effectively corrupt the
1136 : : * previous MultiXact.)
1137 : : */
7678 tgl@sss.pgh.pa.us 1138 : 5315 : START_CRIT_SECTION();
1139 : :
1140 : : /*
1141 : : * Advance counters. As in GetNewTransactionId(), this must not happen
1142 : : * until after file extension has succeeded!
1143 : : */
328 heikki.linnakangas@i 1144 : 5315 : MultiXactState->nextMXact = NextMultiXactId(result);
5034 alvherre@alvh.no-ip. 1145 : 5315 : MultiXactState->nextOffset += nmembers;
1146 : :
7861 tgl@sss.pgh.pa.us 1147 : 5315 : LWLockRelease(MultiXactGenLock);
1148 : :
1149 : : debug_elog4(DEBUG2, "GetNew: returning %u offset %" PRIu64,
1150 : : result, *offset);
1151 : 5315 : return result;
1152 : : }
1153 : :
1154 : : /*
1155 : : * GetMultiXactIdMembers
1156 : : * Return the set of MultiXactMembers that make up a MultiXactId
1157 : : *
1158 : : * Return value is the number of members found, or -1 if there are none,
1159 : : * and *members is set to a newly palloc'ed array of members. It's the
1160 : : * caller's responsibility to free it when done with it.
1161 : : *
1162 : : * from_pgupgrade must be passed as true if and only if only the multixact
1163 : : * corresponds to a value from a tuple that was locked in a 9.2-or-older
1164 : : * installation and later pg_upgrade'd (that is, the infomask is
1165 : : * HEAP_LOCKED_UPGRADED). In this case, we know for certain that no members
1166 : : * can still be running, so we return -1 just like for an empty multixact
1167 : : * without any further checking. It would be wrong to try to resolve such a
1168 : : * multixact: either the multixact is within the current valid multixact
1169 : : * range, in which case the returned result would be bogus, or outside that
1170 : : * range, in which case an error would be raised.
1171 : : *
1172 : : * In all other cases, the passed multixact must be within the known valid
1173 : : * range, that is, greater than or equal to oldestMultiXactId, and less than
1174 : : * nextMXact. Otherwise, an error is raised.
1175 : : *
1176 : : * isLockOnly must be set to true if caller is certain that the given multi
1177 : : * is used only to lock tuples; can be false without loss of correctness,
1178 : : * but passing a true means we can return quickly without checking for
1179 : : * old updates.
1180 : : */
1181 : : int
5034 alvherre@alvh.no-ip. 1182 : 550054 : GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
1183 : : bool from_pgupgrade, bool isLockOnly)
1184 : : {
1185 : : int64 pageno;
1186 : : int64 prev_pageno;
1187 : : int entryno;
1188 : : int slotno;
1189 : : MultiXactOffset *offptr;
1190 : : MultiXactOffset offset;
1191 : : MultiXactOffset nextMXOffset;
1192 : : int length;
1193 : : MultiXactId oldestMXact;
1194 : : MultiXactId nextMXact;
1195 : : MultiXactMember *ptr;
1196 : : LWLock *lock;
1197 : :
1198 : : debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
1199 : :
3786 1200 [ + - - + ]: 550054 : if (!MultiXactIdIsValid(multi) || from_pgupgrade)
1201 : : {
1967 heikki.linnakangas@i 1202 :UBC 0 : *members = NULL;
4725 alvherre@alvh.no-ip. 1203 : 0 : return -1;
1204 : : }
1205 : :
1206 : : /* See if the MultiXactId is in the local cache */
5034 alvherre@alvh.no-ip. 1207 :CBC 550054 : length = mXactCacheGetById(multi, members);
7861 tgl@sss.pgh.pa.us 1208 [ + + ]: 550054 : if (length >= 0)
1209 : : {
1210 : : debug_elog3(DEBUG2, "GetMembers: found %s in the cache",
1211 : : mxid_to_string(multi, length, *members));
1212 : 457555 : return length;
1213 : : }
1214 : :
1215 : : /* Set our OldestVisibleMXactId[] entry if we didn't already */
1216 : 92499 : MultiXactIdSetOldestVisible();
1217 : :
1218 : : /*
1219 : : * If we know the multi is used only for locking and not for updates, then
1220 : : * we can skip checking if the value is older than our oldest visible
1221 : : * multi. It cannot possibly still be running.
1222 : : */
1508 pg@bowt.ie 1223 [ + + + + ]: 96209 : if (isLockOnly &&
248 heikki.linnakangas@i 1224 : 3710 : MultiXactIdPrecedes(multi, *MyOldestVisibleMXactIdSlot()))
1225 : : {
1226 : : debug_elog2(DEBUG2, "GetMembers: a locker-only multi is too old");
4482 alvherre@alvh.no-ip. 1227 : 739 : *members = NULL;
1228 : 739 : return -1;
1229 : : }
1230 : :
1231 : : /*
1232 : : * We check known limits on MultiXact before resorting to the SLRU area.
1233 : : *
1234 : : * An ID older than MultiXactState->oldestMultiXactId cannot possibly be
1235 : : * useful; it has already been removed, or will be removed shortly, by
1236 : : * truncation. If one is passed, an error is raised.
1237 : : *
1238 : : * Also, an ID >= nextMXact shouldn't ever be seen here; if it is seen, it
1239 : : * implies undetected ID wraparound has occurred. This raises a hard
1240 : : * error.
1241 : : *
1242 : : * Shared lock is enough here since we aren't modifying any global state.
1243 : : * Acquire it just long enough to grab the current counter values.
1244 : : */
7861 tgl@sss.pgh.pa.us 1245 : 91760 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
1246 : :
5034 alvherre@alvh.no-ip. 1247 : 91760 : oldestMXact = MultiXactState->oldestMultiXactId;
7678 tgl@sss.pgh.pa.us 1248 : 91760 : nextMXact = MultiXactState->nextMXact;
1249 : :
1250 : 91760 : LWLockRelease(MultiXactGenLock);
1251 : :
5034 alvherre@alvh.no-ip. 1252 [ - + ]: 91760 : if (MultiXactIdPrecedes(multi, oldestMXact))
3786 alvherre@alvh.no-ip. 1253 [ # # ]:UBC 0 : ereport(ERROR,
1254 : : (errcode(ERRCODE_INTERNAL_ERROR),
1255 : : errmsg("MultiXactId %u does no longer exist -- apparent wraparound",
1256 : : multi)));
1257 : :
5034 alvherre@alvh.no-ip. 1258 [ - + ]:CBC 91760 : if (!MultiXactIdPrecedes(multi, nextMXact))
5034 alvherre@alvh.no-ip. 1259 [ # # ]:UBC 0 : ereport(ERROR,
1260 : : (errcode(ERRCODE_INTERNAL_ERROR),
1261 : : errmsg("MultiXactId %u has not been created yet -- apparent wraparound",
1262 : : multi)));
1263 : :
1264 : : /*
1265 : : * Find out the offset at which we need to start reading MultiXactMembers
1266 : : * and the number of members in the multixact. We determine the latter as
1267 : : * the difference between this multixact's starting offset and the next
1268 : : * one's.
1269 : : */
7861 tgl@sss.pgh.pa.us 1270 :CBC 91760 : pageno = MultiXactIdToOffsetPage(multi);
1271 : 91760 : entryno = MultiXactIdToOffsetEntry(multi);
1272 : :
1273 : : /* Acquire the bank lock for the page we need. */
981 alvherre@alvh.no-ip. 1274 : 91760 : lock = SimpleLruGetBankLock(MultiXactOffsetCtl, pageno);
976 1275 : 91760 : LWLockAcquire(lock, LW_EXCLUSIVE);
1276 : :
1277 : : /* read this multi's offset */
237 heikki.linnakangas@i 1278 : 91760 : slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, &multi);
7820 tgl@sss.pgh.pa.us 1279 : 91760 : offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
7861 1280 : 91760 : offptr += entryno;
1281 : 91760 : offset = *offptr;
1282 : :
329 heikki.linnakangas@i 1283 [ - + ]: 91760 : if (offset == 0)
329 heikki.linnakangas@i 1284 [ # # ]:UBC 0 : ereport(ERROR,
1285 : : (errcode(ERRCODE_DATA_CORRUPTED),
1286 : : errmsg("MultiXact %u has invalid offset", multi)));
1287 : :
1288 : : /* read next multi's offset */
1289 : : {
1290 : : MultiXactId tmpMXact;
1291 : :
1292 : : /* handle wraparound if needed */
328 heikki.linnakangas@i 1293 :CBC 91760 : tmpMXact = NextMultiXactId(multi);
1294 : :
7861 tgl@sss.pgh.pa.us 1295 : 91760 : prev_pageno = pageno;
1296 : :
1297 : 91760 : pageno = MultiXactIdToOffsetPage(tmpMXact);
1298 : 91760 : entryno = MultiXactIdToOffsetEntry(tmpMXact);
1299 : :
1300 [ + + ]: 91760 : if (pageno != prev_pageno)
1301 : : {
1302 : : LWLock *newlock;
1303 : :
1304 : : /*
1305 : : * Since we're going to access a different SLRU page, if this page
1306 : : * falls under a different bank, release the old bank's lock and
1307 : : * acquire the lock of the new bank.
1308 : : */
976 alvherre@alvh.no-ip. 1309 : 13 : newlock = SimpleLruGetBankLock(MultiXactOffsetCtl, pageno);
1310 [ - + ]: 13 : if (newlock != lock)
1311 : : {
976 alvherre@alvh.no-ip. 1312 :UBC 0 : LWLockRelease(lock);
1313 : 0 : LWLockAcquire(newlock, LW_EXCLUSIVE);
1314 : 0 : lock = newlock;
1315 : : }
237 heikki.linnakangas@i 1316 :CBC 13 : slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, &tmpMXact);
1317 : : }
1318 : :
7820 tgl@sss.pgh.pa.us 1319 : 91760 : offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
7861 1320 : 91760 : offptr += entryno;
7678 1321 : 91760 : nextMXOffset = *offptr;
1322 : : }
1323 : :
976 alvherre@alvh.no-ip. 1324 : 91760 : LWLockRelease(lock);
1325 : 91760 : lock = NULL;
1326 : :
1327 : : /* Sanity check the next offset */
329 heikki.linnakangas@i 1328 [ - + ]: 91760 : if (nextMXOffset == 0)
329 heikki.linnakangas@i 1329 [ # # ]:UBC 0 : ereport(ERROR,
1330 : : (errcode(ERRCODE_DATA_CORRUPTED),
1331 : : errmsg("MultiXact %u has invalid next offset", multi)));
325 heikki.linnakangas@i 1332 [ - + ]:CBC 91760 : if (nextMXOffset == offset)
325 heikki.linnakangas@i 1333 [ # # ]:UBC 0 : ereport(ERROR,
1334 : : (errcode(ERRCODE_DATA_CORRUPTED),
1335 : : errmsg("MultiXact %u with offset (%" PRIu64 ") has zero members",
1336 : : multi, offset)));
329 heikki.linnakangas@i 1337 [ - + ]:CBC 91760 : if (nextMXOffset < offset)
329 heikki.linnakangas@i 1338 [ # # ]:UBC 0 : ereport(ERROR,
1339 : : (errcode(ERRCODE_DATA_CORRUPTED),
1340 : : errmsg("MultiXact %u has offset (%" PRIu64 ") greater than its next offset (%" PRIu64 ")",
1341 : : multi, offset, nextMXOffset)));
329 heikki.linnakangas@i 1342 [ - + ]:CBC 91760 : if (nextMXOffset - offset > INT32_MAX)
329 heikki.linnakangas@i 1343 [ # # ]:UBC 0 : ereport(ERROR,
1344 : : (errcode(ERRCODE_DATA_CORRUPTED),
1345 : : errmsg("MultiXact %u has too many members (%" PRIu64 ")",
1346 : : multi, nextMXOffset - offset)));
329 heikki.linnakangas@i 1347 :CBC 91760 : length = nextMXOffset - offset;
1348 : :
1349 : : /* read the members */
80 michael@paquier.xyz 1350 :GNC 91760 : ptr = palloc_array(MultiXactMember, length);
7861 tgl@sss.pgh.pa.us 1351 :CBC 91760 : prev_pageno = -1;
976 alvherre@alvh.no-ip. 1352 [ + + ]: 1827988 : for (int i = 0; i < length; i++, offset++)
1353 : : {
1354 : : TransactionId *xactptr;
1355 : : uint32 *flagsptr;
1356 : : int flagsoff;
1357 : : int bshift;
1358 : : int memberoff;
1359 : :
7861 tgl@sss.pgh.pa.us 1360 : 1736228 : pageno = MXOffsetToMemberPage(offset);
5034 alvherre@alvh.no-ip. 1361 : 1736228 : memberoff = MXOffsetToMemberOffset(offset);
1362 : :
7861 tgl@sss.pgh.pa.us 1363 [ + + ]: 1736228 : if (pageno != prev_pageno)
1364 : : {
237 heikki.linnakangas@i 1365 : 91922 : MultiXactMemberSlruReadContext slru_read_context = {multi, offset};
1366 : : LWLock *newlock;
1367 : :
1368 : : /*
1369 : : * Since we're going to access a different SLRU page, if this page
1370 : : * falls under a different bank, release the old bank's lock and
1371 : : * acquire the lock of the new bank.
1372 : : */
976 alvherre@alvh.no-ip. 1373 : 91922 : newlock = SimpleLruGetBankLock(MultiXactMemberCtl, pageno);
1374 [ + - ]: 91922 : if (newlock != lock)
1375 : : {
1376 [ + + ]: 91922 : if (lock)
1377 : 162 : LWLockRelease(lock);
1378 : 91922 : LWLockAcquire(newlock, LW_EXCLUSIVE);
1379 : 91922 : lock = newlock;
1380 : : }
237 heikki.linnakangas@i 1381 : 91922 : slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true,
1382 : : &slru_read_context);
7861 tgl@sss.pgh.pa.us 1383 : 91922 : prev_pageno = pageno;
1384 : : }
1385 : :
1386 : 1736228 : xactptr = (TransactionId *)
5034 alvherre@alvh.no-ip. 1387 : 1736228 : (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
331 heikki.linnakangas@i 1388 [ - + ]: 1736228 : Assert(TransactionIdIsValid(*xactptr));
1389 : :
5034 alvherre@alvh.no-ip. 1390 : 1736228 : flagsoff = MXOffsetToFlagsOffset(offset);
1391 : 1736228 : bshift = MXOffsetToFlagsBitShift(offset);
1392 : 1736228 : flagsptr = (uint32 *) (MultiXactMemberCtl->shared->page_buffer[slotno] + flagsoff);
1393 : :
331 heikki.linnakangas@i 1394 : 1736228 : ptr[i].xid = *xactptr;
1395 : 1736228 : ptr[i].status = (*flagsptr >> bshift) & MXACT_MEMBER_XACT_BITMASK;
1396 : : }
1397 : :
976 alvherre@alvh.no-ip. 1398 : 91760 : LWLockRelease(lock);
1399 : :
1400 : : /*
1401 : : * Copy the result into the local cache.
1402 : : */
331 heikki.linnakangas@i 1403 : 91760 : mXactCachePut(multi, length, ptr);
1404 : :
1405 : : debug_elog3(DEBUG2, "GetMembers: no cache for %s",
1406 : : mxid_to_string(multi, length, ptr));
1967 1407 : 91760 : *members = ptr;
331 1408 : 91760 : return length;
1409 : : }
1410 : :
1411 : : /*
1412 : : * mxactMemberComparator
1413 : : * qsort comparison function for MultiXactMember
1414 : : *
1415 : : * We can't use wraparound comparison for XIDs because that does not respect
1416 : : * the triangle inequality! Any old sort order will do.
1417 : : */
1418 : : static int
5034 alvherre@alvh.no-ip. 1419 : 3050767 : mxactMemberComparator(const void *arg1, const void *arg2)
1420 : : {
1421 : 3050767 : MultiXactMember member1 = *(const MultiXactMember *) arg1;
1422 : 3050767 : MultiXactMember member2 = *(const MultiXactMember *) arg2;
1423 : :
1424 [ + + ]: 3050767 : if (member1.xid > member2.xid)
1425 : 32 : return 1;
1426 [ + + ]: 3050735 : if (member1.xid < member2.xid)
1427 : 3050522 : return -1;
1428 [ + + ]: 213 : if (member1.status > member2.status)
1429 : 16 : return 1;
1430 [ + - ]: 197 : if (member1.status < member2.status)
1431 : 197 : return -1;
5034 alvherre@alvh.no-ip. 1432 :UBC 0 : return 0;
1433 : : }
1434 : :
1435 : : /*
1436 : : * mXactCacheGetBySet
1437 : : * returns a MultiXactId from the cache based on the set of
1438 : : * TransactionIds that compose it, or InvalidMultiXactId if
1439 : : * none matches.
1440 : : *
1441 : : * This is helpful, for example, if two transactions want to lock a huge
1442 : : * table. By using the cache, the second will use the same MultiXactId
1443 : : * for the majority of tuples, thus keeping MultiXactId usage low (saving
1444 : : * both I/O and wraparound issues).
1445 : : *
1446 : : * NB: the passed members array will be sorted in-place.
1447 : : */
1448 : : static MultiXactId
5034 alvherre@alvh.no-ip. 1449 :CBC 76669 : mXactCacheGetBySet(int nmembers, MultiXactMember *members)
1450 : : {
1451 : : dlist_iter iter;
1452 : :
1453 : : debug_elog3(DEBUG2, "CacheGet: looking for %s",
1454 : : mxid_to_string(InvalidMultiXactId, nmembers, members));
1455 : :
1456 : : /* sort the array so comparison is easy */
1457 : 76669 : qsort(members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
1458 : :
1464 drowley@postgresql.o 1459 [ + - + + ]: 308360 : dclist_foreach(iter, &MXactCache)
1460 : : {
1461 : 303045 : mXactCacheEnt *entry = dclist_container(mXactCacheEnt, node,
1462 : : iter.cur);
1463 : :
5034 alvherre@alvh.no-ip. 1464 [ + + ]: 303045 : if (entry->nmembers != nmembers)
7861 tgl@sss.pgh.pa.us 1465 : 85331 : continue;
1466 : :
1467 : : /*
1468 : : * We assume the cache entries are sorted, and that the unused bits in
1469 : : * "status" are zeroed.
1470 : : */
5034 alvherre@alvh.no-ip. 1471 [ + + ]: 217714 : if (memcmp(members, entry->members, nmembers * sizeof(MultiXactMember)) == 0)
1472 : : {
1473 : : debug_elog3(DEBUG2, "CacheGet: found %u", entry->multi);
1464 drowley@postgresql.o 1474 : 71354 : dclist_move_head(&MXactCache, iter.cur);
7861 tgl@sss.pgh.pa.us 1475 : 71354 : return entry->multi;
1476 : : }
1477 : : }
1478 : :
1479 : : debug_elog2(DEBUG2, "CacheGet: not found :-(");
1480 : 5315 : return InvalidMultiXactId;
1481 : : }
1482 : :
1483 : : /*
1484 : : * mXactCacheGetById
1485 : : * returns the composing MultiXactMember set from the cache for a
1486 : : * given MultiXactId, if present.
1487 : : *
1488 : : * If successful, *xids is set to the address of a palloc'd copy of the
1489 : : * MultiXactMember set. Return value is number of members, or -1 on failure.
1490 : : */
1491 : : static int
5034 alvherre@alvh.no-ip. 1492 : 550054 : mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
1493 : : {
1494 : : dlist_iter iter;
1495 : :
1496 : : debug_elog3(DEBUG2, "CacheGet: looking for %u", multi);
1497 : :
1464 drowley@postgresql.o 1498 [ + - + + ]: 4905918 : dclist_foreach(iter, &MXactCache)
1499 : : {
1500 : 4813419 : mXactCacheEnt *entry = dclist_container(mXactCacheEnt, node,
1501 : : iter.cur);
1502 : :
7861 tgl@sss.pgh.pa.us 1503 [ + + ]: 4813419 : if (entry->multi == multi)
1504 : : {
1505 : : MultiXactMember *ptr;
1506 : : Size size;
1507 : :
5034 alvherre@alvh.no-ip. 1508 : 457555 : size = sizeof(MultiXactMember) * entry->nmembers;
1509 : 457555 : ptr = (MultiXactMember *) palloc(size);
1510 : :
1511 : 457555 : memcpy(ptr, entry->members, size);
1512 : :
1513 : : debug_elog3(DEBUG2, "CacheGet: found %s",
1514 : : mxid_to_string(multi,
1515 : : entry->nmembers,
1516 : : entry->members));
1517 : :
1518 : : /*
1519 : : * Note we modify the list while not using a modifiable iterator.
1520 : : * This is acceptable only because we exit the iteration
1521 : : * immediately afterwards.
1522 : : */
1464 drowley@postgresql.o 1523 : 457555 : dclist_move_head(&MXactCache, iter.cur);
1524 : :
1967 heikki.linnakangas@i 1525 : 457555 : *members = ptr;
5034 alvherre@alvh.no-ip. 1526 : 457555 : return entry->nmembers;
1527 : : }
1528 : : }
1529 : :
1530 : : debug_elog2(DEBUG2, "CacheGet: not found");
7861 tgl@sss.pgh.pa.us 1531 : 92499 : return -1;
1532 : : }
1533 : :
1534 : : /*
1535 : : * mXactCachePut
1536 : : * Add a new MultiXactId and its composing set into the local cache.
1537 : : */
1538 : : static void
5034 alvherre@alvh.no-ip. 1539 : 97075 : mXactCachePut(MultiXactId multi, int nmembers, MultiXactMember *members)
1540 : : {
1541 : : mXactCacheEnt *entry;
1542 : :
1543 : : debug_elog3(DEBUG2, "CachePut: storing %s",
1544 : : mxid_to_string(multi, nmembers, members));
1545 : :
7861 tgl@sss.pgh.pa.us 1546 [ + + ]: 97075 : if (MXactContext == NULL)
1547 : : {
1548 : : /* The cache only lives as long as the current transaction */
1549 : : debug_elog2(DEBUG2, "CachePut: initializing memory context");
1550 : 3374 : MXactContext = AllocSetContextCreate(TopTransactionContext,
1551 : : "MultiXact cache context",
1552 : : ALLOCSET_SMALL_SIZES);
1553 : : }
1554 : :
1555 : : entry = (mXactCacheEnt *)
1556 : 97075 : MemoryContextAlloc(MXactContext,
5034 alvherre@alvh.no-ip. 1557 : 97075 : offsetof(mXactCacheEnt, members) +
1558 : : nmembers * sizeof(MultiXactMember));
1559 : :
7861 tgl@sss.pgh.pa.us 1560 : 97075 : entry->multi = multi;
5034 alvherre@alvh.no-ip. 1561 : 97075 : entry->nmembers = nmembers;
1562 : 97075 : memcpy(entry->members, members, nmembers * sizeof(MultiXactMember));
1563 : :
1564 : : /* mXactCacheGetBySet assumes the entries are sorted, so sort them */
1565 : 97075 : qsort(entry->members, nmembers, sizeof(MultiXactMember), mxactMemberComparator);
1566 : :
1464 drowley@postgresql.o 1567 : 97075 : dclist_push_head(&MXactCache, &entry->node);
1568 [ + + ]: 97075 : if (dclist_count(&MXactCache) > MAX_CACHE_ENTRIES)
1569 : : {
1570 : : dlist_node *node;
1571 : :
1572 : 9478 : node = dclist_tail_node(&MXactCache);
1573 : 9478 : dclist_delete_from(&MXactCache, node);
1574 : :
1575 : 9478 : entry = dclist_container(mXactCacheEnt, node, node);
1576 : : debug_elog3(DEBUG2, "CachePut: pruning cached multi %u",
1577 : : entry->multi);
1578 : :
4710 alvherre@alvh.no-ip. 1579 : 9478 : pfree(entry);
1580 : : }
7861 tgl@sss.pgh.pa.us 1581 : 97075 : }
1582 : :
1583 : : char *
5034 alvherre@alvh.no-ip. 1584 : 188088 : mxstatus_to_string(MultiXactStatus status)
1585 : : {
1586 [ + - - - : 188088 : switch (status)
+ - - ]
1587 : : {
1588 : 183634 : case MultiXactStatusForKeyShare:
1589 : 183634 : return "keysh";
5034 alvherre@alvh.no-ip. 1590 :UBC 0 : case MultiXactStatusForShare:
1591 : 0 : return "sh";
1592 : 0 : case MultiXactStatusForNoKeyUpdate:
1593 : 0 : return "fornokeyupd";
1594 : 0 : case MultiXactStatusForUpdate:
1595 : 0 : return "forupd";
5034 alvherre@alvh.no-ip. 1596 :CBC 4454 : case MultiXactStatusNoKeyUpdate:
1597 : 4454 : return "nokeyupd";
5034 alvherre@alvh.no-ip. 1598 :UBC 0 : case MultiXactStatusUpdate:
1599 : 0 : return "upd";
1600 : 0 : default:
1601 [ # # ]: 0 : elog(ERROR, "unrecognized multixact status %d", status);
1602 : : return "";
1603 : : }
1604 : : }
1605 : :
1606 : : char *
1607 : 0 : mxid_to_string(MultiXactId multi, int nmembers, MultiXactMember *members)
1608 : : {
1609 : : static char *str = NULL;
1610 : : StringInfoData buf;
1611 : : int i;
1612 : :
1613 [ # # ]: 0 : if (str != NULL)
1614 : 0 : pfree(str);
1615 : :
1616 : 0 : initStringInfo(&buf);
1617 : :
1618 : 0 : appendStringInfo(&buf, "%u %d[%u (%s)", multi, nmembers, members[0].xid,
1619 : : mxstatus_to_string(members[0].status));
1620 : :
1621 [ # # ]: 0 : for (i = 1; i < nmembers; i++)
1622 : 0 : appendStringInfo(&buf, ", %u (%s)", members[i].xid,
1623 : 0 : mxstatus_to_string(members[i].status));
1624 : :
1625 : 0 : appendStringInfoChar(&buf, ']');
1626 : 0 : str = MemoryContextStrdup(TopMemoryContext, buf.data);
1627 : 0 : pfree(buf.data);
7861 tgl@sss.pgh.pa.us 1628 : 0 : return str;
1629 : : }
1630 : :
1631 : : /*
1632 : : * AtEOXact_MultiXact
1633 : : * Handle transaction end for MultiXact
1634 : : *
1635 : : * This is called at top transaction commit or abort (we don't care which).
1636 : : */
1637 : : void
7861 tgl@sss.pgh.pa.us 1638 :CBC 428126 : AtEOXact_MultiXact(void)
1639 : : {
1640 : : /*
1641 : : * Reset our OldestMemberMXactId and OldestVisibleMXactId values, both of
1642 : : * which should only be valid while within a transaction.
1643 : : *
1644 : : * We assume that storing a MultiXactId is atomic and so we need not take
1645 : : * MultiXactGenLock to do this.
1646 : : */
248 heikki.linnakangas@i 1647 : 428126 : *MyOldestMemberMXactIdSlot() = InvalidMultiXactId;
1648 : 428126 : *MyOldestVisibleMXactIdSlot() = InvalidMultiXactId;
1649 : :
1650 : : /*
1651 : : * Discard the local MultiXactId cache. Since MXactContext was created as
1652 : : * a child of TopTransactionContext, we needn't delete it explicitly.
1653 : : */
7861 tgl@sss.pgh.pa.us 1654 : 428126 : MXactContext = NULL;
1464 drowley@postgresql.o 1655 : 428126 : dclist_init(&MXactCache);
7861 tgl@sss.pgh.pa.us 1656 : 428126 : }
1657 : :
1658 : : /*
1659 : : * AtPrepare_MultiXact
1660 : : * Save multixact state at 2PC transaction prepare
1661 : : *
1662 : : * In this phase, we only store our OldestMemberMXactId value in the two-phase
1663 : : * state file.
1664 : : */
1665 : : void
6191 heikki.linnakangas@i 1666 : 305 : AtPrepare_MultiXact(void)
1667 : : {
248 1668 : 305 : MultiXactId myOldestMember = *MyOldestMemberMXactIdSlot();
1669 : :
6191 1670 [ + + ]: 305 : if (MultiXactIdIsValid(myOldestMember))
1671 : 71 : RegisterTwoPhaseRecord(TWOPHASE_RM_MULTIXACT_ID, 0,
1672 : : &myOldestMember, sizeof(MultiXactId));
1673 : 305 : }
1674 : :
1675 : : /*
1676 : : * PostPrepare_MultiXact
1677 : : * Clean up after successful PREPARE TRANSACTION
1678 : : */
1679 : : void
486 michael@paquier.xyz 1680 : 305 : PostPrepare_MultiXact(FullTransactionId fxid)
1681 : : {
1682 : : MultiXactId myOldestMember;
1683 : :
1684 : : /*
1685 : : * Transfer our OldestMemberMXactId value to the slot reserved for the
1686 : : * prepared transaction.
1687 : : */
248 heikki.linnakangas@i 1688 : 305 : myOldestMember = *MyOldestMemberMXactIdSlot();
6191 1689 [ + + ]: 305 : if (MultiXactIdIsValid(myOldestMember))
1690 : : {
486 michael@paquier.xyz 1691 : 71 : ProcNumber dummyProcNumber = TwoPhaseGetDummyProcNumber(fxid, false);
1692 : :
1693 : : /*
1694 : : * Even though storing MultiXactId is atomic, acquire lock to make
1695 : : * sure others see both changes, not just the reset of the slot of the
1696 : : * current backend. Using a volatile pointer might suffice, but this
1697 : : * isn't a hot spot.
1698 : : */
6191 heikki.linnakangas@i 1699 : 71 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1700 : :
248 1701 : 71 : *PreparedXactOldestMemberMXactIdSlot(dummyProcNumber) = myOldestMember;
1702 : 71 : *MyOldestMemberMXactIdSlot() = InvalidMultiXactId;
1703 : :
6191 1704 : 71 : LWLockRelease(MultiXactGenLock);
1705 : : }
1706 : :
1707 : : /*
1708 : : * We don't need to transfer OldestVisibleMXactId value, because the
1709 : : * transaction is not going to be looking at any more multixacts once it's
1710 : : * prepared.
1711 : : *
1712 : : * We assume that storing a MultiXactId is atomic and so we need not take
1713 : : * MultiXactGenLock to do this.
1714 : : */
248 1715 : 305 : *MyOldestVisibleMXactIdSlot() = InvalidMultiXactId;
1716 : :
1717 : : /*
1718 : : * Discard the local MultiXactId cache like in AtEOXact_MultiXact.
1719 : : */
6191 1720 : 305 : MXactContext = NULL;
1464 drowley@postgresql.o 1721 : 305 : dclist_init(&MXactCache);
6191 heikki.linnakangas@i 1722 : 305 : }
1723 : :
1724 : : /*
1725 : : * multixact_twophase_recover
1726 : : * Recover the state of a prepared transaction at startup
1727 : : */
1728 : : void
486 michael@paquier.xyz 1729 : 9 : multixact_twophase_recover(FullTransactionId fxid, uint16 info,
1730 : : void *recdata, uint32 len)
1731 : : {
1732 : 9 : ProcNumber dummyProcNumber = TwoPhaseGetDummyProcNumber(fxid, false);
1733 : : MultiXactId oldestMember;
1734 : :
1735 : : /*
1736 : : * Get the oldest member XID from the state file record, and set it in the
1737 : : * OldestMemberMXactId slot reserved for this prepared transaction.
1738 : : */
6191 heikki.linnakangas@i 1739 [ - + ]: 9 : Assert(len == sizeof(MultiXactId));
6096 bruce@momjian.us 1740 : 9 : oldestMember = *((MultiXactId *) recdata);
1741 : :
248 heikki.linnakangas@i 1742 : 9 : *PreparedXactOldestMemberMXactIdSlot(dummyProcNumber) = oldestMember;
6191 1743 : 9 : }
1744 : :
1745 : : /*
1746 : : * multixact_twophase_postcommit
1747 : : * Similar to AtEOXact_MultiXact but for COMMIT PREPARED
1748 : : */
1749 : : void
486 michael@paquier.xyz 1750 : 76 : multixact_twophase_postcommit(FullTransactionId fxid, uint16 info,
1751 : : void *recdata, uint32 len)
1752 : : {
1753 : 76 : ProcNumber dummyProcNumber = TwoPhaseGetDummyProcNumber(fxid, true);
1754 : :
6191 heikki.linnakangas@i 1755 [ - + ]: 76 : Assert(len == sizeof(MultiXactId));
1756 : :
248 1757 : 76 : *PreparedXactOldestMemberMXactIdSlot(dummyProcNumber) = InvalidMultiXactId;
6191 1758 : 76 : }
1759 : :
1760 : : /*
1761 : : * multixact_twophase_postabort
1762 : : * This is actually just the same as the COMMIT case.
1763 : : */
1764 : : void
486 michael@paquier.xyz 1765 : 32 : multixact_twophase_postabort(FullTransactionId fxid, uint16 info,
1766 : : void *recdata, uint32 len)
1767 : : {
1768 : 32 : multixact_twophase_postcommit(fxid, info, recdata, len);
6191 heikki.linnakangas@i 1769 : 32 : }
1770 : :
1771 : :
1772 : : /*
1773 : : * Register shared memory needs for MultiXact.
1774 : : */
1775 : : static void
213 1776 : 1307 : MultiXactShmemRequest(void *arg)
1777 : : {
1778 : : Size size;
1779 : :
1780 : : /*
1781 : : * Calculate the size of the MultiXactState struct, and the two
1782 : : * per-backend MultiXactId arrays. They are carved out of the same
1783 : : * allocation.
1784 : : */
248 1785 : 1307 : size = offsetof(MultiXactStateData, perBackendXactIds);
1786 : 1307 : size = add_size(size,
1787 : 1307 : mul_size(sizeof(MultiXactId), NumMemberSlots));
1788 : 1307 : size = add_size(size,
1789 : : mul_size(sizeof(MultiXactId), NumVisibleSlots));
213 1790 : 1307 : ShmemRequestStruct(.name = "Shared MultiXact State",
1791 : : .size = size,
1792 : : .ptr = (void **) &MultiXactState,
1793 : : );
1794 : :
1795 : 1307 : SimpleLruRequest(.desc = &MultiXactOffsetSlruDesc,
1796 : : .name = "multixact_offset",
1797 : : .Dir = "pg_multixact/offsets",
1798 : : .long_segment_names = false,
1799 : :
1800 : : .nslots = multixact_offset_buffers,
1801 : :
1802 : : .sync_handler = SYNC_HANDLER_MULTIXACT_OFFSET,
1803 : : .PagePrecedes = MultiXactOffsetPagePrecedes,
1804 : : .errdetail_for_io_error = MultiXactOffsetIoErrorDetail,
1805 : :
1806 : : .buffer_tranche_id = LWTRANCHE_MULTIXACTOFFSET_BUFFER,
1807 : : .bank_tranche_id = LWTRANCHE_MULTIXACTOFFSET_SLRU,
1808 : : );
1809 : :
1810 : 1307 : SimpleLruRequest(.desc = &MultiXactMemberSlruDesc,
1811 : : .name = "multixact_member",
1812 : : .Dir = "pg_multixact/members",
1813 : : .long_segment_names = true,
1814 : :
1815 : : .nslots = multixact_member_buffers,
1816 : :
1817 : : .sync_handler = SYNC_HANDLER_MULTIXACT_MEMBER,
1818 : : .PagePrecedes = MultiXactMemberPagePrecedes,
1819 : : .errdetail_for_io_error = MultiXactMemberIoErrorDetail,
1820 : :
1821 : : .buffer_tranche_id = LWTRANCHE_MULTIXACTMEMBER_BUFFER,
1822 : : .bank_tranche_id = LWTRANCHE_MULTIXACTMEMBER_SLRU,
1823 : : );
1824 : 1307 : }
1825 : :
1826 : : static void
1827 : 1303 : MultiXactShmemInit(void *arg)
1828 : : {
1829 : 1303 : SlruPagePrecedesUnitTests(MultiXactOffsetCtl, MULTIXACT_OFFSETS_PER_PAGE);
1830 : :
1831 : : /*
1832 : : * members SLRU doesn't call SimpleLruTruncate() or meet criteria for unit
1833 : : * tests
1834 : : */
1835 : :
1836 : : /* Set up array pointers */
1837 : 1303 : OldestMemberMXactId = MultiXactState->perBackendXactIds;
1838 : 1303 : OldestVisibleMXactId = OldestMemberMXactId + NumMemberSlots;
1839 : 1303 : }
1840 : :
1841 : : static void
213 heikki.linnakangas@i 1842 :UBC 0 : MultiXactShmemAttach(void *arg)
1843 : : {
1844 : : /* Set up array pointers */
7861 tgl@sss.pgh.pa.us 1845 : 0 : OldestMemberMXactId = MultiXactState->perBackendXactIds;
248 heikki.linnakangas@i 1846 : 0 : OldestVisibleMXactId = OldestMemberMXactId + NumMemberSlots;
7861 tgl@sss.pgh.pa.us 1847 : 0 : }
1848 : :
1849 : : /*
1850 : : * GUC check_hook for multixact_offset_buffers
1851 : : */
1852 : : bool
981 alvherre@alvh.no-ip. 1853 :CBC 1348 : check_multixact_offset_buffers(int *newval, void **extra, GucSource source)
1854 : : {
1855 : 1348 : return check_slru_buffers("multixact_offset_buffers", newval);
1856 : : }
1857 : :
1858 : : /*
1859 : : * GUC check_hook for multixact_member_buffers
1860 : : */
1861 : : bool
1862 : 1348 : check_multixact_member_buffers(int *newval, void **extra, GucSource source)
1863 : : {
1864 : 1348 : return check_slru_buffers("multixact_member_buffers", newval);
1865 : : }
1866 : :
1867 : : /*
1868 : : * This func must be called ONCE on system install. It creates the initial
1869 : : * MultiXact segments. (The MultiXacts directories are assumed to have been
1870 : : * created by initdb, and MultiXactShmemInit must have been called already.)
1871 : : */
1872 : : void
7861 tgl@sss.pgh.pa.us 1873 : 59 : BootStrapMultiXact(void)
1874 : : {
1875 : : /* Zero the initial pages and flush them to disk */
486 alvherre@kurilemu.de 1876 : 59 : SimpleLruZeroAndWritePage(MultiXactOffsetCtl, 0);
1877 : 59 : SimpleLruZeroAndWritePage(MultiXactMemberCtl, 0);
7861 tgl@sss.pgh.pa.us 1878 : 59 : }
1879 : :
1880 : : /*
1881 : : * This must be called ONCE during postmaster or standalone-backend startup.
1882 : : *
1883 : : * StartupXLOG has already established nextMXact/nextOffset by calling
1884 : : * MultiXactSetNextMXact and/or MultiXactAdvanceNextMXact, and the oldestMulti
1885 : : * info from pg_control and/or MultiXactAdvanceOldest, but we haven't yet
1886 : : * replayed WAL.
1887 : : */
1888 : : void
1889 : 1125 : StartupMultiXact(void)
1890 : : {
4724 alvherre@alvh.no-ip. 1891 : 1125 : MultiXactId multi = MultiXactState->nextMXact;
1892 : 1125 : MultiXactOffset offset = MultiXactState->nextOffset;
1893 : : int64 pageno;
1894 : :
1895 : : /*
1896 : : * Initialize offset's idea of the latest page number.
1897 : : */
1898 : 1125 : pageno = MultiXactIdToOffsetPage(multi);
1003 1899 : 1125 : pg_atomic_write_u64(&MultiXactOffsetCtl->shared->latest_page_number,
1900 : : pageno);
1901 : :
1902 : : /*
1903 : : * Initialize member's idea of the latest page number.
1904 : : */
4724 1905 : 1125 : pageno = MXOffsetToMemberPage(offset);
1003 1906 : 1125 : pg_atomic_write_u64(&MultiXactMemberCtl->shared->latest_page_number,
1907 : : pageno);
4724 1908 : 1125 : }
1909 : :
1910 : : /*
1911 : : * This must be called ONCE at the end of startup/recovery.
1912 : : */
1913 : : void
1914 : 1052 : TrimMultiXact(void)
1915 : : {
1916 : : MultiXactId nextMXact;
1917 : : MultiXactOffset offset;
1918 : : MultiXactId oldestMXact;
1919 : : Oid oldestMXactDB;
1920 : : int64 pageno;
1921 : : int entryno;
1922 : : int flagsoff;
1923 : :
4058 andres@anarazel.de 1924 : 1052 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
1925 : 1052 : nextMXact = MultiXactState->nextMXact;
1926 : 1052 : offset = MultiXactState->nextOffset;
1927 : 1052 : oldestMXact = MultiXactState->oldestMultiXactId;
1928 : 1052 : oldestMXactDB = MultiXactState->oldestMultiXactDB;
1929 : 1052 : LWLockRelease(MultiXactGenLock);
1930 : :
1931 : : /* Clean up offsets state */
1932 : :
1933 : : /*
1934 : : * (Re-)Initialize our idea of the latest page number for offsets.
1935 : : */
1936 : 1052 : pageno = MultiXactIdToOffsetPage(nextMXact);
1003 alvherre@alvh.no-ip. 1937 : 1052 : pg_atomic_write_u64(&MultiXactOffsetCtl->shared->latest_page_number,
1938 : : pageno);
1939 : :
1940 : : /*
1941 : : * Set the offset of nextMXact on the offsets page. This is normally done
1942 : : * in RecordNewMultiXact() of the previous multixact, but let's be sure
1943 : : * the next page exists, if the nextMXact was reset with pg_resetwal for
1944 : : * example.
1945 : : *
1946 : : * Zero out the remainder of the page. See notes in TrimCLOG() for
1947 : : * background. Unlike CLOG, some WAL record covers every pg_multixact
1948 : : * SLRU mutation. Since, also unlike CLOG, we ignore the WAL rule "write
1949 : : * xlog before data," nextMXact successors may carry obsolete, nonzero
1950 : : * offset values.
1951 : : */
4058 andres@anarazel.de 1952 : 1052 : entryno = MultiXactIdToOffsetEntry(nextMXact);
1953 : : {
1954 : : int slotno;
1955 : : MultiXactOffset *offptr;
981 alvherre@alvh.no-ip. 1956 : 1052 : LWLock *lock = SimpleLruGetBankLock(MultiXactOffsetCtl, pageno);
1957 : :
1958 : 1052 : LWLockAcquire(lock, LW_EXCLUSIVE);
328 heikki.linnakangas@i 1959 [ + + + + ]: 1052 : if (entryno == 0 || nextMXact == FirstMultiXactId)
337 1960 : 1033 : slotno = SimpleLruZeroPage(MultiXactOffsetCtl, pageno);
1961 : : else
237 1962 : 19 : slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, &nextMXact);
7820 tgl@sss.pgh.pa.us 1963 : 1052 : offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1964 : 1052 : offptr += entryno;
1965 : :
337 heikki.linnakangas@i 1966 : 1052 : *offptr = offset;
1967 [ + + + - ]: 1052 : if (entryno != 0 && (entryno + 1) * sizeof(MultiXactOffset) != BLCKSZ)
1968 [ + - + - : 1672 : MemSet(offptr + 1, 0, BLCKSZ - (entryno + 1) * sizeof(MultiXactOffset));
+ - + + +
+ ]
1969 : :
7670 tgl@sss.pgh.pa.us 1970 : 1052 : MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
981 alvherre@alvh.no-ip. 1971 : 1052 : LWLockRelease(lock);
1972 : : }
1973 : :
1974 : : /*
1975 : : * And the same for members.
1976 : : *
1977 : : * (Re-)Initialize our idea of the latest page number for members.
1978 : : */
7820 tgl@sss.pgh.pa.us 1979 : 1052 : pageno = MXOffsetToMemberPage(offset);
1003 alvherre@alvh.no-ip. 1980 : 1052 : pg_atomic_write_u64(&MultiXactMemberCtl->shared->latest_page_number,
1981 : : pageno);
1982 : :
1983 : : /*
1984 : : * Zero out the remainder of the current members page. See notes in
1985 : : * TrimCLOG() for motivation.
1986 : : */
5034 1987 : 1052 : flagsoff = MXOffsetToFlagsOffset(offset);
1988 [ + + ]: 1052 : if (flagsoff != 0)
1989 : : {
237 heikki.linnakangas@i 1990 : 17 : MultiXactMemberSlruReadContext slru_read_context = {InvalidMultiXactId, offset};
1991 : : int slotno;
1992 : : TransactionId *xidptr;
1993 : : int memberoff;
981 alvherre@alvh.no-ip. 1994 : 17 : LWLock *lock = SimpleLruGetBankLock(MultiXactMemberCtl, pageno);
1995 : :
1996 : 17 : LWLockAcquire(lock, LW_EXCLUSIVE);
5034 1997 : 17 : memberoff = MXOffsetToMemberOffset(offset);
237 heikki.linnakangas@i 1998 : 17 : slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, &slru_read_context);
5034 alvherre@alvh.no-ip. 1999 : 17 : xidptr = (TransactionId *)
2000 : 17 : (MultiXactMemberCtl->shared->page_buffer[slotno] + memberoff);
2001 : :
2002 [ + + + - : 17 : MemSet(xidptr, 0, BLCKSZ - memberoff);
+ - - + -
- ]
2003 : :
2004 : : /*
2005 : : * Note: we don't need to zero out the flag bits in the remaining
2006 : : * members of the current group, because they are always reset before
2007 : : * writing.
2008 : : */
2009 : :
7670 tgl@sss.pgh.pa.us 2010 : 17 : MultiXactMemberCtl->shared->page_dirty[slotno] = true;
981 alvherre@alvh.no-ip. 2011 : 17 : LWLockRelease(lock);
2012 : : }
2013 : :
2014 : : /* signal that we're officially up */
4058 andres@anarazel.de 2015 : 1052 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2016 : 1052 : MultiXactState->finishedStartup = true;
4171 rhaas@postgresql.org 2017 : 1052 : LWLockRelease(MultiXactGenLock);
2018 : :
2019 : : /* Now compute how far away the next multixid wraparound is. */
331 heikki.linnakangas@i 2020 : 1052 : SetMultiXactIdLimit(oldestMXact, oldestMXactDB);
7861 tgl@sss.pgh.pa.us 2021 : 1052 : }
2022 : :
2023 : : /*
2024 : : * Get the MultiXact data to save in a checkpoint record
2025 : : */
2026 : : void
7820 2027 : 1815 : MultiXactGetCheckptMulti(bool is_shutdown,
2028 : : MultiXactId *nextMulti,
2029 : : MultiXactOffset *nextMultiOffset,
2030 : : MultiXactId *oldestMulti,
2031 : : Oid *oldestMultiDB)
2032 : : {
7861 2033 : 1815 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
7820 2034 : 1815 : *nextMulti = MultiXactState->nextMXact;
2035 : 1815 : *nextMultiOffset = MultiXactState->nextOffset;
5034 alvherre@alvh.no-ip. 2036 : 1815 : *oldestMulti = MultiXactState->oldestMultiXactId;
2037 : 1815 : *oldestMultiDB = MultiXactState->oldestMultiXactDB;
7861 tgl@sss.pgh.pa.us 2038 : 1815 : LWLockRelease(MultiXactGenLock);
2039 : :
2040 : : debug_elog6(DEBUG2,
2041 : : "MultiXact: checkpoint is nextMulti %u, nextOffset %" PRIu64 ", oldestMulti %u in DB %u",
2042 : : *nextMulti, *nextMultiOffset, *oldestMulti, *oldestMultiDB);
2043 : 1815 : }
2044 : :
2045 : : /*
2046 : : * Perform a checkpoint --- either during shutdown, or on-the-fly
2047 : : */
2048 : : void
2049 : 2031 : CheckPointMultiXact(void)
2050 : : {
2051 : : TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_START(true);
2052 : :
2053 : : /*
2054 : : * Write dirty MultiXact pages to disk. This may result in sync requests
2055 : : * queued for later handling by ProcessSyncRequests(), as part of the
2056 : : * checkpoint.
2057 : : */
2232 tmunro@postgresql.or 2058 : 2031 : SimpleLruWriteAll(MultiXactOffsetCtl, true);
2059 : 2031 : SimpleLruWriteAll(MultiXactMemberCtl, true);
2060 : :
2061 : : TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_DONE(true);
7861 tgl@sss.pgh.pa.us 2062 : 2031 : }
2063 : :
2064 : : /*
2065 : : * Set the next-to-be-assigned MultiXactId and offset
2066 : : *
2067 : : * This is used when we can determine the correct next ID/offset exactly
2068 : : * from a checkpoint record. Although this is only called during bootstrap
2069 : : * and XLog replay, we take the lock in case any hot-standby backends are
2070 : : * examining the values.
2071 : : */
2072 : : void
7820 2073 : 1236 : MultiXactSetNextMXact(MultiXactId nextMulti,
2074 : : MultiXactOffset nextMultiOffset)
2075 : : {
328 heikki.linnakangas@i 2076 [ - + ]: 1236 : Assert(MultiXactIdIsValid(nextMulti));
2077 : : debug_elog4(DEBUG2, "MultiXact: setting next multi to %u offset %" PRIu64,
2078 : : nextMulti, nextMultiOffset);
2079 : :
5386 tgl@sss.pgh.pa.us 2080 : 1236 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
7861 2081 : 1236 : MultiXactState->nextMXact = nextMulti;
7820 2082 : 1236 : MultiXactState->nextOffset = nextMultiOffset;
5386 2083 : 1236 : LWLockRelease(MultiXactGenLock);
7861 2084 : 1236 : }
2085 : :
2086 : : /*
2087 : : * Determine the last safe MultiXactId to allocate given the currently oldest
2088 : : * datminmxid (ie, the oldest MultiXactId that might exist in any database
2089 : : * of our cluster), and the OID of the (or a) database with that value.
2090 : : *
2091 : : * This also updates MultiXactState->oldestOffset, by looking up the offset of
2092 : : * MultiXactState->oldestMultiXactId.
2093 : : */
2094 : : void
331 heikki.linnakangas@i 2095 : 2354 : SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid)
2096 : : {
2097 : : MultiXactId multiVacLimit;
2098 : : MultiXactId multiWarnLimit;
2099 : : MultiXactId multiStopLimit;
2100 : : MultiXactId multiWrapLimit;
2101 : : MultiXactId curMulti;
2102 : :
5034 alvherre@alvh.no-ip. 2103 [ - + ]: 2354 : Assert(MultiXactIdIsValid(oldest_datminmxid));
2104 : :
2105 : : /*
2106 : : * We pretend that a wrap will happen halfway through the multixact ID
2107 : : * space, but that's not really true, because multixacts wrap differently
2108 : : * from transaction IDs.
2109 : : */
2110 : 2354 : multiWrapLimit = oldest_datminmxid + (MaxMultiXactId >> 1);
2111 [ - + ]: 2354 : if (multiWrapLimit < FirstMultiXactId)
5034 alvherre@alvh.no-ip. 2112 :UBC 0 : multiWrapLimit += FirstMultiXactId;
2113 : :
2114 : : /*
2115 : : * We'll refuse to continue assigning MultiXactIds once we get within 3M
2116 : : * multi of data loss. See SetTransactionIdLimit.
2117 : : */
2287 noah@leadboat.com 2118 :CBC 2354 : multiStopLimit = multiWrapLimit - 3000000;
5034 alvherre@alvh.no-ip. 2119 [ - + ]: 2354 : if (multiStopLimit < FirstMultiXactId)
5034 alvherre@alvh.no-ip. 2120 :UBC 0 : multiStopLimit -= FirstMultiXactId;
2121 : :
2122 : : /*
2123 : : * We'll start complaining loudly when we get within 100M multis of data
2124 : : * loss. This is kind of arbitrary, but if you let your gas gauge get
2125 : : * down to 5% of full, would you be looking for the next gas station? We
2126 : : * need to be fairly liberal about this number because there are lots of
2127 : : * scenarios where most transactions are done by automatic clients that
2128 : : * won't pay attention to warnings. (No, we're not gonna make this
2129 : : * configurable. If you know enough to configure it, you know enough to
2130 : : * not get in this kind of trouble in the first place.)
2131 : : */
230 nathan@postgresql.or 2132 :CBC 2354 : multiWarnLimit = multiWrapLimit - 100000000;
5034 alvherre@alvh.no-ip. 2133 [ - + ]: 2354 : if (multiWarnLimit < FirstMultiXactId)
5034 alvherre@alvh.no-ip. 2134 :UBC 0 : multiWarnLimit -= FirstMultiXactId;
2135 : :
2136 : : /*
2137 : : * We'll start trying to force autovacuums when oldest_datminmxid gets to
2138 : : * be more than autovacuum_multixact_freeze_max_age mxids old.
2139 : : *
2140 : : * Note: autovacuum_multixact_freeze_max_age is a PGC_POSTMASTER parameter
2141 : : * so that we don't have to worry about dealing with on-the-fly changes in
2142 : : * its value. See SetTransactionIdLimit.
2143 : : */
4648 alvherre@alvh.no-ip. 2144 :CBC 2354 : multiVacLimit = oldest_datminmxid + autovacuum_multixact_freeze_max_age;
5034 2145 [ - + ]: 2354 : if (multiVacLimit < FirstMultiXactId)
5034 alvherre@alvh.no-ip. 2146 :UBC 0 : multiVacLimit += FirstMultiXactId;
2147 : :
2148 : : /* Grab lock for just long enough to set the new limit values */
5034 alvherre@alvh.no-ip. 2149 :CBC 2354 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2150 : 2354 : MultiXactState->oldestMultiXactId = oldest_datminmxid;
2151 : 2354 : MultiXactState->oldestMultiXactDB = oldest_datoid;
2152 : 2354 : MultiXactState->multiVacLimit = multiVacLimit;
2153 : 2354 : MultiXactState->multiWarnLimit = multiWarnLimit;
2154 : 2354 : MultiXactState->multiStopLimit = multiStopLimit;
2155 : 2354 : MultiXactState->multiWrapLimit = multiWrapLimit;
2156 : 2354 : curMulti = MultiXactState->nextMXact;
2157 : 2354 : LWLockRelease(MultiXactGenLock);
2158 : :
2159 : : /* Log the info */
2160 [ + + ]: 2354 : ereport(DEBUG1,
2161 : : (errmsg_internal("MultiXactId wrap limit is %u, limited by database with OID %u",
2162 : : multiWrapLimit, oldest_datoid)));
2163 : :
2164 : : /*
2165 : : * Computing the actual limits is only possible once the data directory is
2166 : : * in a consistent state. There's no need to compute the limits while
2167 : : * still replaying WAL - no decisions about new multis are made even
2168 : : * though multixact creations might be replayed. So we'll only do further
2169 : : * checks after TrimMultiXact() has been called.
2170 : : */
4058 andres@anarazel.de 2171 [ + + ]: 2354 : if (!MultiXactState->finishedStartup)
2172 : 1186 : return;
2173 : :
2174 [ - + ]: 1168 : Assert(!InRecovery);
2175 : :
2176 : : /*
2177 : : * Offsets are 64-bits wide and never wrap around, so we don't need to
2178 : : * consider them for emergency autovacuum purposes. But now that we're in
2179 : : * a consistent state, determine MultiXactState->oldestOffset. It will be
2180 : : * used to adjust the freezing cutoff, to keep the members disk usage in
2181 : : * check.
2182 : : */
331 heikki.linnakangas@i 2183 : 1168 : SetOldestOffset();
2184 : :
2185 : : /*
2186 : : * If past the autovacuum force point, immediately signal an autovac
2187 : : * request. The reason for this is that autovac only processes one
2188 : : * database per invocation. Once it's finished cleaning up the oldest
2189 : : * database, it'll call here, and we'll signal the postmaster to start
2190 : : * another iteration immediately if there are still any old databases.
2191 : : */
2192 [ - + - - ]: 1168 : if (MultiXactIdPrecedes(multiVacLimit, curMulti) && IsUnderPostmaster)
5034 alvherre@alvh.no-ip. 2193 :UBC 0 : SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER);
2194 : :
2195 : : /* Give an immediate warning if past the wrap warn point */
4058 andres@anarazel.de 2196 [ - + ]:CBC 1168 : if (MultiXactIdPrecedes(multiWarnLimit, curMulti))
2197 : : {
2198 : : char *oldest_datname;
2199 : :
2200 : : /*
2201 : : * We can be called when not inside a transaction, for example during
2202 : : * StartupXLOG(). In such a case we cannot do database access, so we
2203 : : * must just report the oldest DB's OID.
2204 : : *
2205 : : * Note: it's also possible that get_database_name fails and returns
2206 : : * NULL, for example because the database just got dropped. We'll
2207 : : * still warn, even though the warning might now be unnecessary.
2208 : : */
5034 alvherre@alvh.no-ip. 2209 [ # # ]:UBC 0 : if (IsTransactionState())
2210 : 0 : oldest_datname = get_database_name(oldest_datoid);
2211 : : else
2212 : 0 : oldest_datname = NULL;
2213 : :
2214 [ # # ]: 0 : if (oldest_datname)
2215 [ # # ]: 0 : ereport(WARNING,
2216 : : (errmsg_plural("database \"%s\" must be vacuumed before %u more MultiXactId is used",
2217 : : "database \"%s\" must be vacuumed before %u more MultiXactIds are used",
2218 : : multiWrapLimit - curMulti,
2219 : : oldest_datname,
2220 : : multiWrapLimit - curMulti),
2221 : : errdetail("Approximately %.2f%% of MultiXactId space remains before wraparound.",
2222 : : (double) (multiWrapLimit - curMulti) / (MaxMultiXactId / 2) * 100),
2223 : : errhint("To avoid MultiXactId assignment failures, execute a database-wide VACUUM in that database.\n"
2224 : : "You might also need to commit or roll back old prepared transactions.")));
2225 : : else
2226 [ # # ]: 0 : ereport(WARNING,
2227 : : (errmsg_plural("database with OID %u must be vacuumed before %u more MultiXactId is used",
2228 : : "database with OID %u must be vacuumed before %u more MultiXactIds are used",
2229 : : multiWrapLimit - curMulti,
2230 : : oldest_datoid,
2231 : : multiWrapLimit - curMulti),
2232 : : errdetail("Approximately %.2f%% of MultiXactId space remains before wraparound.",
2233 : : (double) (multiWrapLimit - curMulti) / (MaxMultiXactId / 2) * 100),
2234 : : errhint("To avoid MultiXactId assignment failures, execute a database-wide VACUUM in that database.\n"
2235 : : "You might also need to commit or roll back old prepared transactions.")));
2236 : : }
2237 : : }
2238 : :
2239 : : /*
2240 : : * Ensure the next-to-be-assigned MultiXactId is at least minMulti,
2241 : : * and similarly nextOffset is at least minMultiOffset.
2242 : : *
2243 : : * This is used when we can determine minimum safe values from an XLog
2244 : : * record (either an on-line checkpoint or an mxact creation log entry).
2245 : : * Although this is only called during XLog replay, we take the lock in case
2246 : : * any hot-standby backends are examining the values.
2247 : : */
2248 : : void
7820 tgl@sss.pgh.pa.us 2249 :CBC 738 : MultiXactAdvanceNextMXact(MultiXactId minMulti,
2250 : : MultiXactOffset minMultiOffset)
2251 : : {
328 heikki.linnakangas@i 2252 [ - + ]: 738 : Assert(MultiXactIdIsValid(minMulti));
2253 : :
5386 tgl@sss.pgh.pa.us 2254 : 738 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
7861 2255 [ + + ]: 738 : if (MultiXactIdPrecedes(MultiXactState->nextMXact, minMulti))
2256 : : {
2257 : : debug_elog3(DEBUG2, "MultiXact: setting next multi to %u", minMulti);
2258 : 6 : MultiXactState->nextMXact = minMulti;
2259 : : }
331 heikki.linnakangas@i 2260 [ + + ]: 738 : if (MultiXactState->nextOffset < minMultiOffset)
2261 : : {
2262 : : debug_elog3(DEBUG2, "MultiXact: setting next offset to %" PRIu64,
2263 : : minMultiOffset);
7820 tgl@sss.pgh.pa.us 2264 : 6 : MultiXactState->nextOffset = minMultiOffset;
2265 : : }
5386 2266 : 738 : LWLockRelease(MultiXactGenLock);
7861 2267 : 738 : }
2268 : :
2269 : : /*
2270 : : * Update our oldestMultiXactId value, but only if it's more recent than what
2271 : : * we had.
2272 : : *
2273 : : * This may only be called during WAL replay.
2274 : : */
2275 : : void
5034 alvherre@alvh.no-ip. 2276 : 782 : MultiXactAdvanceOldest(MultiXactId oldestMulti, Oid oldestMultiDB)
2277 : : {
4058 andres@anarazel.de 2278 [ - + ]: 782 : Assert(InRecovery);
2279 : :
5034 alvherre@alvh.no-ip. 2280 [ - + ]: 782 : if (MultiXactIdPrecedes(MultiXactState->oldestMultiXactId, oldestMulti))
331 heikki.linnakangas@i 2281 :UBC 0 : SetMultiXactIdLimit(oldestMulti, oldestMultiDB);
4514 alvherre@alvh.no-ip. 2282 :CBC 782 : }
2283 : :
2284 : : /*
2285 : : * Make sure that MultiXactOffset has room for a newly-allocated MultiXactId.
2286 : : *
2287 : : * NB: this is called while holding MultiXactGenLock. We want it to be very
2288 : : * fast most of the time; even when it's not so fast, no actual I/O need
2289 : : * happen unless we're forced to write out a dirty log or xlog page to make
2290 : : * room in shared memory.
2291 : : */
2292 : : static void
7861 tgl@sss.pgh.pa.us 2293 : 5315 : ExtendMultiXactOffset(MultiXactId multi)
2294 : : {
2295 : : int64 pageno;
2296 : : LWLock *lock;
2297 : :
2298 : : /*
2299 : : * No work except at first MultiXactId of a page. But beware: just after
2300 : : * wraparound, the first MultiXactId of page zero is FirstMultiXactId.
2301 : : */
2302 [ + + + + ]: 5315 : if (MultiXactIdToOffsetEntry(multi) != 0 &&
2303 : : multi != FirstMultiXactId)
2304 : 5310 : return;
2305 : :
2306 : 5 : pageno = MultiXactIdToOffsetPage(multi);
981 alvherre@alvh.no-ip. 2307 : 5 : lock = SimpleLruGetBankLock(MultiXactOffsetCtl, pageno);
2308 : :
2309 : 5 : LWLockAcquire(lock, LW_EXCLUSIVE);
2310 : :
2311 : : /* Zero the page and make a WAL entry about it */
486 alvherre@kurilemu.de 2312 : 5 : SimpleLruZeroPage(MultiXactOffsetCtl, pageno);
2313 : 5 : XLogSimpleInsertInt64(RM_MULTIXACT_ID, XLOG_MULTIXACT_ZERO_OFF_PAGE,
2314 : : pageno);
2315 : :
981 alvherre@alvh.no-ip. 2316 : 5 : LWLockRelease(lock);
2317 : : }
2318 : :
2319 : : /*
2320 : : * Make sure that MultiXactMember has room for the members of a newly-
2321 : : * allocated MultiXactId.
2322 : : *
2323 : : * Like the above routine, this is called while holding MultiXactGenLock;
2324 : : * same comments apply.
2325 : : */
2326 : : static void
7820 tgl@sss.pgh.pa.us 2327 : 5315 : ExtendMultiXactMember(MultiXactOffset offset, int nmembers)
2328 : : {
2329 : : /*
2330 : : * It's possible that the members span more than one page of the members
2331 : : * file, so we loop to ensure we consider each page. The coding is not
2332 : : * optimal if the members span several pages, but that seems unusual
2333 : : * enough to not worry much about.
2334 : : */
2335 [ + + ]: 10684 : while (nmembers > 0)
2336 : : {
2337 : : int flagsoff;
2338 : : int flagsbit;
2339 : : uint32 difference;
2340 : :
2341 : : /*
2342 : : * Only zero when at first entry of a page.
2343 : : */
5034 alvherre@alvh.no-ip. 2344 : 5369 : flagsoff = MXOffsetToFlagsOffset(offset);
2345 : 5369 : flagsbit = MXOffsetToFlagsBitShift(offset);
2346 [ + + + + ]: 5369 : if (flagsoff == 0 && flagsbit == 0)
2347 : : {
2348 : : int64 pageno;
2349 : : LWLock *lock;
2350 : :
7820 tgl@sss.pgh.pa.us 2351 : 57 : pageno = MXOffsetToMemberPage(offset);
981 alvherre@alvh.no-ip. 2352 : 57 : lock = SimpleLruGetBankLock(MultiXactMemberCtl, pageno);
2353 : :
2354 : 57 : LWLockAcquire(lock, LW_EXCLUSIVE);
2355 : :
2356 : : /* Zero the page and make a WAL entry about it */
486 alvherre@kurilemu.de 2357 : 57 : SimpleLruZeroPage(MultiXactMemberCtl, pageno);
2358 : 57 : XLogSimpleInsertInt64(RM_MULTIXACT_ID,
2359 : : XLOG_MULTIXACT_ZERO_MEM_PAGE, pageno);
2360 : :
981 alvherre@alvh.no-ip. 2361 : 57 : LWLockRelease(lock);
2362 : : }
2363 : :
2364 : : /* Compute the number of items till end of current page. */
331 heikki.linnakangas@i 2365 : 5369 : difference = MULTIXACT_MEMBERS_PER_PAGE - offset % MULTIXACT_MEMBERS_PER_PAGE;
2366 : :
2367 : : /*
2368 : : * Advance to next page. OK if nmembers goes negative.
2369 : : */
4532 alvherre@alvh.no-ip. 2370 : 5369 : nmembers -= difference;
2371 : 5369 : offset += difference;
2372 : : }
7861 tgl@sss.pgh.pa.us 2373 : 5315 : }
2374 : :
2375 : : /*
2376 : : * GetOldestMultiXactId
2377 : : *
2378 : : * Return the oldest MultiXactId that's still possibly still seen as live by
2379 : : * any running transaction. Older ones might still exist on disk, but they no
2380 : : * longer have any running member transaction.
2381 : : *
2382 : : * It's not safe to truncate MultiXact SLRU segments on the value returned by
2383 : : * this function; however, it can be set as the new relminmxid for any table
2384 : : * that VACUUM knows has no remaining MXIDs < the same value. It is only safe
2385 : : * to truncate SLRUs when no table can possibly still have a referencing MXID.
2386 : : */
2387 : : MultiXactId
5034 alvherre@alvh.no-ip. 2388 : 60268 : GetOldestMultiXactId(void)
2389 : : {
2390 : : MultiXactId oldestMXact;
2391 : :
2392 : : /*
2393 : : * This is the oldest valid value among all the OldestMemberMXactId[] and
2394 : : * OldestVisibleMXactId[] entries, or nextMXact if none are valid.
2395 : : */
7861 tgl@sss.pgh.pa.us 2396 : 60268 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
328 heikki.linnakangas@i 2397 : 60268 : oldestMXact = MultiXactState->nextMXact;
248 2398 [ + + ]: 7282987 : for (int i = 0; i < NumMemberSlots; i++)
2399 : : {
2400 : : MultiXactId thisoldest;
2401 : :
7861 tgl@sss.pgh.pa.us 2402 : 7222719 : thisoldest = OldestMemberMXactId[i];
2403 [ + + + + ]: 7257080 : if (MultiXactIdIsValid(thisoldest) &&
2404 : 34361 : MultiXactIdPrecedes(thisoldest, oldestMXact))
2405 : 61 : oldestMXact = thisoldest;
2406 : : }
248 heikki.linnakangas@i 2407 [ + + ]: 7174724 : for (int i = 0; i < NumVisibleSlots; i++)
2408 : : {
2409 : : MultiXactId thisoldest;
2410 : :
7861 tgl@sss.pgh.pa.us 2411 : 7114456 : thisoldest = OldestVisibleMXactId[i];
2412 [ + + + + ]: 7114463 : if (MultiXactIdIsValid(thisoldest) &&
2413 : 7 : MultiXactIdPrecedes(thisoldest, oldestMXact))
2414 : 2 : oldestMXact = thisoldest;
2415 : : }
2416 : :
2417 : 60268 : LWLockRelease(MultiXactGenLock);
2418 : :
5034 alvherre@alvh.no-ip. 2419 : 60268 : return oldestMXact;
2420 : : }
2421 : :
2422 : : /*
2423 : : * Calculate the oldest member offset and install it in MultiXactState, where
2424 : : * it can be used to adjust multixid freezing cutoffs.
2425 : : */
2426 : : static void
331 heikki.linnakangas@i 2427 : 1168 : SetOldestOffset(void)
2428 : : {
2429 : : MultiXactId oldestMultiXactId;
2430 : : MultiXactId nextMXact;
4058 andres@anarazel.de 2431 : 1168 : MultiXactOffset oldestOffset = 0; /* placate compiler */
2432 : : MultiXactOffset nextOffset;
4171 rhaas@postgresql.org 2433 : 1168 : bool oldestOffsetKnown = false;
2434 : :
2435 : : /*
2436 : : * NB: Have to prevent concurrent truncation, we might otherwise try to
2437 : : * lookup an oldestMulti that's concurrently getting truncated away.
2438 : : */
4058 andres@anarazel.de 2439 : 1168 : LWLockAcquire(MultiXactTruncationLock, LW_SHARED);
2440 : :
2441 : : /* Read relevant fields from shared memory. */
4171 rhaas@postgresql.org 2442 : 1168 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
2443 : 1168 : oldestMultiXactId = MultiXactState->oldestMultiXactId;
2444 : 1168 : nextMXact = MultiXactState->nextMXact;
2445 : 1168 : nextOffset = MultiXactState->nextOffset;
4058 andres@anarazel.de 2446 [ - + ]: 1168 : Assert(MultiXactState->finishedStartup);
4209 alvherre@alvh.no-ip. 2447 : 1168 : LWLockRelease(MultiXactGenLock);
2448 : :
2449 : : /*
2450 : : * Determine the offset of the oldest multixact. Normally, we can read
2451 : : * the offset from the multixact itself, but there's an important special
2452 : : * case: if there are no multixacts in existence at all, oldestMXact
2453 : : * obviously can't point to one. It will instead point to the multixact
2454 : : * ID that will be assigned the next time one is needed.
2455 : : */
4171 rhaas@postgresql.org 2456 [ + + ]: 1168 : if (oldestMultiXactId == nextMXact)
2457 : : {
2458 : : /*
2459 : : * When the next multixact gets created, it will be stored at the next
2460 : : * offset.
2461 : : */
2462 : 1146 : oldestOffset = nextOffset;
2463 : 1146 : oldestOffsetKnown = true;
2464 : : }
2465 : : else
2466 : : {
2467 : : /*
2468 : : * Look up the offset at which the oldest existing multixact's members
2469 : : * are stored. If we cannot find it, be careful not to fail, and
2470 : : * leave oldestOffset unchanged. oldestOffset is initialized to zero
2471 : : * at system startup, which prevents truncating members until a proper
2472 : : * value is calculated.
2473 : : *
2474 : : * (We had bugs in early releases of PostgreSQL 9.3.X and 9.4.X where
2475 : : * the supposedly-earliest multixact might not really exist. Those
2476 : : * should be long gone by now, so this should not fail, but let's
2477 : : * still be defensive.)
2478 : : */
2479 : : oldestOffsetKnown =
2480 : 22 : find_multixact_start(oldestMultiXactId, &oldestOffset);
2481 : :
2482 [ + - ]: 22 : if (oldestOffsetKnown)
2483 [ - + ]: 22 : ereport(DEBUG1,
2484 : : (errmsg_internal("oldest MultiXactId member is at offset %" PRIu64,
2485 : : oldestOffset)));
2486 : : else
4058 andres@anarazel.de 2487 [ # # ]:UBC 0 : ereport(LOG,
2488 : : (errmsg("MultiXact member truncation is disabled because oldest checkpointed MultiXact %u does not exist on disk",
2489 : : oldestMultiXactId)));
2490 : : }
2491 : :
4058 andres@anarazel.de 2492 :CBC 1168 : LWLockRelease(MultiXactTruncationLock);
2493 : :
2494 : : /* Install the computed value */
2495 [ + - ]: 1168 : if (oldestOffsetKnown)
2496 : : {
331 heikki.linnakangas@i 2497 : 1168 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2498 : 1168 : MultiXactState->oldestOffset = oldestOffset;
2499 : 1168 : LWLockRelease(MultiXactGenLock);
2500 : : }
4209 alvherre@alvh.no-ip. 2501 : 1168 : }
2502 : :
2503 : : /*
2504 : : * Find the starting offset of the given MultiXactId.
2505 : : *
2506 : : * Returns false if the file containing the multi does not exist on disk.
2507 : : * Otherwise, returns true and sets *result to the starting member offset.
2508 : : *
2509 : : * This function does not prevent concurrent truncation, so if that's
2510 : : * required, the caller has to protect against that.
2511 : : */
2512 : : static bool
4171 rhaas@postgresql.org 2513 : 22 : find_multixact_start(MultiXactId multi, MultiXactOffset *result)
2514 : : {
2515 : : MultiXactOffset offset;
2516 : : int64 pageno;
2517 : : int entryno;
2518 : : int slotno;
2519 : : MultiXactOffset *offptr;
2520 : :
4058 andres@anarazel.de 2521 [ - + ]: 22 : Assert(MultiXactState->finishedStartup);
2522 : :
4209 alvherre@alvh.no-ip. 2523 : 22 : pageno = MultiXactIdToOffsetPage(multi);
2524 : 22 : entryno = MultiXactIdToOffsetEntry(multi);
2525 : :
2526 : : /*
2527 : : * Write out dirty data, so PhysicalPageExists can work correctly.
2528 : : */
2232 tmunro@postgresql.or 2529 : 22 : SimpleLruWriteAll(MultiXactOffsetCtl, true);
2530 : 22 : SimpleLruWriteAll(MultiXactMemberCtl, true);
2531 : :
4171 rhaas@postgresql.org 2532 [ - + ]: 22 : if (!SimpleLruDoesPhysicalPageExist(MultiXactOffsetCtl, pageno))
4171 rhaas@postgresql.org 2533 :UBC 0 : return false;
2534 : :
2535 : : /* lock is acquired by SimpleLruReadPage_ReadOnly */
237 heikki.linnakangas@i 2536 :CBC 22 : slotno = SimpleLruReadPage_ReadOnly(MultiXactOffsetCtl, pageno, &multi);
4209 alvherre@alvh.no-ip. 2537 : 22 : offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
2538 : 22 : offptr += entryno;
2539 : 22 : offset = *offptr;
981 2540 : 22 : LWLockRelease(SimpleLruGetBankLock(MultiXactOffsetCtl, pageno));
2541 : :
4171 rhaas@postgresql.org 2542 : 22 : *result = offset;
2543 : 22 : return true;
2544 : : }
2545 : :
2546 : : /*
2547 : : * GetMultiXactInfo
2548 : : *
2549 : : * Returns information about the current MultiXact state, as of:
2550 : : * multixacts: Number of MultiXacts (nextMultiXactId - oldestMultiXactId)
2551 : : * nextOffset: Next-to-be-assigned offset
2552 : : * oldestMultiXactId: Oldest MultiXact ID still in use
2553 : : * oldestOffset: Oldest offset still in use
2554 : : */
2555 : : void
310 michael@paquier.xyz 2556 : 16595 : GetMultiXactInfo(uint32 *multixacts, MultiXactOffset *nextOffset,
2557 : : MultiXactId *oldestMultiXactId, MultiXactOffset *oldestOffset)
2558 : : {
2559 : : MultiXactId nextMultiXactId;
2560 : :
4199 rhaas@postgresql.org 2561 : 16595 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
310 michael@paquier.xyz 2562 : 16595 : *nextOffset = MultiXactState->nextOffset;
443 2563 : 16595 : *oldestMultiXactId = MultiXactState->oldestMultiXactId;
4199 rhaas@postgresql.org 2564 : 16595 : nextMultiXactId = MultiXactState->nextMXact;
443 michael@paquier.xyz 2565 : 16595 : *oldestOffset = MultiXactState->oldestOffset;
4199 rhaas@postgresql.org 2566 : 16595 : LWLockRelease(MultiXactGenLock);
2567 : :
443 michael@paquier.xyz 2568 : 16595 : *multixacts = nextMultiXactId - *oldestMultiXactId;
4199 rhaas@postgresql.org 2569 : 16595 : }
2570 : :
2571 : : /*
2572 : : * Multixact members can be removed once the multixacts that refer to them
2573 : : * are older than every datminmxid. autovacuum_multixact_freeze_max_age and
2574 : : * vacuum_multixact_freeze_table_age work together to make sure we never have
2575 : : * too many multixacts; we hope that, at least under normal circumstances,
2576 : : * this will also be sufficient to keep us from using too many offsets.
2577 : : * However, if the average multixact has many members, we might accumulate a
2578 : : * large amount of members, consuming disk space, while still using few enough
2579 : : * multixids that the multixid limits fail to trigger relminmxid advancement
2580 : : * by VACUUM.
2581 : : *
2582 : : * To prevent that, if the members space usage exceeds a threshold
2583 : : * (MULTIXACT_MEMBER_LOW_THRESHOLD), we effectively reduce
2584 : : * autovacuum_multixact_freeze_max_age to a value just less than the number of
2585 : : * multixacts in use. We hope that this will quickly trigger autovacuuming on
2586 : : * the table or tables with the oldest relminmxid, thus allowing datminmxid
2587 : : * values to advance and removing some members.
2588 : : *
2589 : : * As the amount of the member space in use grows, we become more aggressive
2590 : : * in clamping this value. That not only causes autovacuum to ramp up, but
2591 : : * also makes any manual vacuums the user issues more aggressive. This
2592 : : * happens because vacuum_get_cutoffs() will clamp the freeze table and the
2593 : : * minimum freeze age cutoffs based on the effective
2594 : : * autovacuum_multixact_freeze_max_age this function returns. At the extreme,
2595 : : * when the members usage reaches MULTIXACT_MEMBER_HIGH_THRESHOLD, we clamp
2596 : : * freeze_max_age to zero, and every vacuum of any table will freeze every
2597 : : * multixact.
2598 : : */
2599 : : int
2600 : 16584 : MultiXactMemberFreezeThreshold(void)
2601 : : {
2602 : : uint32 multixacts;
2603 : : uint32 victim_multixacts;
2604 : : double fraction;
2605 : : int result;
2606 : : MultiXactId oldestMultiXactId;
2607 : : MultiXactOffset oldestOffset;
2608 : : MultiXactOffset nextOffset;
2609 : : uint64 members;
2610 : :
2611 : : /* Read the current offsets and multixact usage. */
310 michael@paquier.xyz 2612 : 16584 : GetMultiXactInfo(&multixacts, &nextOffset, &oldestMultiXactId, &oldestOffset);
2613 : 16584 : members = nextOffset - oldestOffset;
2614 : :
2615 : : /* If member space utilization is low, no special action is required. */
331 heikki.linnakangas@i 2616 [ + - ]: 16584 : if (members <= MULTIXACT_MEMBER_LOW_THRESHOLD)
4199 rhaas@postgresql.org 2617 : 16584 : return autovacuum_multixact_freeze_max_age;
2618 : :
2619 : : /*
2620 : : * Compute a target for relminmxid advancement. The number of multixacts
2621 : : * we try to eliminate from the system is based on how far we are past
2622 : : * MULTIXACT_MEMBER_LOW_THRESHOLD.
2623 : : *
2624 : : * The way this formula works is that when members is exactly at the low
2625 : : * threshold, fraction = 0.0, and we set freeze_max_age equal to
2626 : : * mxid_age(oldestMultiXactId). As members grows further, towards the
2627 : : * high threshold, fraction grows linearly from 0.0 to 1.0, and the result
2628 : : * shrinks from mxid_age(oldestMultiXactId) to 0. Beyond the high
2629 : : * threshold, fraction > 1.0 and the result is clamped to 0.
2630 : : */
331 heikki.linnakangas@i 2631 :UBC 0 : fraction = (double) (members - MULTIXACT_MEMBER_LOW_THRESHOLD) /
2632 : : (MULTIXACT_MEMBER_HIGH_THRESHOLD - MULTIXACT_MEMBER_LOW_THRESHOLD);
2633 : :
2634 : : /* fraction could be > 1.0, but lowest possible freeze age is zero */
2635 [ # # ]: 0 : if (fraction >= 1.0)
4199 rhaas@postgresql.org 2636 : 0 : return 0;
2637 : :
331 heikki.linnakangas@i 2638 : 0 : victim_multixacts = multixacts * fraction;
875 2639 : 0 : result = multixacts - victim_multixacts;
2640 : :
2641 : : /*
2642 : : * Clamp to autovacuum_multixact_freeze_max_age, so that we never make
2643 : : * autovacuum less aggressive than it would otherwise be.
2644 : : */
2645 : 0 : return Min(result, autovacuum_multixact_freeze_max_age);
2646 : : }
2647 : :
2648 : :
2649 : : /*
2650 : : * Delete members segments older than newOldestOffset
2651 : : */
2652 : : static void
294 2653 : 0 : PerformMembersTruncation(MultiXactOffset newOldestOffset)
2654 : : {
2655 : : /*
2656 : : * We step back one entry to avoid passing a cutoff page that hasn't been
2657 : : * created yet in the rare case that oldestOffset would be the first item
2658 : : * on a page and oldestOffset == nextOffset. In that case, if we didn't
2659 : : * subtract one, we'd trigger SimpleLruTruncate's wraparound detection.
2660 : : */
50 2661 [ # # ]: 0 : if (newOldestOffset <= 1)
2662 : 0 : return;
331 2663 : 0 : SimpleLruTruncate(MultiXactMemberCtl,
2664 : : MXOffsetToMemberPage(newOldestOffset - 1));
2665 : : }
2666 : :
2667 : : /*
2668 : : * Delete offsets segments older than newOldestMulti
2669 : : */
2670 : : static void
294 2671 : 0 : PerformOffsetsTruncation(MultiXactId newOldestMulti)
2672 : : {
2673 : : /*
2674 : : * Like in PerformMembersTruncation(), step back one entry to avoid
2675 : : * passing a cutoff page that hasn't been created yet.
2676 : : */
4058 andres@anarazel.de 2677 : 0 : SimpleLruTruncate(MultiXactOffsetCtl,
2678 : : MultiXactIdToOffsetPage(PreviousMultiXactId(newOldestMulti)));
2679 : 0 : }
2680 : :
2681 : : /*
2682 : : * Remove all MultiXactOffset and MultiXactMember segments before the oldest
2683 : : * ones still of interest.
2684 : : *
2685 : : * This is only called on a primary as part of vacuum (via
2686 : : * vac_truncate_clog()). During recovery truncation is done by replaying
2687 : : * truncation WAL records logged here.
2688 : : *
2689 : : * newOldestMulti is the oldest currently required multixact, newOldestMultiDB
2690 : : * is one of the databases preventing newOldestMulti from increasing.
2691 : : */
2692 : : void
4058 andres@anarazel.de 2693 :CBC 116 : TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
2694 : : {
2695 : : MultiXactId oldestMulti;
2696 : : MultiXactId nextMulti;
2697 : : MultiXactOffset newOldestOffset;
2698 : : MultiXactOffset nextOffset;
2699 : :
2700 [ - + ]: 116 : Assert(!RecoveryInProgress());
2701 [ - + ]: 116 : Assert(MultiXactState->finishedStartup);
328 heikki.linnakangas@i 2702 [ - + ]: 116 : Assert(MultiXactIdIsValid(newOldestMulti));
2703 : :
2704 : : /*
2705 : : * We can only allow one truncation to happen at once. Otherwise parts of
2706 : : * members might vanish while we're doing lookups or similar. There's no
2707 : : * need to have an interlock with creating new multis or such, since those
2708 : : * are constrained by the limits (which only grow, never shrink).
2709 : : */
4058 andres@anarazel.de 2710 : 116 : LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
2711 : :
4514 alvherre@alvh.no-ip. 2712 : 116 : LWLockAcquire(MultiXactGenLock, LW_SHARED);
4058 andres@anarazel.de 2713 : 116 : nextMulti = MultiXactState->nextMXact;
4171 rhaas@postgresql.org 2714 : 116 : nextOffset = MultiXactState->nextOffset;
4058 andres@anarazel.de 2715 : 116 : oldestMulti = MultiXactState->oldestMultiXactId;
4514 alvherre@alvh.no-ip. 2716 : 116 : LWLockRelease(MultiXactGenLock);
2717 : :
2718 : : /*
2719 : : * Make sure to only attempt truncation if there's values to truncate
2720 : : * away. In normal processing values shouldn't go backwards, but there's
2721 : : * some corner cases (due to bugs) where that's possible.
2722 : : */
4058 andres@anarazel.de 2723 [ + - ]: 116 : if (MultiXactIdPrecedesOrEquals(newOldestMulti, oldestMulti))
2724 : : {
2725 : 116 : LWLockRelease(MultiXactTruncationLock);
2726 : 116 : return;
2727 : : }
2728 : :
2729 : : /*
2730 : : * Compute up to where to truncate MultiXactMember. Lookup the
2731 : : * corresponding member offset for newOldestMulti for that.
2732 : : */
4058 andres@anarazel.de 2733 [ # # ]:UBC 0 : if (newOldestMulti == nextMulti)
2734 : : {
2735 : : /* there are NO MultiXacts */
2736 : 0 : newOldestOffset = nextOffset;
2737 : : }
2738 [ # # ]: 0 : else if (!find_multixact_start(newOldestMulti, &newOldestOffset))
2739 : : {
2740 [ # # ]: 0 : ereport(LOG,
2741 : : (errmsg("cannot truncate up to MultiXact %u because it does not exist on disk, skipping truncation",
2742 : : newOldestMulti)));
2743 : 0 : LWLockRelease(MultiXactTruncationLock);
2744 : 0 : return;
2745 : : }
2746 : :
2747 : : /*
2748 : : * On crash, MultiXactIdCreateFromMembers() can leave behind multixids
2749 : : * that were not yet written out and hence have zero offset on disk. If
2750 : : * such a multixid becomes oldestMulti, we won't be able to look up its
2751 : : * offset. That should be rare, so we don't try to do anything smart about
2752 : : * it. Just skip the truncation, and hope that by the next truncation
2753 : : * attempt, oldestMulti has advanced to a valid multixid.
2754 : : */
294 heikki.linnakangas@i 2755 [ # # ]: 0 : if (newOldestOffset == 0)
2756 : : {
2757 [ # # ]: 0 : ereport(LOG,
2758 : : (errmsg("cannot truncate up to MultiXact %u because it has invalid offset, skipping truncation",
2759 : : newOldestMulti)));
2760 : 0 : LWLockRelease(MultiXactTruncationLock);
2761 : 0 : return;
2762 : : }
2763 : :
4058 andres@anarazel.de 2764 [ # # ]: 0 : elog(DEBUG1, "performing multixact truncation: "
2765 : : "oldestMulti %u (offsets segment %" PRIx64 "), "
2766 : : "oldestOffset %" PRIu64 " (members segment %" PRIx64 ")",
2767 : : newOldestMulti,
2768 : : MultiXactIdToOffsetSegment(newOldestMulti),
2769 : : newOldestOffset,
2770 : : MXOffsetToMemberSegment(newOldestOffset));
2771 : :
2772 : : /*
2773 : : * Do truncation, and the WAL logging of the truncation, in a critical
2774 : : * section. That way offsets/members cannot get out of sync anymore, i.e.
2775 : : * once consistent the newOldestMulti will always exist in members, even
2776 : : * if we crashed in the wrong moment.
2777 : : */
2778 : 0 : START_CRIT_SECTION();
2779 : :
2780 : : /*
2781 : : * Prevent checkpoints from being scheduled concurrently. This is critical
2782 : : * because otherwise a truncation record might not be replayed after a
2783 : : * crash/basebackup, even though the state of the data directory would
2784 : : * require it.
2785 : : */
1672 rhaas@postgresql.org 2786 [ # # ]: 0 : Assert((MyProc->delayChkptFlags & DELAY_CHKPT_START) == 0);
2787 : 0 : MyProc->delayChkptFlags |= DELAY_CHKPT_START;
2788 : :
2789 : : /* WAL log truncation */
294 heikki.linnakangas@i 2790 : 0 : WriteMTruncateXlogRec(newOldestMultiDB, newOldestMulti, newOldestOffset);
2791 : :
2792 : : /*
2793 : : * Update in-memory limits before performing the truncation, while inside
2794 : : * the critical section: Have to do it before truncation, to prevent
2795 : : * concurrent lookups of those values. Has to be inside the critical
2796 : : * section as otherwise a future call to this function would error out,
2797 : : * while looking up the oldest member in offsets, if our caller crashes
2798 : : * before updating the limits.
2799 : : */
4058 andres@anarazel.de 2800 : 0 : LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
2801 : 0 : MultiXactState->oldestMultiXactId = newOldestMulti;
2802 : 0 : MultiXactState->oldestMultiXactDB = newOldestMultiDB;
331 heikki.linnakangas@i 2803 : 0 : MultiXactState->oldestOffset = newOldestOffset;
4058 andres@anarazel.de 2804 : 0 : LWLockRelease(MultiXactGenLock);
2805 : :
2806 : : /* First truncate members */
294 heikki.linnakangas@i 2807 : 0 : PerformMembersTruncation(newOldestOffset);
2808 : :
2809 : : /* Then offsets */
2810 : 0 : PerformOffsetsTruncation(newOldestMulti);
2811 : :
1672 rhaas@postgresql.org 2812 : 0 : MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
2813 : :
4058 andres@anarazel.de 2814 [ # # ]: 0 : END_CRIT_SECTION();
2815 : 0 : LWLockRelease(MultiXactTruncationLock);
2816 : : }
2817 : :
2818 : : /*
2819 : : * Decide whether a MultiXactOffset page number is "older" for truncation
2820 : : * purposes. Analogous to CLOGPagePrecedes().
2821 : : *
2822 : : * Offsetting the values is optional, because MultiXactIdPrecedes() has
2823 : : * translational symmetry.
2824 : : */
2825 : : static bool
1072 akorotkov@postgresql 2826 :CBC 50817 : MultiXactOffsetPagePrecedes(int64 page1, int64 page2)
2827 : : {
2828 : : MultiXactId multi1;
2829 : : MultiXactId multi2;
2830 : :
7861 tgl@sss.pgh.pa.us 2831 : 50817 : multi1 = ((MultiXactId) page1) * MULTIXACT_OFFSETS_PER_PAGE;
2119 noah@leadboat.com 2832 : 50817 : multi1 += FirstMultiXactId + 1;
7861 tgl@sss.pgh.pa.us 2833 : 50817 : multi2 = ((MultiXactId) page2) * MULTIXACT_OFFSETS_PER_PAGE;
2119 noah@leadboat.com 2834 : 50817 : multi2 += FirstMultiXactId + 1;
2835 : :
2836 [ + + + + ]: 84695 : return (MultiXactIdPrecedes(multi1, multi2) &&
2837 : 33878 : MultiXactIdPrecedes(multi1,
2838 : : multi2 + MULTIXACT_OFFSETS_PER_PAGE - 1));
2839 : : }
2840 : :
2841 : : /*
2842 : : * Decide whether a MultiXactMember page number is "older" for truncation
2843 : : * purposes. There is no "invalid offset number" and members never wrap
2844 : : * around, so use the numbers verbatim.
2845 : : */
2846 : : static bool
1072 akorotkov@postgresql 2847 :UBC 0 : MultiXactMemberPagePrecedes(int64 page1, int64 page2)
2848 : : {
331 heikki.linnakangas@i 2849 : 0 : return page1 < page2;
2850 : : }
2851 : :
2852 : : static int
237 2853 : 0 : MultiXactOffsetIoErrorDetail(const void *opaque_data)
2854 : : {
2855 : 0 : MultiXactId multixid = *(const MultiXactId *) opaque_data;
2856 : :
2857 : 0 : return errdetail("Could not access offset of multixact %u.", multixid);
2858 : : }
2859 : :
2860 : : static int
2861 : 0 : MultiXactMemberIoErrorDetail(const void *opaque_data)
2862 : : {
2863 : 0 : const MultiXactMemberSlruReadContext *context = opaque_data;
2864 : :
2865 [ # # ]: 0 : if (MultiXactIdIsValid(context->multi))
2866 : 0 : return errdetail("Could not access member of multixact %u at offset %" PRIu64 ".",
2867 : 0 : context->multi, context->offset);
2868 : : else
2869 : 0 : return errdetail("Could not access multixact member at offset %" PRIu64 ".",
2870 : 0 : context->offset);
2871 : : }
2872 : :
2873 : :
2874 : : /*
2875 : : * Write a TRUNCATE xlog record
2876 : : *
2877 : : * We must flush the xlog record to disk before returning --- see notes in
2878 : : * TruncateCLOG().
2879 : : */
2880 : : static void
4058 andres@anarazel.de 2881 : 0 : WriteMTruncateXlogRec(Oid oldestMultiDB,
2882 : : MultiXactId oldestMulti,
2883 : : MultiXactOffset oldestOffset)
2884 : : {
2885 : : XLogRecPtr recptr;
2886 : : xl_multixact_truncate xlrec;
2887 : :
2888 : 0 : xlrec.oldestMultiDB = oldestMultiDB;
294 heikki.linnakangas@i 2889 : 0 : xlrec.oldestMulti = oldestMulti;
2890 : 0 : xlrec.oldestOffset = oldestOffset;
2891 : :
4058 andres@anarazel.de 2892 : 0 : XLogBeginInsert();
632 peter@eisentraut.org 2893 : 0 : XLogRegisterData(&xlrec, SizeOfMultiXactTruncate);
4058 andres@anarazel.de 2894 : 0 : recptr = XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_TRUNCATE_ID);
2895 : 0 : XLogFlush(recptr);
2896 : 0 : }
2897 : :
2898 : : /*
2899 : : * MULTIXACT resource manager's routines
2900 : : */
2901 : : void
4368 heikki.linnakangas@i 2902 :CBC 6 : multixact_redo(XLogReaderState *record)
2903 : : {
2904 : 6 : uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
2905 : :
2906 : : /* Backup blocks are not used in multixact records */
2907 [ - + ]: 6 : Assert(!XLogRecHasAnyBlockRefs(record));
2908 : :
7820 tgl@sss.pgh.pa.us 2909 [ - + ]: 6 : if (info == XLOG_MULTIXACT_ZERO_OFF_PAGE)
2910 : : {
2911 : : int64 pageno;
2912 : :
1072 akorotkov@postgresql 2913 :UBC 0 : memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
486 alvherre@kurilemu.de 2914 : 0 : SimpleLruZeroAndWritePage(MultiXactOffsetCtl, pageno);
2915 : : }
7820 tgl@sss.pgh.pa.us 2916 [ - + ]:CBC 6 : else if (info == XLOG_MULTIXACT_ZERO_MEM_PAGE)
2917 : : {
2918 : : int64 pageno;
2919 : :
1072 akorotkov@postgresql 2920 :UBC 0 : memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
486 alvherre@kurilemu.de 2921 : 0 : SimpleLruZeroAndWritePage(MultiXactMemberCtl, pageno);
2922 : : }
7820 tgl@sss.pgh.pa.us 2923 [ + - ]:CBC 6 : else if (info == XLOG_MULTIXACT_CREATE_ID)
2924 : : {
5034 alvherre@alvh.no-ip. 2925 : 6 : xl_multixact_create *xlrec =
1266 tgl@sss.pgh.pa.us 2926 : 6 : (xl_multixact_create *) XLogRecGetData(record);
2927 : : TransactionId max_xid;
2928 : : int i;
2929 : :
2930 : : /* Store the data back into the SLRU files */
5034 alvherre@alvh.no-ip. 2931 : 6 : RecordNewMultiXact(xlrec->mid, xlrec->moff, xlrec->nmembers,
2932 : 6 : xlrec->members);
2933 : :
2934 : : /* Make sure nextMXact/nextOffset are beyond what this record has */
328 heikki.linnakangas@i 2935 : 6 : MultiXactAdvanceNextMXact(NextMultiXactId(xlrec->mid),
5034 alvherre@alvh.no-ip. 2936 : 6 : xlrec->moff + xlrec->nmembers);
2937 : :
2938 : : /*
2939 : : * Make sure nextXid is beyond any XID mentioned in the record. This
2940 : : * should be unnecessary, since any XID found here ought to have other
2941 : : * evidence in the XLOG, but let's be safe.
2942 : : */
4368 heikki.linnakangas@i 2943 : 6 : max_xid = XLogRecGetXid(record);
5034 alvherre@alvh.no-ip. 2944 [ + + ]: 18 : for (i = 0; i < xlrec->nmembers; i++)
2945 : : {
2946 [ - + ]: 12 : if (TransactionIdPrecedes(max_xid, xlrec->members[i].xid))
5034 alvherre@alvh.no-ip. 2947 :UBC 0 : max_xid = xlrec->members[i].xid;
2948 : : }
2949 : :
2779 tmunro@postgresql.or 2950 :CBC 6 : AdvanceNextFullTransactionIdPastXid(max_xid);
2951 : : }
4058 andres@anarazel.de 2952 [ # # ]:UBC 0 : else if (info == XLOG_MULTIXACT_TRUNCATE_ID)
2953 : : {
2954 : : xl_multixact_truncate xlrec;
2955 : :
2956 : 0 : memcpy(&xlrec, XLogRecGetData(record),
2957 : : SizeOfMultiXactTruncate);
2958 : :
2959 [ # # ]: 0 : elog(DEBUG1, "replaying multixact truncation: "
2960 : : "oldestMulti %u (offsets segment %" PRIx64 "), "
2961 : : "oldestOffset %" PRIu64 " (members segment %" PRIx64 ")",
2962 : : xlrec.oldestMulti,
2963 : : MultiXactIdToOffsetSegment(xlrec.oldestMulti),
2964 : : xlrec.oldestOffset,
2965 : : MXOffsetToMemberSegment(xlrec.oldestOffset));
2966 : :
2967 : : /* should not be required, but more than cheap enough */
2968 : 0 : LWLockAcquire(MultiXactTruncationLock, LW_EXCLUSIVE);
2969 : :
2970 : : /*
2971 : : * Advance the horizon values, so they're current at the end of
2972 : : * recovery.
2973 : : */
294 heikki.linnakangas@i 2974 : 0 : SetMultiXactIdLimit(xlrec.oldestMulti, xlrec.oldestMultiDB);
2975 : :
2976 : 0 : PerformMembersTruncation(xlrec.oldestOffset);
2977 : 0 : PerformOffsetsTruncation(xlrec.oldestMulti);
2978 : :
4058 andres@anarazel.de 2979 : 0 : LWLockRelease(MultiXactTruncationLock);
2980 : : }
2981 : : else
7820 tgl@sss.pgh.pa.us 2982 [ # # ]: 0 : elog(PANIC, "multixact_redo: unknown op code %u", info);
7820 tgl@sss.pgh.pa.us 2983 :CBC 6 : }
2984 : :
2985 : : /*
2986 : : * Entrypoint for sync.c to sync offsets files.
2987 : : */
2988 : : int
2232 tmunro@postgresql.or 2989 :UBC 0 : multixactoffsetssyncfiletag(const FileTag *ftag, char *path)
2990 : : {
2991 : 0 : return SlruSyncFileTag(MultiXactOffsetCtl, ftag, path);
2992 : : }
2993 : :
2994 : : /*
2995 : : * Entrypoint for sync.c to sync members files.
2996 : : */
2997 : : int
2998 : 0 : multixactmemberssyncfiletag(const FileTag *ftag, char *path)
2999 : : {
3000 : 0 : return SlruSyncFileTag(MultiXactMemberCtl, ftag, path);
3001 : : }
|