Re: Modifying FK constraints

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Oleg Lebedev <oleglebedev(at)waterford(dot)org>
Cc: Richard Huxton <dev(at)archonet(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Modifying FK constraints
Date: 2004-11-11 14:42:51
Message-ID: 20041111063409.P28036@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 10 Nov 2004, Oleg Lebedev wrote:

>
> In order to find all FK declared on a table I query tg_trigger view. The
> query lists all FKs declared on the table as well as all the ones
> referencing the table. I noticed that FKs that are declared on the table
> have pgtype equal to 21, and FKs referencing the table have pgtype 9 or
> 17.
>
> The following query lists all the FKs declared table 'objective'. Is
> this the right way to do this?
>
> select t.tgconstrname, src.relname, dest.relname, t.tgtype
> from pg_trigger t, pg_class src, pg_class dest
> where t.tgrelid=src.oid
> and t.tgconstrrelid=dest.oid
> and t.tgisconstraint = 't'
> and t.tgtype=21
> and src.relname='objective';

I think that should work as long as you aren't going around making your
own constraint triggers.

I believe you can get info from pg_constraint as well in recent versions,
maybe something like:

select pg_constraint.conname, c.relname, c2.relname from
pg_constraint,pg_class c, pg_class c2 where contype='f' and conrelid=c.oid
and confrelid=c2.oid and c.relname='objective';

You can also get some other information that's hard to get from the
triggers like the referential actions (pg_constraint.confupdtype and
confdeltype).

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David Parker 2004-11-11 14:49:12 checkpoints
Previous Message Kevin Barnard 2004-11-11 14:23:18 Vacuum message