Sample Header Ad - 728x90

Is there a way to a maintain the function signature in C and not in a SQL file?

0 votes
1 answer
74 views
[Currently the docs says,](https://www.postgresql.org/docs/14/xfunc-c.html#XFUNC-C-RETURN-SET) > There are two ways you can build a composite data value (henceforth a “tuple”): you can build it from an array of Datum values, 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](https://stackoverflow.com/a/42579770/124486) . Is there no way to write my function signature there. It seems redundant to have to do something [like this](https://github.com/EvanCarroll/pg-dump-fcinfo/blob/main/src/dump_fcinfo.c#L22) where I tell C what all my types are (the initial defaults) when I then have to tell SQL [the very same thing](https://github.com/EvanCarroll/pg-dump-fcinfo/blob/main/dump_fcinfo--0.0.1.sql#L2) .
Asked by Evan Carroll (65502 rep)
Nov 3, 2021, 03:45 AM
Last activity: Nov 3, 2021, 09:13 AM