public inbox for [email protected]  
help / color / mirror / Atom feed
Return DSA area for hash table from GetNamedDSHash()
4+ messages / 3 participants
[nested] [flat]

* Return DSA area for hash table from GetNamedDSHash()
@ 2026-04-06 22:56  Sami Imseih <[email protected]>
  0 siblings, 2 replies; 4+ messages in thread

From: Sami Imseih @ 2026-04-06 22:56 UTC (permalink / raw)
  To: pgsql-hackers

Hi,

While working on extending tests for dshash.c [1], I realized that a
user that creates a hash table with GetNamedDSHash() has no way
to cap the size of the dsa area underpinning the table by using
dsa_set_size_limit(). This is because the dsa_area created using
this API is not exposed to the user.

This is a gap for users of the GetNamedDSHash() API,
because it's very likely that the callers don't want runaway growth of
these hash tables.

Attached is a new API, dshash_get_dsa_area() that takes in a dshash_table
and returns the area. The caller can then use dsa_set_size_limit() to limit
the size.

We could change the GetNamedDSHash() API to take in a size, but that
will not be ideal since a caller may want to change the size dynamically after
the hash table is created.

I don't have a patch for this yet, but I also think it will make sense for
pg_dsm_registry_allocations to also show the max_size

postgres=# select * from pg_dsm_registry_allocations;
          name          |  type   |  size
------------------------+---------+---------
 test_dsm_registry_dsa  | area    | 1048576
 test_dsm_registry_hash | hash    | 1048576
 test_dsm_registry_dsm  | segment |      20
(3 rows)

Thoughts?


[1] [https://www.postgresql.org/message-id/acXCJODjsCytdpwT%40paquier.xyz]

--
Sami Imseih
Amazon Web Services (AWS)


Attachments:

  [application/octet-stream] v1-0001-Add-function-to-return-DSA-area-for-a-dshash-tabl.patch (1.6K, 2-v1-0001-Add-function-to-return-DSA-area-for-a-dshash-tabl.patch)
  download | inline diff:
From b2f69377435fe170b1321e55d0c3141ebe8a2efc Mon Sep 17 00:00:00 2001
From: Sami Imseih <[email protected]>
Date: Mon, 6 Apr 2026 21:50:24 +0000
Subject: [PATCH v1 1/1] Add function to return DSA area for a dshash table

A caller using GetNamedDSHash() to retrieve a dshash table may also
want to limit its size. Add dshash_get_dsa_area(), which returns the
dsa_area for a given dshash_table, allowing callers to pass it to
dsa_set_size_limit().
---
 src/backend/lib/dshash.c | 11 +++++++++++
 src/include/lib/dshash.h |  1 +
 2 files changed, 12 insertions(+)

diff --git a/src/backend/lib/dshash.c b/src/backend/lib/dshash.c
index 1999989c14f..62ac68d4add 100644
--- a/src/backend/lib/dshash.c
+++ b/src/backend/lib/dshash.c
@@ -363,6 +363,17 @@ dshash_destroy(dshash_table *hash_table)
 	pfree(hash_table);
 }
 
