Re: "Cast" SRF returning record to a table type? - Mailing list pgsql-general

From Jim Nasby
Subject Re: "Cast" SRF returning record to a table type?
Date
Msg-id 5532DCA5.40407@BlueTreble.com
Whole thread Raw
In response to Re: "Cast" SRF returning record to a table type?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: "Cast" SRF returning record to a table type?
List pgsql-general
On 4/18/15 12:47 AM, David G. Johnston wrote:
> If you could find a way to pass a value of type some_table into the
> function - instead of the name/text 'some_table‘ - you could  possibly
> use polymorphic pseudotypes...just imagining here...

Oh, I didn't think about that. Maybe I'll try it.

What I ended up with is this:

CREATE FUNCTION ... (
) RETURNS SETOF text ...
...
RETURN QUERY EXECUTE format(
     'SELECT row(t.*)::text FROM %I.%I AS t'
     , ...
);

So the function is getting a record and casting it to text. To call the
function you have to...

SELECT (function(...))::name_of_table).*

that gives you the same output as if you'd selected directly from the table.
--
Jim Nasby, Data Architect, Blue Treble Consulting
Data in Trouble? Get it in Treble! http://BlueTreble.com


pgsql-general by date:

Previous
From: John R Pierce
Date:
Subject: Re: function to send email with query results
Next
From: Jim Nasby
Date:
Subject: Re: ORDER BY for jsonb