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 1i6VTP-0005TE-Bg for pgsql-general@arkaria.postgresql.org; Sat, 07 Sep 2019 07:49:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i6VTM-0001fM-OI for pgsql-general@arkaria.postgresql.org; Sat, 07 Sep 2019 07:49:28 +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 1i6TSv-0000xo-Hi for pgsql-general@lists.postgresql.org; Sat, 07 Sep 2019 05:40:53 +0000 Received: from mx.rayanbourse.ir ([212.16.69.137]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1i6TSn-00056G-So for pgsql-general@postgresql.org; Sat, 07 Sep 2019 05:40:52 +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; Sat, 7 Sep 2019 09:23:41 +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; Sat, 7 Sep 2019 09:05:52 +0430 Received: from Mailbox.rbc.local ([::1]) by Mailbox.rbc.local ([::1]) with mapi id 15.01.0225.041; Sat, 7 Sep 2019 09:05:52 +0430 From: Ali Alizadeh To: "pgsql-general@postgresql.org" Subject: 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/ZrkyKy86hxQLa1A== Date: Sat, 7 Sep 2019 04:35:52 +0000 Message-ID: <7f6e7d7739c04c2cb33681ec8f705541@rayanbourse.ir> 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: multipart/alternative; boundary="_000_7f6e7d7739c04c2cb33681ec8f705541rayanbourseir_" 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 --_000_7f6e7d7739c04c2cb33681ec8f705541rayanbourseir_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hello. In PostgreSQL 10.10, I have created a trigger function that converts the `N= EW` row to a JSON object using `to_jsonb(NEW)`. But now I need to include t= he records on the other side of the foreign keys in `NEW` record in the JSO= N object in a nested fashion. What is the best and most generic way to accomplish this without prior know= ledge about the schema of the `NEW` record? I need to keep this trigger fun= ction as generic as possible, because I plan to use it on all tables. One l= evel of depth in following foreign keys is currently enough for me. Thank you. --_000_7f6e7d7739c04c2cb33681ec8f705541rayanbourseir_ Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable

Hello.


In PostgreSQL 10.10, I have created a trigger function that converts the= `NEW` row to a JSON object using `to_jsonb(NEW)`. But now I need to include the records on the = other side of the foreign keys in `NEW` record in the JSON object in a nested fashion.


What is the best and most generic way to accomplish this without prior k= nowledge about the schema of the `NEW` record? I need to keep = this trigger function as generic as possible, because I plan to use it on a= ll tables. One level of depth in following foreign keys is currently enough for me.


Thank you.

--_000_7f6e7d7739c04c2cb33681ec8f705541rayanbourseir_--