Returning the name of a primary key

From: Juan Pablo Espino <jp(dot)espino(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Returning the name of a primary key
Date: 2005-05-16 18:25:46
Message-ID: 3e7daec1050516112511c165f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2005-05-16 18:35:03 Re: Returning the name of a primary key
Previous Message Andrew Dunstan 2005-05-16 18:21:12 Re: alternate regression dbs?