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 1i6z6X-0007h9-DB for pgsql-general@arkaria.postgresql.org; Sun, 08 Sep 2019 15:27:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i6z6W-00038U-1V for pgsql-general@arkaria.postgresql.org; Sun, 08 Sep 2019 15:27:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i6z6V-0002qS-GR for pgsql-general@lists.postgresql.org; Sun, 08 Sep 2019 15:27:51 +0000 Received: from mx.rayanbourse.ir ([212.16.69.137]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1i6z6R-0003nK-NR for pgsql-general@postgresql.org; Sun, 08 Sep 2019 15:27:49 +0000 Received: from Mailbox.rbc.local (192.168.20.35) by edge.rbc.local (192.168.20.36) with Microsoft SMTP Server (TLS) id 15.1.225.42; Sun, 8 Sep 2019 17:10:25 +0430 Received: from Mailbox.rbc.local (192.168.20.35) by Mailbox.rbc.local (192.168.20.35) with Microsoft SMTP Server (TLS) id 15.1.225.42; Sun, 8 Sep 2019 16:52:35 +0430 Received: from Mailbox.rbc.local ([::1]) by Mailbox.rbc.local ([::1]) with mapi id 15.01.0225.041; Sun, 8 Sep 2019 16:52:35 +0430 From: Ali Alizadeh To: Adrian Klaver , "pgsql-general@postgresql.org" Subject: Re: Convert a row to a nested JSON document containing all relations in PostgreSQL Thread-Topic: Convert a row to a nested JSON document containing all relations in PostgreSQL Thread-Index: AQHVZTWaksmjsO/ZrkyKy86hxQLa1KcgS4GAgADUxp8= Date: Sun, 8 Sep 2019 12:22:35 +0000 Message-ID: <47223f090e144d8cbd82571e0bdbf6f5@rayanbourse.ir> References: <7f6e7d7739c04c2cb33681ec8f705541@rayanbourse.ir>,<12d11388-7041-841f-c857-a5e5c2c94745@aklaver.com> In-Reply-To: <12d11388-7041-841f-c857-a5e5c2c94745@aklaver.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [192.168.21.168] Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Cyberoam-smtpxy-version: 1.0.6.3 X-Cyberoam-AV-Policy: default X-CTCH-Error: Unable to connect local ctasd List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > 1) What side are you talking about, the parent of the record or the > children? the children. only one level of depth is fine. > 2) What procedural language are you using? PL/pgSQL As I understand, I need to loop over all columns in the "NEW" record, find = out if the column is a foreign key using information_schema or pg_catalog, = find the foreign key details like to which column on which table, then perf= orm a dynamic SQL SELECT (because I presume table and column names would be= strings, not SQL identifiers) over the target table for the target record,= convert the record to JSON and finally assign it to the appropriate key of= the JSON object of top-level row. I'm yet trying to write the actual worki= ng code for this, for which I welcome any help or directions. And there mig= ht be simpler solutions to this problem, which I would like to know about. Also see my question on SO: https://stackoverflow.com/questions/57830543/co= nvert-a-row-to-a-nested-json-document-containing-all-relations-in-postgresq= l=