Text Size: Normal / Large

43.13. pg_constraint

The catalog pg_constraint stores check, primary key, unique, and foreign key constraints on tables. (Column constraints are not treated specially. Every column constraint is equivalent to some table constraint.) Not-null constraints are represented in the pg_attribute catalog.

Check constraints on domains are stored here, too.

Table 43-13. pg_constraint Columns

NameTypeReferencesDescription
connamename Constraint name (not necessarily unique!)
connamespaceoidpg_namespace.oid The OID of the namespace that contains this constraint
contypechar  c = check constraint, f = foreign key constraint, p = primary key constraint, u = unique constraint
condeferrablebool Is the constraint deferrable?
condeferredbool Is the constraint deferred by default?
conrelidoidpg_class.oidThe table this constraint is on; 0 if not a table constraint
contypidoidpg_type.oidThe domain this constraint is on; 0 if not a domain constraint
confrelidoidpg_class.oidIf a foreign key, the referenced table; else 0
confupdtypechar Foreign key update action code
confdeltypechar Foreign key deletion action code
confmatchtypechar Foreign key match type
conkeyint2[]pg_attribute.attnumIf a table constraint, list of columns which the constraint constrains
confkeyint2[]pg_attribute.attnumIf a foreign key, list of the referenced columns
conbintext If a check constraint, an internal representation of the expression
consrctext If a check constraint, a human-readable representation of the expression

Note: consrc is not updated when referenced objects change; for example, it won't track renaming of columns. Rather than relying on this field, it's best to use pg_get_constraintdef() to extract the definition of a check constraint.

Note: pg_class.relchecks needs to agree with the number of check-constraint entries found in this table for the given relation.


User Comments


18 Oct 2007 10:49:22

This query will get you the namespace, table, column of each foreign key that points to the plugged in namespace, table, column

SELECT n.nspname AS namespace, p1.relname AS table,
a1.attname AS column
FROM pg_constraint c, pg_namespace n,
pg_class p1, pg_class p2,
pg_attribute a1, pg_attribute a2
WHERE c.contype = 'f'
AND c.confrelid > 0
AND c.connamespace = n.oid
AND c.conrelid = p1.oid
AND c.confrelid = p2.oid
AND c.conrelid = a1.attrelid
AND a1.attnum = ANY (c.conkey)
AND c.confrelid = a2.attrelid
AND a2.attnum = ANY (c.confkey)
AND n.nspname = 'public'
AND p2.relname = 'users'
AND a2.attname = 'user_id'

Russ
18 Oct 2007 19:27:58

This query is similar to last one I posted, but will give you schema.table.column for each foreign key pointing to the given one for the whole db and not just current schema

SELECT n.nspname AS namespace, p1.relname AS table,
a1.attname AS column
FROM pg_constraint c, pg_namespace n,
pg_class p1, pg_class p2,
pg_attribute a1, pg_attribute a2,
pg_namespace n2
WHERE c.contype = 'f'
AND c.confrelid > 0
AND c.connamespace = n.oid
AND c.conrelid = p1.oid
AND c.confrelid = p2.oid
AND c.conrelid = a1.attrelid
AND a1.attnum = ANY (c.conkey)
AND c.confrelid = a2.attrelid
AND a2.attnum = ANY (c.confkey)
AND p2.relnamespace = n2.oid
AND n2.nspname = 'public'
AND p2.relname = 'users'
AND a2.attname = 'user_id'

New comments cannot be added to old documentation versions.

Privacy Policy | Project hosted by our server sponsors. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group