Created
November 12, 2016 09:44
-
-
Save chribsen/2704938848cb25a80fd476aef45f615d to your computer and use it in GitHub Desktop.
Get indices on a specific table (PostgreSQL)
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
CREATE OR REPLACE FUNCTION getIndices(_table_name varchar) | |
RETURNS TABLE(table_name varchar, index_name varchar, column_name varchar) AS $$ | |
BEGIN | |
RETURN QUERY | |
select | |
t.relname::varchar as table_name, | |
i.relname::varchar as index_name, | |
a.attname::varchar as column_name | |
from | |
pg_class t, | |
pg_class i, | |
pg_index ix, | |
pg_attribute a | |
where | |
t.oid = ix.indrelid | |
and i.oid = ix.indexrelid | |
and a.attrelid = t.oid | |
and a.attnum = ANY(ix.indkey) | |
and t.relkind = 'r' | |
and t.relname = _table_name | |
order by | |
t.relname, | |
i.relname; | |
END; | |
$$ LANGUAGE plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment