How relate pg_class to pg_constraint

Started by Bill Toddabout 17 years ago3 messagesgeneral
Jump to latest
#1Bill Todd
pg@dbginc.com

I need to join pg_class and pg_constraint to get information about
constraints on a table. It appears that pg_constraint.conrelid is the
foreign key but I do not see a relid column in pg_class. What column(s)
define the relationship between these tables? Thanks.

Bill

#2Bill Todd
pg@dbginc.com
In reply to: Bill Todd (#1)
Re: How relate pg_class to pg_constraint

Bill Todd wrote:

I need to join pg_class and pg_constraint to get information about
constraints on a table. It appears that pg_constraint.conrelid is the
foreign key but I do not see a relid column in pg_class. What
column(s) define the relationship between these tables? Thanks.

Bill

Is the relationship pg_constraint.conrelid = pg_class.oid? I assume the
column lists for the system tables do not include the oid column because
everyone (but us newbies) knows that every system table has an oid
column.<g>

Bill

#3Bruce Momjian
bruce@momjian.us
In reply to: Bill Todd (#1)
Re: How relate pg_class to pg_constraint

Bill Todd <pg@dbginc.com> writes:

I need to join pg_class and pg_constraint to get information about constraints
on a table. It appears that pg_constraint.conrelid is the foreign key but I do
not see a relid column in pg_class. What column(s) define the relationship
between these tables? Thanks.

There's a system column called "oid" on all the system tables which is the
primary key. It doesn't show up unless you explicitly list it in the target
list of the select.

So you need a join like WHERE pg_class.oid = conrelid

If all you need is the name to display for users then there's a convenience
type called regclass which you can use by doing "SELECT conrelid::regclass
from pg_constraint". There are similar regtype and a few others like it too.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's 24x7 Postgres support!