On Mon, Sep 08, 2025 at 04:31:43PM -0500, Sami Imseih wrote: > I still think we need to mention EXEC_BACKEND somehow. The way it's done > in [0], it says "On Windows (and anywhere else where EXEC_BACKEND is > defined)" > > So we do have precedent of mentioning EXEC_BACKEND in docs, and it’s > clearer than the ambiguity of saying 'on some builds'/'in other builds'.
Added in v2. -- nathan
>From 2ed006e00e011707a1b86c31f13f7366590e56d9 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <[email protected]> Date: Mon, 8 Sep 2025 16:08:00 -0500 Subject: [PATCH v2 1/1] Fix shmem_startup_hook documentation. --- doc/src/sgml/xfunc.sgml | 11 +++++++---- src/test/modules/test_slru/test_slru.c | 21 +++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml index da21ef56891..6c9b447c667 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -3668,11 +3668,14 @@ LWLockRelease(AddinShmemInitLock); </programlisting> <literal>shmem_startup_hook</literal> provides a convenient place for the initialization code, but it is not strictly required that all such code - be placed in this hook. Each backend will execute the registered - <literal>shmem_startup_hook</literal> shortly after it attaches to shared - memory. Note that add-ins should still acquire + be placed in this hook. On Windows (and anywhere else where + <literal>EXEC_BACKEND</literal> is defined), each backend executes the + registered <literal>shmem_startup_hook</literal> shortly after it + attaches to shared memory, so add-ins should still acquire <function>AddinShmemInitLock</function> within this hook, as shown in the - example above. + example above. On other platforms, the postmaster process executes the + <literal>shmem_startup_hook</literal> once, and each backend + automatically inherits the pointers to shared memory. </para> <para> diff --git a/src/test/modules/test_slru/test_slru.c b/src/test/modules/test_slru/test_slru.c index 8c0367eeee4..f41422cca7d 100644 --- a/src/test/modules/test_slru/test_slru.c +++ b/src/test/modules/test_slru/test_slru.c @@ -219,8 +219,8 @@ test_slru_shmem_startup(void) */ const bool long_segment_names = true; const char slru_dir_name[] = "pg_test_slru"; - int test_tranche_id; - int test_buffer_tranche_id; + int test_tranche_id = -1; + int test_buffer_tranche_id = -1; if (prev_shmem_startup_hook) prev_shmem_startup_hook(); @@ -231,10 +231,19 @@ test_slru_shmem_startup(void) */ (void) MakePGDirectory(slru_dir_name); - /* initialize the SLRU facility */ - test_tranche_id = LWLockNewTrancheId("test_slru_tranche"); - - test_buffer_tranche_id = LWLockNewTrancheId("test_buffer_tranche"); + /* + * Initialize the SLRU facility. + * + * In EXEC_BACKEND builds, the shmem_startup_hook is called in the + * postmaster and in each backend, but we only need to generate the LWLock + * tranches once. Note that these IDs are not used by SimpleLruInit() in + * the IsUnderPostmaster (i.e., backend) case. + */ + if (!IsUnderPostmaster) + { + test_tranche_id = LWLockNewTrancheId("test_slru_tranche"); + test_buffer_tranche_id = LWLockNewTrancheId("test_buffer_tranche"); + } TestSlruCtl->PagePrecedes = test_slru_page_precedes_logically; SimpleLruInit(TestSlruCtl, "TestSLRU", -- 2.39.5 (Apple Git-154)
