postgres.git / summary / log / commit / refs

commit    d4a7e1886c1c34ab548f1a84374ba248faa2ae8a
Author:   Michael Paquier <michael@paquier.xyz>
Date:     Tue May 26 03:26:36 2026 +0000
First released in: 16.15

    test_slru: Fix LWLock initialization for EXEC_BACKEND builds
    
    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 | 35 +++++++++++++++++++++++++--------- src/tools/pgindent/typedefs.list | 1 + 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/test/modules/test_slru/test_slru.c b/src/test/modules/test_slru/test_slru.c index ae21444c476..2471be0c5b1 100644 --- a/src/test/modules/test_slru/test_slru.c +++ b/src/test/modules/test_slru/test_slru.c @@ -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 diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index 8f7aab02bad..5ee0565a209 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -2784,6 +2784,7 @@ Tcl_Time TempNamespaceStatus TestDecodingData TestDecodingTxnData +TestSlruSharedState TestSpec TextFreq TextPositionState [parent: 273be484aadd]