Convert *GetDatum() and DatumGet*() macros to inline functions - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Convert *GetDatum() and DatumGet*() macros to inline functions
Date
Msg-id 8528fb7e-0aa2-6b54-85fb-0c0886dbd6ed@enterprisedb.com
Whole thread Raw
Responses Re: Convert *GetDatum() and DatumGet*() macros to inline functions
List pgsql-hackers
I once wrote code like this:

     char *oid = get_from_somewhere();
     ...

     values[i++] = ObjectIdGetDatum(oid);

This compiles cleanly and even appears to work in practice, except of 
course it doesn't.

The FooGetDatum() macros just cast whatever you give it to Datum, 
without checking whether the input was really foo.

To address this, I converted these macros to inline functions, which 
enables type checking of the input argument.  For symmetry, I also 
converted the corresponding DatumGetFoo() macros (but those are less 
likely to cover mistakes, since the input argument is always Datum). 
This is patch 0002.

(I left some of the DatumGet... of the varlena types in fmgr.h as 
macros.  These ultimately map to functions that do type checking, so 
there would be little more to be learnt from that.  But we could do 
those for consistency as well.)

This whole thing threw up a bunch of compiler warnings and errors, which 
revealed a number of existing misuses.  These are fixed in patch 0001. 
These include

- using FooGetDatum on things that are already Datum,

- using DatumGetPointer on things that are already pointers,

- using PG_RETURN_TYPE on things that are Datum,

- using PG_RETURN_TYPE of the wrong type,

and others, including my personal favorite:

- using PointerGetDatum where DatumGetPointer should be used.

(AFAICT, unlike my initial example, I don't think any of those would 
cause wrong behavior.)
Attachment

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [PATCH] Add native windows on arm64 support
Next
From: Andres Freund
Date:
Subject: Re: CI and test improvements