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