test_slru: Fix LWLock tranche allocation in EXEC_BACKEND builds.
authorNathan Bossart <nathan@postgresql.org>
Tue, 9 Sep 2025 19:09:36 +0000 (14:09 -0500)
committerNathan Bossart <nathan@postgresql.org>
Tue, 9 Sep 2025 19:09:36 +0000 (14:09 -0500)
Currently, test_slru's shmem_startup_hook unconditionally generates
new LWLock tranche IDs.  This is fine on non-EXEC_BACKEND builds,
where only the postmaster executes this hook, but on EXEC_BACKEND
builds, every backend executes it, too.  To fix, only generate the
tranche IDs in the postmaster process by checking the
IsUnderPostmaster variable.

This is arguably a bug fix and could be back-patched, but since the
damage is limited to some extra unused tranche IDs in a test
module, I'm not going to bother.

Reported-by: Sami Imseih <samimseih@gmail.com>
Reviewed-by: Sami Imseih <samimseih@gmail.com>
Discussion: https://postgr.es/m/CAA5RZ0vaAuonaf12CeDddQJu5xKL%2B6xVyS%2B_q1%2BcH%3D33JXV82w%40mail.gmail.com

src/test/modules/test_slru/test_slru.c

index 8c0367eeee424b21c92a4c01301506d0b53fc0b9..e963466aef1cdb758d835f61d957ed1ba721c572 100644 (file)
@@ -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,18 @@ 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
+    * tranche ID variables are not used by SimpleLruInit() when
+    * IsUnderPostmaster is true.
+    */
+   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",