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 1i74Uf-0007vN-Bs for pgsql-general@arkaria.postgresql.org; Sun, 08 Sep 2019 21:13:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i74Tg-0004RN-1H for pgsql-general@arkaria.postgresql.org; Sun, 08 Sep 2019 21:12:08 +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 1i74Te-0004J0-Qp for pgsql-general@lists.postgresql.org; Sun, 08 Sep 2019 21:12:07 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i74Ta-0002FG-5q for pgsql-general@postgresql.org; Sun, 08 Sep 2019 21:12:06 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 96C5C21F40; Sun, 8 Sep 2019 17:11:59 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sun, 08 Sep 2019 17:11:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=t MEOn4JCU19/dwB6klMcpijkJtboca+XQbbc3KiZnnY=; b=paM0k587h85/wJ2fY SQCB1zEDapvS/PsO1BJL2EvoQDZT7CLiz5tw2TwWJAQ9TivboXWz8CIimyiZN+sD wNhN68nZe+e2QQkEDc+8K71Xg5Clg+KuqdZk/DkWl50oghWD5fRFkDKN/thSDYdr BfuKO7ZEkknkPwsvMZkMansHmHLFl36Kuo2PjRR2u7xcMmi9VGY+ELt8vnjq9bB/ ORqNaUkbwAL4arHe8mXSYfK1csjKPLPwCcTgNcB5QeSWZum3qOKSoLpFXm0MMVy8 MkzrjqawUcqvCj/CagAIn3D1878xhlnk47fyEyKQX5WTMVqZ6Z1AkbXhZdUhoHFY 6Exbg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=tMEOn4JCU19/dwB6klMcpijkJtboca+XQbbc3KiZn nY=; b=DQr/ebK4l9PUkR3SFRQqJDqm7HsMisciExSmC6Ng+Djo/IoTvQNYZnLeA LWikOpy7lcPXgoontW8P+KuJPkqzNxISiKg2RERdgzVuE+xvfv3kVNttWUBYgqhR EVZHeRiVH3O8X0/pHaClLPw/8JneIJ7ezdLaEJKerUEDXj7sfYZ4NETWzb7sq4lM 85k3BnjChZm1rlaVirnE/UXHvmmLegZPk9VILg3sz2xlK4lj4UI9J3pm1AWRoymw 5TD6H6ZIiv2Ml9o/zNKNqZJYb4LhB5lRBbcTmbwUGKws8gMjHdd88OiYQ338uPg/ fu+RwYvY/RzXbdMqVBuwi88qIaaBw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudekgedgudehlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghdpshhtrggtkhhovhgv rhhflhhofidrtghomhenucfkphepjedurddvuddvrdefvddruddtheenucfrrghrrghmpe hmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhen ucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.3] (71-212-32-105.tukw.qwest.net [71.212.32.105]) by mail.messagingengine.com (Postfix) with ESMTPA id E4D3C8005A; Sun, 8 Sep 2019 17:11:57 -0400 (EDT) Subject: Re: Convert a row to a nested JSON document containing all relations in PostgreSQL To: Ali Alizadeh , "pgsql-general@postgresql.org" References: <7f6e7d7739c04c2cb33681ec8f705541@rayanbourse.ir> <12d11388-7041-841f-c857-a5e5c2c94745@aklaver.com> <47223f090e144d8cbd82571e0bdbf6f5@rayanbourse.ir> From: Adrian Klaver Message-ID: <02caac58-9db3-724a-566a-1486ded5f253@aklaver.com> Date: Sun, 8 Sep 2019 14:11:56 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <47223f090e144d8cbd82571e0bdbf6f5@rayanbourse.ir> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/8/19 5:22 AM, Ali Alizadeh wrote: > >> 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 Probably easier to look whether there are FK's on a table: select * from information_schema.table_constraints where table_schema = 'some_schema' and table_name ='some_name' and constraint_type = 'FOREIGN KEY'; Then use that to look up the columns(to account for multi-column FK's): select table_name, column_name from information_schema.constraint_column_usage where constraint_name = 'fk_name'; details like to which column on which table, then perform a dynamic SQL SELECT (because I presume table and column names would be strings, not SQL identifiers) over the target Take a look at the format() example here: https://www.postgresql.org/docs/11/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN https://www.postgresql.org/docs/11/functions-string.html#FUNCTIONS-STRING-FORMAT 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 working code for this, for which I welcome any help or directions. And there might be simpler solutions to this problem, which I would like to know about. > > Also see my question on SO: https://stackoverflow.com/questions/57830543/convert-a-row-to-a-nested-json-document-containing-all-relations-in-postgresql > -- Adrian Klaver adrian.klaver@aklaver.com