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 38b602b028.

Discussion: https://postgr.es/m/agr6-cIQ4EUA86Cs@paquier.xyz
Backpatch-through: 16
This commit is contained in:
Michael Paquier 2026-05-26 12:26:36 +09:00
parent 273be484aa
commit d4a7e1886c
2 changed files with 27 additions and 9 deletions

View 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

View file

@ -2784,6 +2784,7 @@ Tcl_Time
TempNamespaceStatus
TestDecodingData
TestDecodingTxnData
TestSlruSharedState
TestSpec
TextFreq
TextPositionState