Re: "38.10.10. Shared Memory and LWLocks" may require a clarification

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Aleksander Alekseev <aleksander(at)timescale(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: "38.10.10. Shared Memory and LWLocks" may require a clarification
Date: 2023-10-27 06:52:25
Message-ID: ZTteKRrIdaYAsGWv@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, May 23, 2023 at 01:47:52PM +0300, Aleksander Alekseev wrote:
> That's me still talking to myself :)

Let's be two then.

> Evidently this works differently from what I initially thought on
> Windows due to lack of fork() on this system.

This comes down to the fact that processes executed with EXEC_BACKEND,
because Windows does not know how to do a fork(), need to update their
local variables to point to the shared memory structures already
created, so we have to call CreateSharedMemoryAndSemaphores() in this
case.

> PFA the patch v3. Your feedback is most welcomed.

+ <para>
+ It is convenient to use <literal>shmem_startup_hook</literal> which allows
+ placing all the code responsible for initializing shared memory in one place.
+ When using <literal>shmem_startup_hook</literal> the extension still needs
+ to acquire <function>AddinShmemInitLock</function> in order to work properly
+ on all the supported platforms including Windows.

Yeah, AddinShmemInitLock is useful because extensions have no base
point outside that, and they may want to update their local variables.
Still, this is not completely exact because EXEC_BACKEND on
non-Windows platform would still need it, so this should be mentioned.
Another thing is that extensions may do like autoprewarm.c, where
the shmem area is not initialized in the startup shmem hook. This is
a bit cheating because this is a scenario where shmem_request_hook is
not requested, so shmem needs overallocation, but you'd also need a
LWLock in this case, even for non-WIN32.

+ on all the supported platforms including Windows. This is also the reason
+ why the return value of <function>GetNamedLWLockTranche</function> is
+ conventionally stored in shared memory instead of local process memory.
+ </para>

Not sure to follow this sentence, the result of GetNamedLWLockTranche
is the lock, so this sentence does not seem really necessary?

While we're on it, why not improving this part of the documentation more
modern? We don't mention LWLockNewTrancheId() and
LWLockRegisterTranche() at all, so do you think that it would be worth
adding a sample of code with that, mentioning autoprewarm.c as
example?
--
Michael

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Laurenz Albe 2023-10-27 06:58:02 Re: Postgres Partitions Limitations (5.11.2.3)
Previous Message Laurenz Albe 2023-10-27 06:48:49 Re: unnest multirange, returned order