views:

115

answers:

1

Given a table name, how do I extract a list of primary key columns and their datatypes from a plpgsql function?

+2  A: 

Take a look at pg_constraint system table. Or information_schema.table_constraints view if you prefer stick close to the SQL standard.

For a complete example connect to a DB using psql with the "-E" option and type \d <some_table> - you'll see the actual queries used in describing a table.

Milen A. Radev
Additionally, combine this with the data from pg_indexes, and you should be pretty good. Really a primary key is just a unique index with not null on all of the fields.
Grant Johnson