Datum as struct

Peter Eisentraut <peter@eisentraut.org>

From: Peter Eisentraut <peter@eisentraut.org>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2025-07-31T14:02:35Z
Lists: pgsql-hackers

Attachments

Another draft patch set that I had lying around that was mentioned in [0].

The idea is to change Datum to a struct so that you can no longer rely 
on it being implicitly convertable to other C types, which enforces use 
of proper DatumGet*() and *GetDatum() functions, which might ultimately 
help with portability and robustness and the like.  (An alternative idea 
is to make Datum a union so that you don't need so many casts to begin 
with.  But this has a lot of the same issues, so it can be considered 
implicitly here.)

The first three patches clean up the use of the conversion functions. 
Some are missing, some are superfluous, some are used the wrong way 
around (!!).

The fourth patch tidies up the use of varatt.h macros.  These should 
arguably not be used on Datum values but instead be converted with 
DatumGetPointer().  This is very similar to the patch that I also 
included in the thread "Convert varatt.h macros to static inline 
functions" that I just posted.

The fifth patch cleans up some untidy use of hash functions with the 
wrong argument type.

The last patch is the actual conversion.  As you can see there, and as 
was also mentioned in [0], it's quite a bit of churn.  I'm not seriously 
proposing it at this point, but maybe it can serve as a guide for 
refactoring some of the interfaces to make the impact smaller, or 
something like that.

But I think the patches 0001 through 0005 are useful now.

I tested this against the original patch in [0].  It fixes some of the 
issues discussed there but not all of them.


[0]: 
https://www.postgresql.org/message-id/flat/1749799.1752797397%40sss.pgh.pa.us

Commits

  1. Use consistent type for pgaio_io_get_id() result

  2. Mop-up for Datum conversion cleanups.

  3. Add missing Datum conversions

  4. Remove useless/superfluous Datum conversions

  5. Fix incorrect lack of Datum conversion in _int_matchsel()

  6. Improve tests of date_trunc() with infinity and unsupported units

  7. Fix incorrect Datum conversion in timestamptz_trunc_internal()

  8. Fix various hash function uses

  9. Fix mixups of FooGetDatum() vs. DatumGetFoo()