Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vIu2Q-001z8Y-2L for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Nov 2025 19:28:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vIu2O-007G7U-1J for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Nov 2025 19:28:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vIu2O-007G7L-0E for pgsql-hackers@lists.postgresql.org; Tue, 11 Nov 2025 19:28:20 +0000 Received: from mail-io1-xd29.google.com ([2607:f8b0:4864:20::d29]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vIu2I-007CsL-1J for pgsql-hackers@postgresql.org; Tue, 11 Nov 2025 19:28:19 +0000 Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-9489361c8efso1755839f.0 for ; Tue, 11 Nov 2025 11:28:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762889291; x=1763494091; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=3SeF4KE9w9BCeYET8Nqie/Rs7M6GRF1rzfyUnnMum5g=; b=HSL4CMwwEz2OnBGciH/M/EjJAHbCUTMCKRAuFdYRlwmHsK+9xZ2r+eWNi1nVm3LGo3 c7M+nWati2RZ50F7Eqratu/yGqbAe+bsVIS3WUJl7wr3eRvhAV+6YC+oLONSSixGpDsJ 1OwuDoRZw9S/Th4n5xHiflC2bwprQ9mEOnECUdpnQX4cUXeA+0+0UWgPlhKRqcTLIf7t 1nO81AdkRpvB8nC8iPB7srEi1Nm755r2XJSPZZa+fC+OxJvZRvt1SeYMmlColubagWqs 5gDTrQN8GGsDTXN+3TEh9s30iGHdxE/bkbAcv1QbRb3eevt6VIDSre1YYh4vai8ZTx/4 tPDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762889291; x=1763494091; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3SeF4KE9w9BCeYET8Nqie/Rs7M6GRF1rzfyUnnMum5g=; b=tCdHq18rfuN8wCBJXoG5s4onr8UdK5SNz5Q1JPRGiOi1Qx5SOeXFiG52EYRO7auiso UzdGv/3BjDzdTtwYa/EKxNFDbV0cPqXVrlzn51fsmMVXuuV4+sgq7aR5TmqburDNSd9O QGjKGloNSJHQGEtoIR1bbt4PYvqgv6pIXWTA+XepcXj4F9bMz2hzX1tm2cCYQaiDGGRJ 4629Ccd97bJW5aXmejvrmwJwON2k9xGu4AgQdMitVWRYnm0gXBgE+t/+7zVc28EVlmwX 4rwZHb/Vs2cECgCkuuu1/S+OZGlOXtpp23O2E0ycOkfxNaGxts63/jaM900BXT6Amb6e G2lw== X-Forwarded-Encrypted: i=1; AJvYcCULffGC+rgD5dOfl9JQNCKcABPEUgOLu6HLHh2wkOlFUKhwsp1rG2butJ48Rlirf6GbW3SfoH1hGf2rQSN9@postgresql.org X-Gm-Message-State: AOJu0YzHHCkeRxb/plO+omc66umDPwP389l24yYtMiSzoP8SSklyXyzV uz5YAmTSAhg7v8Hs/RnZcC7mkMkjT2TBSj9GupP5V4WvZP2CmDTTY0Mj X-Gm-Gg: ASbGnctBRMPbWq8SDY1HDT0dnuY90TMjaP93tOd3ryPUwJjkGD6Brnwlh7+ZAtQoydi Nj/FxbYYgZN8gOH6wsJ3EUhyJMlzSGpxaq77GbXhe0ygkOgacIFpJx2qi5Kov5LBJ8b+KjKCA61 jO1Phz1a0JqVWXifVb93YrzYhYy8svZJm1zMU5TprVK1wUWWD7pPkYQE3g+T9R85iKiRJaFZroN UU0X4R0QzZCMVR+Nsz0+vJCQYNdMWJZvg7b2lgsbeRbOzQhBsnzQ7LVTd5FWmXU8Ls12HeMI/vK z7QUJO3/Z3qfN5zr+1g+uHCeA8dGtF79XrWifocXmvDz4odOWe1n+DleWxGsz4mOJ3gKesB67ga MfhbfQyv5b8I7hdLy26G5ZebZSC1ij72ibyGLEmL+eeQ9feHgJ2UX0EceRhlTRa90IeOVWRdUUR UTMON/YP0oVAfnUVizH6C2Pf02gQGF5vU7U29x/tHoTaVnUVKcooqBfQNhScf5NGYI3LAnFH6C2 we6 X-Google-Smtp-Source: AGHT+IHctsCnZnyqRU9v4te44kgPVJM7zawRVP2ZpDihKEJ2gb1YZFikMMaM0NIJamBjuzATqikYLw== X-Received: by 2002:a05:6e02:16ca:b0:433:3505:51c2 with SMTP id e9e14a558f8ab-43473d1a096mr4091715ab.9.1762889291173; Tue, 11 Nov 2025 11:28:11 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-5b7ab0d66f4sm190901173.40.2025.11.11.11.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Nov 2025 11:28:10 -0800 (PST) Date: Tue, 11 Nov 2025 13:28:08 -0600 From: Nathan Bossart To: Sami Imseih Cc: Alexander Lakhin , Rahila Syed , Bertrand Drouvot , Tom Lane , Andres Freund , pgsql-hackers Subject: Re: Improve LWLock tranche name visibility across backends Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="H+ButhVHn6MoYls/" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --H+ButhVHn6MoYls/ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Nov 10, 2025 at 05:17:47PM -0600, Sami Imseih wrote: > Maybe we can improve the error message by showing > the "name", like this, In case, there are nested calls. > > ``` > else if (!entry->initialized) > { > ereport(ERROR, > (errmsg("requested DSM segment \"%s\" failed initialization", name))); > } > ``` Done. -- nathan --H+ButhVHn6MoYls/ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0001-DSM-registry-ERROR-if-entry-was-not-initialized.patch From 0b12db92e17dd315cc08b5a918196b685e3f6f61 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 10 Nov 2025 11:43:51 -0600 Subject: [PATCH v3 1/2] DSM registry: ERROR if entry was not initialized. --- src/backend/storage/ipc/dsm_registry.c | 34 +++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c index a926b9c3f32..7eba8a8cffb 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -93,6 +93,7 @@ typedef struct DSMRegistryEntry { char name[NAMEDATALEN]; DSMREntryType type; + bool initialized; union { NamedDSMState dsm; @@ -216,6 +217,7 @@ GetNamedDSMSegment(const char *name, size_t size, dsm_segment *seg; entry->type = DSMR_ENTRY_TYPE_DSM; + entry->initialized = false; /* Initialize the segment. */ seg = dsm_create(size, 0); @@ -228,13 +230,21 @@ GetNamedDSMSegment(const char *name, size_t size, if (init_callback) (*init_callback) (ret); + + entry->initialized = true; } else if (entry->type != DSMR_ENTRY_TYPE_DSM) ereport(ERROR, - (errmsg("requested DSM segment does not match type of existing entry"))); + (errmsg("requested DSM segment \"%s\" does not match type of existing entry", + name))); + else if (!entry->initialized) + ereport(ERROR, + (errmsg("requested DSM segment \"%s\" failed initialization", + name))); else if (entry->dsm.size != size) ereport(ERROR, - (errmsg("requested DSM segment size does not match size of existing segment"))); + (errmsg("requested DSM segment \"%s\" does not match size of existing entry", + name))); else { NamedDSMState *state = &entry->dsm; @@ -297,6 +307,7 @@ GetNamedDSA(const char *name, bool *found) NamedDSAState *state = &entry->dsa; entry->type = DSMR_ENTRY_TYPE_DSA; + entry->initialized = false; /* Initialize the LWLock tranche for the DSA. */ state->tranche = LWLockNewTrancheId(name); @@ -308,10 +319,17 @@ GetNamedDSA(const char *name, bool *found) /* Store handle for other backends to use. */ state->handle = dsa_get_handle(ret); + + entry->initialized = true; } else if (entry->type != DSMR_ENTRY_TYPE_DSA) ereport(ERROR, - (errmsg("requested DSA does not match type of existing entry"))); + (errmsg("requested DSA \"%s\" does not match type of existing entry", + name))); + else if (!entry->initialized) + ereport(ERROR, + (errmsg("requested DSA \"%s\" failed initialization", + name))); else { NamedDSAState *state = &entry->dsa; @@ -372,6 +390,7 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found) dsa_area *dsa; entry->type = DSMR_ENTRY_TYPE_DSH; + entry->initialized = false; /* Initialize the LWLock tranche for the hash table. */ dsh_state->tranche = LWLockNewTrancheId(name); @@ -389,10 +408,17 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found) /* Store handles for other backends to use. */ dsh_state->dsa_handle = dsa_get_handle(dsa); dsh_state->dsh_handle = dshash_get_hash_table_handle(ret); + + entry->initialized = true; } else if (entry->type != DSMR_ENTRY_TYPE_DSH) ereport(ERROR, - (errmsg("requested DSHash does not match type of existing entry"))); + (errmsg("requested DSHash \"%s\" does not match type of existing entry", + name))); + else if (!entry->initialized) + ereport(ERROR, + (errmsg("requested DSHash \"%s\" failed initialization", + name))); else { NamedDSHState *dsh_state = &entry->dsh; -- 2.39.5 (Apple Git-154) --H+ButhVHn6MoYls/ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0002-test_dsa-Avoid-leaking-LWLock-tranches.patch From bc876604668340706ead2dc64db2b692f0658618 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 10 Nov 2025 12:03:47 -0600 Subject: [PATCH v3 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) --H+ButhVHn6MoYls/--