On Fri, Mar 27, 2026 at 04:50:12PM -0500, Nathan Bossart wrote:
> On Fri, Mar 27, 2026 at 05:22:33PM -0400, Andres Freund wrote:
>> TRAP: failed Assert("MemoryContextIsValid(context)"), File: "mcxt.c", Line:
>> 1270, PID: 230491
>> [...](ExceptionalCondition+0x54)[0xaaaae186c204]
>> [...](MemoryContextAllocExtended+0x0)[0xaaaae18a2a24]
>> [...](RequestNamedLWLockTranche+0x6c)[0xaaaae16e7310]
>> [...](process_shmem_requests+0x28)[0xaaaae1881628]
>> [...](PostgresSingleUserMain+0xc4)[0xaaaae1701a34]
>> [...](main+0x6ac)[0xaaaae12a2adc]
>> /lib/aarch64-linux-gnu/libc.so.6(__libc_start_main+0xe8)[0xffff99713dd8]
>> [...](+0xf2b98)[0xaaaae12a2b98]
>> Aborted
>> pg_rewind: error: postgres single-user mode in target cluster failed
>
> Hm. AFAICT PostmasterContext isn't created in single-user mode, and the
> commit in question has RequestNamedLWLockTranche() allocate requests there.
> I guess the idea is to allow backends to free that memory after forking
> from postmaster, but we don't do that for the NamedLWLockTrancheRequests
> list. Maybe we should surround the last part of that function with
> MemoryContextSwitchTo(...) to either TopMemoryContext or PostmasterContext
> depending on whether we're in single-user mode.
Concretely, like the attached.
--
nathan
>From 714c765399656c9743e3ad76fe52d1cfc8994952 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <[email protected]>
Date: Fri, 27 Mar 2026 17:00:49 -0500
Subject: [PATCH 1/1] fix RequestNamedLWLockTranche
---
src/backend/storage/lmgr/lwlock.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/src/backend/storage/lmgr/lwlock.c
b/src/backend/storage/lmgr/lwlock.c
index 7a68071302a..f5b2a6d479d 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -630,6 +630,7 @@ void
RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
{
NamedLWLockTrancheRequest *request;
+ MemoryContext oldcontext;
if (!process_shmem_requests_in_progress)
elog(FATAL, "cannot request additional LWLocks outside
shmem_request_hook");
@@ -652,10 +653,17 @@ RequestNamedLWLockTranche(const char *tranche_name, int
num_lwlocks)
errdetail("No more than %d tranches may be
registered.",
MAX_USER_DEFINED_TRANCHES)));
- request = MemoryContextAllocZero(PostmasterContext,
sizeof(NamedLWLockTrancheRequest));
+ if (IsPostmasterEnvironment)
+ oldcontext = MemoryContextSwitchTo(PostmasterContext);
+ else
+ oldcontext = MemoryContextSwitchTo(TopMemoryContext);
+
+ request = palloc0(sizeof(NamedLWLockTrancheRequest));
strlcpy(request->tranche_name, tranche_name, NAMEDATALEN);
request->num_lwlocks = num_lwlocks;
NamedLWLockTrancheRequests = lappend(NamedLWLockTrancheRequests,
request);
+
+ MemoryContextSwitchTo(oldcontext);
}
/*
--
2.50.1 (Apple Git-155)