Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id D79DE632FEF for ; Thu, 5 Nov 2009 18:22:03 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 00701-09 for ; Thu, 5 Nov 2009 22:21:47 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iw0-f186.google.com (mail-iw0-f186.google.com [209.85.223.186]) by mail.postgresql.org (Postfix) with ESMTP id 9BDB5632FA1 for ; Thu, 5 Nov 2009 18:21:52 -0400 (AST) Received: by iwn16 with SMTP id 16so377634iwn.29 for ; Thu, 05 Nov 2009 14:21:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=V7qi/g6Mc6BC4KMXZcETyzKdiPbhES2GoRw9dQBtml8=; b=TJGIeqZ3MZfoCPG5H7zFS2Hpe7+wRvuIWLnFRDFzucdWC46MPsXvi/KmYCIMPW+c7X GIdK1lrvfcN1nUTACcRHfM6AEKv6E/1DVL3/GZbQt+bO+2jOkIlqPjSJU987XZZt82tK Cjr2zCCTHB/fwPh+D+MIxcX7LOcLhtAr5r744= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=ZAG0fIqtTMFN/RJZIq/2vycilbUy9V8lzM1TnwrjsofdDHkokph7+kVEKaMRhSOgTn Ltr5G8rh2xdrbD8MMIZ2lf6DWmVQiSWnT/EJ4Ic2U19W1W9obZdvzfs0obkv8ogKPDgl aANVMR4M4R48vj/bUjLeGtRarGjqGGS3NIJgc= MIME-Version: 1.0 Received: by 10.231.21.157 with SMTP id j29mr3689023ibb.28.1257459710608; Thu, 05 Nov 2009 14:21:50 -0800 (PST) In-Reply-To: <4AF33EB9.4060901@krap.dk> References: <4AF33EB9.4060901@krap.dk> Date: Thu, 5 Nov 2009 15:21:50 -0700 Message-ID: Subject: Re: Foreign key columns From: Scott Marlowe To: Svenne Krap Cc: sql pgsql Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.765 tagged_above=-10 required=5 tests=AWL=-0.655, BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 200911/29 X-Sequence-Number: 33380 On Thu, Nov 5, 2009 at 2:08 PM, Svenne Krap wrote: > Hi. > > Is there a simple way to get foreign key data... for example I found a > view, that does what I want ... > > It delivers > > =A0 fk_table =A0 | =A0 =A0 fk_column =A0 =A0 =A0| =A0 =A0 pk_table =A0 = =A0 =A0| pk_column | =A0 =A0 =A0 =A0 =A0 constraint_name > > --------------+--------------------+-------------------+-----------+-----= --------------------------------- > > =A0organisation | customer_rep =A0 =A0 =A0 | person =A0 =A0 =A0 =A0 =A0 = =A0| id =A0 =A0 =A0 =A0| organisation_customer_rep_fkey > > =A0organisation | ekstra_skema =A0 =A0 =A0 | ekstra_skema =A0 =A0 =A0| id= =A0 =A0 =A0 =A0| org_schema_fkey > > =A0organisation | in_group =A0 =A0 =A0 =A0 =A0 | organisation =A0 =A0 =A0= | id =A0 =A0 =A0 =A0| organisation_in_group_fkey > > =A0organisation | org_paying_company | organisation =A0 =A0 =A0| id =A0 = =A0 =A0 =A0| organisation_org_paying_company_fkey > > =A0organisation | primary_contact =A0 =A0| person =A0 =A0 =A0 =A0 =A0 =A0= | id =A0 =A0 =A0 =A0| primary_contact_fkey > > =A0organisation | type =A0 =A0 =A0 =A0 =A0 =A0 =A0 | organisation_type | = id =A0 =A0 =A0 =A0| organisation_type_fkey > > > > The query in question is > > SELECT FK.TABLE_NAME as FK_Table, CU.COLUMN_NAME as FK_Column, > PK.TABLE_NAME as PK_Table, PT.COLUMN_NAME as PK_Column, > C.CONSTRAINT_NAME as Constraint_Name > FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C > INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME > =3D FK.CONSTRAINT_NAME > INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON > C.UNIQUE_CONSTRAINT_NAME =3D PK.CONSTRAINT_NAME > INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = =3D > CU.CONSTRAINT_NAME > INNER JOIN ( > =A0 =A0 SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM > INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 > =A0 =A0 =A0 =A0INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON > i1.CONSTRAINT_NAME =3D i2.CONSTRAINT_NAME > =A0 =A0 =A0 =A0WHERE i1.CONSTRAINT_TYPE =3D 'PRIMARY KEY') PT ON PT.TABLE= _NAME =3D > PK.TABLE_NAME WHERE FK.TABLE_NAME=3D'organisation' ORDER BY 1,2,3,4; > > > The only problem is that this query is sloooooow, runs in tens of seconds= ... It runs in 112 milliseconds on my machine. Maybe your catalogs are extremely bloated? > Is there a good native (i.e. fast) pgsql-query to find that type of > information? This one seems to work pretty well. If you want to see a query to find such things, the easy way is to start psql with the -E switch, and issue a \d command on the organisation table and steal the SQL from there. That query will be pgsql specific, and possibly / likely pgsql VERSION dependent, so know that going into it.