Re: How to use the "setof" of CREATE FUNCTION - Mailing list pgsql-hackers

From Tom Lane
Subject Re: How to use the "setof" of CREATE FUNCTION
Date
Msg-id 20291.967659660@sss.pgh.pa.us
Whole thread Raw
In response to How to use the "setof" of CREATE FUNCTION  (Fabien Thiriet <fabien@freever.com>)
List pgsql-hackers
Fabien Thiriet <fabien@freever.com> writes:
> CREATE FUNCTION foo(varchar) RETURNS setof myTable
> AS 'UPDATE .......;
> INSERT.......;
> SELECT myTable.field2 from myTable'
> LANGUAGE 'sql';

> I always get an error saying that there is a type mismatch between what is
> behing the "setof" and what is return by this function (myTable.field2)

Well, yeah: you declared the function to return a set of the tuple
datatype myTable, not a set of whatever field2's datatype is.
Perhaps you wanted

CREATE FUNCTION foo(varchar) RETURNS setof myTable
AS 'UPDATE .......;
INSERT.......;
SELECT * from myTable'
LANGUAGE 'sql';

which hands back the entire table.  Alternatively, if you do want to
return just the one column, you should declare the function to return
setof whatever-type-field2-is.

Note that functions returning sets are not as useful as they should be,
because you can only call them in limited places (at the top level of
a SELECT-list item, IIRC).  Functions returning tuples are not as
useful as they should be either, because you can't do anything with
the result except select out an individual column; worse, there's this
bizarre syntax for it --- you can't write the obvious foo(x).bar,
for some reason, but have to do x.foo.bar, which only works for simple
field-of-a-relation arguments.  Ugh.  This whole area needs work.
        regards, tom lane


pgsql-hackers by date:

Previous
From: "Mikheev, Vadim"
Date:
Subject: RE: Silent deadlock possible in current sources
Next
From: "Mark Hollomon"
Date:
Subject: Re: New relkind for views