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 1vQ5r5-009hW1-29 for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Dec 2025 15:30:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vQ5r4-003Rxe-15 for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Dec 2025 15:30:22 +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 1vQ5qY-003Oxm-1c for pgsql-hackers@lists.postgresql.org; Mon, 01 Dec 2025 15:29:50 +0000 Received: from mail-io1-xd2c.google.com ([2607:f8b0:4864:20::d2c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vQ5qV-002asH-0N for pgsql-hackers@lists.postgresql.org; Mon, 01 Dec 2025 15:29:49 +0000 Received: by mail-io1-xd2c.google.com with SMTP id ca18e2360f4ac-9490c3ac6ebso150519539f.2 for ; Mon, 01 Dec 2025 07:29:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764602985; x=1765207785; darn=lists.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=m5EzU4cfJpbfSq0S1Y39YJK0Eta0XBmKt/8F0M0LfM0=; b=EJKP4ZSaQLPPGP4AzS4eBaoJ3lOpLLbUdvBPU50cqvMXNY1e8EFw6WSw55QF+slVZ3 9WjGLKGu2odF5eiczql4lGVTCZod0bFrcSP4DrRazLT3GgB5DfqsugVajj/VH0noFKND 0OOsG7ET8Icrw+bKdFcmajgSPEF8GOy6lnlg3flmcXmAXnZB0p+HtjkEIMsWZ1uIhkpa gnReQAON00/WYcpw13C3Bpdnubom/sz6vRHAB3bNQUO7Cuo+MNn35lfvu0p9N7lxMlfU cyp2k1HbWPUp702oiMYTAFUK4bmQoCQfIKAQqWr+FyoUVZeGSXRf2BK+HE4t108lqkU6 EO1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764602985; x=1765207785; 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=m5EzU4cfJpbfSq0S1Y39YJK0Eta0XBmKt/8F0M0LfM0=; b=ZQf5gmsOBHybiZItlFsONfQfPs1mRzNMQ2qGvUdrtCWQyd6UGz9Zc5vN+sX9jDczNC U0QTaCPnYwdThuP/5wag4leSL0I8FSdKQnb5NG7cmO4m/54pfKxkH84jEk5PSWaMvbVo bJJLRQ4KXVdnLqOHUqUmsbHhM/aalDJckAh4m7e5NwPQw5tg3b3Mb+bje+pOAD4TWiHf CgMEMw7Wz2INXLe7CPuF+jQyz3JVBq58I0faf3sRGF2HZZAw+65LWQ+PFGJ3aTAdK/iQ I0xcmzpXwyNQbTpkYcrc8mW4SAu7QIxrJ1KF87s80ksAz5mrayrFeOBWuseJ54tG9oED VzTA== X-Forwarded-Encrypted: i=1; AJvYcCXG+sfFa4PL9ubsYCpdhHkc5c3AWu7LRocJvzqtMqrM4sfLV19y4KUZ5XTGwrL5Rjr1GG158jctrxT+uhdy@lists.postgresql.org X-Gm-Message-State: AOJu0YyoagfGv8NUBikYc9ZuKTlvZ0oShZyNHtflC28ScfxIIYbpKs7O d0r0157QOpbh2Mg7GApSwYGF14cM3kxOC5Dhf+C2RDMY788bGbccYddc X-Gm-Gg: ASbGnct96VjZxVmbWH/NDNAQzzXeELzbMc7Re+owznHEDxj3U8EA1FHQTqT3uK56U/i Gvy2cUaNpGv7GRYhiRfZXZnTRgfPeaO8GSIEgjC9ZCPWDwi6hJOCYrRGz/FXsfGt++T6ewfvdsb iyoSI+IXS2a3yToFP7AQgdu2cvyBmcYL8WBTuf6yw8TedFyMogH/dJYH90CNqgRNdfTZJQ+GMSB UlUAEbFWxFsb+G5tkdfLLr+lO2i3+2YNNYqdvaSE7UX8HTCvAzsnXivUF+wIqXryPUFCegy8qsy EB1bzstTg67K5QUsWn31VEIQWtO0Q03/XYFbfCjt6gw8rHx0X4/g3MyoRD+0WaqT3wSxKtv9lnA FmFop6I3rCW3vPGSs2Emipke95tjZJXXtVX36djFrwiAbtfUmmlJGRh2hAfzR4TnEAbUbPFAKHg eeqyLObDrs+hj4P8gxDXpxJ3fWIpOr+aUAto43NaPY4qYSDOtgorJ8JwjxBcBGUUC2ZnTKVu+oY 01uSY3qhn7oe5Y= X-Google-Smtp-Source: AGHT+IEFyR7fP+8BSh2957jc5xkKQsnKfFPFgMyDQqsXYypDylSkq6z2SWx/4WYCygwC5Amhj3oMQA== X-Received: by 2002:a05:6638:3172:b0:5ab:7a51:85fb with SMTP id 8926c6da1cb9f-5b967a8a068mr23874087173.16.1764602985415; Mon, 01 Dec 2025 07:29:45 -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-5b9bc782bbesm5569426173.49.2025.12.01.07.29.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Dec 2025 07:29:44 -0800 (PST) Date: Mon, 1 Dec 2025 09:29:43 -0600 From: Nathan Bossart To: Rahila Syed Cc: Robert Haas , PostgreSQL Hackers Subject: Re: show size of DSAs and dshash tables in pg_dsm_registry_allocations Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="yd3uzAJlpJ8QAvQg" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --yd3uzAJlpJ8QAvQg Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Nov 27, 2025 at 01:51:39PM +0530, Rahila Syed wrote: > Thank you for this improvement. I think it will be very helpful to have > the ability to report dsa and dshash memory sizes. Thanks for reviewing! > 1. As a result of this change, the following comment in > pg_get_dsm_registry_allocations is incorrect. Fixed. > + LWLockAcquire(&control->lock, LW_SHARED); > > The dsa_get_total_size function takes LW_EXCLUSIVE lock for the same > purpose. I wonder why that is the case and whether both should be > consistent. That function was added by commit ee1b30f, which AFAICT used an exclusive lock just to stay consistent with the rest of dsa.c [0]. I don't see any discussion about this in the original DSA thread [1]. Perhaps we could go through dsa.c and switch to LW_SHARED where appropriate, although I doubt it makes much difference. > +size_t > +dsa_get_total_size_from_handle(dsa_handle handle) > > I believe this function will report the size as long as the dsa control > structure is created within a dsm segment, since all dsm segments are > tracked by the global list - dsm_segment_list, regardless of whether the > dsa is created with dsa_create or dsa_create_in_place. In that case, > perhaps we should update the comment above to reflect this. Sorry, I'm not following what you think we should update the comment to say. > 4. Since, with this change, the size column will show memory allocation > regardless of whether it is currently mapped in the local process, I > think it would be helpful to add a boolean column to display the mapped > status as a future enhancement. Maybe, although I'm struggling to think of a scenario where that information would be useful. [0] https://postgr.es/m/CAD21AoDoWrbNf%2BK2Fwg2n%3DCZDHigjkndwqy_86BGgXBp9Kbq4Q%40mail.gmail.com [1] https://postgr.es/m/CAEepm%3D1z5WLuNoJ80PaCvz6EtG9dN0j-KuHcHtU6QEfcPP5-qA%40mail.gmail.com -- nathan --yd3uzAJlpJ8QAvQg Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v2-0001-rework-DSM-registry-view.patch From df18d34c52f0f744995c4f01b3b05205ed01b535 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 26 Nov 2025 16:48:13 -0600 Subject: [PATCH v2 1/1] rework DSM registry view --- doc/src/sgml/system-views.sgml | 4 +-- src/backend/storage/ipc/dsm_registry.c | 22 ++++-------- 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, 49 insertions(+), 21 deletions(-) diff --git a/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml index 7db8f73eba2..162c76b729a 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..0f3e08c44d8 100644 --- a/src/backend/storage/ipc/dsm_registry.c +++ b/src/backend/storage/ipc/dsm_registry.c @@ -463,26 +463,18 @@ 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]); - /* - * Since we can't know the size of DSA/dshash entries without first - * attaching to them, return NULL for those. - */ + /* Be careful to only return the sizes of initialized entries. */ 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) --yd3uzAJlpJ8QAvQg--