| From: | "Jelte Fennema-Nio" <postgres(at)jeltef(dot)nl> |
|---|---|
| To: | "Bertrand Drouvot" <bertranddrouvot(dot)pg(at)gmail(dot)com>, "Heikki Linnakangas" <hlinnaka(at)iki(dot)fi>, "Nathan Bossart" <nathandbossart(at)gmail(dot)com>, "PostgreSQL Hackers" <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Safer and simpler HTAB macros (foreach_hash and hash_make), take two |
| Date: | 2026-11-27 00:02:35 |
| Message-ID: | DN5JWODBRJ63.22T08ZLOGP9L1@jeltef.nl |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi all,
This is a restart of the "Safer hash table initialization macro"
thread[1]. That patchset needed a rework after Heikki's new shmem
request API (283e823f9dc), so starting a fresh thread seemed the best
option.
The idea is mostly the same as before. With this patchset, creating and
iterating over a hash table looks like this:
PortalHashTable = hash_make(PortalHashEnt, portalname,
"Portal hash", PORTALS_PER_USER,
.mcxt = TopMemoryContext);
foreach_hash(PortalHashEnt, hentry, PortalHashTable)
{
...
}
While currently it looks like this:
HASHCTL ctl;
HASH_SEQ_STATUS status;
PortalHashEnt *hentry;
ctl.keysize = MAX_PORTALNAME_LEN;
ctl.entrysize = sizeof(PortalHashEnt);
PortalHashTable = hash_create("Portal hash", PORTALS_PER_USER,
&ctl, HASH_ELEM | HASH_STRINGS);
hash_seq_init(&status, PortalHashTable);
while ((hentry = hash_seq_search(&status)) != NULL)
{
...
}
The main change from the previous attempt is that options are now passed
as named arguments, like .mcxt above. That approach is taken from
Heikki's ShmemRequestHash.
The benefits of foreach_hash should be pretty obvious, and are similar
to those of the foreach_node macros introduced in 14dd0f27d7c[2].
The benefits of hash_make might be less obvious, so I've listed them
here for completeness:
1. Key and entry sizes are derived from the types.
2. The key member is referenced in the code, so it cannot be removed
without the compiler noticing.
3. A static assertion checks that the key is the first member.
4. HASH_STRINGS or HASH_BLOBS is chosen based on the key type.
5. The other HASH_* flags are derived from the options that are set,
and the always required HASH_ELEM is no longer needed.
6. The memory context defaults to CurrentMemoryContext instead of
TopMemoryContext, like for all other functions that allocate. So a
hash table **no longer leaks by default**.
7. Creating a hash table becomes a single statement.
0001-0002 add the macros, 0003 adds a C++ test for them. 0004-0006
convert the whole tree, mostly to show what the result looks like.
[1]: https://www.postgresql.org/message-id/aS2b3LoUypW1/Gdz(at)ip-10-97-1-34(dot)eu-west-3(dot)compute(dot)internal
[2]: https://postgr.es/m/CAGECzQSwXKnxGwW1_Q5JE%2B8Ja20kyAbhBHO04vVrQsLcDciwXA%40mail.gmail.com
Jelte
P.S. The conversion commits are LLM-ed. If we wanna commit those, they
need a careful look to see that they are indeed equivalent. I did that
before, but not for the latest version. I'd rather only do that when/if
people actually think we should commit that bulk conversion.
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Add-foreach_hash-macro.patch | text/x-patch | 3.6 KB |
| v1-0002-Add-hash_make-macros.patch | text/x-patch | 18.6 KB |
| v1-0003-Test-the-hash_make-and-foreach_hash-macros-in-a-C.patch | text/x-patch | 2.3 KB |
| v1-0004-Use-foreach_hash-macro-throughout-the-codebase.patch | text/x-patch | 78.4 KB |
| v1-0005-Use-hash_make-macros-throughout-the-codebase.patch | text/x-patch | 96.2 KB |
| v1-0006-Inline-functions-that-have-now-become-trivial.patch | text/x-patch | 4.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Noboru Saito | 2026-11-27 00:11:34 | Re: [DOC][PATCH] Improving the HTML layout of release notes |
| Previous Message | 신성준 | 2026-11-26 23:49:55 | Re: Preserve tablespace of exclusion constraint indexes in ALTER TYPE |