how to find foreign key details (column, that is)

From: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: how to find foreign key details (column, that is)
Date: 2008-12-14 18:41:24
Message-ID: 20081214184124.GA8624@merkur.hilbert.loc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello all,

suppose I know that there are several tables with foreign
keys pointing to

demographics.identity.pk

With the help of pg_constraint I can get a list of *tables*
which hold those foreign keys (schema = demographics, tbl = identity,
col = pk):

select
%(schema)s as referenced_schema,
%(tbl)s as referenced_table,
%(col)s as referenced_column,
conrelid::regclass as foreign_table,
confkey as referenced_column_list
from
pg_constraint
where
contype = 'f'
and
confrelid = (
select oid from pg_class where relname = %(tbl)s and relnamespace = (
select oid from pg_namespace where nspname = %(schema)s
)
) and
(
select attnum
from pg_attribute
where
attrelid = (select oid from pg_class where relname = %(tbl)s and relnamespace = (
select oid from pg_namespace where nspname = %(schema)s
))
and
attname = %(col)s
) = any(confkey)

I cannot, however, for the life of it find out how to learn
the *column* the foreign key is on. IOW I can find out that

clinical.allergy

has a foreign key to

demographics.identity.pk

but I cannot find out that the column representing the
foreign key is

clinical.allergy.fk_identity

How can I go about this ?

Thanks,
Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Sergio Borgonovo 2008-12-14 19:16:35 from 2 keys to serial
Previous Message Bob Gobeille 2008-12-14 16:28:11 log rotate and open connections