There are two ways you can build a composite data value (henceforth a “tuple”): you can build it from an array of
Datumvalues, or from an array of C strings that can be passed to the input conversion functions of the tuple's column data types.
When it comes to returning that "tuple" it says,
One way to declare this function in SQL is:
CREATE TYPE __retcomposite AS (f1 integer, f2 integer, f3 integer); CREATE OR REPLACE FUNCTION retcomposite(integer, integer) RETURNS SETOF __retcomposite AS 'filename', 'retcomposite' LANGUAGE C IMMUTABLE STRICT;
A different way is to use OUT parameters:
CREATE OR REPLACE FUNCTION retcomposite(IN integer, IN integer, OUT f1 integer, OUT f2 integer, OUT f3 integer) RETURNS SETOF record AS 'filename', 'retcomposite' LANGUAGE C IMMUTABLE STRICT;
Notice that in this method the output type of the function is formally an anonymous record type.
But is there a way to put the contents of
__retcomposite in C too? It seems kind of sloppy to have to write/maintain the prototype for the function in C, and in another language (SQL).
To expand a bit, there is a hook when you load the .so into the database with
_PG_init. Is there no way to write my function signature there. It seems redundant to have to do something like this where I tell C what all my types are (the initial defaults) when I then have to tell SQL the very same thing.