]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
test_slru: Fix LWLock initialization for EXEC_BACKEND builds
authorMichael Paquier <michael@paquier.xyz>
Tue, 26 May 2026 03:26:36 +0000 (12:26 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 26 May 2026 03:26:36 +0000 (12:26 +0900)
The LWLock used by this test module was defined as a process-local
variable, which was broken under -DEXEC_BACKEND, each backend getting
its own copy of the lock state.  The shmem_startup_hook unconditionally
called LWLockRegisterTranche() and LWLockInitialize(), which means that
every backend would allocate a new tranche ID (which is still OK for
this module) but reset the lock's atomic state (which was bad).

This commit moves the LWLock to shared memory, so as it is initialized
only once, similarly to pg_prewarm/autoprewarm.c.

This change is only for REL_16_STABLE, per a report from buildfarm
member gokiburi (the system has been upgraded recently, so perhaps it
began failing due to some ALSR changes?).  I have been able to reproduce
the problem on the same host with -DEXEC_BACKEND, and checked that this
commit addresses the issue.  In v17 and v18, the test module wastes
tranche IDs, which only impacts the visibility of the locks like in
pg_stat_activity.  The use of the SLRU bank locks ensures that the
LWLock state is safe.  On HEAD, the logic of the module is safer thanks
to 38b602b0289f.

Discussion: https://postgr.es/m/agr6-cIQ4EUA86Cs@paquier.xyz
Backpatch-through: 16

src/test/modules/test_slru/test_slru.c
src/tools/pgindent/typedefs.list

index ae21444c47637103b3dc73fadc60a23d271ef7c4..2471be0c5b1e734222efd5552c0aa84ddd371b3a 100644 (file)
@@ -40,9 +40,16 @@ PG_FUNCTION_INFO_V1(test_slru_delete_all);
 /* Number of SLRU page slots */
 #define NUM_TEST_BUFFERS               16
 
-/* SLRU control lock */
-LWLock         TestSLRULock;
-#define TestSLRULock (&TestSLRULock)
+typedef struct TestSlruSharedState
+{
+       /* SLRU control lock */
+       LWLock          lock;
+} TestSlruSharedState;
+
+/* Pointer to shared-memory state. */
+static TestSlruSharedState *test_slru_state = NULL;
+
+#define TestSLRULock (&test_slru_state->lock)
 
 static SlruCtlData TestSlruCtlData;
 #define TestSlruCtl                    (&TestSlruCtlData)
@@ -202,6 +209,7 @@ test_slru_shmem_request(void)
 
        /* reserve shared memory for the test SLRU */
        RequestAddinShmemSpace(SimpleLruShmemSize(NUM_TEST_BUFFERS, 0));
+       RequestAddinShmemSpace(MAXALIGN(sizeof(TestSlruSharedState)));
 }
 
 static bool
@@ -214,7 +222,7 @@ static void
 test_slru_shmem_startup(void)
 {
        const char      slru_dir_name[] = "pg_test_slru";
-       int                     test_tranche_id;
+       bool            found;
 
        if (prev_shmem_startup_hook)
                prev_shmem_startup_hook();
@@ -225,15 +233,24 @@ test_slru_shmem_startup(void)
         */
        (void) MakePGDirectory(slru_dir_name);
 
-       /* initialize the SLRU facility */
-       test_tranche_id = LWLockNewTrancheId();
-       LWLockRegisterTranche(test_tranche_id, "test_slru_tranche");
-       LWLockInitialize(TestSLRULock, test_tranche_id);
+       LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
+       test_slru_state = ShmemInitStruct("test_slru",
+                                                                         sizeof(TestSlruSharedState),
+                                                                         &found);
+       if (!found)
+       {
+               /* First time through ... */
+               LWLockInitialize(&test_slru_state->lock, LWLockNewTrancheId());
+       }
+
+       LWLockRelease(AddinShmemInitLock);
+       LWLockRegisterTranche(test_slru_state->lock.tranche, "test_slru");
 
+       /* initialize the SLRU facility */
        TestSlruCtl->PagePrecedes = test_slru_page_precedes_logically;
        SimpleLruInit(TestSlruCtl, "TestSLRU",
                                  NUM_TEST_BUFFERS, 0, TestSLRULock, slru_dir_name,
-                                 test_tranche_id, SYNC_HANDLER_NONE);
+                                 test_slru_state->lock.tranche, SYNC_HANDLER_NONE);
 }
 
 void
index 8f7aab02badd85b251c045c584370e95b04cca6d..5ee0565a2099f1ab512d886c99f75e6bd75647af 100644 (file)
@@ -2784,6 +2784,7 @@ Tcl_Time
 TempNamespaceStatus
 TestDecodingData
 TestDecodingTxnData
+TestSlruSharedState
 TestSpec
 TextFreq
 TextPositionState