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 1vQCwa-001ksl-1i for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Dec 2025 23:04:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vQCwZ-004hWt-05 for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Dec 2025 23:04:31 +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 1vQCwY-004hWk-2G for pgsql-hackers@lists.postgresql.org; Mon, 01 Dec 2025 23:04:31 +0000 Received: from mail-oa1-x2f.google.com ([2001:4860:4864:20::2f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vQCwW-002dCb-27 for pgsql-hackers@lists.postgresql.org; Mon, 01 Dec 2025 23:04:30 +0000 Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-3eae4e590a4so1877828fac.1 for ; Mon, 01 Dec 2025 15:04:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764630266; x=1765235066; 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=k6ZiVTPpLqGpjv4qTeBGBLwGvmGAbCWHnnHG+ZIx7t0=; b=mWQXKJPYavBBWKnSNawzKzHGYyzMpHF6RUXku0DKoSLqIvP6T7/b6fNCy0mjHhjCIY n6bfrB4YcQHn1lVeVDInI1jYxEie0x3cfrjUG5se66Jk5vwWb8qWTBAX1mXgNdv04qN2 3raTDASzRNOl7Draqy/8tQKOB+p+5bpYnMvCcd9MDJVyhJxUW9s/1dvxSCtc+0zm+9Wb yl5r6bAmVJZxCAIxeUlWNn+QtkjS5gU/wOyH7CjrpXKZgrM1aHC3zt8tFaPwrSGU4JrH Zu21jzdEudF9/vkmtbj5AmZB4DGu9a3BJRWKI0I19MD35sRGfRZY8UvgG5Ui4HB5LBDI EzwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764630266; x=1765235066; 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=k6ZiVTPpLqGpjv4qTeBGBLwGvmGAbCWHnnHG+ZIx7t0=; b=muQRC7P3R0zfTf/A3HPPUTyI5mYRSsNyjZrrlw2MKyQL5WAkrnTkqzQIEAWbpwkqQN Esm2RYJQEHbZGIl2u7k9aUWOH+72oJJhcAoxHq/udgBkpYYuKg7INlcpuVCLDV63oZLN 19gLUIOmVxlCQV7rSeBByiwuZYE7ySxfu4ZFUZzowlj2C9XFh/5lvnikcqHxvU2SgeNO qBarrc0wh9KxHxa30by7WAlNesLxdg8nJ9fns8QjwZ5/nAUwf4+WJKefrQ7p49EpV5jU ibF4bddXlN1YB3qW7zuwdv7wO+2pedzHtInUdoZDIJBQ36h2eh5EyQdtTz0y2l7AoD2Y IEeA== X-Forwarded-Encrypted: i=1; AJvYcCUThe72/Pk7lOPkiPUQZuIWOmix0htC5hdeCmZMC5Ob58C4qfEy0bM90ipqEOuejm1rAkg6EifuZanFifuo@lists.postgresql.org X-Gm-Message-State: AOJu0YwG6B7AlPmSs0PNJM2bOq37zy/cOwP2x5KsF7MQ/F3pToP0vjQw v0++/K6iD4juPBvApqpeloLpzHlA4i7doUJUSc3HoUOIXXxO0YUXCt6c X-Gm-Gg: ASbGnctajUIV+iUxKup0JN3+SLwhSR3hPNlZ7eF063/5Z9UcoDmu//Ro3P1cj7oii+M ORvFC+KIQ1Cx3anRTJe8b1rMXzSs/1I1glnv40LbgpeQWXwOvazcAfgszau3rFDsLDo/7jSCR98 JqA1ut05AZREawH389BIFEd5TBRO83ORrAFi63kndBvCKEC/Jogy+c2Tl/qhDT4643uzuZqUOo2 0tyLUC3cfJ9JS3MCVafdeVKJHEm1SmRcO2a0+A1XNkzPHljyZ9blFE7UvZbzi74WEwxflJrgltS lXKanZt4zkuojQR1vAaL48cIzGas0RFpuscyfFDujV7Oty119iBh3e3tgHvBCoWba28nm9KAyNN shW91qeDpSjxTY3KmHvZ2JmJYyQ3DZ0HYnHFK86qshnj0ddYBN0tbHzV5LJtJl2c6uudOJ6OEJW AKRjV2pKQ9w5vj6eac4zdfNyq4sWqLIVh2Z4iAHCsn6raoh42Dqul5O1oVLfwfmSJPmp6xFjFhS kL/RtHfYGUdsso= X-Google-Smtp-Source: AGHT+IEq/jGh6uk1DDlvChAydp9QYOdolKsfunG7CnTitY6r4iuqWVlXblbJFu8dKf9FXma9T3g2cg== X-Received: by 2002:a05:6871:c313:b0:3ec:554d:c233 with SMTP id 586e51a60fabf-3ecbe565b2dmr18107109fac.28.1764630265659; Mon, 01 Dec 2025 15:04:25 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-3f0dd029507sm6097130fac.17.2025.12.01.15.04.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Dec 2025 15:04:25 -0800 (PST) Date: Mon, 1 Dec 2025 17:04:23 -0600 From: Nathan Bossart To: Chao Li Cc: Rahila Syed , 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="jJt2Y/LqGp3oC5LK" 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 --jJt2Y/LqGp3oC5LK Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Dec 02, 2025 at 05:35:34AM +0800, Chao Li wrote: > V2 overall looks good to me. I just got a question and a few nit comments. Thanks for reviewing. > Why do you error out instead of reporting 0/NULL usage here? If attaching to the control segment fails, something has gone horribly wrong. I don't think returning 0 for the size is appropriate in that case. Also note that other functions in this file ERROR when attaching fails (e.g., dsa_attach()). > ``` > + else if (entry->type == DSMR_ENTRY_TYPE_DSH && > + entry->dsh.dsa_handle !=DSA_HANDLE_INVALID) > ``` > > Missing a white space after !=. I agree, but for some reason, pgindent insists on removing that space. I'm leaving that for another thread. > ``` > - 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. > ``` > > “Failed initialization”, I guess “to” is needed after “failed”. IMHO it's fine as-is. We could say "failed to initialize", but I'm not sure that's materially better at conveying the information. > ``` > -SELECT name, type, size IS DISTINCT FROM 0 AS size > +SELECT name, type, size > 0 AS size > ``` > > As you changed the third column from “size” to “size > 0”, now it’s a > bool column, so maybe rename the column alias from “size” to something > indicating a bool type, such as “size_ok”, “has_size”, etc. It was already a bool column, but your point still stands. I changed it to "size_ok". -- nathan --jJt2Y/LqGp3oC5LK Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v3-0001-rework-DSM-registry-view.patch From f28e45a16271265a7d79b4e00594b5a515909df0 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Wed, 26 Nov 2025 16:48:13 -0600 Subject: [PATCH v3 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 | 12 +++---- .../sql/test_dsm_registry.sql | 4 +-- 6 files changed, 53 insertions(+), 25 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..9128e171b1b 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,8 +1,8 @@ -SELECT name, type, size IS DISTINCT FROM 0 AS size +SELECT name, type, size > 0 AS size_ok FROM pg_dsm_registry_allocations WHERE name like 'test_dsm_registry%' ORDER BY name; - name | type | size -------+------+------ + name | type | size_ok +------+------+--------- (0 rows) CREATE EXTENSION test_dsm_registry; @@ -32,11 +32,11 @@ 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_ok FROM pg_dsm_registry_allocations WHERE name like 'test_dsm_registry%' ORDER BY name; - name | type | size -------------------------+---------+------ + name | type | size_ok +------------------------+---------+--------- test_dsm_registry_dsa | area | t test_dsm_registry_dsm | segment | t test_dsm_registry_hash | hash | t 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..a606e8872a1 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_ok 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_ok FROM pg_dsm_registry_allocations WHERE name like 'test_dsm_registry%' ORDER BY name; -- 2.39.5 (Apple Git-154) --jJt2Y/LqGp3oC5LK--