Re: "Cast" SRF returning record to a table type?

From: Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: "Cast" SRF returning record to a table type?
Date: 2015-04-18 22:37:25
Message-ID: 5532DCA5.40407@BlueTreble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jim Nasby 2015-04-18 22:40:44 Re: ORDER BY for jsonb
Previous Message Andreas Kretschmer 2015-04-18 11:04:32 Re: function to send email with query results