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

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Jim Nasby <Jim(dot)Nasby(at)bluetreble(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 05:47:11
Message-ID: CAKFQuwYyB02-yVhB3YL_DBkfkcx6TU9=LzZHNmCSASumhrcJXg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Friday, April 17, 2015, Jim Nasby <Jim(dot)Nasby(at)bluetreble(dot)com> wrote:

> On 4/17/15 7:39 PM, David G. Johnston wrote:
>
>> On Friday, April 17, 2015, Jim Nasby <Jim(dot)Nasby(at)bluetreble(dot)com
>> <mailto:Jim(dot)Nasby(at)bluetreble(dot)com>> wrote:
>>
>> I'm working on a function that will return a set of test data, for
>> unit testing database stuff. It does a few things, but ultimately
>> returns SETOF record that's essentially:
>>
>> RETURN QUERY EXECUTE 'SELECT * FROM ' || table_name;
>>
>> Because it's always going to return a real relation, I'd like to be
>> able to the equivalent of:
>>
>> SELECT ... FROM my_function( 'some_table' )::some_table;
>>
>>
>> Unfortunately this means "cast the existing type to some_table" and
>> "record" is not a valid type in this context.
>>
>>
>> Is there any trick that would allow that to work? I know that
>> instead of 'SELECT * ...' I can do 'SELECT row(t.*) FROM ' ||
>> table_name || ' AS t' and then do
>>
>> SELECT ... FROM my_function( 'some_table' ) AS data( d some_table )
>>
>> but I'm hoping to avoid the extra level of indirection.
>>
>> Haven't explored this specific code in depth...but which part - the
>> function alias or the select row(t.*)? They seem to be independent
>> concerns.
>>
>
> I'm saying that I know I can use the row construct as a poor work-around.
> What I actually want though is a way to tell this query:
>
> SELECT ... FROM my_function( 'some_table' )
>
> that my_function is returning a record that exactly matches "my_table". I
> suspect there's not actually any way to do that :(
>
>
No matter what you do inside the function you have to write that last query
as "from my_function('some_table') AS (rel some_table)" otherwise the
planer is clueless. You cannot defer the type until runtime. Your cast
form is slightly more succinct but I cannot see making it work when the
current method is serviceable.

Inside the function I would have thought that select * shoud work - no need
to use the row(t.*) construct - but the later seems reasonably direct...

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...

Select ... From my_func(null::some_table)
Create function my_func(tbl any) returns setof any ....
Use typeof to get a text string of the tbl arg's type.

You could maybe also return a refcursor...

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2015-04-18 05:52:50 Re: [SQL] function to send email with query results
Previous Message Suresh Raja 2015-04-18 05:30:51 function to send email with query results