Re: Anonymous code block with parameters

Merlin Moncure <mmoncure@gmail.com>

From: Merlin Moncure <mmoncure@gmail.com>
To: Hannu Krosing <hannu@2ndquadrant.com>
Cc: Marko Tiikkaja <marko@joh.to>, Vik Fearing <vik.fearing@dalibo.com>, Pavel Stehule <pavel.stehule@gmail.com>, Andres Freund <andres@2ndquadrant.com>, Heikki Linnakangas <hlinnakangas@vmware.com>, Craig Ringer <craig@2ndquadrant.com>, Kalyanov Dmitry <kalyanov.dmitry@gmail.com>, PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2014-09-19T18:20:09Z
Lists: pgsql-hackers
On Fri, Sep 19, 2014 at 9:26 AM, Merlin Moncure <mmoncure@gmail.com> wrote:
> On Thu, Sep 18, 2014 at 5:22 PM, Hannu Krosing <hannu@2ndquadrant.com> wrote:
>> Though it would be even nicer to have fully in-line type definition
>>
>> SELECT (tup).* FROM
>>   (
>>     SELECT CASE WHEN .. THEN ROW(1,2,3)::(a int, b text, c int2)
>>             WHEN .. THEN ROW(2,3,4)
>>             ELSE ROW (3,4,5) END AS tup
>>     FROM ..
>>   ) ss
>
> +1.   Workaround at present (which I mostly use during json serialization) is:
>
> SELECT (tup).* FROM
>   (
>     SELECT CASE WHEN .. THEN
>                (SELECT q FROM (SELECT 1, 2, 3) q)
>             WHEN .. THEN
>                (SELECT q FROM (SELECT 2, 3, 4) q)
>             ELSE (SELECT q FROM (SELECT 3, 4, 5) q)
>             END AS tup
>     FROM ..
>   ) ss

actually, this trick *only* works during json serialization -- it
allows control over the column names that row() masks over.  trying to
expand (tup).* still gives the dreaded "ERROR:  record type has not
been registered".  That's because this works:

select (q).* from (select 1 as a, 2 as b) q;

but this doesn't:

select ((select q from (select a,b) q)).* from (select 1 as a, 2 as b) q;

merlin