pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Fabio Ugo Venchiarutti <f.venchiarutti@ocado.com>
To: Dave Cramer <pg@fastcrypt.com>
To: Mitar <mmitar@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Automatically parsing in-line composite types
Date: Tue, 29 Oct 2019 16:06:24 +0000
Message-ID: <e72efb14-4015-5820-850b-8b8ae14de7bd@ocado.com> (raw)
In-Reply-To: <CADK3HHKbTx2yLwikjcjaZ_Qfm1-ZWh=U=uH-pc14vDp-j7NUzw@mail.gmail.com>
References: <CAKLmikMrm778-eETLvVAd1W_u0R8TB+suAFO6jhMTmXQg3yhGg@mail.gmail.com>
	<CAKLmikPK_XeWT978gpozHsMFQ+JURqJsUmDGpC2gLt1sOdvmcw@mail.gmail.com>
	<CADK3HHKbTx2yLwikjcjaZ_Qfm1-ZWh=U=uH-pc14vDp-j7NUzw@mail.gmail.com>


On 29/10/2019 12:23, Dave Cramer wrote:
> 
> 
> On Wed, 23 Oct 2019 at 15:50, Mitar <mmitar@gmail.com 
> <mailto:mmitar@gmail.com>> wrote:
> 
>     Hi!
> 
>     Bump my previous question. I find it surprising that it seems this
>     information is not possible to be reconstructed by the client, when
>     the server has to have it internally. Is this a new feature request or
>     am I missing something?
> 
>      > I am trying to understand how could I automatically parse an in-line
>      > composite type. By in-line composite type I mean a type corresponding
>      > to ROW. For example, in the following query:
>      >
>      > SELECT _id, body, (SELECT array_agg(ROW(comments._id, comments.body))
>      > FROM comments WHERE comments.post_id=posts._id) AS comments FROM
>     posts
>      >
>      > It looks like I can figure out that "comments" is an array of
>     records.
>      > But then there is no way really to understand how to parse those
>      > records? So what are types of fields in the record?
>      >
>      > I start the parsing process by looking at types returned in
>      > RowDescription message and then reading descriptions in pg_type
>     table.
>      >
>      > Is there some other way to get full typing information of the
>     result I
>      > am assuming is available to PostreSQL internally?
> 
> 
> 
> Reading the RowDescription is the only way I am aware of.
> 
> 
> Dave Cramer
> 
> davec@postgresintl.com <mailto:davec@postgresintl.com>
> www.postgresintl.com <http://www.postgresintl.com;


Perhaps I misunderstood your question, but that sounds like my average 
use-case for the object-relational type system & JSON/JSONB 
functions/types: defining nested structured types as temporary relations 
in my queries and spew out their hierarchical JSON representation - 
often as a single big field (ironically I hate storing JSON in 
relational databases unless I'm storing something really opaque like 
dashboard layouts).


EG:

SELECT
     t.relname AS t_name,
     array_to_json(ARRAY_AGG(ats)) AS fields_json
FROM
     pg_class AS t INNER JOIN (
         SELECT
             ia.attrelid AS table_id,
             ia.attnum AS column_number,
             ia.attname AS column_name
         FROM
             pg_attribute AS ia
     ) AS ats
     ON
         (t.relkind = 'r')
     AND
         (t.relname IN ('pg_type', 'pg_constraint'))
     AND
         (ats.table_id = t.oid)
GROUP BY
     t.relname


You can use subqueries and array_agg() to deepen your output tree all 
the way to a stack overflow, a single <whatever>_to_json() call at the 
top will recursively traverse and convert whatever you feed it.


In your case you can just emit your composite type as a JSON object or 
array thereof (types and relations are the same thing).





-- 
Regards

Fabio Ugo Venchiarutti
OSPCFC Network Engineering Dpt.
Ocado Technology

-- 


Notice: 
This email is confidential and may contain copyright material of 
members of the Ocado Group. Opinions and views expressed in this message 
may not necessarily reflect the opinions and views of the members of the 
Ocado Group.

If you are not the intended recipient, please notify us 
immediately and delete all copies of this message. Please note that it is 
your responsibility to scan this message for viruses.

References to the 
"Ocado Group" are to Ocado Group plc (registered in England and Wales with 
number 7098618) and its subsidiary undertakings (as that expression is 
defined in the Companies Act 2006) from time to time. The registered office 
of Ocado Group plc is Buildings One & Two, Trident Place, Mosquito Way, 
Hatfield, Hertfordshire, AL10 9UL.





view thread (17+ messages)  latest in thread

Message-ID: <e72efb14-4015-5820-850b-8b8ae14de7bd@ocado.com>
Permalink:  ../e72efb14-4015-5820-850b-8b8ae14de7bd@ocado.com/
Also on:    postgresql.org/message-id/e72efb14-4015-5820-850b-8b8ae14de7bd@ocado.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: f.venchiarutti@ocado.com, pg@fastcrypt.com, mmitar@gmail.com
  Subject: Re: Automatically parsing in-line composite types
  In-Reply-To: <e72efb14-4015-5820-850b-8b8ae14de7bd@ocado.com>
  References: <CAKLmikMrm778-eETLvVAd1W_u0R8TB+suAFO6jhMTmXQg3yhGg@mail.gmail.com> <CAKLmikPK_XeWT978gpozHsMFQ+JURqJsUmDGpC2gLt1sOdvmcw@mail.gmail.com> <CADK3HHKbTx2yLwikjcjaZ_Qfm1-ZWh=U=uH-pc14vDp-j7NUzw@mail.gmail.com> <e72efb14-4015-5820-850b-8b8ae14de7bd@ocado.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox