Database Reference
In-Depth Information
a = DatumGetInt32(GetAttributeByName(th, "a",
&aisnull));
The third argument to
GetAttributeByName(...)
is an address of a
bool
which
is set to true if the field was
NULL
.
There is also a companion function
GetAttributeByNum()
if you prefer to get the
attributes by their numbers instead of names.
Constructing a return tuple
Constructing the return tuple(s) is almost as easy.
First, you get the called functions return type descriptor using
get_call_result_type()
function.
get_call_result_type(fcinfo, &resultTypeId,
&resultTupleDesc);
The first argument to this function is the
FunctionCallInfo
structure
fcinfo
which is used when calling the function you are currently writing (hidden behind the
PG_FUNCTION_ARGS
macro in the C function declaration), the other two arguments
are addresses of the return type
Oid
and
TupleDesc
to receive the return tuple
descriptor in case the function returns a record type.
Next, there is a safety assert for checking that the return type is really a record (or
composite) type.
Assert(resultTypeId == TYPEFUNC_COMPOSITE);
This is to guard against errors in the
CREATE FUNCTION
declaration in SQL which
tells PostgreSQL about this new function.
And there is still one thing before we construct the tuple.