Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is Datum in PostgreSQL C Language functions?

Tags:

c

postgresql

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.

like image 230
JessePinkman Avatar asked Nov 29 '18 16:11

JessePinkman


People also ask

What is C in PostgreSQL?

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.


2 Answers

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.

like image 158
Laurenz Albe Avatar answered Nov 03 '22 04:11

Laurenz Albe


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 Datum has to know which type it is, since the Datum itself doesn't contain that information. Usually, C code will work with a value in a "native" representation, and then convert to or from Datum in 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