GetNamedLWLockTranche crashes on Windows in normal backend

From: Sami Imseih <samimseih(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: GetNamedLWLockTranche crashes on Windows in normal backend
Date: 2025-08-22 19:21:55
Message-ID: CAA5RZ0v1_15QPg5Sqd2Qz5rh_qcsyCeHHmRDY89xVHcy2yt5BQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While working on [0], I observed that $SUBJECT. I encountered this issue
while building test cases for [0], and in which GetNamedLWLockTranche is
called outside of startup.

This crash, reproducible on HEAD, occurs because NamedLWLockTrancheRequestArray
( a local array populated during shmem_request ) is not copied over in
exec:ed backends.
This is reproducible on all EXEC_BACKEND environments.
On Linux, this is not an issue because of the magic of fork.

Since this data is not in shared memory, it will need much extra handling
in launch_backend.c, if at all safe, to get it copied. I am not too
familiar with
EXEC_BACKEND, so I could be wrong.
NamedLWLockTrancheRequests, which tracks the length of
NamedLWLockTrancheRequestArray is copied successfully.

Now, I wonder if the correct solution is just to allow GetNamedLWLockTranche
to be called ONLY when !IsUnderPostmaster, and error out if that's not
the case.
The docs [1] for the function mention it should be used during startup, and I
can't envision a reason one would want to use it outside.

We can also create a new global bool for shmem_startup progress, similar to
process_shmem_requests_in_progress, and ensure that this function is only
called at that time.

I repro'd this on a Windows machine, but one can also enable EXEC_BACKEND
in pg_config_manual.h and call GetNamedLWLockTranche by a normal backend.

I did this by injecting
```
GetNamedLWLockTranche("pg_stat_statements");
```
inside pgss_ExecutorEnd

[0] https://www.postgresql.org/message-id/CAA5RZ0vvED3naph8My8Szv6DL4AxOVK3eTPS0qXsaKi%3DbVdW2A%40mail.gmail.com
[1] https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-SHARED-ADDIN-AT-STARTUP

--
Sami Imseih
Amazon Web Services (AWS)

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexandra Wang 2025-08-22 19:33:22 Re: SQL:2023 JSON simplified accessor support
Previous Message Tom Lane 2025-08-22 19:11:34 Re: Making jsonb_agg() faster