Re: Safer and simpler HTAB macros (foreach_hash and hash_make), take two

From: "Jelte Fennema-Nio" <postgres(at)jeltef(dot)nl>
To: "Kyotaro Horiguchi" <horikyota(dot)ntt(at)gmail(dot)com>
Cc: <bertranddrouvot(dot)pg(at)gmail(dot)com>, <hlinnaka(at)iki(dot)fi>, <nathandbossart(at)gmail(dot)com>, <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Safer and simpler HTAB macros (foreach_hash and hash_make), take two
Date: 2026-11-29 10:55:23
Message-ID: DN7N1L3HD7TB.1W6HQDMKRLLWM@jeltef.nl
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri Nov 27, 2026 at 3:28 AM CET, Kyotaro Horiguchi wrote:
> However, I would prefer to keep semantically significant choices,
> such as the key comparison semantics and the memory context, explicit.

For most key types only a single comparison semantic makes sense by
definition: HASH_STRINGS for NameData and HASH_BLOBS for everything
but char arrays and NameData. Only for char arrays both HASH_STRINGS
and HASH_BLOBS could be valid. In practice char arrays seem to always
contain strings though: nothing in the tree hashes one as binary data,
so force_blobs is only an opt-out for future code and extensions. I'd
be okay with requiring an explicit HASH_STRINGS or HASH_BLOBS for char
array keys (but keep the auto detection for anything except char
arrays). My preference is to keep it as it is though.

> The requirement to explicitly terminate a hash scan when exiting a
> foreach_hash loop early is one example.

I totally agree that's easy to miss, but that requirement was already
there: where we call hash_seq_term() today, we now call
foreach_hash_term() in this patch. It's also pretty rare: only 5 of the
111 converted loops need it.

> Given this distribution, I don't see a compelling reason to change
> the default from TopMemoryContext to CurrentMemoryContext.

My numbers are close to yours: of the 96 call sites 47 use
TopMemoryContext, 25 explicitly use CurrentMemoryContext and 24 some
other context. So TopMemoryContext is the most common one, but half of
the call sites already have to override it. Of those 24, 4 pass the
context that is already current, because of a MemoryContextSwitchTo()
call right above. Those could simply use the default, which the
attached 0007 makes them do. That brings it to 29, so 30% instead of
20%.

My main argument is not about the numbers though. I think the
important part is that using TopMemoryContext should require being
explicit, to avoid accidental leaks like the ones fixed in 0b392314315
and aaff0a559f8. That leaves the question whether we should change the
default to CurrentMemoryContext, or require being explicit about using
that too. We don't require that for any of our other allocating APIs,
so I think it would be weird to require it here. And for a case where
you should have used a different context than CurrentMemoryContext,
it'll usually fail hard if you didn't: the hash table is freed too
early, which crashes right away in an assert-enabled build.

> As for the bulk conversion, I would prefer to avoid it, given the
> potential impact on backpatching

I'm fine with the conversion patches not getting committed. They are
mostly there as a review aid, to show that the new APIs are nicer to
use. I'll already be very happy if I can use the new APIs in my own
code and extensions.

That said, I tried to estimate how often that would happen. Of the 2048
commits on REL_16_STABLE since it branched off, 5 change lines that
overlap with or are adjacent to lines changed by the conversion. So
that's about 1.5 conflicts per year for that branch, across all committers.

Attachment Content-Type Size
v2-0001-Add-foreach_hash-macro.patch text/x-patch 3.6 KB
v2-0002-Add-hash_make-macros.patch text/x-patch 18.6 KB
v2-0003-Test-the-hash_make-and-foreach_hash-macros-in-a-C.patch text/x-patch 2.3 KB
v2-0004-Use-foreach_hash-macro-throughout-the-codebase.patch text/x-patch 78.4 KB
v2-0005-Use-hash_make-macros-throughout-the-codebase.patch text/x-patch 96.2 KB
v2-0006-Inline-functions-that-have-now-become-trivial.patch text/x-patch 4.6 KB
v2-0007-Don-t-pass-the-current-memory-context-to-hash_mak.patch text/x-patch 2.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrei Lepikhov 2026-11-29 13:23:21 Re: Preserve presorted ORDER BY keys when reordering DISTINCT ON keys
Previous Message Andrei Lepikhov 2026-11-29 09:56:23 Re: Use local buffer for packed numerics