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 1vO0ON-005XwR-1F for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Nov 2025 21:16:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vO0OL-00Brp1-34 for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Nov 2025 21:16:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vO0OL-00Brog-1L for pgsql-hackers@lists.postgresql.org; Tue, 25 Nov 2025 21:16:05 +0000 Received: from mail-il1-x12c.google.com ([2607:f8b0:4864:20::12c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vO0OJ-001SQz-02 for pgsql-hackers@lists.postgresql.org; Tue, 25 Nov 2025 21:16:04 +0000 Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-4336f8e97c3so24748245ab.3 for ; Tue, 25 Nov 2025 13:16:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764105363; x=1764710163; darn=lists.postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=5zrcTfF1wn0VFtAduR5FrFg76c6dT3DLCym+Wwa9O+k=; b=mtdFODGf/ozhgtyQ0KODvls20iSFSUtEo4fNTB5RAcYnECTemPLFqNH2ApEFnqe97D au2o1NdPwOo+buP9i1P8C2lCTiRjqXlhxsLVR9LvnSx0lo7RlbS0CHrIYGb1Prwy4ZAd cLTMfM7sJUrmvkpJSnPHlbj0F+xqmwqp8F3Z1RyOJ6QqoL7FeuYq1OyzpDg/vBPrKKPo FDI9rUOZSGqPlq1qO2C9KdxajmyOazjYjFPnZ8OSnSWk/aDyVx8LdzDfW6KFi9U021wK 9e7RfwQWcNVEIEj9crG2Yh+/necK3ndSqe00jyPKDRcS9UkR2FED8HJ2tEiCQx6O+EO5 Mr0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764105363; x=1764710163; h=in-reply-to:content-transfer-encoding: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=5zrcTfF1wn0VFtAduR5FrFg76c6dT3DLCym+Wwa9O+k=; b=OnjiPkMQZiqiGfr96hOOMwnPmhzc7Cvtkv/BZS2kFp/9AL48XUolqzd4XLc7QC/KQP qk/bUOBXnnoLx6+RoAhL0qaSapx8wnFyULDby/iSfI8CsukXF3BwBz0YATaUzSoXTjVf Or13Nkuea4O9u8mxm3wIQh0kqjR9oAlh1ayEg/28n9cmyo2+WuUShJtnjNoIrQJLxi37 9C07wQNa1ORogqEEkuBpB0mJjRngfdlRb9br3oqp3l41a/TFYbZIVg2IpCw383ek8J6R 0HcvVTX8F10BcX3qhezW5WWXxCsRrDMX8hBploFEyUcMuDYL8LFtZq+TybzMVp7LnnO1 AFVA== X-Gm-Message-State: AOJu0YwhrNNb0oiS0f0p3ySr3PSh298dNNnYCTFZUZezewpF0WAQTKux fva2fu4BdHYd1BG7NFxjEtcIgPvagBJ4e0lzVRIOu+BxbNmDUwfZC7sF X-Gm-Gg: ASbGncuPRuDudUeFvlYRVeOdq9Z1Y5uWQ+G/O7ADzSS0UtAveBMM2L4clyOBZNba2I9 zvM4m6Q365qUo5bI4tRPcXdcD9H/aVltZm8u6vKMVUc7g5NiR4dCRDszmmILwKh1/PBMY0ffgGT B9I+FSdLdSsCy0oFuGT73Z202XmL9PKDR5KpIDN9xehVPOto7BQuZFOuukqoksMXFTccXlLd5SE 167OCdcNGlVO9lxYXTgluh1uMLb8WnOU4YpGG+AdE3R/18SJMmBIe6CvKYxhrVBSho853MDGiCn r6hvAjPC0A2+mtd/gLIYzFepdlBnAlOT672O8Z+W02950BVNCmmxmsz1xeziPnUDNIYRAwQWXFE 36OyG5BJp19xdS9/RKciyQBo+s6GmpLvrfVJtufDABsotCWupOOsxQbrBalzP2ISSPMY9pkzGvN Blh5OextdFsx8etFsCmD1WmkBIApqv5t8eTJdMZdtwyBwg/VOUzyZbTxugD9QTUUmc/3qtHLneF Rqs X-Google-Smtp-Source: AGHT+IF/BL2+WqSXTeNVh6CZErphQPbHfHqyLvlcrX26buADS9qCj9dozO/3l4VqPUO9+hLE3T++BA== X-Received: by 2002:a05:6e02:18c5:b0:433:6aa2:f6aa with SMTP id e9e14a558f8ab-435b8e620e9mr172799905ab.29.1764105363081; Tue, 25 Nov 2025 13:16:03 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-435a90b6babsm77712295ab.20.2025.11.25.13.16.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Nov 2025 13:16:02 -0800 (PST) Date: Tue, 25 Nov 2025 15:16:00 -0600 From: Nathan Bossart To: Robert Haas Cc: PostgreSQL Hackers Subject: Re: pgsql: Teach DSM registry to ERROR if attaching to an uninitialized ent Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="kUugHEtL9xji53W5" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --kUugHEtL9xji53W5 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Nov 25, 2025 at 03:58:47PM -0500, Robert Haas wrote: > On Tue, Nov 25, 2025 at 12:45 PM Nathan Bossart > wrote: >> I gave your idea a try, and I like the result a lot. IMHO it makes the >> code much easier to reason about. > > I looked through this and I agree this looks good. Thanks for working on it. My tests seem happy, so I will plan on committing these patches tomorrow. The only difference in v4 is that pg_get_dsm_registry_allocations() will no longer show partially-initialized entries. I thought that was the best option in this case because such entries shouldn't have any allocated memory, and retrying GetNamed*() would set *found to false. -- nathan --kUugHEtL9xji53W5 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v4-0001-Revert-Teach-DSM-registry-to-ERROR-if-attaching-t.patch From 767081b66138df6038465094416502ee592e7e09 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 24 Nov 2025 11:03:52 -0600 Subject: [PATCH v4 1/2] Revert "Teach DSM registry to ERROR if attaching to an uninitialized entry." This reverts commit 1165a933aab1355757a43cfd9193b6cce06f573b. --- src/backend/storage/ipc/dsm_registry.c | 34 +++----------------------- 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c index 7eba8a8cffb..a926b9c3f32 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -93,7 +93,6 @@ typedef struct DSMRegistryEntry { char name[NAMEDATALEN]; DSMREntryType type; - bool initialized; union { NamedDSMState dsm; @@ -217,7 +216,6 @@ 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); @@ -230,21 +228,13 @@ 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 \"%s\" does not match type of existing entry", - name))); - else if (!entry->initialized) - ereport(ERROR, - (errmsg("requested DSM segment \"%s\" failed initialization", - name))); + (errmsg("requested DSM segment does not match type of existing entry"))); else if (entry->dsm.size != size) ereport(ERROR, - (errmsg("requested DSM segment \"%s\" does not match size of existing entry", - name))); + (errmsg("requested DSM segment size does not match size of existing segment"))); else { NamedDSMState *state = &entry->dsm; @@ -307,7 +297,6 @@ 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); @@ -319,17 +308,10 @@ 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 \"%s\" does not match type of existing entry", - name))); - else if (!entry->initialized) - ereport(ERROR, - (errmsg("requested DSA \"%s\" failed initialization", - name))); + (errmsg("requested DSA does not match type of existing entry"))); else { NamedDSAState *state = &entry->dsa; @@ -390,7 +372,6 @@ 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); @@ -408,17 +389,10 @@ 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 \"%s\" does not match type of existing entry", - name))); - else if (!entry->initialized) - ereport(ERROR, - (errmsg("requested DSHash \"%s\" failed initialization", - name))); + (errmsg("requested DSHash does not match type of existing entry"))); else { NamedDSHState *dsh_state = &entry->dsh; -- 2.39.5 (Apple Git-154) --kUugHEtL9xji53W5 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v4-0002-handle-ERRORs-in-DSM-registry-functions.patch From 07cac41a5b69d62ac0d5fa0646cc35c3210399ce Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 24 Nov 2025 12:28:23 -0600 Subject: [PATCH v4 2/2] handle ERRORs in DSM registry functions --- src/backend/storage/ipc/dsm_registry.c | 121 ++++++++++++++++--------- 1 file changed, 78 insertions(+), 43 deletions(-) diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c index a926b9c3f32..ef6533b1100 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -155,9 +155,10 @@ init_dsm_registry(void) { /* Initialize dynamic shared hash table for registry. */ dsm_registry_dsa = dsa_create(LWTRANCHE_DSM_REGISTRY_DSA); + dsm_registry_table = dshash_create(dsm_registry_dsa, &dsh_params, NULL); + dsa_pin(dsm_registry_dsa); dsa_pin_mapping(dsm_registry_dsa); - dsm_registry_table = dshash_create(dsm_registry_dsa, &dsh_params, NULL); /* Store handles in shared memory for other backends to use. */ DSMRegistryCtx->dsah = dsa_get_handle(dsm_registry_dsa); @@ -188,6 +189,8 @@ GetNamedDSMSegment(const char *name, size_t size, DSMRegistryEntry *entry; MemoryContext oldcontext; void *ret; + NamedDSMState *state; + dsm_segment *seg; Assert(found); @@ -210,36 +213,36 @@ GetNamedDSMSegment(const char *name, size_t size, init_dsm_registry(); entry = dshash_find_or_insert(dsm_registry_table, name, found); + state = &entry->dsm; if (!(*found)) { - NamedDSMState *state = &entry->dsm; - dsm_segment *seg; - entry->type = DSMR_ENTRY_TYPE_DSM; + state->handle = DSM_HANDLE_INVALID; + state->size = size; + } + else if (entry->type != DSMR_ENTRY_TYPE_DSM) + ereport(ERROR, + (errmsg("requested DSM segment does not match type of existing entry"))); + else if (state->size != size) + ereport(ERROR, + (errmsg("requested DSM segment size does not match size of existing segment"))); + + if (state->handle == DSM_HANDLE_INVALID) + { + *found = false; /* Initialize the segment. */ seg = dsm_create(size, 0); + if (init_callback) + (*init_callback) (dsm_segment_address(seg)); + dsm_pin_segment(seg); dsm_pin_mapping(seg); state->handle = dsm_segment_handle(seg); - state->size = size; - ret = dsm_segment_address(seg); - - if (init_callback) - (*init_callback) (ret); } - else if (entry->type != DSMR_ENTRY_TYPE_DSM) - ereport(ERROR, - (errmsg("requested DSM segment does not match type of existing entry"))); - else if (entry->dsm.size != size) - ereport(ERROR, - (errmsg("requested DSM segment size does not match size of existing segment"))); else { - NamedDSMState *state = &entry->dsm; - dsm_segment *seg; - /* If the existing segment is not already attached, attach it now. */ seg = dsm_find_mapping(state->handle); if (seg == NULL) @@ -250,10 +253,9 @@ GetNamedDSMSegment(const char *name, size_t size, dsm_pin_mapping(seg); } - - ret = dsm_segment_address(seg); } + ret = dsm_segment_address(seg); dshash_release_lock(dsm_registry_table, entry); MemoryContextSwitchTo(oldcontext); @@ -274,6 +276,7 @@ GetNamedDSA(const char *name, bool *found) DSMRegistryEntry *entry; MemoryContext oldcontext; dsa_area *ret; + NamedDSAState *state; Assert(found); @@ -292,14 +295,28 @@ GetNamedDSA(const char *name, bool *found) init_dsm_registry(); entry = dshash_find_or_insert(dsm_registry_table, name, found); + state = &entry->dsa; if (!(*found)) { - NamedDSAState *state = &entry->dsa; - entry->type = DSMR_ENTRY_TYPE_DSA; + state->handle = DSA_HANDLE_INVALID; + state->tranche = -1; + } + else if (entry->type != DSMR_ENTRY_TYPE_DSA) + ereport(ERROR, + (errmsg("requested DSA does not match type of existing entry"))); + + if (state->tranche == -1) + { + *found = false; /* Initialize the LWLock tranche for the DSA. */ state->tranche = LWLockNewTrancheId(name); + } + + if (state->handle == DSA_HANDLE_INVALID) + { + *found = false; /* Initialize the DSA. */ ret = dsa_create(state->tranche); @@ -309,17 +326,11 @@ GetNamedDSA(const char *name, bool *found) /* Store handle for other backends to use. */ state->handle = dsa_get_handle(ret); } - else if (entry->type != DSMR_ENTRY_TYPE_DSA) + else if (dsa_is_attached(state->handle)) ereport(ERROR, - (errmsg("requested DSA does not match type of existing entry"))); + (errmsg("requested DSA already attached to current process"))); else { - NamedDSAState *state = &entry->dsa; - - if (dsa_is_attached(state->handle)) - ereport(ERROR, - (errmsg("requested DSA already attached to current process"))); - /* Attach to existing DSA. */ ret = dsa_attach(state->handle); dsa_pin_mapping(ret); @@ -346,6 +357,7 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found) DSMRegistryEntry *entry; MemoryContext oldcontext; dshash_table *ret; + NamedDSHState *dsh_state; Assert(params); Assert(found); @@ -365,45 +377,57 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found) init_dsm_registry(); entry = dshash_find_or_insert(dsm_registry_table, name, found); + dsh_state = &entry->dsh; if (!(*found)) { - NamedDSHState *dsh_state = &entry->dsh; - dshash_parameters params_copy; - dsa_area *dsa; - entry->type = DSMR_ENTRY_TYPE_DSH; + dsh_state->dsa_handle = DSA_HANDLE_INVALID; + dsh_state->dsh_handle = DSHASH_HANDLE_INVALID; + dsh_state->tranche = -1; + } + else if (entry->type != DSMR_ENTRY_TYPE_DSH) + ereport(ERROR, + (errmsg("requested DSHash does not match type of existing entry"))); + + if (dsh_state->tranche == -1) + { + *found = false; /* Initialize the LWLock tranche for the hash table. */ dsh_state->tranche = LWLockNewTrancheId(name); + } + + if (dsh_state->dsa_handle == DSA_HANDLE_INVALID) + { + dshash_parameters params_copy; + dsa_area *dsa; + + *found = false; /* Initialize the DSA for the hash table. */ dsa = dsa_create(dsh_state->tranche); - dsa_pin(dsa); - dsa_pin_mapping(dsa); /* Initialize the dshash table. */ memcpy(¶ms_copy, params, sizeof(dshash_parameters)); params_copy.tranche_id = dsh_state->tranche; ret = dshash_create(dsa, ¶ms_copy, NULL); + dsa_pin(dsa); + dsa_pin_mapping(dsa); + /* 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); } - else if (entry->type != DSMR_ENTRY_TYPE_DSH) + else if (dsa_is_attached(dsh_state->dsa_handle)) ereport(ERROR, - (errmsg("requested DSHash does not match type of existing entry"))); + (errmsg("requested DSHash already attached to current process"))); else { - NamedDSHState *dsh_state = &entry->dsh; dsa_area *dsa; /* XXX: Should we verify params matches what table was created with? */ - if (dsa_is_attached(dsh_state->dsa_handle)) - ereport(ERROR, - (errmsg("requested DSHash already attached to current process"))); - /* Attach to existing DSA for the hash table. */ dsa = dsa_attach(dsh_state->dsa_handle); dsa_pin_mapping(dsa); @@ -439,6 +463,17 @@ pg_get_dsm_registry_allocations(PG_FUNCTION_ARGS) Datum vals[3]; bool nulls[3] = {0}; + /* Do not show partially-initialized entries. */ + if (entry->type == DSMR_ENTRY_TYPE_DSM && + entry->dsm.handle == DSM_HANDLE_INVALID) + continue; + if (entry->type == DSMR_ENTRY_TYPE_DSA && + entry->dsa.handle == DSA_HANDLE_INVALID) + continue; + if (entry->type == DSMR_ENTRY_TYPE_DSH && + entry->dsh.dsa_handle == DSA_HANDLE_INVALID) + continue; + vals[0] = CStringGetTextDatum(entry->name); vals[1] = CStringGetTextDatum(DSMREntryTypeNames[entry->type]); -- 2.39.5 (Apple Git-154) --kUugHEtL9xji53W5--