Thread: SETOF function call

SETOF function call

From
Olivier Thauvin
Date:
I hope I am on good list, else point me where I should ask.

I create a C function like this:

CREATE
-- IFUPDATE or REPLACE   FUNCTION rpmquery(rpmheader, INT) RETURNS SETOF TEXT AS   'MODULE_PATHNAME', 'header_query'
LANGUAGEC IMMUTABLE STRICT; 

(do not take care, code come from the .sql.in)

And the function works well, rpmheader is a new type with its own in/out
function.

But I have a problem, the second argument is an integer, ie an internal value.
So I want to create a function wrapper as rpmquery(rpmheader, TEXT), the goal
of this function is to first convert the "human text value" to "internal
integer value" and then call the first function.

But I am unable to to create a single function like I allready did with
function returning single Datum:

PG_FUNCTION_INFO_V1(header_querytxt);

Datum
header_querytxt(PG_FUNCTION_ARGS)
{   elog(NOTICE, "querytxt");   Datum tag;/* converting TEXT value to integer one */   tag =
DirectFunctionCall1(rpmtagvalue,PG_GETARG_TEXT_P(1));   elog(NOTICE, "querytxt %d", tag);/* header_query = function
acceptinginteger instead text)   PG_RETURN_POINTER(DirectFunctionCall2(header_query,
PG_GETARG_BYTEA_P_COPY(0),tag)); 
}

I get a segfault just after second elog. This is maybe simple for anyone
reading this list, but I am unable to find an example, or a documention to do
this.
A simple example, or a URL is welcome.

Re: SETOF function call

From
Tom Lane
Date:
Olivier Thauvin <olivier.thauvin@aerov.jussieu.fr> writes:
> Datum
> header_querytxt(PG_FUNCTION_ARGS)
> {
>     elog(NOTICE, "querytxt");
>     Datum tag;
>     /* converting TEXT value to integer one */
>     tag = DirectFunctionCall1(rpmtagvalue, PG_GETARG_TEXT_P(1));
>     elog(NOTICE, "querytxt %d", tag);
>     /* header_query = function accepting integer instead text)
>     PG_RETURN_POINTER(DirectFunctionCall2(header_query,
>     PG_GETARG_BYTEA_P_COPY(0), tag));
> }

If that's a cut-and-paste, then you seem to be short a */ on the second
comment ...
        regards, tom lane


Re: SETOF function call

From
Philip Yarra
Date:
sizeof(Datum)  == sizeof(long) - is that compatible with %d formatting (I'm 
guessing something like vsprintf takes place in elog)? Wouldn't this need %ld 
or %lu? 

Sorry if this misses the point, I wasn't clear from original post if the 
segfault was on elog or after it.

Regards, Philip.

On Tuesday 19 April 2005 11:53, Tom Lane wrote:
> Olivier Thauvin <olivier.thauvin@aerov.jussieu.fr> writes:
> > Datum
> > header_querytxt(PG_FUNCTION_ARGS)
> > {
> >     elog(NOTICE, "querytxt");
> >     Datum tag;
> >     /* converting TEXT value to integer one */
> >     tag = DirectFunctionCall1(rpmtagvalue, PG_GETARG_TEXT_P(1));
> >     elog(NOTICE, "querytxt %d", tag);
> >     /* header_query = function accepting integer instead text)
> >     PG_RETURN_POINTER(DirectFunctionCall2(header_query,
> >     PG_GETARG_BYTEA_P_COPY(0), tag));
> > }
> 
> If that's a cut-and-paste, then you seem to be short a */ on the second
> comment ...
> 
>             regards, tom lane
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
>       joining column's datatypes do not match
> 
> -----------------
> Utiba Pty Ltd 
> This message has been scanned for viruses and
> dangerous content by Utiba mail server and is 
> believed to be clean.
> 
> 


-----------------
Utiba Pty Ltd 
This message has been scanned for viruses and
dangerous content by Utiba mail server and is 
believed to be clean.



Re: SETOF function call

From
Olivier Thauvin
Date:
Le Tuesday 19 April 2005 04:16, vous avez écrit :
> sizeof(Datum)  == sizeof(long) - is that compatible with %d formatting (I'm
> guessing something like vsprintf takes place in elog)? Wouldn't this need
> %ld or %lu?
>
> Sorry if this misses the point, I wasn't clear from original post if the
> segfault was on elog or after it.

No, the elog works fine, and will be removed in final code, so the the mistake
between int and long int is not a real issue here.

The code segfault later, in the other function.

To be clean I am trying to in C code this:

CREATE FUNCTION rpmquery(rpmheader, TEXT) RETURNS SETOF TEXT AS $$       select * from rpmquery($1, rpmtag($2));   $$
LANGUAGESQL; 

Maybe it is easier to it as sql functions, but for my knowledge I like to
understand what I am doing wrong :)

Current buggy code is there, at the end:
http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/pgrpm/pgrpm/pgheader.c?rev=1.1&content-type=text/x-cvsweb-markup

Of course patch/comments are welcome.

>
> Regards, Philip.
>
> On Tuesday 19 April 2005 11:53, Tom Lane wrote:
> > Olivier Thauvin <olivier.thauvin@aerov.jussieu.fr> writes:
> > > Datum
> > > header_querytxt(PG_FUNCTION_ARGS)
> > > {
> > >     elog(NOTICE, "querytxt");
> > >     Datum tag;
> > >     /* converting TEXT value to integer one */
> > >     tag = DirectFunctionCall1(rpmtagvalue, PG_GETARG_TEXT_P(1));
> > >     elog(NOTICE, "querytxt %d", tag);
> > >     /* header_query = function accepting integer instead text)
> > >     PG_RETURN_POINTER(DirectFunctionCall2(header_query,
> > >     PG_GETARG_BYTEA_P_COPY(0), tag));
> > > }
> >
> > If that's a cut-and-paste, then you seem to be short a */ on the second
> > comment ...
> >
> >             regards, tom lane
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 9: the planner will ignore your desire to choose an index scan if
> > your joining column's datatypes do not match
> >
> > -----------------
> > Utiba Pty Ltd
> > This message has been scanned for viruses and
> > dangerous content by Utiba mail server and is
> > believed to be clean.
>
> -----------------
> Utiba Pty Ltd
> This message has been scanned for viruses and
> dangerous content by Utiba mail server and is
> believed to be clean.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)