Skip to content

Instantly share code, notes, and snippets.

@matiasleidemer
Created January 21, 2011 15:22
Show Gist options
  • Save matiasleidemer/789826 to your computer and use it in GitHub Desktop.
Save matiasleidemer/789826 to your computer and use it in GitHub Desktop.
select s.table_name, s.column_name, s.data_type, s.character_maximum_length, s.ordinal_position, s.is_nullable, s.column_default, x.conname, x.rel_table
from information_schema.columns s
left join
(
SELECT p1.relname AS table, a1.attname AS column, c.conname, p2.relname as rel_table
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 c.confdeltype = 'a'
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'
) x on x.table = s.table_name and x.column = s.column_name
where
s.table_schema = 'public'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment