pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
List all columns referencing an FK
2+ messages / 2 participants
[nested] [flat]

* List all columns referencing an FK
@ 2018-02-08 10:31  Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Andreas Joseph Krogh @ 2018-02-08 10:31 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi all.
 
Back in 2008 I asked this 
question: http://www.postgresql-archive.org/Finding-all-tables-that-have-foreign-keys-referencing-a-table-td21...
 
The solution was (and still is) this:
select confrelid::regclass, af.attname as fcol, conrelid::regclass, a.attname 
ascol from pg_attribute af, pg_attribute a, (select conrelid,confrelid,conkey[i]
asconkey, confkey[i] as confkey from (select conrelid,confrelid,conkey,confkey, 
generate_series(1,array_upper(conkey,1)) as i from pg_constraint where contype =
'f') ss) ss2 where af.attnum = confkey and af.attrelid = confrelid and a.attnum 
= conkeyand a.attrelid = conrelid AND confrelid::regclass = 'onp_user'::
regclass ANDaf.attname = 'id' 
 
(it lists all columns in all tables referencing the onp_user.id column)
 
I wonder, is this now possible using information_schema only, or are there 
still pieces missing in the standard holding this back?
 
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
 <https://www.visena.com;

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: List all columns referencing an FK
@ 2018-04-09 17:45  Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  parent: Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Peter Eisentraut @ 2018-04-09 17:45 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; pgsql-general@lists.postgresql.org

On 2/8/18 05:31, Andreas Joseph Krogh wrote:
> Back in 2008 I asked this
> question: http://www.postgresql-archive.org/Finding-all-tables-that-have-foreign-keys-referencing-a-table-td21...

> I wonder, is this now possible using information_schema only, or are
> there still pieces missing in the standard holding this back?

I think you'll still have the same problems if the same constraint name
appears more than once per schema.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-04-09 17:45 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-02-08 10:31 List all columns referencing an FK Andreas Joseph Krogh <andreas@visena.com>
2018-04-09 17:45 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox