1
/*-------------------------------------------------------------------------
4
* PostgreSQL multi-transaction-log manager
6
* The pg_multixact manager is a pg_clog-like manager that stores an array
7
* of TransactionIds for each MultiXactId. It is a fundamental part of the
8
* shared-row-lock implementation. A share-locked tuple stores a
9
* MultiXactId in its Xmax, and a transaction that needs to wait for the
10
* tuple to be unlocked can sleep on the potentially-several TransactionIds
11
* that compose the MultiXactId.
13
* We use two SLRU areas, one for storing the offsets at which the data
14
* starts for each MultiXactId in the other one. This trick allows us to
15
* store variable length arrays of TransactionIds. (We could alternatively
16
* use one area containing counts and TransactionIds, with valid MultiXactId
17
* values pointing at slots containing counts; but that way seems less robust
18
* since it would get completely confused if someone inquired about a bogus
19
* MultiXactId that pointed to an intermediate slot containing an XID.)
21
* XLOG interactions: this module generates an XLOG record whenever a new
22
* OFFSETs or MEMBERs page is initialized to zeroes, as well as an XLOG record
23
* whenever a new MultiXactId is defined. This allows us to completely
24
* rebuild the data entered since the last checkpoint during XLOG replay.
25
* Because this is possible, we need not follow the normal rule of
26
* "write WAL before data"; the only correctness guarantee needed is that
27
* we flush and sync all dirty OFFSETs and MEMBERs pages to disk before a
28
* checkpoint is considered complete. If a page does make it to disk ahead
29
* of corresponding WAL records, it will be forcibly zeroed before use anyway.
30
* Therefore, we don't need to mark our pages with LSN information; we have
31
* enough synchronization already.
33
* Like clog.c, and unlike subtrans.c, we have to preserve state across
34
* crashes and ensure that MXID and offset numbering increases monotonically
35
* across a crash. We do this in the same way as it's done for transaction
36
* IDs: the WAL record is guaranteed to contain evidence of every MXID we
37
* could need to worry about, and we just make sure that at the end of
38
* replay, the next-MXID and next-offset counters are at least as large as
39
* anything we saw during replay.
42
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
43
* Portions Copyright (c) 1994, Regents of the University of California
45
* src/backend/access/transam/multixact.c
47
*-------------------------------------------------------------------------
51
#include "access/multixact.h"
52
#include "access/slru.h"
53
#include "access/transam.h"
54
#include "access/twophase.h"
55
#include "access/twophase_rmgr.h"
56
#include "access/xact.h"
57
#include "miscadmin.h"
59
#include "storage/backendid.h"
60
#include "storage/lmgr.h"
61
#include "storage/procarray.h"
62
#include "utils/builtins.h"
63
#include "utils/memutils.h"
67
* Defines for MultiXactOffset page sizes. A page is the same BLCKSZ as is
68
* used everywhere else in Postgres.
70
* Note: because both MultiXactOffsets and TransactionIds are 32 bits and
71
* wrap around at 0xFFFFFFFF, MultiXact page numbering also wraps around at
72
* 0xFFFFFFFF/MULTIXACT_*_PER_PAGE, and segment numbering at
73
* 0xFFFFFFFF/MULTIXACT_*_PER_PAGE/SLRU_SEGMENTS_PER_PAGE. We need take no
74
* explicit notice of that fact in this module, except when comparing segment
75
* and page numbers in TruncateMultiXact
76
* (see MultiXact{Offset,Member}PagePrecedes).
79
/* We need four bytes per offset and also four bytes per member */
80
#define MULTIXACT_OFFSETS_PER_PAGE (BLCKSZ / sizeof(MultiXactOffset))
81
#define MULTIXACT_MEMBERS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
83
#define MultiXactIdToOffsetPage(xid) \
84
((xid) / (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
85
#define MultiXactIdToOffsetEntry(xid) \
86
((xid) % (MultiXactOffset) MULTIXACT_OFFSETS_PER_PAGE)
88
#define MXOffsetToMemberPage(xid) \
89
((xid) / (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
90
#define MXOffsetToMemberEntry(xid) \
91
((xid) % (TransactionId) MULTIXACT_MEMBERS_PER_PAGE)
95
* Links to shared-memory data structures for MultiXact control
97
static SlruCtlData MultiXactOffsetCtlData;
98
static SlruCtlData MultiXactMemberCtlData;
100
#define MultiXactOffsetCtl (&MultiXactOffsetCtlData)
101
#define MultiXactMemberCtl (&MultiXactMemberCtlData)
104
* MultiXact state shared across all backends. All this state is protected
105
* by MultiXactGenLock. (We also use MultiXactOffsetControlLock and
106
* MultiXactMemberControlLock to guard accesses to the two sets of SLRU
107
* buffers. For concurrency's sake, we avoid holding more than one of these
110
typedef struct MultiXactStateData
112
/* next-to-be-assigned MultiXactId */
113
MultiXactId nextMXact;
115
/* next-to-be-assigned offset */
116
MultiXactOffset nextOffset;
118
/* the Offset SLRU area was last truncated at this MultiXactId */
119
MultiXactId lastTruncationPoint;
122
* Per-backend data starts here. We have two arrays stored in the area
123
* immediately following the MultiXactStateData struct. Each is indexed by
126
* In both arrays, there's a slot for all normal backends (1..MaxBackends)
127
* followed by a slot for max_prepared_xacts prepared transactions. Valid
128
* BackendIds start from 1; element zero of each array is never used.
130
* OldestMemberMXactId[k] is the oldest MultiXactId each backend's current
131
* transaction(s) could possibly be a member of, or InvalidMultiXactId
132
* when the backend has no live transaction that could possibly be a
133
* member of a MultiXact. Each backend sets its entry to the current
134
* nextMXact counter just before first acquiring a shared lock in a given
135
* transaction, and clears it at transaction end. (This works because only
136
* during or after acquiring a shared lock could an XID possibly become a
137
* member of a MultiXact, and that MultiXact would have to be created
138
* during or after the lock acquisition.)
140
* OldestVisibleMXactId[k] is the oldest MultiXactId each backend's
141
* current transaction(s) think is potentially live, or InvalidMultiXactId
142
* when not in a transaction or not in a transaction that's paid any
143
* attention to MultiXacts yet. This is computed when first needed in a
144
* given transaction, and cleared at transaction end. We can compute it
145
* as the minimum of the valid OldestMemberMXactId[] entries at the time
146
* we compute it (using nextMXact if none are valid). Each backend is
147
* required not to attempt to access any SLRU data for MultiXactIds older
148
* than its own OldestVisibleMXactId[] setting; this is necessary because
149
* the checkpointer could truncate away such data at any instant.
151
* The checkpointer can compute the safe truncation point as the oldest
152
* valid value among all the OldestMemberMXactId[] and
153
* OldestVisibleMXactId[] entries, or nextMXact if none are valid.
154
* Clearly, it is not possible for any later-computed OldestVisibleMXactId
155
* value to be older than this, and so there is no risk of truncating data
156
* that is still needed.
158
MultiXactId perBackendXactIds[1]; /* VARIABLE LENGTH ARRAY */
159
} MultiXactStateData;
162
* Last element of OldestMemberMXactID and OldestVisibleMXactId arrays.
163
* Valid elements are (1..MaxOldestSlot); element 0 is never used.
165
#define MaxOldestSlot (MaxBackends + max_prepared_xacts)
167
/* Pointers to the state data in shared memory */
168
static MultiXactStateData *MultiXactState;
169
static MultiXactId *OldestMemberMXactId;
170
static MultiXactId *OldestVisibleMXactId;
174
* Definitions for the backend-local MultiXactId cache.
176
* We use this cache to store known MultiXacts, so we don't need to go to
177
* SLRU areas everytime.
179
* The cache lasts for the duration of a single transaction, the rationale
180
* for this being that most entries will contain our own TransactionId and
181
* so they will be uninteresting by the time our next transaction starts.
182
* (XXX not clear that this is correct --- other members of the MultiXact
183
* could hang around longer than we did. However, it's not clear what a
184
* better policy for flushing old cache entries would be.)
186
* We allocate the cache entries in a memory context that is deleted at
187
* transaction end, so we don't need to do retail freeing of entries.
189
typedef struct mXactCacheEnt
191
struct mXactCacheEnt *next;
194
TransactionId xids[1]; /* VARIABLE LENGTH ARRAY */
197
static mXactCacheEnt *MXactCache = NULL;
198
static MemoryContext MXactContext = NULL;
201
#ifdef MULTIXACT_DEBUG
202
#define debug_elog2(a,b) elog(a,b)
203
#define debug_elog3(a,b,c) elog(a,b,c)
204
#define debug_elog4(a,b,c,d) elog(a,b,c,d)
205
#define debug_elog5(a,b,c,d,e) elog(a,b,c,d,e)
207
#define debug_elog2(a,b)
208
#define debug_elog3(a,b,c)
209
#define debug_elog4(a,b,c,d)
210
#define debug_elog5(a,b,c,d,e)
213
/* internal MultiXactId management */
214
static void MultiXactIdSetOldestVisible(void);
215
static MultiXactId CreateMultiXactId(int nxids, TransactionId *xids);
216
static void RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
217
int nxids, TransactionId *xids);
218
static MultiXactId GetNewMultiXactId(int nxids, MultiXactOffset *offset);
220
/* MultiXact cache management */
221
static MultiXactId mXactCacheGetBySet(int nxids, TransactionId *xids);
222
static int mXactCacheGetById(MultiXactId multi, TransactionId **xids);
223
static void mXactCachePut(MultiXactId multi, int nxids, TransactionId *xids);
225
#ifdef MULTIXACT_DEBUG
226
static char *mxid_to_string(MultiXactId multi, int nxids, TransactionId *xids);
229
/* management of SLRU infrastructure */
230
static int ZeroMultiXactOffsetPage(int pageno, bool writeXlog);
231
static int ZeroMultiXactMemberPage(int pageno, bool writeXlog);
232
static bool MultiXactOffsetPagePrecedes(int page1, int page2);
233
static bool MultiXactMemberPagePrecedes(int page1, int page2);
234
static bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2);
235
static bool MultiXactOffsetPrecedes(MultiXactOffset offset1,
236
MultiXactOffset offset2);
237
static void ExtendMultiXactOffset(MultiXactId multi);
238
static void ExtendMultiXactMember(MultiXactOffset offset, int nmembers);
239
static void TruncateMultiXact(void);
240
static void WriteMZeroPageXlogRec(int pageno, uint8 info);
245
* Construct a MultiXactId representing two TransactionIds.
247
* The two XIDs must be different.
249
* NB - we don't worry about our local MultiXactId cache here, because that
250
* is handled by the lower-level routines.
253
MultiXactIdCreate(TransactionId xid1, TransactionId xid2)
255
MultiXactId newMulti;
256
TransactionId xids[2];
258
AssertArg(TransactionIdIsValid(xid1));
259
AssertArg(TransactionIdIsValid(xid2));
261
Assert(!TransactionIdEquals(xid1, xid2));
264
* Note: unlike MultiXactIdExpand, we don't bother to check that both XIDs
265
* are still running. In typical usage, xid2 will be our own XID and the
266
* caller just did a check on xid1, so it'd be wasted effort.
272
newMulti = CreateMultiXactId(2, xids);
274
debug_elog5(DEBUG2, "Create: returning %u for %u, %u",
275
newMulti, xid1, xid2);
282
* Add a TransactionId to a pre-existing MultiXactId.
284
* If the TransactionId is already a member of the passed MultiXactId,
285
* just return it as-is.
287
* Note that we do NOT actually modify the membership of a pre-existing
288
* MultiXactId; instead we create a new one. This is necessary to avoid
289
* a race condition against MultiXactIdWait (see notes there).
291
* NB - we don't worry about our local MultiXactId cache here, because that
292
* is handled by the lower-level routines.
295
MultiXactIdExpand(MultiXactId multi, TransactionId xid)
297
MultiXactId newMulti;
298
TransactionId *members;
299
TransactionId *newMembers;
304
AssertArg(MultiXactIdIsValid(multi));
305
AssertArg(TransactionIdIsValid(xid));
307
debug_elog4(DEBUG2, "Expand: received multi %u, xid %u",
310
nmembers = GetMultiXactIdMembers(multi, &members);
315
* The MultiXactId is obsolete. This can only happen if all the
316
* MultiXactId members stop running between the caller checking and
317
* passing it to us. It would be better to return that fact to the
318
* caller, but it would complicate the API and it's unlikely to happen
319
* too often, so just deal with it by creating a singleton MultiXact.
321
newMulti = CreateMultiXactId(1, &xid);
323
debug_elog4(DEBUG2, "Expand: %u has no members, create singleton %u",
329
* If the TransactionId is already a member of the MultiXactId, just
330
* return the existing MultiXactId.
332
for (i = 0; i < nmembers; i++)
334
if (TransactionIdEquals(members[i], xid))
336
debug_elog4(DEBUG2, "Expand: %u is already a member of %u",
344
* Determine which of the members of the MultiXactId are still running,
345
* and use them to create a new one. (Removing dead members is just an
346
* optimization, but a useful one. Note we have the same race condition
347
* here as above: j could be 0 at the end of the loop.)
349
newMembers = (TransactionId *)
350
palloc(sizeof(TransactionId) * (nmembers + 1));
352
for (i = 0, j = 0; i < nmembers; i++)
354
if (TransactionIdIsInProgress(members[i]))
355
newMembers[j++] = members[i];
358
newMembers[j++] = xid;
359
newMulti = CreateMultiXactId(j, newMembers);
364
debug_elog3(DEBUG2, "Expand: returning new multi %u", newMulti);
370
* MultiXactIdIsRunning
371
* Returns whether a MultiXactId is "running".
373
* We return true if at least one member of the given MultiXactId is still
374
* running. Note that a "false" result is certain not to change,
375
* because it is not legal to add members to an existing MultiXactId.
378
MultiXactIdIsRunning(MultiXactId multi)
380
TransactionId *members;
384
debug_elog3(DEBUG2, "IsRunning %u?", multi);
386
nmembers = GetMultiXactIdMembers(multi, &members);
390
debug_elog2(DEBUG2, "IsRunning: no members");
395
* Checking for myself is cheap compared to looking in shared memory, so
396
* first do the equivalent of MultiXactIdIsCurrent(). This is not needed
397
* for correctness, it's just a fast path.
399
for (i = 0; i < nmembers; i++)
401
if (TransactionIdIsCurrentTransactionId(members[i]))
403
debug_elog3(DEBUG2, "IsRunning: I (%d) am running!", i);
410
* This could be made faster by having another entry point in procarray.c,
411
* walking the PGPROC array only once for all the members. But in most
412
* cases nmembers should be small enough that it doesn't much matter.
414
for (i = 0; i < nmembers; i++)
416
if (TransactionIdIsInProgress(members[i]))
418
debug_elog4(DEBUG2, "IsRunning: member %d (%u) is running",
427
debug_elog3(DEBUG2, "IsRunning: %u is not running", multi);
433
* MultiXactIdIsCurrent
434
* Returns true if the current transaction is a member of the MultiXactId.
436
* We return true if any live subtransaction of the current top-level
437
* transaction is a member. This is appropriate for the same reason that a
438
* lock held by any such subtransaction is globally equivalent to a lock
439
* held by the current subtransaction: no such lock could be released without
440
* aborting this subtransaction, and hence releasing its locks. So it's not
441
* necessary to add the current subxact to the MultiXact separately.
444
MultiXactIdIsCurrent(MultiXactId multi)
447
TransactionId *members;
451
nmembers = GetMultiXactIdMembers(multi, &members);
456
for (i = 0; i < nmembers; i++)
458
if (TransactionIdIsCurrentTransactionId(members[i]))
471
* MultiXactIdSetOldestMember
472
* Save the oldest MultiXactId this transaction could be a member of.
474
* We set the OldestMemberMXactId for a given transaction the first time
475
* it's going to acquire a shared lock. We need to do this even if we end
476
* up using a TransactionId instead of a MultiXactId, because there is a
477
* chance that another transaction would add our XID to a MultiXactId.
479
* The value to set is the next-to-be-assigned MultiXactId, so this is meant
480
* to be called just before acquiring a shared lock.
483
MultiXactIdSetOldestMember(void)
485
if (!MultiXactIdIsValid(OldestMemberMXactId[MyBackendId]))
487
MultiXactId nextMXact;
490
* You might think we don't need to acquire a lock here, since
491
* fetching and storing of TransactionIds is probably atomic, but in
492
* fact we do: suppose we pick up nextMXact and then lose the CPU for
493
* a long time. Someone else could advance nextMXact, and then
494
* another someone else could compute an OldestVisibleMXactId that
495
* would be after the value we are going to store when we get control
496
* back. Which would be wrong.
498
LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
501
* We have to beware of the possibility that nextMXact is in the
502
* wrapped-around state. We don't fix the counter itself here, but we
503
* must be sure to store a valid value in our array entry.
505
nextMXact = MultiXactState->nextMXact;
506
if (nextMXact < FirstMultiXactId)
507
nextMXact = FirstMultiXactId;
509
OldestMemberMXactId[MyBackendId] = nextMXact;
511
LWLockRelease(MultiXactGenLock);
513
debug_elog4(DEBUG2, "MultiXact: setting OldestMember[%d] = %u",
514
MyBackendId, nextMXact);
519
* MultiXactIdSetOldestVisible
520
* Save the oldest MultiXactId this transaction considers possibly live.
522
* We set the OldestVisibleMXactId for a given transaction the first time
523
* it's going to inspect any MultiXactId. Once we have set this, we are
524
* guaranteed that the checkpointer won't truncate off SLRU data for
525
* MultiXactIds at or after our OldestVisibleMXactId.
527
* The value to set is the oldest of nextMXact and all the valid per-backend
528
* OldestMemberMXactId[] entries. Because of the locking we do, we can be
529
* certain that no subsequent call to MultiXactIdSetOldestMember can set
530
* an OldestMemberMXactId[] entry older than what we compute here. Therefore
531
* there is no live transaction, now or later, that can be a member of any
532
* MultiXactId older than the OldestVisibleMXactId we compute here.
535
MultiXactIdSetOldestVisible(void)
537
if (!MultiXactIdIsValid(OldestVisibleMXactId[MyBackendId]))
539
MultiXactId oldestMXact;
542
LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
545
* We have to beware of the possibility that nextMXact is in the
546
* wrapped-around state. We don't fix the counter itself here, but we
547
* must be sure to store a valid value in our array entry.
549
oldestMXact = MultiXactState->nextMXact;
550
if (oldestMXact < FirstMultiXactId)
551
oldestMXact = FirstMultiXactId;
553
for (i = 1; i <= MaxOldestSlot; i++)
555
MultiXactId thisoldest = OldestMemberMXactId[i];
557
if (MultiXactIdIsValid(thisoldest) &&
558
MultiXactIdPrecedes(thisoldest, oldestMXact))
559
oldestMXact = thisoldest;
562
OldestVisibleMXactId[MyBackendId] = oldestMXact;
564
LWLockRelease(MultiXactGenLock);
566
debug_elog4(DEBUG2, "MultiXact: setting OldestVisible[%d] = %u",
567
MyBackendId, oldestMXact);
573
* Sleep on a MultiXactId.
575
* We do this by sleeping on each member using XactLockTableWait. Any
576
* members that belong to the current backend are *not* waited for, however;
577
* this would not merely be useless but would lead to Assert failure inside
578
* XactLockTableWait. By the time this returns, it is certain that all
579
* transactions *of other backends* that were members of the MultiXactId
580
* are dead (and no new ones can have been added, since it is not legal
581
* to add members to an existing MultiXactId).
583
* But by the time we finish sleeping, someone else may have changed the Xmax
584
* of the containing tuple, so the caller needs to iterate on us somehow.
587
MultiXactIdWait(MultiXactId multi)
589
TransactionId *members;
592
nmembers = GetMultiXactIdMembers(multi, &members);
598
for (i = 0; i < nmembers; i++)
600
TransactionId member = members[i];
602
debug_elog4(DEBUG2, "MultiXactIdWait: waiting for %d (%u)",
604
if (!TransactionIdIsCurrentTransactionId(member))
605
XactLockTableWait(member);
613
* ConditionalMultiXactIdWait
614
* As above, but only lock if we can get the lock without blocking.
617
ConditionalMultiXactIdWait(MultiXactId multi)
620
TransactionId *members;
623
nmembers = GetMultiXactIdMembers(multi, &members);
629
for (i = 0; i < nmembers; i++)
631
TransactionId member = members[i];
633
debug_elog4(DEBUG2, "ConditionalMultiXactIdWait: trying %d (%u)",
635
if (!TransactionIdIsCurrentTransactionId(member))
637
result = ConditionalXactLockTableWait(member);
651
* Make a new MultiXactId
653
* Make XLOG, SLRU and cache entries for a new MultiXactId, recording the
654
* given TransactionIds as members. Returns the newly created MultiXactId.
656
* NB: the passed xids[] array will be sorted in-place.
659
CreateMultiXactId(int nxids, TransactionId *xids)
662
MultiXactOffset offset;
663
XLogRecData rdata[2];
664
xl_multixact_create xlrec;
666
debug_elog3(DEBUG2, "Create: %s",
667
mxid_to_string(InvalidMultiXactId, nxids, xids));
670
* See if the same set of XIDs already exists in our cache; if so, just
671
* re-use that MultiXactId. (Note: it might seem that looking in our
672
* cache is insufficient, and we ought to search disk to see if a
673
* duplicate definition already exists. But since we only ever create
674
* MultiXacts containing our own XID, in most cases any such MultiXacts
675
* were in fact created by us, and so will be in our cache. There are
676
* corner cases where someone else added us to a MultiXact without our
677
* knowledge, but it's not worth checking for.)
679
multi = mXactCacheGetBySet(nxids, xids);
680
if (MultiXactIdIsValid(multi))
682
debug_elog2(DEBUG2, "Create: in cache!");
687
* Assign the MXID and offsets range to use, and make sure there is space
688
* in the OFFSETs and MEMBERs files. NB: this routine does
689
* START_CRIT_SECTION().
691
multi = GetNewMultiXactId(nxids, &offset);
694
* Make an XLOG entry describing the new MXID.
696
* Note: we need not flush this XLOG entry to disk before proceeding. The
697
* only way for the MXID to be referenced from any data page is for
698
* heap_lock_tuple() to have put it there, and heap_lock_tuple() generates
699
* an XLOG record that must follow ours. The normal LSN interlock between
700
* the data page and that XLOG record will ensure that our XLOG record
701
* reaches disk first. If the SLRU members/offsets data reaches disk
702
* sooner than the XLOG record, we do not care because we'll overwrite it
703
* with zeroes unless the XLOG record is there too; see notes at top of
710
rdata[0].data = (char *) (&xlrec);
711
rdata[0].len = MinSizeOfMultiXactCreate;
712
rdata[0].buffer = InvalidBuffer;
713
rdata[0].next = &(rdata[1]);
714
rdata[1].data = (char *) xids;
715
rdata[1].len = nxids * sizeof(TransactionId);
716
rdata[1].buffer = InvalidBuffer;
717
rdata[1].next = NULL;
719
(void) XLogInsert(RM_MULTIXACT_ID, XLOG_MULTIXACT_CREATE_ID, rdata);
721
/* Now enter the information into the OFFSETs and MEMBERs logs */
722
RecordNewMultiXact(multi, offset, nxids, xids);
724
/* Done with critical section */
727
/* Store the new MultiXactId in the local cache, too */
728
mXactCachePut(multi, nxids, xids);
730
debug_elog2(DEBUG2, "Create: all done");
737
* Write info about a new multixact into the offsets and members files
739
* This is broken out of CreateMultiXactId so that xlog replay can use it.
742
RecordNewMultiXact(MultiXactId multi, MultiXactOffset offset,
743
int nxids, TransactionId *xids)
749
MultiXactOffset *offptr;
752
LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
754
pageno = MultiXactIdToOffsetPage(multi);
755
entryno = MultiXactIdToOffsetEntry(multi);
758
* Note: we pass the MultiXactId to SimpleLruReadPage as the "transaction"
759
* to complain about if there's any I/O error. This is kinda bogus, but
760
* since the errors will always give the full pathname, it should be clear
761
* enough that a MultiXactId is really involved. Perhaps someday we'll
762
* take the trouble to generalize the slru.c error reporting code.
764
slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
765
offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
770
MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
772
/* Exchange our lock */
773
LWLockRelease(MultiXactOffsetControlLock);
775
LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
779
for (i = 0; i < nxids; i++, offset++)
781
TransactionId *memberptr;
783
pageno = MXOffsetToMemberPage(offset);
784
entryno = MXOffsetToMemberEntry(offset);
786
if (pageno != prev_pageno)
788
slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
789
prev_pageno = pageno;
792
memberptr = (TransactionId *)
793
MultiXactMemberCtl->shared->page_buffer[slotno];
794
memberptr += entryno;
796
*memberptr = xids[i];
798
MultiXactMemberCtl->shared->page_dirty[slotno] = true;
801
LWLockRelease(MultiXactMemberControlLock);
806
* Get the next MultiXactId.
808
* Also, reserve the needed amount of space in the "members" area. The
809
* starting offset of the reserved space is returned in *offset.
811
* This may generate XLOG records for expansion of the offsets and/or members
812
* files. Unfortunately, we have to do that while holding MultiXactGenLock
813
* to avoid race conditions --- the XLOG record for zeroing a page must appear
814
* before any backend can possibly try to store data in that page!
816
* We start a critical section before advancing the shared counters. The
817
* caller must end the critical section after writing SLRU data.
820
GetNewMultiXactId(int nxids, MultiXactOffset *offset)
823
MultiXactOffset nextOffset;
825
debug_elog3(DEBUG2, "GetNew: for %d xids", nxids);
827
/* MultiXactIdSetOldestMember() must have been called already */
828
Assert(MultiXactIdIsValid(OldestMemberMXactId[MyBackendId]));
830
LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
832
/* Handle wraparound of the nextMXact counter */
833
if (MultiXactState->nextMXact < FirstMultiXactId)
834
MultiXactState->nextMXact = FirstMultiXactId;
837
* Assign the MXID, and make sure there is room for it in the file.
839
result = MultiXactState->nextMXact;
841
ExtendMultiXactOffset(result);
844
* Reserve the members space, similarly to above. Also, be careful not to
845
* return zero as the starting offset for any multixact. See
846
* GetMultiXactIdMembers() for motivation.
848
nextOffset = MultiXactState->nextOffset;
852
nxids++; /* allocate member slot 0 too */
855
*offset = nextOffset;
857
ExtendMultiXactMember(nextOffset, nxids);
860
* Critical section from here until caller has written the data into the
861
* just-reserved SLRU space; we don't want to error out with a partly
862
* written MultiXact structure. (In particular, failing to write our
863
* start offset after advancing nextMXact would effectively corrupt the
864
* previous MultiXact.)
866
START_CRIT_SECTION();
869
* Advance counters. As in GetNewTransactionId(), this must not happen
870
* until after file extension has succeeded!
872
* We don't care about MultiXactId wraparound here; it will be handled by
873
* the next iteration. But note that nextMXact may be InvalidMultiXactId
874
* after this routine exits, so anyone else looking at the variable must
875
* be prepared to deal with that. Similarly, nextOffset may be zero, but
876
* we won't use that as the actual start offset of the next multixact.
878
(MultiXactState->nextMXact)++;
880
MultiXactState->nextOffset += nxids;
882
LWLockRelease(MultiXactGenLock);
884
debug_elog4(DEBUG2, "GetNew: returning %u offset %u", result, *offset);
889
* GetMultiXactIdMembers
890
* Returns the set of TransactionIds that make up a MultiXactId
892
* We return -1 if the MultiXactId is too old to possibly have any members
893
* still running; in that case we have not actually looked them up, and
897
GetMultiXactIdMembers(MultiXactId multi, TransactionId **xids)
903
MultiXactOffset *offptr;
904
MultiXactOffset offset;
908
MultiXactId nextMXact;
909
MultiXactId tmpMXact;
910
MultiXactOffset nextOffset;
913
debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
915
Assert(MultiXactIdIsValid(multi));
917
/* See if the MultiXactId is in the local cache */
918
length = mXactCacheGetById(multi, xids);
921
debug_elog3(DEBUG2, "GetMembers: found %s in the cache",
922
mxid_to_string(multi, length, *xids));
926
/* Set our OldestVisibleMXactId[] entry if we didn't already */
927
MultiXactIdSetOldestVisible();
930
* We check known limits on MultiXact before resorting to the SLRU area.
932
* An ID older than our OldestVisibleMXactId[] entry can't possibly still
933
* be running, and we'd run the risk of trying to read already-truncated
934
* SLRU data if we did try to examine it.
936
* Conversely, an ID >= nextMXact shouldn't ever be seen here; if it is
937
* seen, it implies undetected ID wraparound has occurred. We just
938
* silently assume that such an ID is no longer running.
940
* Shared lock is enough here since we aren't modifying any global state.
941
* Also, we can examine our own OldestVisibleMXactId without the lock,
942
* since no one else is allowed to change it.
944
if (MultiXactIdPrecedes(multi, OldestVisibleMXactId[MyBackendId]))
946
debug_elog2(DEBUG2, "GetMembers: it's too old");
952
* Acquire the shared lock just long enough to grab the current counter
953
* values. We may need both nextMXact and nextOffset; see below.
955
LWLockAcquire(MultiXactGenLock, LW_SHARED);
957
nextMXact = MultiXactState->nextMXact;
958
nextOffset = MultiXactState->nextOffset;
960
LWLockRelease(MultiXactGenLock);
962
if (!MultiXactIdPrecedes(multi, nextMXact))
964
debug_elog2(DEBUG2, "GetMembers: it's too new!");
970
* Find out the offset at which we need to start reading MultiXactMembers
971
* and the number of members in the multixact. We determine the latter as
972
* the difference between this multixact's starting offset and the next
973
* one's. However, there are some corner cases to worry about:
975
* 1. This multixact may be the latest one created, in which case there is
976
* no next one to look at. In this case the nextOffset value we just
977
* saved is the correct endpoint.
979
* 2. The next multixact may still be in process of being filled in: that
980
* is, another process may have done GetNewMultiXactId but not yet written
981
* the offset entry for that ID. In that scenario, it is guaranteed that
982
* the offset entry for that multixact exists (because GetNewMultiXactId
983
* won't release MultiXactGenLock until it does) but contains zero
984
* (because we are careful to pre-zero offset pages). Because
985
* GetNewMultiXactId will never return zero as the starting offset for a
986
* multixact, when we read zero as the next multixact's offset, we know we
987
* have this case. We sleep for a bit and try again.
989
* 3. Because GetNewMultiXactId increments offset zero to offset one to
990
* handle case #2, there is an ambiguity near the point of offset
991
* wraparound. If we see next multixact's offset is one, is that our
992
* multixact's actual endpoint, or did it end at zero with a subsequent
993
* increment? We handle this using the knowledge that if the zero'th
994
* member slot wasn't filled, it'll contain zero, and zero isn't a valid
995
* transaction ID so it can't be a multixact member. Therefore, if we
996
* read a zero from the members array, just ignore it.
998
* This is all pretty messy, but the mess occurs only in infrequent corner
999
* cases, so it seems better than holding the MultiXactGenLock for a long
1000
* time on every multixact creation.
1003
LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
1005
pageno = MultiXactIdToOffsetPage(multi);
1006
entryno = MultiXactIdToOffsetEntry(multi);
1008
slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
1009
offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1013
Assert(offset != 0);
1016
* Use the same increment rule as GetNewMultiXactId(), that is, don't
1017
* handle wraparound explicitly until needed.
1019
tmpMXact = multi + 1;
1021
if (nextMXact == tmpMXact)
1023
/* Corner case 1: there is no next multixact */
1024
length = nextOffset - offset;
1028
MultiXactOffset nextMXOffset;
1030
/* handle wraparound if needed */
1031
if (tmpMXact < FirstMultiXactId)
1032
tmpMXact = FirstMultiXactId;
1034
prev_pageno = pageno;
1036
pageno = MultiXactIdToOffsetPage(tmpMXact);
1037
entryno = MultiXactIdToOffsetEntry(tmpMXact);
1039
if (pageno != prev_pageno)
1040
slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, tmpMXact);
1042
offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1044
nextMXOffset = *offptr;
1046
if (nextMXOffset == 0)
1048
/* Corner case 2: next multixact is still being filled in */
1049
LWLockRelease(MultiXactOffsetControlLock);
1054
length = nextMXOffset - offset;
1057
LWLockRelease(MultiXactOffsetControlLock);
1059
ptr = (TransactionId *) palloc(length * sizeof(TransactionId));
1062
/* Now get the members themselves. */
1063
LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
1067
for (i = 0; i < length; i++, offset++)
1069
TransactionId *xactptr;
1071
pageno = MXOffsetToMemberPage(offset);
1072
entryno = MXOffsetToMemberEntry(offset);
1074
if (pageno != prev_pageno)
1076
slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, multi);
1077
prev_pageno = pageno;
1080
xactptr = (TransactionId *)
1081
MultiXactMemberCtl->shared->page_buffer[slotno];
1084
if (!TransactionIdIsValid(*xactptr))
1086
/* Corner case 3: we must be looking at unused slot zero */
1087
Assert(offset == 0);
1091
ptr[truelength++] = *xactptr;
1094
LWLockRelease(MultiXactMemberControlLock);
1097
* Copy the result into the local cache.
1099
mXactCachePut(multi, truelength, ptr);
1101
debug_elog3(DEBUG2, "GetMembers: no cache for %s",
1102
mxid_to_string(multi, truelength, ptr));
1107
* mXactCacheGetBySet
1108
* returns a MultiXactId from the cache based on the set of
1109
* TransactionIds that compose it, or InvalidMultiXactId if
1112
* This is helpful, for example, if two transactions want to lock a huge
1113
* table. By using the cache, the second will use the same MultiXactId
1114
* for the majority of tuples, thus keeping MultiXactId usage low (saving
1115
* both I/O and wraparound issues).
1117
* NB: the passed xids[] array will be sorted in-place.
1120
mXactCacheGetBySet(int nxids, TransactionId *xids)
1122
mXactCacheEnt *entry;
1124
debug_elog3(DEBUG2, "CacheGet: looking for %s",
1125
mxid_to_string(InvalidMultiXactId, nxids, xids));
1127
/* sort the array so comparison is easy */
1128
qsort(xids, nxids, sizeof(TransactionId), xidComparator);
1130
for (entry = MXactCache; entry != NULL; entry = entry->next)
1132
if (entry->nxids != nxids)
1135
/* We assume the cache entries are sorted */
1136
if (memcmp(xids, entry->xids, nxids * sizeof(TransactionId)) == 0)
1138
debug_elog3(DEBUG2, "CacheGet: found %u", entry->multi);
1139
return entry->multi;
1143
debug_elog2(DEBUG2, "CacheGet: not found :-(");
1144
return InvalidMultiXactId;
1149
* returns the composing TransactionId set from the cache for a
1150
* given MultiXactId, if present.
1152
* If successful, *xids is set to the address of a palloc'd copy of the
1153
* TransactionId set. Return value is number of members, or -1 on failure.
1156
mXactCacheGetById(MultiXactId multi, TransactionId **xids)
1158
mXactCacheEnt *entry;
1160
debug_elog3(DEBUG2, "CacheGet: looking for %u", multi);
1162
for (entry = MXactCache; entry != NULL; entry = entry->next)
1164
if (entry->multi == multi)
1169
size = sizeof(TransactionId) * entry->nxids;
1170
ptr = (TransactionId *) palloc(size);
1173
memcpy(ptr, entry->xids, size);
1175
debug_elog3(DEBUG2, "CacheGet: found %s",
1176
mxid_to_string(multi, entry->nxids, entry->xids));
1177
return entry->nxids;
1181
debug_elog2(DEBUG2, "CacheGet: not found");
1187
* Add a new MultiXactId and its composing set into the local cache.
1190
mXactCachePut(MultiXactId multi, int nxids, TransactionId *xids)
1192
mXactCacheEnt *entry;
1194
debug_elog3(DEBUG2, "CachePut: storing %s",
1195
mxid_to_string(multi, nxids, xids));
1197
if (MXactContext == NULL)
1199
/* The cache only lives as long as the current transaction */
1200
debug_elog2(DEBUG2, "CachePut: initializing memory context");
1201
MXactContext = AllocSetContextCreate(TopTransactionContext,
1202
"MultiXact Cache Context",
1203
ALLOCSET_SMALL_MINSIZE,
1204
ALLOCSET_SMALL_INITSIZE,
1205
ALLOCSET_SMALL_MAXSIZE);
1208
entry = (mXactCacheEnt *)
1209
MemoryContextAlloc(MXactContext,
1210
offsetof(mXactCacheEnt, xids) +
1211
nxids * sizeof(TransactionId));
1213
entry->multi = multi;
1214
entry->nxids = nxids;
1215
memcpy(entry->xids, xids, nxids * sizeof(TransactionId));
1217
/* mXactCacheGetBySet assumes the entries are sorted, so sort them */
1218
qsort(entry->xids, nxids, sizeof(TransactionId), xidComparator);
1220
entry->next = MXactCache;
1224
#ifdef MULTIXACT_DEBUG
1226
mxid_to_string(MultiXactId multi, int nxids, TransactionId *xids)
1228
char *str = palloc(15 * (nxids + 1) + 4);
1231
snprintf(str, 47, "%u %d[%u", multi, nxids, xids[0]);
1233
for (i = 1; i < nxids; i++)
1234
snprintf(str + strlen(str), 17, ", %u", xids[i]);
1242
* AtEOXact_MultiXact
1243
* Handle transaction end for MultiXact
1245
* This is called at top transaction commit or abort (we don't care which).
1248
AtEOXact_MultiXact(void)
1251
* Reset our OldestMemberMXactId and OldestVisibleMXactId values, both of
1252
* which should only be valid while within a transaction.
1254
* We assume that storing a MultiXactId is atomic and so we need not take
1255
* MultiXactGenLock to do this.
1257
OldestMemberMXactId[MyBackendId] = InvalidMultiXactId;
1258
OldestVisibleMXactId[MyBackendId] = InvalidMultiXactId;
1261
* Discard the local MultiXactId cache. Since MXactContext was created as
1262
* a child of TopTransactionContext, we needn't delete it explicitly.
1264
MXactContext = NULL;
1269
* AtPrepare_MultiXact
1270
* Save multixact state at 2PC tranasction prepare
1272
* In this phase, we only store our OldestMemberMXactId value in the two-phase
1276
AtPrepare_MultiXact(void)
1278
MultiXactId myOldestMember = OldestMemberMXactId[MyBackendId];
1280
if (MultiXactIdIsValid(myOldestMember))
1281
RegisterTwoPhaseRecord(TWOPHASE_RM_MULTIXACT_ID, 0,
1282
&myOldestMember, sizeof(MultiXactId));
1286
* PostPrepare_MultiXact
1287
* Clean up after successful PREPARE TRANSACTION
1290
PostPrepare_MultiXact(TransactionId xid)
1292
MultiXactId myOldestMember;
1295
* Transfer our OldestMemberMXactId value to the slot reserved for the
1296
* prepared transaction.
1298
myOldestMember = OldestMemberMXactId[MyBackendId];
1299
if (MultiXactIdIsValid(myOldestMember))
1301
BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid);
1304
* Even though storing MultiXactId is atomic, acquire lock to make
1305
* sure others see both changes, not just the reset of the slot of the
1306
* current backend. Using a volatile pointer might suffice, but this
1309
LWLockAcquire(MultiXactGenLock, LW_EXCLUSIVE);
1311
OldestMemberMXactId[dummyBackendId] = myOldestMember;
1312
OldestMemberMXactId[MyBackendId] = InvalidMultiXactId;
1314
LWLockRelease(MultiXactGenLock);
1318
* We don't need to transfer OldestVisibleMXactId value, because the
1319
* transaction is not going to be looking at any more multixacts once it's
1322
* We assume that storing a MultiXactId is atomic and so we need not take
1323
* MultiXactGenLock to do this.
1325
OldestVisibleMXactId[MyBackendId] = InvalidMultiXactId;
1328
* Discard the local MultiXactId cache like in AtEOX_MultiXact
1330
MXactContext = NULL;
1335
* multixact_twophase_recover
1336
* Recover the state of a prepared transaction at startup
1339
multixact_twophase_recover(TransactionId xid, uint16 info,
1340
void *recdata, uint32 len)
1342
BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid);
1343
MultiXactId oldestMember;
1346
* Get the oldest member XID from the state file record, and set it in the
1347
* OldestMemberMXactId slot reserved for this prepared transaction.
1349
Assert(len == sizeof(MultiXactId));
1350
oldestMember = *((MultiXactId *) recdata);
1352
OldestMemberMXactId[dummyBackendId] = oldestMember;
1356
* multixact_twophase_postcommit
1357
* Similar to AtEOX_MultiXact but for COMMIT PREPARED
1360
multixact_twophase_postcommit(TransactionId xid, uint16 info,
1361
void *recdata, uint32 len)
1363
BackendId dummyBackendId = TwoPhaseGetDummyBackendId(xid);
1365
Assert(len == sizeof(MultiXactId));
1367
OldestMemberMXactId[dummyBackendId] = InvalidMultiXactId;
1371
* multixact_twophase_postabort
1372
* This is actually just the same as the COMMIT case.
1375
multixact_twophase_postabort(TransactionId xid, uint16 info,
1376
void *recdata, uint32 len)
1378
multixact_twophase_postcommit(xid, info, recdata, len);
1382
* Initialization of shared memory for MultiXact. We use two SLRU areas,
1383
* thus double memory. Also, reserve space for the shared MultiXactState
1384
* struct and the per-backend MultiXactId arrays (two of those, too).
1387
MultiXactShmemSize(void)
1391
#define SHARED_MULTIXACT_STATE_SIZE \
1392
add_size(sizeof(MultiXactStateData), \
1393
mul_size(sizeof(MultiXactId) * 2, MaxOldestSlot))
1395
size = SHARED_MULTIXACT_STATE_SIZE;
1396
size = add_size(size, SimpleLruShmemSize(NUM_MXACTOFFSET_BUFFERS, 0));
1397
size = add_size(size, SimpleLruShmemSize(NUM_MXACTMEMBER_BUFFERS, 0));
1403
MultiXactShmemInit(void)
1407
debug_elog2(DEBUG2, "Shared Memory Init for MultiXact");
1409
MultiXactOffsetCtl->PagePrecedes = MultiXactOffsetPagePrecedes;
1410
MultiXactMemberCtl->PagePrecedes = MultiXactMemberPagePrecedes;
1412
SimpleLruInit(MultiXactOffsetCtl,
1413
"MultiXactOffset Ctl", NUM_MXACTOFFSET_BUFFERS, 0,
1414
MultiXactOffsetControlLock, "pg_multixact/offsets");
1415
SimpleLruInit(MultiXactMemberCtl,
1416
"MultiXactMember Ctl", NUM_MXACTMEMBER_BUFFERS, 0,
1417
MultiXactMemberControlLock, "pg_multixact/members");
1419
/* Initialize our shared state struct */
1420
MultiXactState = ShmemInitStruct("Shared MultiXact State",
1421
SHARED_MULTIXACT_STATE_SIZE,
1423
if (!IsUnderPostmaster)
1427
/* Make sure we zero out the per-backend state */
1428
MemSet(MultiXactState, 0, SHARED_MULTIXACT_STATE_SIZE);
1434
* Set up array pointers. Note that perBackendXactIds[0] is wasted space
1435
* since we only use indexes 1..MaxOldestSlot in each array.
1437
OldestMemberMXactId = MultiXactState->perBackendXactIds;
1438
OldestVisibleMXactId = OldestMemberMXactId + MaxOldestSlot;
1442
* This func must be called ONCE on system install. It creates the initial
1443
* MultiXact segments. (The MultiXacts directories are assumed to have been
1444
* created by initdb, and MultiXactShmemInit must have been called already.)
1447
BootStrapMultiXact(void)
1451
LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
1453
/* Create and zero the first page of the offsets log */
1454
slotno = ZeroMultiXactOffsetPage(0, false);
1456
/* Make sure it's written out */
1457
SimpleLruWritePage(MultiXactOffsetCtl, slotno);
1458
Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
1460
LWLockRelease(MultiXactOffsetControlLock);
1462
LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
1464
/* Create and zero the first page of the members log */
1465
slotno = ZeroMultiXactMemberPage(0, false);
1467
/* Make sure it's written out */
1468
SimpleLruWritePage(MultiXactMemberCtl, slotno);
1469
Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
1471
LWLockRelease(MultiXactMemberControlLock);
1475
* Initialize (or reinitialize) a page of MultiXactOffset to zeroes.
1476
* If writeXlog is TRUE, also emit an XLOG record saying we did this.
1478
* The page is not actually written, just set up in shared memory.
1479
* The slot number of the new page is returned.
1481
* Control lock must be held at entry, and will be held at exit.
1484
ZeroMultiXactOffsetPage(int pageno, bool writeXlog)
1488
slotno = SimpleLruZeroPage(MultiXactOffsetCtl, pageno);
1491
WriteMZeroPageXlogRec(pageno, XLOG_MULTIXACT_ZERO_OFF_PAGE);
1497
* Ditto, for MultiXactMember
1500
ZeroMultiXactMemberPage(int pageno, bool writeXlog)
1504
slotno = SimpleLruZeroPage(MultiXactMemberCtl, pageno);
1507
WriteMZeroPageXlogRec(pageno, XLOG_MULTIXACT_ZERO_MEM_PAGE);
1513
* This must be called ONCE during postmaster or standalone-backend startup.
1515
* StartupXLOG has already established nextMXact/nextOffset by calling
1516
* MultiXactSetNextMXact and/or MultiXactAdvanceNextMXact. Note that we
1517
* may already have replayed WAL data into the SLRU files.
1519
* We don't need any locks here, really; the SLRU locks are taken
1520
* only because slru.c expects to be called with locks held.
1523
StartupMultiXact(void)
1525
MultiXactId multi = MultiXactState->nextMXact;
1526
MultiXactOffset offset = MultiXactState->nextOffset;
1530
/* Clean up offsets state */
1531
LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
1534
* Initialize our idea of the latest page number.
1536
pageno = MultiXactIdToOffsetPage(multi);
1537
MultiXactOffsetCtl->shared->latest_page_number = pageno;
1540
* Zero out the remainder of the current offsets page. See notes in
1541
* StartupCLOG() for motivation.
1543
entryno = MultiXactIdToOffsetEntry(multi);
1547
MultiXactOffset *offptr;
1549
slotno = SimpleLruReadPage(MultiXactOffsetCtl, pageno, true, multi);
1550
offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1553
MemSet(offptr, 0, BLCKSZ - (entryno * sizeof(MultiXactOffset)));
1555
MultiXactOffsetCtl->shared->page_dirty[slotno] = true;
1558
LWLockRelease(MultiXactOffsetControlLock);
1560
/* And the same for members */
1561
LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
1564
* Initialize our idea of the latest page number.
1566
pageno = MXOffsetToMemberPage(offset);
1567
MultiXactMemberCtl->shared->latest_page_number = pageno;
1570
* Zero out the remainder of the current members page. See notes in
1571
* StartupCLOG() for motivation.
1573
entryno = MXOffsetToMemberEntry(offset);
1577
TransactionId *xidptr;
1579
slotno = SimpleLruReadPage(MultiXactMemberCtl, pageno, true, offset);
1580
xidptr = (TransactionId *) MultiXactMemberCtl->shared->page_buffer[slotno];
1583
MemSet(xidptr, 0, BLCKSZ - (entryno * sizeof(TransactionId)));
1585
MultiXactMemberCtl->shared->page_dirty[slotno] = true;
1588
LWLockRelease(MultiXactMemberControlLock);
1591
* Initialize lastTruncationPoint to invalid, ensuring that the first
1592
* checkpoint will try to do truncation.
1594
MultiXactState->lastTruncationPoint = InvalidMultiXactId;
1598
* This must be called ONCE during postmaster or standalone-backend shutdown
1601
ShutdownMultiXact(void)
1603
/* Flush dirty MultiXact pages to disk */
1604
TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_START(false);
1605
SimpleLruFlush(MultiXactOffsetCtl, false);
1606
SimpleLruFlush(MultiXactMemberCtl, false);
1607
TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_DONE(false);
1611
* Get the next MultiXactId and offset to save in a checkpoint record
1614
MultiXactGetCheckptMulti(bool is_shutdown,
1615
MultiXactId *nextMulti,
1616
MultiXactOffset *nextMultiOffset)
1618
LWLockAcquire(MultiXactGenLock, LW_SHARED);
1620
*nextMulti = MultiXactState->nextMXact;
1621
*nextMultiOffset = MultiXactState->nextOffset;
1623
LWLockRelease(MultiXactGenLock);
1625
debug_elog4(DEBUG2, "MultiXact: checkpoint is nextMulti %u, nextOffset %u",
1626
*nextMulti, *nextMultiOffset);
1630
* Perform a checkpoint --- either during shutdown, or on-the-fly
1633
CheckPointMultiXact(void)
1635
TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_START(true);
1637
/* Flush dirty MultiXact pages to disk */
1638
SimpleLruFlush(MultiXactOffsetCtl, true);
1639
SimpleLruFlush(MultiXactMemberCtl, true);
1642
* Truncate the SLRU files. This could be done at any time, but
1643
* checkpoint seems a reasonable place for it. There is one exception: if
1644
* we are called during xlog recovery, then shared->latest_page_number
1645
* isn't valid (because StartupMultiXact hasn't been called yet) and so
1646
* SimpleLruTruncate would get confused. It seems best not to risk
1647
* removing any data during recovery anyway, so don't truncate.
1649
if (!RecoveryInProgress())
1650
TruncateMultiXact();
1652
TRACE_POSTGRESQL_MULTIXACT_CHECKPOINT_DONE(true);
1656
* Set the next-to-be-assigned MultiXactId and offset
1658
* This is used when we can determine the correct next ID/offset exactly
1659
* from a checkpoint record. We need no locking since it is only called
1660
* during bootstrap and XLog replay.
1663
MultiXactSetNextMXact(MultiXactId nextMulti,
1664
MultiXactOffset nextMultiOffset)
1666
debug_elog4(DEBUG2, "MultiXact: setting next multi to %u offset %u",
1667
nextMulti, nextMultiOffset);
1668
MultiXactState->nextMXact = nextMulti;
1669
MultiXactState->nextOffset = nextMultiOffset;
1673
* Ensure the next-to-be-assigned MultiXactId is at least minMulti,
1674
* and similarly nextOffset is at least minMultiOffset
1676
* This is used when we can determine minimum safe values from an XLog
1677
* record (either an on-line checkpoint or an mxact creation log entry).
1678
* We need no locking since it is only called during XLog replay.
1681
MultiXactAdvanceNextMXact(MultiXactId minMulti,
1682
MultiXactOffset minMultiOffset)
1684
if (MultiXactIdPrecedes(MultiXactState->nextMXact, minMulti))
1686
debug_elog3(DEBUG2, "MultiXact: setting next multi to %u", minMulti);
1687
MultiXactState->nextMXact = minMulti;
1689
if (MultiXactOffsetPrecedes(MultiXactState->nextOffset, minMultiOffset))
1691
debug_elog3(DEBUG2, "MultiXact: setting next offset to %u",
1693
MultiXactState->nextOffset = minMultiOffset;
1698
* Make sure that MultiXactOffset has room for a newly-allocated MultiXactId.
1700
* NB: this is called while holding MultiXactGenLock. We want it to be very
1701
* fast most of the time; even when it's not so fast, no actual I/O need
1702
* happen unless we're forced to write out a dirty log or xlog page to make
1703
* room in shared memory.
1706
ExtendMultiXactOffset(MultiXactId multi)
1711
* No work except at first MultiXactId of a page. But beware: just after
1712
* wraparound, the first MultiXactId of page zero is FirstMultiXactId.
1714
if (MultiXactIdToOffsetEntry(multi) != 0 &&
1715
multi != FirstMultiXactId)
1718
pageno = MultiXactIdToOffsetPage(multi);
1720
LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
1722
/* Zero the page and make an XLOG entry about it */
1723
ZeroMultiXactOffsetPage(pageno, true);
1725
LWLockRelease(MultiXactOffsetControlLock);
1729
* Make sure that MultiXactMember has room for the members of a newly-
1730
* allocated MultiXactId.
1732
* Like the above routine, this is called while holding MultiXactGenLock;
1733
* same comments apply.
1736
ExtendMultiXactMember(MultiXactOffset offset, int nmembers)
1739
* It's possible that the members span more than one page of the members
1740
* file, so we loop to ensure we consider each page. The coding is not
1741
* optimal if the members span several pages, but that seems unusual
1742
* enough to not worry much about.
1744
while (nmembers > 0)
1749
* Only zero when at first entry of a page.
1751
entryno = MXOffsetToMemberEntry(offset);
1756
pageno = MXOffsetToMemberPage(offset);
1758
LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
1760
/* Zero the page and make an XLOG entry about it */
1761
ZeroMultiXactMemberPage(pageno, true);
1763
LWLockRelease(MultiXactMemberControlLock);
1766
/* Advance to next page (OK if nmembers goes negative) */
1767
offset += (MULTIXACT_MEMBERS_PER_PAGE - entryno);
1768
nmembers -= (MULTIXACT_MEMBERS_PER_PAGE - entryno);
1773
* Remove all MultiXactOffset and MultiXactMember segments before the oldest
1774
* ones still of interest.
1776
* This is called only during checkpoints. We assume no more than one
1777
* backend does this at a time.
1779
* XXX do we have any issues with needing to checkpoint here?
1782
TruncateMultiXact(void)
1784
MultiXactId nextMXact;
1785
MultiXactOffset nextOffset;
1786
MultiXactId oldestMXact;
1787
MultiXactOffset oldestOffset;
1792
* First, compute where we can safely truncate. Per notes above, this is
1793
* the oldest valid value among all the OldestMemberMXactId[] and
1794
* OldestVisibleMXactId[] entries, or nextMXact if none are valid.
1796
LWLockAcquire(MultiXactGenLock, LW_SHARED);
1799
* We have to beware of the possibility that nextMXact is in the
1800
* wrapped-around state. We don't fix the counter itself here, but we
1801
* must be sure to use a valid value in our calculation.
1803
nextMXact = MultiXactState->nextMXact;
1804
if (nextMXact < FirstMultiXactId)
1805
nextMXact = FirstMultiXactId;
1807
oldestMXact = nextMXact;
1808
for (i = 1; i <= MaxOldestSlot; i++)
1810
MultiXactId thisoldest;
1812
thisoldest = OldestMemberMXactId[i];
1813
if (MultiXactIdIsValid(thisoldest) &&
1814
MultiXactIdPrecedes(thisoldest, oldestMXact))
1815
oldestMXact = thisoldest;
1816
thisoldest = OldestVisibleMXactId[i];
1817
if (MultiXactIdIsValid(thisoldest) &&
1818
MultiXactIdPrecedes(thisoldest, oldestMXact))
1819
oldestMXact = thisoldest;
1822
/* Save the current nextOffset too */
1823
nextOffset = MultiXactState->nextOffset;
1825
LWLockRelease(MultiXactGenLock);
1827
debug_elog3(DEBUG2, "MultiXact: truncation point = %u", oldestMXact);
1830
* If we already truncated at this point, do nothing. This saves time
1831
* when no MultiXacts are getting used, which is probably not uncommon.
1833
if (MultiXactState->lastTruncationPoint == oldestMXact)
1837
* We need to determine where to truncate MultiXactMember. If we found a
1838
* valid oldest MultiXactId, read its starting offset; otherwise we use
1839
* the nextOffset value we saved above.
1841
if (oldestMXact == nextMXact)
1842
oldestOffset = nextOffset;
1848
MultiXactOffset *offptr;
1850
/* lock is acquired by SimpleLruReadPage_ReadOnly */
1852
pageno = MultiXactIdToOffsetPage(oldestMXact);
1853
entryno = MultiXactIdToOffsetEntry(oldestMXact);
1855
slotno = SimpleLruReadPage_ReadOnly(MultiXactOffsetCtl, pageno, oldestMXact);
1856
offptr = (MultiXactOffset *) MultiXactOffsetCtl->shared->page_buffer[slotno];
1858
oldestOffset = *offptr;
1860
LWLockRelease(MultiXactOffsetControlLock);
1864
* The cutoff point is the start of the segment containing oldestMXact. We
1865
* pass the *page* containing oldestMXact to SimpleLruTruncate.
1867
cutoffPage = MultiXactIdToOffsetPage(oldestMXact);
1869
SimpleLruTruncate(MultiXactOffsetCtl, cutoffPage);
1872
* Also truncate MultiXactMember at the previously determined offset.
1874
cutoffPage = MXOffsetToMemberPage(oldestOffset);
1876
SimpleLruTruncate(MultiXactMemberCtl, cutoffPage);
1879
* Set the last known truncation point. We don't need a lock for this
1880
* since only one backend does checkpoints at a time.
1882
MultiXactState->lastTruncationPoint = oldestMXact;
1886
* Decide which of two MultiXactOffset page numbers is "older" for truncation
1889
* We need to use comparison of MultiXactId here in order to do the right
1890
* thing with wraparound. However, if we are asked about page number zero, we
1891
* don't want to hand InvalidMultiXactId to MultiXactIdPrecedes: it'll get
1892
* weird. So, offset both multis by FirstMultiXactId to avoid that.
1893
* (Actually, the current implementation doesn't do anything weird with
1894
* InvalidMultiXactId, but there's no harm in leaving this code like this.)
1897
MultiXactOffsetPagePrecedes(int page1, int page2)
1902
multi1 = ((MultiXactId) page1) * MULTIXACT_OFFSETS_PER_PAGE;
1903
multi1 += FirstMultiXactId;
1904
multi2 = ((MultiXactId) page2) * MULTIXACT_OFFSETS_PER_PAGE;
1905
multi2 += FirstMultiXactId;
1907
return MultiXactIdPrecedes(multi1, multi2);
1911
* Decide which of two MultiXactMember page numbers is "older" for truncation
1912
* purposes. There is no "invalid offset number" so use the numbers verbatim.
1915
MultiXactMemberPagePrecedes(int page1, int page2)
1917
MultiXactOffset offset1;
1918
MultiXactOffset offset2;
1920
offset1 = ((MultiXactOffset) page1) * MULTIXACT_MEMBERS_PER_PAGE;
1921
offset2 = ((MultiXactOffset) page2) * MULTIXACT_MEMBERS_PER_PAGE;
1923
return MultiXactOffsetPrecedes(offset1, offset2);
1927
* Decide which of two MultiXactIds is earlier.
1929
* XXX do we need to do something special for InvalidMultiXactId?
1930
* (Doesn't look like it.)
1933
MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
1935
int32 diff = (int32) (multi1 - multi2);
1941
* Decide which of two offsets is earlier.
1944
MultiXactOffsetPrecedes(MultiXactOffset offset1, MultiXactOffset offset2)
1946
int32 diff = (int32) (offset1 - offset2);
1953
* Write an xlog record reflecting the zeroing of either a MEMBERs or
1954
* OFFSETs page (info shows which)
1957
WriteMZeroPageXlogRec(int pageno, uint8 info)
1961
rdata.data = (char *) (&pageno);
1962
rdata.len = sizeof(int);
1963
rdata.buffer = InvalidBuffer;
1965
(void) XLogInsert(RM_MULTIXACT_ID, info, &rdata);
1969
* MULTIXACT resource manager's routines
1972
multixact_redo(XLogRecPtr lsn, XLogRecord *record)
1974
uint8 info = record->xl_info & ~XLR_INFO_MASK;
1976
/* Backup blocks are not used in multixact records */
1977
Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
1979
if (info == XLOG_MULTIXACT_ZERO_OFF_PAGE)
1984
memcpy(&pageno, XLogRecGetData(record), sizeof(int));
1986
LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
1988
slotno = ZeroMultiXactOffsetPage(pageno, false);
1989
SimpleLruWritePage(MultiXactOffsetCtl, slotno);
1990
Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
1992
LWLockRelease(MultiXactOffsetControlLock);
1994
else if (info == XLOG_MULTIXACT_ZERO_MEM_PAGE)
1999
memcpy(&pageno, XLogRecGetData(record), sizeof(int));
2001
LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
2003
slotno = ZeroMultiXactMemberPage(pageno, false);
2004
SimpleLruWritePage(MultiXactMemberCtl, slotno);
2005
Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
2007
LWLockRelease(MultiXactMemberControlLock);
2009
else if (info == XLOG_MULTIXACT_CREATE_ID)
2011
xl_multixact_create *xlrec = (xl_multixact_create *) XLogRecGetData(record);
2012
TransactionId *xids = xlrec->xids;
2013
TransactionId max_xid;
2016
/* Store the data back into the SLRU files */
2017
RecordNewMultiXact(xlrec->mid, xlrec->moff, xlrec->nxids, xids);
2019
/* Make sure nextMXact/nextOffset are beyond what this record has */
2020
MultiXactAdvanceNextMXact(xlrec->mid + 1, xlrec->moff + xlrec->nxids);
2023
* Make sure nextXid is beyond any XID mentioned in the record. This
2024
* should be unnecessary, since any XID found here ought to have other
2025
* evidence in the XLOG, but let's be safe.
2027
max_xid = record->xl_xid;
2028
for (i = 0; i < xlrec->nxids; i++)
2030
if (TransactionIdPrecedes(max_xid, xids[i]))
2035
* We don't expect anyone else to modify nextXid, hence startup
2036
* process doesn't need to hold a lock while checking this. We still
2037
* acquire the lock to modify it, though.
2039
if (TransactionIdFollowsOrEquals(max_xid,
2040
ShmemVariableCache->nextXid))
2042
LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
2043
ShmemVariableCache->nextXid = max_xid;
2044
TransactionIdAdvance(ShmemVariableCache->nextXid);
2045
LWLockRelease(XidGenLock);
2049
elog(PANIC, "multixact_redo: unknown op code %u", info);
2053
multixact_desc(StringInfo buf, uint8 xl_info, char *rec)
2055
uint8 info = xl_info & ~XLR_INFO_MASK;
2057
if (info == XLOG_MULTIXACT_ZERO_OFF_PAGE)
2061
memcpy(&pageno, rec, sizeof(int));
2062
appendStringInfo(buf, "zero offsets page: %d", pageno);
2064
else if (info == XLOG_MULTIXACT_ZERO_MEM_PAGE)
2068
memcpy(&pageno, rec, sizeof(int));
2069
appendStringInfo(buf, "zero members page: %d", pageno);
2071
else if (info == XLOG_MULTIXACT_CREATE_ID)
2073
xl_multixact_create *xlrec = (xl_multixact_create *) rec;
2076
appendStringInfo(buf, "create multixact %u offset %u:",
2077
xlrec->mid, xlrec->moff);
2078
for (i = 0; i < xlrec->nxids; i++)
2079
appendStringInfo(buf, " %u", xlrec->xids[i]);
2082
appendStringInfo(buf, "UNKNOWN");