Thread: Variable number of arguments in C language function.

Variable number of arguments in C language function.

From
Daryl Tester
Date:
Hi,

Under PostgreSQL 7.1.3, I'm attempting to create a 'C' language
function using the V1 style interface which accepts a variable
number of parameters.  That part appears to work fine, in that
I can retrieve the number of arguments from the fcinfo structure
passed into the C function, and am able to extract the appropriate
argument.

What I'm attempting to do is, and coming unstuck at, is to "CREATE
FUNCTION" that allows a variable number of arguments to be passed
in, without having to define a seperate function in pg_proc for
each argument count.  There is a comment in the FmgrInfo structure
in include/fmgr.h about fn_nargs stating "or -1 if variable arg",
but setting pronargs to -1 for the appropriate pg_proc entry just
resulted in "Function pg_exec(unknown) does not exist".

My goal was to be able to pass in arguments of any type into the
function and have it handle (and convert) the argument type, but
(and this brainwave only occurred in the last five minutes) I
suspect that I may not be able to extract the data type from Datum,
and that's the reason why this typing mechanism for pg_proc exists
in the first place.  The ultimate aim is to create a "pg_exec"
function that will allow passing in of ints, floats and strings
and execve them as arguments, with the non-string arguments being
converted to string equivalents.  Is this achievable, or am I
barking up the wrong forest?  Thanks in advance.

--
Regards,
  Daryl Tester,  Software Wrangler and Bit Herder, IOCANE Pty. Ltd.

Re: Variable number of arguments in C language function.

From
Tom Lane
Date:
Daryl Tester <Daryl.Tester@iocane.com.au> writes:
> What I'm attempting to do is, and coming unstuck at, is to "CREATE
> FUNCTION" that allows a variable number of arguments to be passed
> in, without having to define a seperate function in pg_proc for
> each argument count.

There isn't any support for variable numbers of arguments; you're just
going to have to make N pg_proc entries pointing at the same C function.

> There is a comment in the FmgrInfo structure
> in include/fmgr.h about fn_nargs stating "or -1 if variable arg",

That's merely a note about a possible future expansion...

> My goal was to be able to pass in arguments of any type into the
> function and have it handle (and convert) the argument type, but
> (and this brainwave only occurred in the last five minutes) I
> suspect that I may not be able to extract the data type from Datum,

Nope, you won't.  PG is not designed for weakly typed functions;
I doubt it's possible to make this work at all.

            regards, tom lane

Re: Variable number of arguments in C language function.

From
Daryl Tester
Date:
Tom Lane wrote:

> There isn't any support for variable numbers of arguments; you're just
> going to have to make N pg_proc entries pointing at the same C function.

Cool, I can live with this now that I know about the restriction
on argument types.  My concern was having an entry for each
permutation of type and number of arguments, which would have
gotten unwieldy pretty quickly.  Thanks for the quick reply.

I'd have to square it with my employers first, but how would I go
about contributing this code when it's done?  Or wouldn't there
be much call for it (given the caveats of executing as the postgres
backend uid and all)?


--
Regards,
  Daryl Tester,  Software Wrangler and Bit Herder, IOCANE Pty. Ltd.