+/*
+ * Get the DSA area used by this hash table.
+ */
+dsa_area *
+dshash_get_dsa_area(dshash_table *hash_table)
+{
+	Assert(hash_table->control->magic == DSHASH_MAGIC);
+
+	return hash_table->area;
+}
+
 /*
  * Get a handle that can be used by other processes to attach to this hash
  * table.
diff --git a/src/include/lib/dshash.h b/src/include/lib/dshash.h
index 64b758b381b..b868084df04 100644
--- a/src/include/lib/dshash.h
+++ b/src/include/lib/dshash.h
@@ -89,6 +89,7 @@ extern dshash_table *dshash_attach(dsa_area *area,
 								   dshash_table_handle handle,
 								   void *arg);
 extern void dshash_detach(dshash_table *hash_table);
+extern dsa_area *dshash_get_dsa_area(dshash_table *hash_table);
 extern dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table);
 extern void dshash_destroy(dshash_table *hash_table);
 
-- 
2.50.1



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Return DSA area for hash table from GetNamedDSHash()
@ 2026-04-07 03:59  jie wang <[email protected]>
  parent: Sami Imseih <[email protected]>
  1 sibling, 0 replies; 4+ messages in thread

From: jie wang @ 2026-04-07 03:59 UTC (permalink / raw)
  To: Sami Imseih <[email protected]>; +Cc: pgsql-hackers

Sami Imseih <[email protected]> 于2026年4月7日周二 06:56写道:

> Hi,
>
> While working on extending tests for dshash.c [1], I realized that a
> user that creates a hash table with GetNamedDSHash() has no way
> to cap the size of the dsa area underpinning the table by using
> dsa_set_size_limit(). This is because the dsa_area created using
> this API is not exposed to the user.
>
> This is a gap for users of the GetNamedDSHash() API,
> because it's very likely that the callers don't want runaway growth of
> these hash tables.
>
> Attached is a new API, dshash_get_dsa_area() that takes in a dshash_table
> and returns the area. The caller can then use dsa_set_size_limit() to limit
> the size.
>
> We could change the GetNamedDSHash() API to take in a size, but that
> will not be ideal since a caller may want to change the size dynamically
> after
> the hash table is created.
>
> I don't have a patch for this yet, but I also think it will make sense for
> pg_dsm_registry_allocations to also show the max_size
>
> postgres=# select * from pg_dsm_registry_allocations;
>           name          |  type   |  size
> ------------------------+---------+---------
>  test_dsm_registry_dsa  | area    | 1048576
>  test_dsm_registry_hash | hash    | 1048576
>  test_dsm_registry_dsm  | segment |      20
> (3 rows)
>
> Thoughts?
>
>
> [1] [https://www.postgresql.org/message-id/acXCJODjsCytdpwT%40paquier.xyz]
>
> --
> Sami Imseih
> Amazon Web Services (AWS)
>


Hi,

I think an assert check could be added in this patch for better safety.
 Assert(hash_table != NULL);

Best regards,
--
wang jie


^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Return DSA area for hash table from GetNamedDSHash()
@ 2026-04-08 01:08  Michael Paquier <[email protected]>
  parent: Sami Imseih <[email protected]>
  1 sibling, 1 reply; 4+ messages in thread

From: Michael Paquier @ 2026-04-08 01:08 UTC (permalink / raw)
  To: Sami Imseih <[email protected]>; +Cc: pgsql-hackers

On Mon, Apr 06, 2026 at 05:56:21PM -0500, Sami Imseih wrote:
> Attached is a new API, dshash_get_dsa_area() that takes in a dshash_table
> and returns the area. The caller can then use dsa_set_size_limit() to limit
> the size.

+dsa_area *
+dshash_get_dsa_area(dshash_table *hash_table)
+{
+	Assert(hash_table->control->magic == DSHASH_MAGIC);
+
+	return hash_table->area;

Rather than an API that returns the DSA area, perhaps it would be more
natural to have a wrapper that calls dsa_set_size_limit(), using an
existing dshash_table in input?
--
Michael


Attachments:

  [application/pgp-signature] signature.asc (833B, 2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Return DSA area for hash table from GetNamedDSHash()
@ 2026-04-09 21:11  Sami Imseih <[email protected]>
  parent: Michael Paquier <[email protected]>
  0 siblings, 0 replies; 4+ messages in thread

From: Sami Imseih @ 2026-04-09 21:11 UTC (permalink / raw)
  To: Michael Paquier <[email protected]>; [email protected]; +Cc: pgsql-hackers

Thanks for the replies!

> I think an assert check could be added in this patch for better safety.
>  Assert(hash_table != NULL);
>

I followed the same approach we take for dshash_destroy() and
dshash_get_hash_table_handle(). The caller is responsible for
not passing in a NULL hash table, else that assert will segfault.


> +dsa_area *
> +dshash_get_dsa_area(dshash_table *hash_table)
> +{
> +       Assert(hash_table->control->magic == DSHASH_MAGIC);
> +
> +       return hash_table->area;
>
> Rather than an API that returns the DSA area, perhaps it would be more
> natural to have a wrapper that calls dsa_set_size_limit(), using an
> existing dshash_table in input?

hm, having GetNamedDSA return dsa_area for direct use while requiring
a special wrapper for the dshash case creates an inconsistent API in
dsm_registry.h. dshash_get_dsa_area() means either way the dsa_area is
obtained, dsa_set_size_limit() can be used to set the size.

--
Sami





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-04-09 21:11 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-04-06 22:56 Return DSA area for hash table from GetNamedDSHash() Sami Imseih <[email protected]>
2026-04-07 03:59 ` jie wang <[email protected]>
2026-04-08 01:08 ` Michael Paquier <[email protected]>
2026-04-09 21:11   ` Sami Imseih <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox