Returning the name of a primary key - Mailing list pgsql-hackers

From Juan Pablo Espino
Subject Returning the name of a primary key
Date
Msg-id 3e7daec1050516112511c165f@mail.gmail.com
Whole thread Raw
Responses Re: Returning the name of a primary key  (Alvaro Herrera <alvherre@surnet.cl>)
Re: Returning the name of a primary key  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hello all

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:

char *
give_pkey(char * table_char)TupleDesc        tupdesc;Form_pg_attribute    att;Form_pg_index        ind;int
  i, c=0, temp=-1; 
tupdesc = (TupleDesc) RelationNameGetTupleDesc(tabla_char);
       ind = <<something that idicates which table is>>       for (i=0; i<(tupdesc->natts); i++)       {           att
=tupdesc->attrs[i];           c = c + 1; 
           /* Something that can compare each attribute to determine
if it is a primary key ?*/           if ((ind->indisprimary) && (temp=-1))           {                temp = c;
      att = tupdesc->attrs[temp];            }         }         return pstrdup(NameStr(att->attname)); 
}

Sorry, I don't have much experience in c programming, thanks in
advance for any suggestions, regards,

Juan P. Espino


pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: alternate regression dbs?
Next
From: Hannu Krosing
Date:
Subject: Re: bitmap scans, btree scans, and tid order