Re: About primary keys. - Mailing list pgsql-sql

From Tom Lane
Subject Re: About primary keys.
Date
Msg-id 22741.992961525@sss.pgh.pa.us
Whole thread Raw
In response to Re: About primary keys.  (David BOURIAUD <david.bouriaud@ac-rouen.fr>)
List pgsql-sql
David BOURIAUD <david.bouriaud@ac-rouen.fr> writes:
>> Caveat:  Does not work for complex primary keys.

> Thanks, but that is my problem actually !

Here's a rather brute-force approach:

select relname,(select attname from pg_attribute where attrelid = c.oid and attnum = indkey[0]),(select attname from
pg_attributewhere attrelid = c.oid and attnum = indkey[1]),(select attname from pg_attribute where attrelid = c.oid and
attnum= indkey[2]),(select attname from pg_attribute where attrelid = c.oid and attnum = indkey[3])
 
from pg_class c, pg_index i
where c.oid = indrelid and indisprimary;

You can carry this out to however many key columns you want to
deal with.  The sub-selects will yield NULLs for the columns after
the last key column of a particular index, which is just what you
want here.
        regards, tom lane


pgsql-sql by date:

Previous
From: Roberto Mello
Date:
Subject: Re: About primary keys.
Next
From: David BOURIAUD
Date:
Subject: Re: About primary keys.