Thread: Returning composite types from a plpgsql function

Returning composite types from a plpgsql function

From
Joseph Barillari
Date:
Hi,

I've read in the mailing list archives that plpgsql does not support
assignment to array variables, so it is impossible to use them to
return data from a plpgsql function.

I'm curious as to how one might return more than one base type from
such a function, short of creating a temporary table and inserting the
value into that. For example:

CREATE TABLE tmp (a INTEGER, b INTEGER); --define the type

CREATE OR REPLACE FUNCTION frob()
RETURNS tmp AS '
DECLARE
ret tmp%ROWTYPE;
BEGIN
ret.a := 1;
ret.b := 2;
return ret;
END;'
LANGUAGE 'plpgsql';

Unfortunately, when I execute this, I get:

cal=> select frob();  frob
-----------172795360
(1 row)

Is there some sort of type coercion I have to do to get this to work?

Thanks,

Joe

Re: Returning composite types from a plpgsql function

From
Tom Lane
Date:
Joseph Barillari <jbarilla@princeton.edu> writes:
> Unfortunately, when I execute this, I get:

> cal=3D> select frob();
>    frob=20=20=20=20
> -----------
>  172795360
> (1 row)

You can't do anything useful with a rowtype function result except
select a single field from it.  The manual's discussion for SQL
functions also applies to PL-language functions:
http://www.ca.postgresql.org/users-lounge/docs/7.2/postgres/xfunc-sql.html#AEN26354

There is work afoot to allowSELECT ... FROM function(...)
which would make functions returning rowtypes a whole lot more useful.
        regards, tom lane