Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iPU43-0008UP-Te for pgsql-general@arkaria.postgresql.org; Tue, 29 Oct 2019 16:09:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iPU42-00040a-OW for pgsql-general@arkaria.postgresql.org; Tue, 29 Oct 2019 16:09:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iPU0s-0008T3-Lo for pgsql-general@lists.postgresql.org; Tue, 29 Oct 2019 16:06:30 +0000 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iPU0p-00021p-4O for pgsql-general@lists.postgresql.org; Tue, 29 Oct 2019 16:06:30 +0000 Received: by mail-wr1-x432.google.com with SMTP id a15so1991499wrf.9 for ; Tue, 29 Oct 2019 09:06:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ocado.com; s=google; h=reply-to:subject:to:cc:references:from:organization:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=DEzOCDdJzudWlvRQcIC1c3T70kGyayVGeozc1vNHPQs=; b=vj4VdW9iZ74EK2lzFRb6qSyYXojZUz5cs2bezUZpLSQ7N/3LXBt94fM4xshbfsKBzt LOSBwjo29UEFiIxDT3wMYaMJTYFcdWwsX44HQpe3Yb1HrH/9YrPczmLX7l1oYsLLS7dY 1Sf33XlPh/zUdw+R/MqMQkMnYxc23iWQpXClRphDB1+qhYd1Qr4Zd9MV4Fh/Xod2WkBp ffJ3EmuIFzgAPOKBcuJ4mDeSlsZXZp+4U7+mhmK178AvWmA42/yzl1nbQuGRBnMXSGOQ meI1HCONssDar6xHETsmdSsH1dzN6O7fbvjf7SBusXbXxK5SIxyphfLsYhj5+G4efWJI y8Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:subject:to:cc:references:from :organization:message-id:date:user-agent:mime-version:in-reply-to :content-language; bh=DEzOCDdJzudWlvRQcIC1c3T70kGyayVGeozc1vNHPQs=; b=dCJUTBb46EGN4aU87j+KF0HrtG36aBKN/Qm2asB68dnEy7Nzmkdq0ePM5pIWDg4NH2 /b+DMh0oDMZubJAk8dLh7M8zg4ZHN+FOwavb6xPzMIf6UXR4z+R9fQmCE/apuQCbFCOv cPTmI4IXvh/313I+Ygd48Aiz62KnE1lldKwoB4Xp2ovM0MJTAMP/3xZbV+CQVLTkg4hI Ppx1Hk4Mx9kqDgRFh4B841f3FiAdB1PclxaohlWLZyMp14PghtI/znZ6JTV5qkS+Dtym jsQfCGlJIp3pJiqeHVcQ5NzylWVFwPY+oM0EI2mLO/i4XiimV6rEUX4086VPRVEsSXxX RZcQ== X-Gm-Message-State: APjAAAVyFFEFSO2ZPn78gcZ0mgdE4TMforZwAwKFTpuUhn4pPVV281bM gvy5X0J+dD8jbTsLn8UNu5W9QgK7FxsIbnN63L+tM4j1PUZml1ZWd/C1NiyOVdGhyXIk31J2x/m TcfOXMj3ySHSGRUMhbj7dktnTNYGXwA== X-Google-Smtp-Source: APXvYqyUXb9LpZyP1bU1Y2fU5Qkqa2g7QalVOXDJ5z2usoTFhbpbfeQbVQzUowT7E0Junrd4PlLLQg== X-Received: by 2002:a5d:4b51:: with SMTP id w17mr8209485wrs.357.1572365185400; Tue, 29 Oct 2019 09:06:25 -0700 (PDT) Received: from [10.62.12.22] ([91.206.1.124]) by smtp.gmail.com with ESMTPSA id c144sm3336220wmd.1.2019.10.29.09.06.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Oct 2019 09:06:24 -0700 (PDT) Reply-To: f.venchiarutti@ocado.com Subject: Re: Automatically parsing in-line composite types To: Dave Cramer , Mitar Cc: "pgsql-generallists.postgresql.org" References: From: Fabio Ugo Venchiarutti Organization: Ocado Technology Message-ID: Date: Tue, 29 Oct 2019 16:06:24 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 29/10/2019 12:23, Dave Cramer wrote: > > > On Wed, 23 Oct 2019 at 15:50, Mitar > 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 > 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 _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.