Re: Returning the name of a primary key

From: "John Hansen" <john(at)geeknet(dot)com(dot)au>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Juan Pablo Espino" <jp(dot)espino(at)gmail(dot)com>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Returning the name of a primary key
Date: 2005-05-17 10:52:54
Message-ID: 5066E5A966339E42AA04BA10BA706AE50A9323@rodrick.geeknet.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Tom, Juan,

Wouldn't this simple SQL do the trick?

CREATE OR REPLACE FUNCTION pk_column(text) RETURNS SETOF text
AS '
SELECT attname::text
FROM pg_class, pg_constraint, pg_attribute
WHERE pg_class.oid = conrelid
AND contype=''p''
AND attrelid = pg_class.oid
AND attnum = ANY (conkey)
AND relname=$1;
'
LANGUAGE sql VOLATILE STRICT;

> -----Original Message-----
> From: pgsql-hackers-owner(at)postgresql(dot)org
> [mailto:pgsql-hackers-owner(at)postgresql(dot)org] On Behalf Of Tom Lane
> Sent: Tuesday, May 17, 2005 4:49 AM
> To: Juan Pablo Espino
> Cc: pgsql-hackers(at)postgresql(dot)org
> Subject: Re: [HACKERS] Returning the name of a primary key
>
> Juan Pablo Espino <jp(dot)espino(at)gmail(dot)com> writes:
> > I need to write a function that retrieve the name of at least one
> > table primary key, if it exists. The only argument passed to the
> > function is the table name. I have thought something like this:
>
> You need to be searching the list of indexes, not the
> attributes per se.
> ATExecDropNotNull() might be a useful example.
>
> regards, tom lane
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index
> scan if your
> joining column's datatypes do not match
>
>

Browse pgsql-hackers by date

  From Date Subject
Next Message Hannu Krosing 2005-05-17 11:22:33 Re: Learning curves and such (was Re: pgFoundry)
Previous Message Hannu Krosing 2005-05-17 07:39:47 Re: SQL99 hierarchical queries stalled