pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Joseph Krogh <andreas@visena.com>
To: pgsql-general@lists.postgresql.org
Subject: List all columns referencing an FK
Date: Thu, 8 Feb 2018 11:31:57 +0100 (CET)
Message-ID: <VisenaEmail.a.56e72d528104dec7.16174f5fd5e@tc7-visena> (raw)

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;

view thread (2+ messages)  latest in thread

Message-ID: <VisenaEmail.a.56e72d528104dec7.16174f5fd5e@tc7-visena>
Permalink:  ../VisenaEmail.a.56e72d528104dec7.16174f5fd5e@tc7-visena/
Also on:    postgresql.org/message-id/VisenaEmail.a.56e72d528104dec7.16174f5fd5e@tc7-visena

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andreas@visena.com
  Subject: Re: List all columns referencing an FK
  In-Reply-To: <VisenaEmail.a.56e72d528104dec7.16174f5fd5e@tc7-visena>
  References: <VisenaEmail.a.56e72d528104dec7.16174f5fd5e@tc7-visena>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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