List columns with indexes in PostgreSQL

后端 未结 23 1995
我在风中等你
我在风中等你 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条回答
  •  被撕碎了的回忆
    2020-11-27 09:54

    Similar to the accepted answer but having left join on pg_attribute as normal join or query with pg_attribute doesnt give indices which are like :
    create unique index unique_user_name_index on users (lower(name))

    select 
        row_number() over (order by c.relname),
        c.relname as index, 
        t.relname as table, 
        array_to_string(array_agg(a.attname), ', ') as column_names 
    from pg_class c
    join pg_index i on c.oid = i.indexrelid and c.relkind='i' and c.relname not like 'pg_%' 
    join pg_class t on t.oid = i.indrelid
    left join pg_attribute a on a.attrelid = t.oid and a.attnum = ANY(i.indkey) 
    group by t.relname, c.relname order by c.relname;
    

提交回复
热议问题