Обсуждение: Returning sets from functions?

Поиск
Список
Период
Сортировка

Returning sets from functions?

От
mig@utdt.edu
Дата:
Consider   create function followers(text) returns setof text as          'select id from ids where id > $1 '
language'sql';
 

The function seems to work OK:   select followers('h');
does return all indexes after the index 'h'.

On the other hand,   select * from ids where id in (select followers('h'));
produces the message ERROR:  An operand to the '=' operator returns a set of text,       but '=' takes single values,
notsets.
 

Apparently the phrase   select followers('h');
returns a set with members which are 'setof text'. psql has no trouble
displaying all rows, but the parser does ...

So, my question is: can I produce a set in a function so that it
mimicks perfectly the return set of a select query?

I am using PG 6.5.3; is this already solved in the 7.0 beta?

Thanks

Miguel Sofer



Re: Returning sets from functions?

От
mig@utdt.edu
Дата:
Sorry, I forgot part of my question in the previous message: How do you return a "setof" value from a PG/Tcl procedure?
Returninga list does not seem to work ...
 

### PREVIOUS MESSAGE WAS ...

Consider   create function followers(text) returns setof text as          'select id from ids where id > $1 '
language'sql';
 

The function seems to work OK:   select followers('h');
does return all indexes after the index 'h'.

On the other hand,   select * from ids where id in (select followers('h'));
produces the message ERROR:  An operand to the '=' operator returns a set of text,       but '=' takes single values,
notsets.
 

Apparently the phrase   select followers('h');
returns a set with members which are 'setof text'. psql has no trouble
displaying all rows, but the parser does ...

So, my question is: can I produce a set in a function so that it
mimicks perfectly the return set of a select query?

I am using PG 6.5.3; is this already solved in the 7.0 beta?

Thanks

Miguel Sofer



Re: Re: Returning sets from functions?

От
Tom Lane
Дата:
mig@utdt.edu writes:
> Consider
>     create function followers(text) returns setof text as 
>           'select id from ids where id > $1 '
>           language 'sql';

> The function seems to work OK:
>     select followers('h');
> does return all indexes after the index 'h'.

> On the other hand,
>     select * from ids where id in (select followers('h'));
> produces the message
>   ERROR:  An operand to the '=' operator returns a set of text,
>         but '=' takes single values, not sets.

Seems to work in 7.0, which actually surprises me quite a bit ;-).
Functions returning sets are only very shakily implemented right now.
We've cured a few bugs since 6.5, but it's going to be difficult
to make much further progress with them until the fabled querytree
redesign happens (which will also bring outer joins and some other
good stuff).  Current plans are to tackle that for 7.2, which might
be out by the end of the year or so.

> Sorry, I forgot part of my question in the previous message:
>   How do you return a "setof" value from a PG/Tcl procedure?

Right now I think you can't ...
        regards, tom lane