Created
April 20, 2015 19:14
-
-
Save dpirotte/4a5f4edb558f38b3eb28 to your computer and use it in GitHub Desktop.
Show indexes that are eligible for a `NOT NULL` constraint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
t.relname as table_name, | |
i.relname as index_name, | |
a.attname as column_name, | |
pg_size_pretty(pg_relation_size(i.relname::regclass)) as index_size, | |
st.null_frac as null_frac | |
from | |
pg_class t, | |
pg_class i, | |
pg_index ix, | |
pg_attribute a, | |
pg_stats st | |
where | |
t.oid = ix.indrelid | |
and i.oid = ix.indexrelid | |
and a.attrelid = t.oid | |
and st.tablename = t.relname | |
and st.attname = a.attname | |
and a.attnum = ANY(ix.indkey) | |
and t.relkind = 'r' | |
and st.null_frac > 0.5 | |
and st.schemaname = current_schema | |
order by | |
t.relname, | |
i.relname; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment