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 1iVd1Q-0005FQ-Tf for pgsql-general@arkaria.postgresql.org; Fri, 15 Nov 2019 14:56:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVd1P-0006ow-AV for pgsql-general@arkaria.postgresql.org; Fri, 15 Nov 2019 14:56:27 +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 1iVd1P-0006op-0I for pgsql-general@lists.postgresql.org; Fri, 15 Nov 2019 14:56:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVd1M-0001l9-0Y for pgsql-general@lists.postgresql.org; Fri, 15 Nov 2019 14:56:26 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id xAFEtHwM025426; Fri, 15 Nov 2019 09:55:17 -0500 From: Tom Lane To: Jeff Ross cc: pgsql-general@lists.postgresql.org Subject: Re: Query which shows FK child columns? In-reply-to: <3c146287-5d90-2710-a449-077d01a97975@openvistas.net> References: <3c146287-5d90-2710-a449-077d01a97975@openvistas.net> Comments: In-reply-to Jeff Ross message dated "Thu, 14 Nov 2019 16:43:01 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <25424.1573829717.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 15 Nov 2019 09:55:17 -0500 Message-ID: <25425.1573829717@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Jeff Ross writes: > On 11/14/19 11:49 AM, Ron wrote: >> I have a query which shows the parents and children in FK relations, = >> along with the parent column name, but can't seem to find the child = >> column names. >> Is there a way to find the child column names without having to dig = >> into pg_constraint? > I do not think you can do this without using pg_constraint. In principle, you can get useful information out of a join of information_schema.referential_constraints and information_schema.key_column_usage, but I think the only appeal that would have is (theoretical) portability to other DBMSes. It'd likely be horribly slow in any nontrivial database, because the information_schema views really don't map very well onto the Postgres catalogs, so the view definitions are overcomplicated already ... and then you gotta join them to get what you want. Aside from manual queries of pg_constraint, you might find it useful to do what psql and pg_dump do, namely use one of the built-in functions that reconstruct the text form of some SQL entity. In this case pg_catalog.pg_get_constraintdef(oid) might serve. For example, in a database containing only d1=3D# create table pk(a int, b int, primary key(a,b)); CREATE TABLE d1=3D# create table fk(x int, y int, foreign key (x,y) references pk); CREATE TABLE I get d1=3D# table information_schema.referential_constraints; constraint_catalog | constraint_schema | constraint_name | unique_constra= int_catalog | unique_constraint_schema | unique_constraint_name | match_op= tion | update_rule | delete_rule = --------------------+-------------------+-----------------+---------------= ------------+--------------------------+------------------------+---------= -----+-------------+------------- d1 | public | fk_x_y_fkey | d1 = | public | pk_pkey | NONE = | NO ACTION | NO ACTION (1 row) d1=3D# table information_schema.key_column_usage; = constraint_catalog | constraint_schema | constraint_name | table_catalog = | table_schema | table_name | column_name | ordinal_position | position_in= _unique_constraint = --------------------+-------------------+-----------------+---------------= +--------------+------------+-------------+------------------+------------= ------------------- d1 | public | pk_pkey | d1 = | public | pk | a | 1 | = = d1 | public | pk_pkey | d1 = | public | pk | b | 2 | = = d1 | public | fk_x_y_fkey | d1 = | public | fk | x | 1 | = 1 d1 | public | fk_x_y_fkey | d1 = | public | fk | y | 2 | = 2 (4 rows) so something could be made out of that, but not without some work to link up the FK and unique constraints. Meanwhile d1=3D# select conname, conrelid::regclass, pg_catalog.pg_get_constraintdef= (oid) from pg_constraint; conname | conrelid | = pg_get_constraintdef = ------------------------------+----------+--------------------------------= -------------------------------------------------------------------- cardinal_number_domain_check | - | CHECK ((VALUE >=3D 0)) yes_or_no_check | - | CHECK (((VALUE)::text =3D ANY (= (ARRAY['YES'::character varying, 'NO'::character varying])::text[]))) pk_pkey | pk | PRIMARY KEY (a, b) fk_x_y_fkey | fk | FOREIGN KEY (x, y) REFERENCES p= k(a, b) (4 rows) (Those first two CHECK constraints seem to belong to domains defined in the information_schema itself.) Of course, if what you need is something that can be programmatically analyzed, these text forms aren't too helpful --- but if you just want to see what the constraints are, then this is a good way. regards, tom lane