return query/composite types

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

return query/composite types

От:
Merlin Moncure <mmoncure@gmail.com>
Дата:
create table foo(a int, b int);
postgres=# create function rfoo() returns setof foo as $$ begin return
query select foo from foo; end; $$ language plpgsql;
CREATE FUNCTION
Time: 25.606 ms
postgres=#
postgres=#
postgres=# select rfoo();
ERROR:  structure of query does not match function result type
DETAIL:  Number of returned columns (1) does not match expected column
count (2).


Any reason why the above shouldn't work?  Why does 'return query'
assume that returned composite types are expanded?

merlin

Re: return query/composite types

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Merlin Moncure  writes:
> create table foo(a int, b int);
> postgres=# create function rfoo() returns setof foo as $$ begin return
> query select foo from foo; end; $$ language plpgsql;

Use "select * from ..." instead.
		regards, tom lane

Re: return query/composite types

От:
Merlin Moncure <mmoncure@gmail.com>
Дата:
On Fri, Feb 20, 2009 at 3:00 PM, Tom Lane  wrote:
> Merlin Moncure  writes:
>> create table foo(a int, b int);
>> postgres=# create function rfoo() returns setof foo as $$ begin return
>> query select foo from foo; end; $$ language plpgsql;
>
> Use "select * from ..." instead.

Yeah...I was thinking maybe that shouldn't be required:
1. it's illogical and conflicts with regular non 'returns query'
semantics (declare foo, assign, return)
2. if 'foo' is result of set returning function (like unnest), you
need to make extra subquery to prevent that function from executing
lots of extra times.
e.g.
select unnest(foo) from  will unnest the set six times if
foo has six fields.  This is a bit of a landmine since type returning
functions are _fairly_ common use for composite types.

These aren't really complaints since the workarounds are trivial, just
casual wondering if the behavior is correct.

merlin

Re: return query/composite types

От:
Merlin Moncure <mmoncure@gmail.com>
Дата:
On Fri, Feb 20, 2009 at 3:25 PM, Merlin Moncure  wrote:
> On Fri, Feb 20, 2009 at 3:00 PM, Tom Lane  wrote:
>> Merlin Moncure  writes:
>>> create table foo(a int, b int);
>>> postgres=# create function rfoo() returns setof foo as $$ begin return
>>> query select foo from foo; end; $$ language plpgsql;
>>
>> Use "select * from ..." instead.
>
> Yeah...I was thinking maybe that shouldn't be required:
> 1. it's illogical and conflicts with regular non 'returns query'
> semantics (declare foo, assign, return)
> 2. if 'foo' is result of set returning function (like unnest), you
> need to make extra subquery to prevent that function from executing
> lots of extra times.
> e.g.
> select unnest(foo) from  will unnest the set six times if

er,
select (unnest(foo)).* from  will unnest the set six times if                                ^^^

Re: return query/composite types

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Merlin Moncure  writes:
> On Fri, Feb 20, 2009 at 3:00 PM, Tom Lane  wrote:
>> Use "select * from ..." instead.

> Yeah...I was thinking maybe that shouldn't be required:

If you allow both interpretations then you create a syntactic ambiguity
(at least for the case of single-column composite types).  We have
historically allowed both in SQL functions, but I think that's a design
error that shouldn't be repeated in other PLs.
		regards, tom lane

FAQ