Skip to content

Commit d24c565

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 3cb828d commit d24c565

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
@@ -1241,7 +1241,10 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
12411241
debug_elog3(DEBUG2, "GetMembers: asked for %u", multi);
12421242

12431243
if (!MultiXactIdIsValid(multi) || from_pgupgrade)
1244+
{
1245+
*members = NULL;
12441246
return -1;
1247+
}
12451248

12461249
/* See if the MultiXactId is in the local cache */
12471250
length = mXactCacheGetById(multi, members);
@@ -1292,13 +1295,10 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
12921295
LWLockRelease(MultiXactGenLock);
12931296

12941297
if (MultiXactIdPrecedes(multi, oldestMXact))
1295-
{
12961298
ereport(ERROR,
12971299
(errcode(ERRCODE_INTERNAL_ERROR),
12981300
errmsg("MultiXactId %u does no longer exist -- apparent wraparound",
12991301
multi)));
1300-
return -1;
1301-
}
13021302

13031303
if (!MultiXactIdPrecedes(multi, nextMXact))
13041304
ereport(ERROR,
@@ -1398,7 +1398,6 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
13981398
LWLockRelease(MultiXactOffsetSLRULock);
13991399

14001400
ptr = (MultiXactMember *) palloc(length * sizeof(MultiXactMember));
1401-
*members = ptr;
14021401

14031402
/* Now get the members themselves. */
14041403
LWLockAcquire(MultiXactMemberSLRULock, LW_EXCLUSIVE);
@@ -1443,13 +1442,17 @@ GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members,
14431442

14441443
LWLockRelease(MultiXactMemberSLRULock);
14451444

1445+
/* A multixid with zero members should not happen */
1446+
Assert(truelength > 0);
1447+
14461448
/*
14471449
* Copy the result into the local cache.
14481450
*/
14491451
mXactCachePut(multi, truelength, ptr);
14501452

14511453
debug_elog3(DEBUG2, "GetMembers: no cache for %s",
14521454
mxid_to_string(multi, truelength, ptr));
1455+
*members = ptr;
14531456
return truelength;
14541457
}
14551458

@@ -1550,7 +1553,6 @@ mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
15501553

15511554
size = sizeof(MultiXactMember) * entry->nmembers;
15521555
ptr = (MultiXactMember *) palloc(size);
1553-
*members = ptr;
15541556

15551557
memcpy(ptr, entry->members, size);
15561558

@@ -1566,6 +1568,7 @@ mXactCacheGetById(MultiXactId multi, MultiXactMember **members)
15661568
*/
15671569
dlist_move_head(&MXactCache, iter.cur);
15681570

1571+
*members = ptr;
15691572
return entry->nmembers;
15701573
}
15711574
}

0 commit comments

Comments
 (0)