Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Created March 17, 2016 03:14
Show Gist options
  • Save ryanguill/7f496fe5ad5e81e1b1e6 to your computer and use it in GitHub Desktop.
Save ryanguill/7f496fe5ad5e81e1b1e6 to your computer and use it in GitHub Desktop.
SELECT
n.nspname
, c.relname
, c.relhaspkey
, sum(case when i.indisunique = 't' then 1 else 0 end)
FROM
pg_catalog.pg_class c
INNER
JOIN pg_namespace n
ON c.relnamespace = n.oid
AND n.nspname NOT IN ('information_schema', 'pg_catalog')
AND c.relkind='r'
LEFT OUTER
JOIN pg_index i on c.oid = i.indrelid
WHERE
c.relhaspkey = 'f'
group BY
n.nspname
,c.relname
,c.relhaspkey
having sum(case when i.indisunique = 't' then 1 else 0 end) = 0
ORDER BY c.relhaspkey, c.relname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment