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 1vOOKk-005kPy-1h for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Nov 2025 22:49:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vOOKi-0021x6-34 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Nov 2025 22:49:57 +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 1vOOKi-0021wx-1y for pgsql-hackers@lists.postgresql.org; Wed, 26 Nov 2025 22:49:56 +0000 Received: from mail-io1-xd2b.google.com ([2607:f8b0:4864:20::d2b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vOOKg-001gKX-2Z for pgsql-hackers@lists.postgresql.org; Wed, 26 Nov 2025 22:49:56 +0000 Received: by mail-io1-xd2b.google.com with SMTP id ca18e2360f4ac-949031532f9so16449739f.0 for ; Wed, 26 Nov 2025 14:49:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764197391; x=1764802191; darn=lists.postgresql.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=NmX0XLyiSIu6voiZHq25TLncejoHd49DDxVb6Tqwu6Q=; b=AhCbCZHuKsuW2455wD/Cc1163j1Q/TRLEZPtNAJq7lKcnH4fevp0defciirZbPHUCx 6OC/gJxV5KGWbMYFjnNKjeHL4J4CblqhQB/Dcnr8Zd27L4PmTxKReiUnmOwQBn0Izl+9 XTR2tuiKHnXByQfhQaXT25uqEwidENg8uPP06CaBs83MTWyCKjx/nW/rBx+xZQaZpHB0 cdGWpGjD9XWs/6Pzbi/YMjCSHWRTMPzFNqUZYYdpzhL7Pxwj0L1fqGk0zqTSyAPEy0uF x3XpkA9qkZtUGmxz3O4mJB9bItgKDn3q8oAYPVKfi/R30eJ9J/bOfk9WAG+cV+rIKms+ 1T2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764197391; x=1764802191; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NmX0XLyiSIu6voiZHq25TLncejoHd49DDxVb6Tqwu6Q=; b=l8fqogQW9r78E2laQ2FXxVVp7w5JAVaXGDkxLhrRzeX8jdI7gh+qGnry2rTdthLJiT zJDx3U0dC55mVd7aLzaBwK+LHZxy/b9bOrEX1nd57NkWX6BGBzcV9qoag8eoqRPcLaom pHobYye/MGEpyV1X0gsKbmhtkjiGF9xuuqwhL5kH+O395yCZBNByw5Jzo65kkE7HPg6T VYtSOFKHtthiDaIrxqQtTKlm4ZFQ9luZbSmCO355Po80rRRzCMOT6PPPY4oF9T1kqy2l y8VHiBB9AiBdRUUtktkbxmjZwMF6/6o9I06U+3uhKxt/OGxxayPOhMxerdyPVzWPof1N 6Fsg== X-Gm-Message-State: AOJu0YzlE+nBAIHr3MLZmvpOMhh6yqn12egku3HN+z1VQ21eYE6ninRf UVsbn8gvioxlwJyQw89Utru3Bcg6Jf/M/UsjPBp7vYvBwVdCi5DGRXfw X-Gm-Gg: ASbGncuWjNRK1OCWv3WCAm5VDxAjoigq8fIkCCKD3khCfHAWqzaEnd/K9OYWZQ5PDYn Z8fIAyjbG1pTWa4cRCg/8BVxZnHpVfjxw2JnOcEEvTqUVJSqNHbQTIF5AulfTziB5gMDgHlYIHn 36jf1L8ZEuJXUx0Lf5Wc4NJlHKN8bc0+9v6Hsd6W6vR3XMhyHNQFRYgHbksgJOQco9ifpblXyVg Lg37YcECtEUxtTToAhDR10ZfKPKvbHNIpt10cZkazqrtbdxktvtb1S+0KD+UwCocNXzY+QHUr52 gKwIZE2wkxMTQEV7xgG0JjWf7REfhW4yj1z9Ea3yFs9Gv2KpNYg5GxdjApEm7xd+/17/OX+N267 hlBcm6fGStbwBWmV2Z9xzb4ALVaqi1FbjZTczWM4AXqQcPS3sNNzP/gkdunUAvwjyb5q7NPeQbP rHR2djWsPakbCKlnbv91sv9LwQrfubdKplCbWkpyAJQ5burjQGJZkMcatNE9G3rfxYHrqldRBeJ eST X-Google-Smtp-Source: AGHT+IH3quZ9JTteyXj/lwi656VYW+k3FP1glrEPyLnRSiG/0CY09Lc/zabyzqcHYw4a4PTvzvHZzQ== X-Received: by 2002:a02:a585:0:b0:5b7:d710:6610 with SMTP id 8926c6da1cb9f-5b965b4d877mr14685688173.11.1764197391403; Wed, 26 Nov 2025 14:49:51 -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-5b954b588e6sm9137234173.54.2025.11.26.14.49.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Nov 2025 14:49:50 -0800 (PST) Date: Wed, 26 Nov 2025 16:49:49 -0600 From: Nathan Bossart To: Robert Haas Cc: PostgreSQL Hackers Subject: show size of DSAs and dshash tables in pg_dsm_registry_allocations Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="qPihe5i8c6jBTp1x" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --qPihe5i8c6jBTp1x Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Nov 25, 2025 at 07:13:03PM -0500, Robert Haas wrote: > In my ideal world, it would probably show partially-initialized entires > in some distinguishable way, like with a null size. I reevaluated this view, and I think we can do what you suggest here. Right now, we show NULL for DSAs and dshash tables because 1) we might not be attached to them and 2) we don't have a pointer to the dsa_area * or dshash_table * in local memory. If we introduce a function that looks up the size of a DSA given its handle (transiently attaching to the control segment if needed), we can show the size for all successfully-initialized entries. Then, we can make NULL mean that the entry was partially-initialized. Patch attached. -- nathan --qPihe5i8c6jBTp1x Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v1-0001-rework-DSM-registry-view.patch From fc5f0d4eea19df142032c8da4110b1e8aade59f0 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 26 Nov 2025 16:48:13 -0600 Subject: [PATCH v1 1/1] rework DSM registry view --- doc/src/sgml/system-views.sgml | 4 +-- src/backend/storage/ipc/dsm_registry.c | 17 ++++----- src/backend/utils/mmgr/dsa.c | 35 +++++++++++++++++++ src/include/utils/dsa.h | 1 + .../expected/test_dsm_registry.out | 4 +-- .../sql/test_dsm_registry.sql | 4 +-- 6 files changed, 48 insertions(+), 17 deletions(-) diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 0e623e7fb86..3d083128a4b 100644 --- a/doc/src/sgml/system-views.sgml +++ b/doc/src/sgml/system-views.sgml @@ -1150,8 +1150,8 @@ AND c1.path[c2.level] = c2.path[c2.level]; size int8 - Size of the allocation in bytes. NULL for entries of type - area and hash. + Size of the allocation in bytes. NULL for entries that failed + initialization. diff --git a/src/backend/storage/ipc/dsm_registry.c b/src/backend/storage/ipc/dsm_registry.c index ef6533b1100..4e327f69789 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -463,17 +463,6 @@ 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]); @@ -483,6 +472,12 @@ pg_get_dsm_registry_allocations(PG_FUNCTION_ARGS) */ if (entry->type == DSMR_ENTRY_TYPE_DSM) vals[2] = Int64GetDatum(entry->dsm.size); + else if (entry->type == DSMR_ENTRY_TYPE_DSA && + entry->dsa.handle != DSA_HANDLE_INVALID) + vals[2] = Int64GetDatum(dsa_get_total_size_from_handle(entry->dsa.handle)); + else if (entry->type == DSMR_ENTRY_TYPE_DSH && + entry->dsh.dsa_handle !=DSA_HANDLE_INVALID) + vals[2] = Int64GetDatum(dsa_get_total_size_from_handle(entry->dsh.dsa_handle)); else nulls[2] = true; diff --git a/src/backend/utils/mmgr/dsa.c b/src/backend/utils/mmgr/dsa.c index be43e9351c3..030f9463712 100644 --- a/src/backend/utils/mmgr/dsa.c +++ b/src/backend/utils/mmgr/dsa.c @@ -1050,6 +1050,41 @@ dsa_get_total_size(dsa_area *area) return size; } +/* + * Same as dsa_get_total_size(), but accepts a DSA handle. The area must have + * been created with dsa_create (not dsa_create_in_place). + */ +size_t +dsa_get_total_size_from_handle(dsa_handle handle) +{ + size_t size; + bool already_attached; + dsm_segment *segment; + dsa_area_control *control; + + already_attached = dsa_is_attached(handle); + if (already_attached) + segment = dsm_find_mapping(handle); + else + segment = dsm_attach(handle); + + if (segment == NULL) + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("could not attach to dynamic shared area"))); + + control = (dsa_area_control *) dsm_segment_address(segment); + + LWLockAcquire(&control->lock, LW_SHARED); + size = control->total_segment_size; + LWLockRelease(&control->lock); + + if (!already_attached) + dsm_detach(segment); + + return size; +} + /* * Aggressively free all spare memory in the hope of returning DSM segments to * the operating system. diff --git a/src/include/utils/dsa.h b/src/include/utils/dsa.h index f2104dacbfc..42449ff22de 100644 --- a/src/include/utils/dsa.h +++ b/src/include/utils/dsa.h @@ -161,6 +161,7 @@ extern dsa_pointer dsa_allocate_extended(dsa_area *area, size_t size, int flags) extern void dsa_free(dsa_area *area, dsa_pointer dp); extern void *dsa_get_address(dsa_area *area, dsa_pointer dp); extern size_t dsa_get_total_size(dsa_area *area); +extern size_t dsa_get_total_size_from_handle(dsa_handle handle); extern void dsa_trim(dsa_area *area); extern void dsa_dump(dsa_area *area); diff --git a/src/test/modules/test_dsm_registry/expected/test_dsm_registry.out b/src/test/modules/test_dsm_registry/expected/test_dsm_registry.out index ca8abbb377e..75d9eda0756 100644 --- a/src/test/modules/test_dsm_registry/expected/test_dsm_registry.out +++ b/src/test/modules/test_dsm_registry/expected/test_dsm_registry.out @@ -1,4 +1,4 @@ -SELECT name, type, size IS DISTINCT FROM 0 AS size +SELECT name, type, size > 0 AS size FROM pg_dsm_registry_allocations WHERE name like 'test_dsm_registry%' ORDER BY name; name | type | size @@ -32,7 +32,7 @@ SELECT get_val_in_hash('test'); (1 row) \c -SELECT name, type, size IS DISTINCT FROM 0 AS size +SELECT name, type, size > 0 AS size FROM pg_dsm_registry_allocations WHERE name like 'test_dsm_registry%' ORDER BY name; name | type | size diff --git a/src/test/modules/test_dsm_registry/sql/test_dsm_registry.sql b/src/test/modules/test_dsm_registry/sql/test_dsm_registry.sql index 965a3f1ebb6..1b9ee3ebf18 100644 --- a/src/test/modules/test_dsm_registry/sql/test_dsm_registry.sql +++ b/src/test/modules/test_dsm_registry/sql/test_dsm_registry.sql @@ -1,4 +1,4 @@ -SELECT name, type, size IS DISTINCT FROM 0 AS size +SELECT name, type, size > 0 AS size FROM pg_dsm_registry_allocations WHERE name like 'test_dsm_registry%' ORDER BY name; CREATE EXTENSION test_dsm_registry; @@ -8,6 +8,6 @@ SELECT set_val_in_hash('test', '1414'); SELECT get_val_in_shmem(); SELECT get_val_in_hash('test'); \c -SELECT name, type, size IS DISTINCT FROM 0 AS size +SELECT name, type, size > 0 AS size FROM pg_dsm_registry_allocations WHERE name like 'test_dsm_registry%' ORDER BY name; -- 2.39.5 (Apple Git-154) --qPihe5i8c6jBTp1x--