I need a query that returns:
"table_name", "field_name", "field_type", "contraint_name"
until now i have:
select conrelid::regclass AS table_name,
regexp_replace(pg_get_constraintdef(c.oid), '.*\((.*)\)', '\1') as fields,
conname as contraint_name
from pg_constraint c
join pg_namespace n ON n.oid = c.connamespace
join pg_attribute at on
--join pg_type t ON t.typnamespace = n.oid
where contype ='f'
A foreign key may be based on multiple columns, so
conkey
andconfkey
ofpg_constraint
are arrays. You have to unnest the arrays to get a list of column names or types. You can use these functions:The functions may be very handy when querying constraints and indexes. Your query is nice and simple with them: