pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
To: Andres Freund <andres@anarazel.de>
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Sami Imseih <samimseih@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Subject: Re: Clean up NamedLWLockTranche stuff
Date: Fri, 27 Mar 2026 17:05:06 -0500
Message-ID: <acb_Eo1XtmCO_9z7@nathan> (raw)
In-Reply-To: <acb7lL5zrsieS9k3@nathan>
References: <acVEqGemyK-Yjswa@nathan>
	<adab98a3-a0e3-4455-9b63-ae6f1ecd18c9@iki.fi>
	<CAA5RZ0ueB84OUews-mS0+ojeWaDtcoV1qY4CNZD8PrdGcaXW-g@mail.gmail.com>
	<d78edccb-27c0-4c9c-baee-1ce7745c8cc4@iki.fi>
	<0ba6cb8c-a893-4445-b8fa-fa9f52be8f39@iki.fi>
	<CAA5RZ0s+-_d6RBSfmLvekGLTCi2EZ7hiHp3RhMVDEexGXq=7Vg@mail.gmail.com>
	<CAA5RZ0sLENRM+BicUjQFs_rP38oPx3gm0SsGrD0-jMhhM+HZ_w@mail.gmail.com>
	<1e1dd445-730e-40c9-b0f7-4d153bb013fb@iki.fi>
	<xsgt5dpoo27vezqf4a255v35sxsgtyxklwolganultoftmuzlo@vjpujzjhzmk2>
	<acb7lL5zrsieS9k3@nathan>

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

Attachments:

0001-fix-RequestNamedLWLockTranche.patch (text/plain, 1.5K) download From 714c765399656c9743e3ad76fe52d1cfc8994952 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <nathan@postgresql.org> 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)

view thread (18+ messages)  latest in thread

Message-ID: <acb_Eo1XtmCO_9z7@nathan>
Permalink:  ../acb_Eo1XtmCO_9z7@nathan/
Also on:    postgresql.org/message-id/acb_Eo1XtmCO_9z7@nathan

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com, andres@anarazel.de, hlinnaka@iki.fi, samimseih@gmail.com, robertmhaas@gmail.com
  Subject: Re: Clean up NamedLWLockTranche stuff
  In-Reply-To: <acb_Eo1XtmCO_9z7@nathan>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox