List columns with indexes in PostgreSQL

后端 未结 23 2008
我在风中等你
我在风中等你 2020-11-27 09:14

I would like to get the columns that an index is on in PostgreSQL.

In MySQL you can use SHOW INDEXES FOR table and look at the Column_name

23条回答
  •  猫巷女王i
    2020-11-27 09:40

    @cope360 's excellent answer, converted to use join syntax.

    select t.relname as table_name
         , i.relname as index_name
         , array_to_string(array_agg(a.attname), ', ') as column_names
    from pg_class t
    join pg_index ix
    on t.oid = ix.indrelid
    join pg_class i
    on i.oid = ix.indexrelid
    join pg_attribute a
    on a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    where t.relkind = 'r'
    and t.relname like 'test%'
    group by t.relname
           , i.relname
    order by t.relname
           , i.relname
    ;
    

提交回复
热议问题