Expression to construct a anonymous record with named columns? - Mailing list pgsql-general

From Benedikt Grundmann
Subject Expression to construct a anonymous record with named columns?
Date
Msg-id CAMr3DCMJfbxFAK08rpOZSoGKNMCA+fM+YO6X=RkTeczrX_8sLQ@mail.gmail.com
Whole thread Raw
Responses Re: Expression to construct a anonymous record with named columns?
List pgsql-general
Hello,

Is there a way to construct write an expression that constructs a record with with named columns.  Specificially without the need for a corresponding named type.

That is

postgres=# select row(1, 2, 3);
   row  
---------
 (1,2,3)
(1 row)

Creates a unnamed record type.  And indeed it is for example not possible to expand it:

postgres=# select (row(1, 2, 3)).*;
ERROR:  record type has not been registered

On the other hand columns listed in a multi column select clause create a row type that is expandable and named:
postgres=# select ((bar.*).x).a from (select x from (select 1 as a, 2 as b) x) bar;
 a
---
 1
(1 row)

But it seems to not be possible to do so without a from clause:

postgres=# select ((select x from (select 1 as a, 2 as b) x)).a;
ERROR:  syntax error at or near "."
LINE 1: select ((select x from (select 1 as a, 2 as b) x)).a;
                                                          ^
postgres=# select ((select x from (select 1 as a, 2 as b) x)).*;
ERROR:  syntax error at or near "."
LINE 1: select ((select x from (select 1 as a, 2 as b) x)).*;

So named anonymous records / row types seem to be strangely second class.  Can somebody clarify the restrictions and rationale or even better show a way to do the equivalent of (made up syntax ahead):

select row(1 as a, 2 as b);

Cheers,

Bene

pgsql-general by date:

Previous
From: Chris Angelico
Date:
Subject: Re: Using psql -f to load a UTF8 file
Next
From: Alan Millington
Date:
Subject: Re: Using psql -f to load a UTF8 file