Re: GetNamedLWLockTranche crashes on Windows in normal backend

From: Sami Imseih <samimseih(at)gmail(dot)com>
To: Nathan Bossart <nathandbossart(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: GetNamedLWLockTranche crashes on Windows in normal backend
Date: 2025-08-25 18:44:22
Message-ID: CAA5RZ0un+WRqsM35JUE97LOdrjGbE1VZpL0h0kaQn-_y5j8U0A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Mon, Aug 25, 2025 at 12:58:08PM -0500, Sami Imseih wrote:
> >> If this fails, why doesn't the call in pgss_shmem_startup() also fail? Was
> >> pg_stat_statements not loaded via shared_preload_libraries?
> >
> > because the array is valid in postmaster, but it's not for a normal backend,
> > since it's not getting copied.
> >
> > Yes, pg_stat_statements was loaded via shared_preload_libraries. Nothing
> > was done out of the ordinary.
> >
> >> And is the
> >> failure a segfault or a "requested tranche is not registered" error?
> >
> > It's a segfault.
>
> Oh, I see what's happening. We are never calling GetNamedLWLockTranche()
> in backends because ShmemInitStruct() always sets *found.
>
> I'd argue that calling GetNamedLWLockTranche() outside of an "if (!found)"
> block in a shmem_startup_hook doesn't follow convention, but the docs have
> this note:
>
> shmem_startup_hook provides a convenient place for the initialization
> code, but it is not strictly required that all such code be placed in
> this hook.
>
> Like the inaccurate sentence immediately following this one [0], it was
> added by commit 964152c. IMHO we should adjust the documentation to match
> reality.
>
> [0] https://postgr.es/m/aJ9QIF3g2QQko9wq%40nathan

Why not ERROR out completely if we are calling this function outside
of postmaster
startup? I don't think we should be satisfied with just a
documentation change, since this
leads to a segfault. We can do something like below at the top of
GetNamedLWLockTranche

```
if (IsUnderPostmaster)
elog(ERROR, "GetNamedLWLockTranche can only be called during
postmaster startup");
```

Another approach is to just change GetNamedLWLockTranche to use
NamedLWLockTrancheArray since that is already copied in EXEC_BACKEND, and allow
GetNamedLWLockTranche to continue to be used outside of startup. In
this case, we
will need to add num_lwlocks field to NamedLWLockTrancheArray. This
might be better
to backpatch, since we will not be changing user facing behavior.

Attached is a repro patch. You will need to set
shared_preload_libraries = 'pg_stat_statements'
as well.

--
Sami

Attachment Content-Type Size
v1-0001-repro.patch application/octet-stream 1.4 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2025-08-25 18:51:28 Re: GetNamedLWLockTranche crashes on Windows in normal backend
Previous Message Tomas Vondra 2025-08-25 18:33:27 Re: index prefetching