List columns with indexes in PostgreSQL

后端 未结 23 2006
我在风中等你
我在风中等你 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:32

    Extend to good answer of @Cope360. To get for certain table ( incase their is same table name but different schema ), just using table OID.

    select
         t.relname as table_name
        ,i.relname as index_name
        ,a.attname as column_name
        ,a.attrelid tableid
    
    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 like 'tbassettype'
        and a.attrelid = '"dbLegal".tbassettype'::regclass
    order by
        t.relname,
        i.relname;
    

    Explain : I have table name 'tbassettype' in both schema 'dbAsset' and 'dbLegal'. To get only table on dbLegal, just let a.attrelid = its OID.

提交回复
热议问题