If you use the more flexible returns table
instead of (the somewhat outdated) returns record
, then things get really easy:
CREATE FUNCTION foo(int) RETURNS table (a int, b text)
AS
$$
SELECT $1 as a, 'Hello #'||$1 as b;
$ LANGUAGE SQL;
now you can use:
select b
from foo(6);
If you are concerned about "tables" vs. "records" you can also define a type
to overcome the additional result set definition:
create type foo_return as (a int, b text);
CREATE FUNCTION foo(int) RETURNS foo_return
AS
$$
SELECT $1, 'Hello #'||$1;
$$ LANGUAGE SQL;
You can still the above select then:
select b
from foo(6);
A third maybe more "C" like approach would be to use out parameters (as shown in the manual)
CREATE FUNCTION foo(p1 int, out a int, out b text)
AS
$$
SELECT $1, 'Hello #'||$1;
$$
LANGUAGE SQL;
Then you don't need a from
:
select (foo(1)).b;