Re: [GENERAL] Returning a RECORD, not SETOF RECORD - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [GENERAL] Returning a RECORD, not SETOF RECORD
Date
Msg-id 5830.1114785365@sss.pgh.pa.us
Whole thread Raw
In response to Re: [GENERAL] Returning a RECORD, not SETOF RECORD  (Thomas Hallgren <thhal@mailblocks.com>)
Responses Re: [GENERAL] Returning a RECORD, not SETOF RECORD  (Michael Fuhr <mike@fuhr.org>)
List pgsql-hackers
Thomas Hallgren <thhal@mailblocks.com> writes:
> Ideally I'd like to write something like this:

> SELECT xyz(a, b) AS (x int, y int, z timestamptz) FROM abc;

> but that yields a syntax error.

While that's probably doable if anyone were really motivated,
I'm not sure it's worth the trouble in view of the recent OUT-parameter
improvements.  IMHO most of the use cases for such a thing would be
better served by declaring the function with OUT parameters.  The
AS-clause-column-list functionality was invented for functions where the
result type is truly not known when the function is written, such as
dblink.  But it's pretty hard to believe that many people need to write
such things.

Your example can be done like this in CVS tip:

regression=# create function xyz(int, int, out x int, out y int, out z timestamptz) as $$ select $1, $2, now() $$
languagesql;
 
CREATE FUNCTION
regression=# select xyz(unique1,unique2) from tenk1 limit 5;                  xyz
------------------------------------------(8800,0,"2005-04-29 10:26:37.738946-04")(1891,1,"2005-04-29
10:26:37.738946-04")(3420,2,"2005-04-2910:26:37.738946-04")(9850,3,"2005-04-29 10:26:37.738946-04")(7164,4,"2005-04-29
10:26:37.738946-04")
(5 rows)

Notice that this returns the record as a single column.  In most cases
you would probably wish that the record were burst into multiple
columns, which you can do easily with

regression=# select (xyz(unique1,unique2)).* from tenk1 limit 5; x   | y |               z
------+---+-------------------------------8800 | 0 | 2005-04-29 10:27:53.197948-041891 | 1 | 2005-04-29
10:27:53.197948-043420| 2 | 2005-04-29 10:27:53.197948-049850 | 3 | 2005-04-29 10:27:53.197948-047164 | 4 | 2005-04-29
10:27:53.197948-04
(5 rows)

but AFAICS that is not amenable to having an AS plastered on it (unless
the AS goes inside the parentheses, which'd be a really spectacular
abuse of the syntax).
        regards, tom lane


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [proposal] protocol extension to support loadable stream filters
Next
From: Bruno Wolff III
Date:
Subject: Re: Feature freeze date for 8.1