Re: GetNamedLWLockTranche crashes on Windows in normal backend - Mailing list pgsql-hackers

From Sami Imseih
Subject Re: GetNamedLWLockTranche crashes on Windows in normal backend
Date
Msg-id CAA5RZ0un+WRqsM35JUE97LOdrjGbE1VZpL0h0kaQn-_y5j8U0A@mail.gmail.com
Whole thread Raw
In response to Re: GetNamedLWLockTranche crashes on Windows in normal backend  (Nathan Bossart <nathandbossart@gmail.com>)
Responses Re: GetNamedLWLockTranche crashes on Windows in normal backend
Re: GetNamedLWLockTranche crashes on Windows in normal backend
List 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

pgsql-hackers by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: index prefetching
Next
From: Sami Imseih
Date:
Subject: Re: GetNamedLWLockTranche crashes on Windows in normal backend