Get the columns that an index operates on
First get the oid of the table, the oid of the indexes, and the column numbers used by those indexes. The column numbers will be something like "1 3 4", which means the first, third, and fourth columns of the searched-for table are used by the index. (Usually, there's just one number because many indexes are made for one column.)
select tbl.oid as table_oid, idx.indexrelid as index_oid, idx.indkey as column_numbers, idx_info.relname as index_name from pg_index as idx join pg_class as tbl on tbl.oid = idx.indrelid join pg_namespace as schm on tbl.relnamespace = schm.oid join pg_class as idx_info on idx.indexrelid = idx_info.oid where schm.nspname = 'my_schema' and tbl.relname = 'my_table'; table_oid | index_oid | column_numbers | index_name -----------+-----------+----------------+--------------------------------- 48645160 | 215366881 | 3 | my_table_my_col_idx 48645160 | 48877631 | 1 | my_table_my_other_col_idx
Now you can get the names of the columns operated on by each index. For each row (representing one index) from the above query, split i.indkey by space into column numbers. For each column number, run the following query to get the column name.
select c.attname as column_name from pg_attribute as c where c.attrelid = $table_oid -- from previous query and c.attnum = $column_number -- from previous query