Skip to content

Commit fe705ef

Browse files
committed
doc: Expand section related to LWLocks and shared memory
The documentation includes a section describing how to define custom LWLocks in extensions using the shmem hooks. However, it has never mentioned the second, more flexible method based on the following routines: - LWLockNewTrancheId() to allocate a tranche ID. - LWLockRegisterTranche() to associate a name to a tranche ID. - LWLockInitialize() to initialize a LWLock with a tranche ID. autoprewarm.c is the only example of extension in the tree that allocates a LWLock this way. This commit adds some documentation about all that. While on it, a comment is added about the need of AddinShmemInitLock. This is required especially for EXEC_BACKEND builds (aka Windows, normally), as per a remark from Alexander, because backends can execute shmem initialization paths concurrently. Author: Aleksander Alekseev, Michael Paquier Discussion: https://postgr.es/m/CAJ7c6TPKhFgL+54cdTD9yGpG4+sNcyJ+N1GvQqAxgWENAOa3VA@mail.gmail.com
1 parent 6ec62b7 commit fe705ef

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

doc/src/sgml/xfunc.sgml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3428,6 +3428,29 @@ void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
34283428
<filename>contrib/pg_stat_statements/pg_stat_statements.c</filename> in the
34293429
<productname>PostgreSQL</productname> source tree.
34303430
</para>
3431+
<para>
3432+
There is another, more flexible method of obtaining LWLocks. First,
3433+
allocate a <literal>tranche_id</literal> from a shared counter by
3434+
calling:
3435+
<programlisting>
3436+
int LWLockNewTrancheId(void)
3437+
</programlisting>
3438+
Next, each individual process using the <literal>tranche_id</literal>
3439+
should associate it with a <literal>tranche_name</literal> by calling:
3440+
<programlisting>
3441+
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
3442+
</programlisting>
3443+
It is also required to call <function>LWLockInitialize</function> once
3444+
per LWLock, passing the <literal>tranche_id</literal> as argument:
3445+
<programlisting>
3446+
void LWLockInitialize(LWLock *lock, int tranche_id)
3447+
</programlisting>
3448+
A complete usage example of <function>LWLockNewTrancheId</function>,
3449+
<function>LWLockInitialize</function> and
3450+
<function>LWLockRegisterTranche</function> can be found in
3451+
<filename>contrib/pg_prewarm/autoprewarm.c</filename> in the
3452+
<productname>PostgreSQL</productname> source tree.
3453+
</para>
34313454
<para>
34323455
To avoid possible race-conditions, each backend should use the LWLock
34333456
<function>AddinShmemInitLock</function> when connecting to and initializing
@@ -3451,6 +3474,13 @@ if (!ptr)
34513474
}
34523475
</programlisting>
34533476
</para>
3477+
<para>
3478+
It is convenient to use <literal>shmem_startup_hook</literal> which allows
3479+
placing all the code responsible for initializing shared memory in one
3480+
place. When using <literal>shmem_startup_hook</literal> the extension
3481+
still needs to acquire <function>AddinShmemInitLock</function> in order to
3482+
work properly on all the supported platforms.
3483+
</para>
34543484
</sect2>
34553485

34563486
<sect2 id="xfunc-addin-wait-events">

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy