I quickly put together a patch for the stuff we've discussed in this thread. WDYT?
-- nathan
>From 11289e1e69fc7c6fdbe5a73483efc2daf1197ec9 Mon Sep 17 00:00:00 2001 From: Nathan Bossart <nat...@postgresql.org> Date: Mon, 8 Sep 2025 16:08:00 -0500 Subject: [PATCH v1 1/1] Fix shmem_startup_hook documentation. --- doc/src/sgml/xfunc.sgml | 10 ++++++---- src/test/modules/test_slru/test_slru.c | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml index da21ef56891..80d862ff9c2 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -3668,11 +3668,13 @@ 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. In some builds, each backend will execute 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. In other builds, the postmaster process will execute the + <literal>shmem_startup_hook</literal> once, and each backend will + automatically inherit 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)