Skip to content

Commit 42f782b

Browse files
committed
Tidy up GetMultiXactIdMembers()'s behavior on error
One of the error paths left *members uninitialized. That's not a live bug, because most callers don't look at *members when the function returns -1, but let's be tidy. One caller, in heap_lock_tuple(), does "if (members != NULL) pfree(members)", but AFAICS it never passes an invalid 'multi' value so it should not reach that error case. The callers are also a bit inconsistent in their expectations. heap_lock_tuple() pfrees the 'members' array if it's not-NULL, others pfree() it if "nmembers >= 0", and others if "nmembers > 0". That's not a live bug either, because the function should never return 0, but add an Assert for that to make it more clear. I left the callers alone for now. I also moved the line where we set *nmembers. It wasn't wrong before, but I like to do that right next to the 'return' statement, to make it clear that it's always set on return. Also remove one unreachable return statement after ereport(ERROR), for brevity and for consistency with the similar if-block right after it. Author: Greg Nancarrow with the additional changes by me Backpatch-through: 9.6, all supported versions
1 parent 3989f8f commit 42f782b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/backend/access/transam/multixact.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,10 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
12221222
debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
12231223

12241224
if (!MultiXactIdIsValid(multi) || from_pgupgrade)
1225+
{
1226+
*members = NULL;
12251227
return -1;
1228+
}
12261229

12271230
/* See if the MultiXactId is in the local cache */
12281231
length = mXactCacheGetById(multi, members);
@@ -1273,13 +1276,10 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
12731276
LWLockRelease(MultiXactGenLock);
12741277

12751278
if (MultiXactIdPrecedes(multi, oldestMXact))
1276-
{
12771279
ereport(ERROR,
12781280
(errcode(ERRCODE_INTERNAL_ERROR),
12791281
errmsg("MultiXactId %u does no longer exist -- apparent wraparound",
12801282
multi)));
1281-
return -1;
1282-
}
12831283

12841284
if (!MultiXactIdPrecedes(multi, nextMXact))
12851285
ereport(ERROR,
@@ -1379,7 +1379,6 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
13791379
LWLockRelease(MultiXactOffsetSLRULock);
13801380

13811381
ptr = (MultiXactMember *) palloc(length * sizeof(MultiXactMember));
1382-
*members = ptr;
13831382

13841383
/* Now get the members themselves. */
13851384
LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
@@ -1424,13 +1423,17 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
14241423

14251424
LWLockRelease(MultiXactMemberSLRULock);
14261425

1426+
/* A multixid with zero members should not happen */
1427+
Assert(truelength > 0);
1428+
14271429
/*
14281430
* Copy the result into the local cache.
14291431
*/
14301432
mXactCachePut(multi, truelength, ptr);
14311433

14321434
debug_elog3(DEBUG2, "GetMembers: no cache for %s",
14331435
mxid_to_string(multi, truelength, ptr));
1436+
*members = ptr;
14341437
return truelength;
14351438
}
14361439

@@ -1531,7 +1534,6 @@ mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
15311534

15321535
size = sizeof(MultiXactMember) * entry->nmembers;
15331536
ptr = (MultiXactMember *) palloc(size);
1534-
*members = ptr;
15351537

15361538
memcpy(ptr, entry->members, size);
15371539

@@ -1547,6 +1549,7 @@ mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
15471549
*/
15481550
dlist_move_head(&MXactCache, iter.cur);
15491551

1552+
*members = ptr;
15501553
return entry->nmembers;
15511554
}
15521555
}

0 commit comments

Comments
 (0)