Database Reference
In-Depth Information
BlessTupleDesc(resultTupleDesc);
The purpose of
BlessTupleDesc()
is to fill in the missing parts of the structure,
which are not needed for internal operations on the tuple, but are essential when the
tuple is returned from the function.
So we are done with the tuple descriptor and finally, we can construct the tuple or
record itself to be returned.
The tuple is constructed using the
heap_form_tuple( resultTupleDesc,
retvals, retnulls );
function which uses the
TupleDesc
we just prepared. It
also needs an array of
Datum
to be used as values in the return tuple, and an array
of
bool
, which is used to determine if any field should be set to
NULL
instead of
their corresponding
Datum
value. As all our fields are of type
int32
their values in
retvals
are set using
Int32GetDatum(<localvar>)
. The array
retnull
is a
simple array of
bool
and needs no special tricks to set its values.
And finally we return the constructed tuple:
PG_RETURN_DATUM( HeapTupleGetDatum( rettuple )
);
Here, we first construct a
Datum
from the tuple we just constructed using
HeapTupleGetDatum()
and then use the
PG_RETURN_DATUM
macro.
Interlude - what is Datum
In this chapter, we use something called a
Datum
in several places. This calls for a
bit of explanation about what a "
Datum
" is.
In short, a
Datum
is any data item the PostgreSQL processes and passes around. A
Datum
itself does not contain any type information or info about if the field is actually
NULL
. It is just a pointer to some memory. You always have to find out (or know be-
forehand) the type of any Datum you use and also how to find out if your data may
be
NULL
instead of any real value.