Re: Polymorphic "setof record" function?

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Polymorphic "setof record" function?
Date: 2009-01-13 16:22:05
Message-ID: 20090113162205.GJ3008@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jan 13, 2009 at 02:50:49PM +0100, Christian Schrrrder wrote:
> I have written a function that returns a setof record. The function has
> a table name as a parameter and the resulting records have the same
> structure as this table. Is there any easy way to specify this when I
> call the function? If the table has many columns then it's annoying to
> specify all of them.

I'm struggling to understand PG as well. I'd expect to be able to
use the normal :: cast operator, but it doesn't seem to function as
expected. I came up with the following code:

CREATE TEMP TABLE foo (
cola INTEGER, colb TEXT
);

INSERT INTO foo (cola, colb) VALUES
(1, 'hi'), (2, 'bye'),
(3, 'hello'), (4, 'testing');

SELECT (x::foo).cola
FROM (SELECT x::record FROM foo x LIMIT 10) x;

CREATE FUNCTION bar() RETURNS SETOF RECORD LANGUAGE SQL AS $$
SELECT * FROM foo LIMIT 10; $$;

SELECT (x::foo).cola FROM (
SELECT bar()) AS xxx(x);

I get "cannot cast type record to foo" from the final query, which I
don't understand at all. It suggests that casting something to a value
of type RECORD sometimes keeps the real type information around, and
sometimes not. Why?

Sam

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Emanuel Calvo Franco 2009-01-13 16:31:10 Re: Cast for text->Integer missing in 8.3.5
Previous Message Emanuel Calvo Franco 2009-01-13 15:39:59 Re: Polymorphic "setof record" function?