select target_schema as table_schema, target_table as table_name, a.attname as column_name,
conname AS constraint_name,
CASE WHEN contype='f' THEN 'FOREIGN KEY'
WHEN contype='p' THEN 'PRIMARY KEY'
WHEN contype='u' THEN 'UNIQUE'
WHEN contype='t' THEN 'CONSTRAINT TRIGGER'
WHEN contype='c' THEN 'CHECK'
ELSE 'EXCLUSION' END AS constraint_type,
condeferrable AS is_deferrable,
condeferred AS is_deferred
from (select
(select nspname from pg_namespace where oid=m.relnamespace) as target_schema,
m.relname as target_table,
m.oid as target_table_oid,
conname,
contype,
condeferrable,
condeferred,
unnest(o.conkey) AS target_col
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
inner join pg_class m on m.oid = o.conrelid
where o.conrelid in (select oid from pg_class c where c.relkind = 'r')
and (condeferrable=true or condeferred=true)) t
inner join pg_attribute a on t.target_col = a.attnum and t.target_table_oid = a.attrelid and a.attisdropped = false
order by target_schema, target_table, attname;
Collections
This query belongs to the following collections:
Name
Description
Find problems by overview
Queries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .
Categories
This query is classified under the following categories:
Name
Description
CHECK constraints
Queries of this category provide information about CHECK constraints.
Relationships between tables
Queries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly.
Transactions
Queries of this catergory provide information about the use of transactions.
Uniqueness
Queries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes.