]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix RequestNamedLWLockTranche in single-user mode
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 27 Mar 2026 23:02:11 +0000 (01:02 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 27 Mar 2026 23:02:11 +0000 (01:02 +0200)
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

src/backend/storage/lmgr/lwlock.c

index 7a68071302a0e6fb2725ec2ec3a5c8dd0197843d..f5b2a6d479dfc524a7d3306e7495a845ecead2a7 100644 (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);
 }
 
 /*