Skip to content

Instantly share code, notes, and snippets.

@dpirotte
Created April 20, 2015 19:14
Show Gist options
  • Save dpirotte/4a5f4edb558f38b3eb28 to your computer and use it in GitHub Desktop.
Save dpirotte/4a5f4edb558f38b3eb28 to your computer and use it in GitHub Desktop.
Show indexes that are eligible for a `NOT NULL` constraint
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