From 37ef7e1fd21cc1aa93cdc1af4fd84a26516faaa3 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 10 Nov 2025 12:03:47 -0600 Subject: [PATCH v2 2/2] test_dsa: Avoid leaking LWLock tranches. --- src/test/modules/test_dsa/test_dsa.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/test/modules/test_dsa/test_dsa.c b/src/test/modules/test_dsa/test_dsa.c index 01d5c6fa67f..21e4ce6a745 100644 --- a/src/test/modules/test_dsa/test_dsa.c +++ b/src/test/modules/test_dsa/test_dsa.c @@ -13,25 +13,35 @@ #include "postgres.h" #include "fmgr.h" +#include "storage/dsm_registry.h" #include "storage/lwlock.h" #include "utils/dsa.h" #include "utils/resowner.h" PG_MODULE_MAGIC; +static void +init_tranche(void *ptr) +{ + int *tranche_id = (int *) ptr; + + *tranche_id = LWLockNewTrancheId("test_dsa"); +} + /* Test basic DSA functionality */ PG_FUNCTION_INFO_V1(test_dsa_basic); Datum test_dsa_basic(PG_FUNCTION_ARGS) { - int tranche_id; + int *tranche_id; + bool found; dsa_area *a; dsa_pointer p[100]; - /* XXX: this tranche is leaked */ - tranche_id = LWLockNewTrancheId("test_dsa"); + tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int), + init_tranche, &found); - a = dsa_create(tranche_id); + a = dsa_create(*tranche_id); for (int i = 0; i < 100; i++) { p[i] = dsa_allocate(a, 1000); @@ -62,17 +72,18 @@ PG_FUNCTION_INFO_V1(test_dsa_resowners); Datum test_dsa_resowners(PG_FUNCTION_ARGS) { - int tranche_id; + int *tranche_id; + bool found; dsa_area *a; dsa_pointer p[10000]; ResourceOwner oldowner; ResourceOwner childowner; - /* XXX: this tranche is leaked */ - tranche_id = LWLockNewTrancheId("test_dsa"); + tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int), + init_tranche, &found); /* Create DSA in parent resource owner */ - a = dsa_create(tranche_id); + a = dsa_create(*tranche_id); /* * Switch to child resource owner, and do a bunch of allocations in the -- 2.39.5 (Apple Git-154)