Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 7A7016332EC for ; Thu, 5 Nov 2009 17:15:47 -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 90704-01-2 for ; Thu, 5 Nov 2009 21:15:32 +0000 (UTC) X-Greylist: delayed 00:07:20.754217 by SQLgrey-1.7.6 Received: from kracon.net (kracon.net [81.7.185.66]) by mail.postgresql.org (Postfix) with ESMTP id 85107634347 for ; Thu, 5 Nov 2009 17:15:36 -0400 (AST) Received: (qmail 4785 invoked from network); 5 Nov 2009 21:08:12 -0000 Received: from unknown (HELO ?127.0.0.1?) (127.0.0.1) by localhost with SMTP; 5 Nov 2009 21:08:12 -0000 Message-ID: <4AF33EB9.4060901@krap.dk> Date: Thu, 05 Nov 2009 22:08:09 +0100 From: Svenne Krap User-Agent: Thunderbird 2.0.0.23 (X11/20091022) MIME-Version: 1.0 To: sql pgsql Subject: Foreign key columns X-Stationery: 0.4.10 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 200911/28 X-Sequence-Number: 33379 Hi. Is there a simple way to get foreign key data... for example I found a view, that does what I want ... It delivers fk_table | fk_column | pk_table | pk_column | constraint_name --------------+--------------------+-------------------+-----------+-------------------------------------- organisation | customer_rep | person | id | organisation_customer_rep_fkey organisation | ekstra_skema | ekstra_skema | id | org_schema_fkey organisation | in_group | organisation | id | organisation_in_group_fkey organisation | org_paying_company | organisation | id | organisation_org_paying_company_fkey organisation | primary_contact | person | id | primary_contact_fkey organisation | type | organisation_type | id | 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 = FK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN ( SELECT i1.TABLE_NAME, i2.COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1 INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY') PT ON PT.TABLE_NAME = PK.TABLE_NAME WHERE FK.TABLE_NAME='organisation' ORDER BY 1,2,3,4; The only problem is that this query is sloooooow, runs in tens of seconds... Is there a good native (i.e. fast) pgsql-query to find that type of information? Svenne