Fix RequestNamedLWLockTranche in single-user mode

PostmasterContext is not available in single-user mode, use
TopMemoryContext instead. Also make sure that we use the correct
memory context in the lappend().

Author: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://www.postgresql.org/message-id/acb_Eo1XtmCO_9z7@nathan
This commit is contained in:
Heikki Linnakangas 2026-03-28 01:02:11 +02:00
parent 1f6f200cab
commit 2407c8db15

View file

@ -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);
}
/*