So far I know that Datum is one of the data types used in C language functions in PostgreSQL which can represent any value in valid SQL type. What I am not getting is that, if it can hold any type of value, then how does the calling function know the data type of the value returned by called function ? Is Datum internally a structure which contains such additional information? Please explain.
The libpq library is the C interface to PostgreSQL. It is a set of library functions that allow client programs to interact with PostgreSQL. It is also the underlying engine for several other PostgreSQL application interfaces, including those written for C++, Perl, PHP, Ruby, Python, and Tcl.
Datum is the generic type to hold the internal representation of an — er — datum that can be stored in a PostgreSQL table. It is defined in postgres.h, and the comment is instructive:
/*
 * A Datum contains either a value of a pass-by-value type or a pointer to a
 * value of a pass-by-reference type.  Therefore, we require:
 *
 * sizeof(Datum) == sizeof(void *) == 4 or 8
 *
 * The macros below and the analogous macros for other types should be used to
 * convert between a Datum and the appropriate C type.
 */
You use the DatumGet* macros to cast it to one of the specific data types.
Datum does not contain any information about the data type, this knowledge has to come from elsewhere.
When writing a C function, the data type of the arguments will always be as declared in the function.
From PostgreSQL mailing list, quoting rsmogura:
[...] in simple words, datum is like
void *with additional size header.
and Tom Lane:
It's the backend-internal representation of a single value of any SQL data type. The code using the
Datumhas to know which type it is, since theDatumitself doesn't contain that information. Usually, C code will work with a value in a "native" representation, and then convert to or fromDatumin order to pass the value through data-type-independent interfaces.
The Datum is defined as typedef uintptr_t Datum, therefore it is 4 or 8 bytes on the platforms supported by PostgreSQL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With