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