| From: | "Jelte Fennema-Nio" <postgres(at)jeltef(dot)nl> |
|---|---|
| To: | "Nathan Bossart" <nathandbossart(at)gmail(dot)com> |
| Cc: | "Kyotaro Horiguchi" <horikyota(dot)ntt(at)gmail(dot)com>, <bertranddrouvot(dot)pg(at)gmail(dot)com>, <hlinnaka(at)iki(dot)fi>, <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-30 21:38:44 |
| Message-ID: | DN8VCPOBSN8R.16FO8VKK439X7@jeltef.nl |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Mon, 30 Nov 2026 at 19:13, Nathan Bossart <nathandbossart(at)gmail(dot)com> wrote:
> I wondered about calling it unconditionally from the macros, but I'm
> guessing that requires something that looks like PG_END_TRY(), which we
> probably want to avoid.
Even such a macro won't allow return/goto to safely break out of the
loop, which are 4 out of the 5 cases where we currently call
hash_seq_term. Only "break" would then be handled. There's already a
warning at commit about forgetting the hash_seq_term call (I updated
that warning text now in the attached v3 to mention foreach_hash too).
> Regardless of whether we _should_ change the default memory context, I
> don't think it belongs in this patch set. IMHO the new macros should aim
> to change as little as possible, and then we can look into changing the
> semantics once they're committed. IOW let's not change multiple variables
> at once if we can avoid it.
I think generally an introduction of a new API is exactly when it's
possible to make such changes. i.e. I wouldn't want to change the
default of the existing hash_create API, because that would silently
break many extensions. But since the new API isn't used by anyone, we
actually can safely change the default.
If we introduce the new hash_make API with TopMemoryContext being the
default, that would lock that in too (unless we change it before it's
ever released). I think that would be a shame because TopMemoryContext
being the default is very confusing IMO.
The only way to avoid locking down the choice, while also not changing
the default, is to provide no default at all. Kyotaro Horiguchi also
suggested requiring the memory context to always be set explicitly. If
you agree that's better, then that's probably the way to go (even though
I would prefer CurrentMemoryContext being the default). Let me know if
you want me to change the patchset that way.
> That's encouraging. We didn't convert everything to foreach_ptr() and
> friends, but I don't think we did much analysis on the potential rate of
> conflicts like this.
Out of curiosity I did the same analysis for a hypothetical bulk
conversion of foreach_ptr and friends. That would have resulted in 67
conflicts, so ~20 conflicts per year. So quite a lot more, which isn't
too surprising as we use a loop over a List a lot more than HTAB.
> If we did bulk convert, would we want to do something
> with the existing functions? We'd probably want all new code to use the
> new macros, but obviously we need to continue supporting the functions for
> third-party code for a while. Something to think about...
I guess we could add __attribute__((deprecated("use foreach_hash"))) to
nudge people to use the new functions/macros. We don't use the
deprecated attribute anywhere else in the codebase though. If we want to
do that, we should probably do it in a separate patchset and start with
some other functions first.
| Attachment | Content-Type | Size |
|---|---|---|
| v3-0001-Add-foreach_hash-macro.patch | text/x-patch | 4.6 KB |
| v3-0002-Add-hash_make-macros.patch | text/x-patch | 18.6 KB |
| v3-0003-Test-the-hash_make-and-foreach_hash-macros-in-a-C.patch | text/x-patch | 2.3 KB |
| v3-0004-Use-foreach_hash-macro-throughout-the-codebase.patch | text/x-patch | 78.4 KB |
| v3-0005-Use-hash_make-macros-throughout-the-codebase.patch | text/x-patch | 96.2 KB |
| v3-0006-Inline-functions-that-have-now-become-trivial.patch | text/x-patch | 4.6 KB |
| v3-0007-Don-t-pass-the-current-memory-context-to-hash_mak.patch | text/x-patch | 2.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Ayoub Kazar | 2026-11-30 21:47:21 | Re: Simplify the uniqueness proof for self-join elimination |
| Previous Message | Alberto Piai | 2026-11-30 20:34:46 | Re: Adding a stored generated column without long-lived locks |