﻿SELECT n.nspname as schema,
       pa.relname as parent,
       nc.nspname as dep_schema,
       ch.relname as dependent,
       'table'    as type      
  FROM pg_constraint cn
  JOIN pg_class pa ON (pa.oid = cn.confrelid)
  JOIN pg_class ch ON (ch.oid = cn.conrelid)
  JOIN pg_namespace nc ON (nc.oid = cn.connamespace)
  JOIN pg_namespace n ON (n.oid = pa.relnamespace)
 WHERE pa.relname LIKE '%%'
   AND contype = 'f'
UNION
SELECT v.table_schema as schema,
       v.table_name as parent,
       v.view_schema as dep_schema,
       v.view_name as dependent,
       'view' as type
  FROM information_schema.view_table_usage v 
 WHERE v.table_name LIKE '%%'
   AND v.table_schema <> 'information_schema'
   AND v.table_schema <> 'pg_catalog'
 ORDER BY 1, 2, 3, 4;
