Skip to content

Commit 28f294a

Browse files
committed
Fix handling of multixacts predating pg_upgrade
After pg_upgrade, it is possible that some tuples' Xmax have multixacts corresponding to the old installation; such multixacts cannot have running members anymore. In many code sites we already know not to read them and clobber them silently, but at least when VACUUM tries to freeze a multixact or determine whether one needs freezing, there's an attempt to resolve it to its member transactions by calling GetMultiXactIdMembers, and if the multixact value is "in the future" with regards to the current valid multixact range, an error like this is raised: ERROR: MultiXactId 123 has not been created yet -- apparent wraparound and vacuuming fails. Per discussion with Andrew Gierth, it is completely bogus to try to resolve multixacts coming from before a pg_upgrade, regardless of where they stand with regards to the current valid multixact range. It's possible to get from under this problem by doing SELECT FOR UPDATE of the problem tuples, but if tables are large, this is slow and tedious, so a more thorough solution is desirable. To fix, we realize that multixacts in xmax created in 9.2 and previous have a specific bit pattern that is never used in 9.3 and later (we already knew this, per comments and infomask tests sprinkled in various places, but we weren't leveraging this knowledge appropriately). Whenever the infomask of the tuple matches that bit pattern, we just ignore the multixact completely as if Xmax wasn't set; or, in the case of tuple freezing, we act as if an unwanted value is set and clobber it without decoding. This guarantees that no errors will be raised, and that the values will be progressively removed until all tables are clean. Most callers of GetMultiXactIdMembers are patched to recognize directly that the value is a removable "empty" multixact and avoid calling GetMultiXactIdMembers altogether. To avoid changing the signature of GetMultiXactIdMembers() in back branches, we keep the "allow_old" boolean flag but rename it to "from_pgupgrade"; if the flag is true, we always return an empty set instead of looking up the multixact. (I suppose we could remove the argument in the master branch, but I chose not to do so in this commit). This was broken all along, but the error-facing message appeared first because of commit 8e9a16a and was partially fixed in a25c2b7. This fix, backpatched all the way back to 9.3, goes approximately in the same direction as a25c2b7 but should cover all cases. Bug analysis by Andrew Gierth and Álvaro Herrera. A number of public reports match this bug: https://www.postgresql.org/message-id/20140330040029.GY4582@tamriel.snowman.net https://www.postgresql.org/message-id/538F3D70.6080902@publicrelay.com https://www.postgresql.org/message-id/556439CF.7070109@pscs.co.uk https://www.postgresql.org/message-id/SG2PR06MB0760098A111C88E31BD4D96FB3540@SG2PR06MB0760.apcprd06.prod.outlook.com https://www.postgresql.org/message-id/20160615203829.5798.4594@wrigleys.postgresql.org
1 parent dafdcbb commit 28f294a

File tree

5 files changed

+94
-73
lines changed

5 files changed

+94
-73
lines changed

contrib/pgrowlocks/pgrowlocks.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ pgrowlocks(PG_FUNCTION_ARGS)
165165

166166
values[Atnum_ismulti] = pstrdup("true");
167167

168-
allow_old = !(infomask & HEAP_LOCK_MASK) &&
169-
(infomask & HEAP_XMAX_LOCK_ONLY);
168+
allow_old = HEAP_LOCKED_UPGRADED(infomask);
170169
nmembers = GetMultiXactIdMembers(xmax, &members, allow_old);
171170
if (nmembers == -1)
172171
{

src/backend/access/heap/heapam.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,6 +3359,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
33593359
* HEAP_XMAX_INVALID bit set; that's fine.)
33603360
*/
33613361
if ((oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) ||
3362+
HEAP_LOCKED_UPGRADED(oldtup.t_data->t_infomask) ||
33623363
(checked_lockers && !locker_remains))
33633364
xmax_new_tuple = InvalidTransactionId;
33643365
else
@@ -4641,8 +4642,7 @@ compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask,
46414642
* pg_upgrade; both MultiXactIdIsRunning and MultiXactIdExpand assume
46424643
* that such multis are never passed.
46434644
*/
4644-
if (!(old_infomask & HEAP_LOCK_MASK) &&
4645-
HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))
4645+
if (HEAP_LOCKED_UPGRADED(old_infomask))
46464646
{
46474647
old_infomask &= ~HEAP_XMAX_IS_MULTI;
46484648
old_infomask |= HEAP_XMAX_INVALID;
@@ -5001,6 +5001,16 @@ heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid,
50015001
int i;
50025002
MultiXactMember *members;
50035003

5004+
/*
5005+
* We don't need a test for pg_upgrade'd tuples: this is only
5006+
* applied to tuples after the first in an update chain. Said
5007+
* first tuple in the chain may well be locked-in-9.2-and-
5008+
* pg_upgraded, but that one was already locked by our caller,
5009+
* not us; and any subsequent ones cannot be because our
5010+
* caller must necessarily have obtained a snapshot later than
5011+
* the pg_upgrade itself.
5012+
*/
5013+
Assert(!HEAP_LOCKED_UPGRADED(mytup.t_data->t_infomask));
50045014
nmembers = GetMultiXactIdMembers(rawxmax, &members, false);
50055015
for (i = 0; i < nmembers; i++)
50065016
{
@@ -5329,14 +5339,14 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
53295339
bool has_lockers;
53305340
TransactionId update_xid;
53315341
bool update_committed;
5332-
bool allow_old;
53335342

53345343
*flags = 0;
53355344

53365345
/* We should only be called in Multis */
53375346
Assert(t_infomask & HEAP_XMAX_IS_MULTI);
53385347

5339-
if (!MultiXactIdIsValid(multi))
5348+
if (!MultiXactIdIsValid(multi) ||
5349+
HEAP_LOCKED_UPGRADED(t_infomask))
53405350
{
53415351
/* Ensure infomask bits are appropriately set/reset */
53425352
*flags |= FRM_INVALIDATE_XMAX;
@@ -5349,14 +5359,8 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
53495359
* was a locker only, it can be removed without any further
53505360
* consideration; but if it contained an update, we might need to
53515361
* preserve it.
5352-
*
5353-
* Don't assert MultiXactIdIsRunning if the multi came from a
5354-
* pg_upgrade'd share-locked tuple, though, as doing that causes an
5355-
* error to be raised unnecessarily.
53565362
*/
5357-
Assert((!(t_infomask & HEAP_LOCK_MASK) &&
5358-
HEAP_XMAX_IS_LOCKED_ONLY(t_infomask)) ||
5359-
!MultiXactIdIsRunning(multi));
5363+
Assert(!MultiXactIdIsRunning(multi));
53605364
if (HEAP_XMAX_IS_LOCKED_ONLY(t_infomask))
53615365
{
53625366
*flags |= FRM_INVALIDATE_XMAX;
@@ -5397,9 +5401,8 @@ FreezeMultiXactId(MultiXactId multi, uint16 t_infomask,
53975401
* anything.
53985402
*/
53995403

5400-
allow_old = !(t_infomask & HEAP_LOCK_MASK) &&
5401-
HEAP_XMAX_IS_LOCKED_ONLY(t_infomask);
5402-
nmembers = GetMultiXactIdMembers(multi, &members, allow_old);
5404+
nmembers =
5405+
GetMultiXactIdMembers(multi, &members, false);
54035406
if (nmembers <= 0)
54045407
{
54055408
/* Nothing worth keeping */
@@ -5959,14 +5962,15 @@ static bool
59595962
DoesMultiXactIdConflict(MultiXactId multi, uint16 infomask,
59605963
LockTupleMode lockmode)
59615964
{
5962-
bool allow_old;
59635965
int nmembers;
59645966
MultiXactMember *members;
59655967
bool result = false;
59665968
LOCKMODE wanted = tupleLockExtraInfo[lockmode].hwlock;
59675969

5968-
allow_old = !(infomask & HEAP_LOCK_MASK) && HEAP_XMAX_IS_LOCKED_ONLY(infomask);
5969-
nmembers = GetMultiXactIdMembers(multi, &members, allow_old);
5970+
if (HEAP_LOCKED_UPGRADED(infomask))
5971+
return false;
5972+
5973+
nmembers = GetMultiXactIdMembers(multi, &members, false);
59705974
if (nmembers >= 0)
59715975
{
59725976
int i;
@@ -6037,14 +6041,14 @@ static bool
60376041
Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status,
60386042
int *remaining, uint16 infomask, bool nowait)
60396043
{
6040-
bool allow_old;
60416044
bool result = true;
60426045
MultiXactMember *members;
60436046
int nmembers;
60446047
int remain = 0;
60456048

6046-
allow_old = !(infomask & HEAP_LOCK_MASK) && HEAP_XMAX_IS_LOCKED_ONLY(infomask);
6047-
nmembers = GetMultiXactIdMembers(multi, &members, allow_old);
6049+
/* for pre-pg_upgrade tuples, no need to sleep at all */
6050+
nmembers = HEAP_LOCKED_UPGRADED(infomask) ? -1 :
6051+
GetMultiXactIdMembers(multi, &members, false);
60486052

60496053
if (nmembers >= 0)
60506054
{
@@ -6168,20 +6172,18 @@ heap_tuple_needs_freeze(HeapTupleHeader tuple, TransactionId cutoff_xid,
61686172
/* no xmax set, ignore */
61696173
;
61706174
}
6175+
else if (HEAP_LOCKED_UPGRADED(tuple->t_infomask))
6176+
return true;
61716177
else if (MultiXactIdPrecedes(multi, cutoff_multi))
61726178
return true;
61736179
else
61746180
{
61756181
MultiXactMember *members;
61766182
int nmembers;
61776183
int i;
6178-
bool allow_old;
61796184

61806185
/* need to check whether any member of the mxact is too old */
6181-
6182-
allow_old = !(tuple->t_infomask & HEAP_LOCK_MASK) &&
6183-
HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask);
6184-
nmembers = GetMultiXactIdMembers(multi, &members, allow_old);
6186+
nmembers = GetMultiXactIdMembers(multi, &members, false);
61856187

61866188
for (i = 0; i < nmembers; i++)
61876189
{
@@ -6858,7 +6860,8 @@ heap_xlog_freeze_tuple(HeapTupleHeader tuple, TransactionId cutoff_xid,
68586860
xid = HeapTupleHeaderGetRawXmax(tuple);
68596861
if ((tuple->t_infomask & HEAP_XMAX_IS_MULTI) ?
68606862
(MultiXactIdIsValid(xid) &&
6861-
MultiXactIdPrecedes(xid, cutoff_multi)) :
6863+
(HEAP_LOCKED_UPGRADED(tuple->t_infomask) ||
6864+
MultiXactIdPrecedes(xid, cutoff_multi))) :
68626865
(TransactionIdIsNormal(xid) &&
68636866
TransactionIdPrecedes(xid, cutoff_xid)))
68646867
{

src/backend/access/transam/multixact.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,23 +1189,29 @@ GetNewMultiXactId(int nmembers, MultiXactOffset *offset)
11891189

11901190
/*
11911191
* GetMultiXactIdMembers
1192-
* Returns the set of MultiXactMembers that make up a MultiXactId
1193-
*
1194-
* If the given MultiXactId is older than the value we know to be oldest, we
1195-
* return -1. The caller is expected to allow that only in permissible cases,
1196-
* i.e. when the infomask lets it presuppose that the tuple had been
1197-
* share-locked before a pg_upgrade; this means that the HEAP_XMAX_LOCK_ONLY
1198-
* needs to be set, but HEAP_XMAX_KEYSHR_LOCK and HEAP_XMAX_EXCL_LOCK are not
1199-
* set.
1200-
*
1201-
* Other border conditions, such as trying to read a value that's larger than
1202-
* the value currently known as the next to assign, raise an error. Previously
1203-
* these also returned -1, but since this can lead to the wrong visibility
1204-
* results, it is dangerous to do that.
1192+
* Return the set of MultiXactMembers that make up a MultiXactId
1193+
*
1194+
* Return value is the number of members found, or -1 if there are none,
1195+
* and *members is set to a newly palloc'ed array of members. It's the
1196+
* caller's responsibility to free it when done with it.
1197+
*
1198+
* from_pgupgrade must be passed as true if and only if only the multixact
1199+
* corresponds to a value from a tuple that was locked in a 9.2-or-older
1200+
* installation and later pg_upgrade'd (that is, the infomask is
1201+
* HEAP_LOCKED_UPGRADED). In this case, we know for certain that no members
1202+
* can still be running, so we return -1 just like for an empty multixact
1203+
* without any further checking. It would be wrong to try to resolve such a
1204+
* multixact: either the multixact is within the current valid multixact
1205+
* range, in which case the returned result would be bogus, or outside that
1206+
* range, in which case an error would be raised.
1207+
*
1208+
* In all other cases, the passed multixact must be within the known valid
1209+
* range, that is, greater to or equal than oldestMultiXactId, and less than
1210+
* nextMXact. Otherwise, an error is raised.
12051211
*/
12061212
int
12071213
GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
1208-
bool allow_old)
1214+
bool from_pgupgrade)
12091215
{
12101216
int pageno;
12111217
int prev_pageno;
@@ -1224,7 +1230,7 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
12241230

12251231
debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
12261232

1227-
if (!MultiXactIdIsValid(multi))
1233+
if (!MultiXactIdIsValid(multi) || from_pgupgrade)
12281234
return -1;
12291235

12301236
/* See if the MultiXactId is in the local cache */
@@ -1244,18 +1250,11 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
12441250
*
12451251
* An ID older than MultiXactState->oldestMultiXactId cannot possibly be
12461252
* useful; it has already been removed, or will be removed shortly, by
1247-
* truncation. Returning the wrong values could lead to an incorrect
1248-
* visibility result. However, to support pg_upgrade we need to allow an
1249-
* empty set to be returned regardless, if the caller is willing to accept
1250-
* it; the caller is expected to check that it's an allowed condition
1251-
* (such as ensuring that the infomask bits set on the tuple are
1252-
* consistent with the pg_upgrade scenario). If the caller is expecting
1253-
* this to be called only on recently created multis, then we raise an
1254-
* error.
1253+
* truncation. If one is passed, an error is raised.
12551254
*
1256-
* Conversely, an ID >= nextMXact shouldn't ever be seen here; if it is
1257-
* seen, it implies undetected ID wraparound has occurred. This raises a
1258-
* hard error.
1255+
* Also, an ID >= nextMXact shouldn't ever be seen here; if it is seen, it
1256+
* implies undetected ID wraparound has occurred. This raises a hard
1257+
* error.
12591258
*
12601259
* Shared lock is enough here since we aren't modifying any global state.
12611260
* Acquire it just long enough to grab the current counter values. We may
@@ -1271,7 +1270,7 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
12711270

12721271
if (MultiXactIdPrecedes(multi, oldestMXact))
12731272
{
1274-
ereport(allow_old ? DEBUG1 : ERROR,
1273+
ereport(ERROR,
12751274
(errcode(ERRCODE_INTERNAL_ERROR),
12761275
errmsg("MultiXactId %u does no longer exist -- apparent wraparound",
12771276
multi)));
@@ -1443,7 +1442,7 @@ MultiXactHasRunningRemoteMembers(MultiXactId multi)
14431442
int nmembers;
14441443
int i;
14451444

1446-
nmembers = GetMultiXactIdMembers(multi, &members, true);
1445+
nmembers = GetMultiXactIdMembers(multi, &members, false);
14471446
if (nmembers <= 0)
14481447
return false;
14491448

src/backend/utils/time/tqual.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,9 @@ HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
701701

702702
if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
703703
{
704-
if (MultiXactHasRunningRemoteMembers(xmax))
704+
if (HEAP_LOCKED_UPGRADED(tuple->t_infomask))
705+
return HeapTupleMayBeUpdated;
706+
else if (MultiXactHasRunningRemoteMembers(xmax))
705707
return HeapTupleBeingUpdated;
706708
else
707709
return HeapTupleMayBeUpdated;
@@ -725,6 +727,7 @@ HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
725727

726728
/* not LOCKED_ONLY, so it has to have an xmax */
727729
Assert(TransactionIdIsValid(xmax));
730+
Assert(!HEAP_LOCKED_UPGRADED(tuple->t_infomask));
728731

729732
/* updating subtransaction must have aborted */
730733
if (!TransactionIdIsCurrentTransactionId(xmax))
@@ -787,15 +790,12 @@ HeapTupleSatisfiesUpdate(HeapTupleHeader tuple, CommandId curcid,
787790
{
788791
TransactionId xmax;
789792

793+
if (HEAP_LOCKED_UPGRADED(tuple->t_infomask))
794+
return HeapTupleMayBeUpdated;
795+
790796
if (HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask))
791797
{
792-
/*
793-
* If it's only locked but neither EXCL_LOCK nor KEYSHR_LOCK is
794-
* set, it cannot possibly be running. Otherwise need to check.
795-
*/
796-
if ((tuple->t_infomask & (HEAP_XMAX_EXCL_LOCK |
797-
HEAP_XMAX_KEYSHR_LOCK)) &&
798-
MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))
798+
if (MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))
799799
return HeapTupleBeingUpdated;
800800

801801
SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId);
@@ -1401,25 +1401,20 @@ HeapTupleSatisfiesVacuum(HeapTupleHeader tuple, TransactionId OldestXmin,
14011401
* "Deleting" xact really only locked it, so the tuple is live in any
14021402
* case. However, we should make sure that either XMAX_COMMITTED or
14031403
* XMAX_INVALID gets set once the xact is gone, to reduce the costs of
1404-
* examining the tuple for future xacts. Also, marking dead
1405-
* MultiXacts as invalid here provides defense against MultiXactId
1406-
* wraparound (see also comments in heap_freeze_tuple()).
1404+
* examining the tuple for future xacts.
14071405
*/
14081406
if (!(tuple->t_infomask & HEAP_XMAX_COMMITTED))
14091407
{
14101408
if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
14111409
{
14121410
/*
1413-
* If it's only locked but neither EXCL_LOCK nor KEYSHR_LOCK
1414-
* are set, it cannot possibly be running; otherwise have to
1415-
* check.
1411+
* If it's a pre-pg_upgrade tuple, the multixact cannot
1412+
* possibly be running; otherwise have to check.
14161413
*/
1417-
if ((tuple->t_infomask & (HEAP_XMAX_EXCL_LOCK |
1418-
HEAP_XMAX_KEYSHR_LOCK)) &&
1414+
if (!HEAP_LOCKED_UPGRADED(tuple->t_infomask) &&
14191415
MultiXactIdIsRunning(HeapTupleHeaderGetRawXmax(tuple)))
14201416
return HEAPTUPLE_LIVE;
14211417
SetHintBits(tuple, buffer, HEAP_XMAX_INVALID, InvalidTransactionId);
1422-
14231418
}
14241419
else
14251420
{

src/include/access/htup_details.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,31 @@ struct HeapTupleHeaderData
203203
(((infomask) & HEAP_XMAX_LOCK_ONLY) || \
204204
(((infomask) & (HEAP_XMAX_IS_MULTI | HEAP_LOCK_MASK)) == HEAP_XMAX_EXCL_LOCK))
205205

206+
/*
207+
* A tuple that has HEAP_XMAX_IS_MULTI and HEAP_XMAX_LOCK_ONLY but neither of
208+
* XMAX_EXCL_LOCK and XMAX_KEYSHR_LOCK must come from a tuple that was
209+
* share-locked in 9.2 or earlier and then pg_upgrade'd.
210+
*
211+
* In 9.2 and prior, HEAP_XMAX_IS_MULTI was only set when there were multiple
212+
* FOR SHARE lockers of that tuple. That set HEAP_XMAX_LOCK_ONLY (with a
213+
* different name back then) but neither of HEAP_XMAX_EXCL_LOCK and
214+
* HEAP_XMAX_KEYSHR_LOCK. That combination is no longer possible in 9.3 and
215+
* up, so if we see that combination we know for certain that the tuple was
216+
* locked in an earlier release; since all such lockers are gone (they cannot
217+
* survive through pg_upgrade), such tuples can safely be considered not
218+
* locked.
219+
*
220+
* We must not resolve such multixacts locally, because the result would be
221+
* bogus, regardless of where they stand with respect to the current valid
222+
* multixact range.
223+
*/
224+
#define HEAP_LOCKED_UPGRADED(infomask) \
225+
( \
226+
((infomask) & HEAP_XMAX_IS_MULTI) && \
227+
((infomask) & HEAP_XMAX_LOCK_ONLY) && \
228+
(((infomask) & (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)) == 0) \
229+
)
230+
206231
/*
207232
* Use these to test whether a particular lock is applied to a tuple
208233
*/

0 commit comments

Comments
 (0)