Re: Polymorphic "setof record" function? - Mailing list pgsql-general

From Sam Mason
Subject Re: Polymorphic "setof record" function?
Date
Msg-id 20090113162205.GJ3008@frubble.xen.chris-lamb.co.uk
Whole thread Raw
In response to Polymorphic "setof record" function?  (Christian Schröder <cs@deriva.de>)
List 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

pgsql-general by date:

Previous
From: "Nykolyn, Andrew"
Date:
Subject: Cast for text->Integer missing in 8.3.5
Next
From: Emanuel Calvo Franco
Date:
Subject: Re: Cast for text->Integer missing in 8.3.5