List all index names, column names and its table name of a PostgreSQL database

后端 未结 6 1653
走了就别回头了
走了就别回头了 2020-12-12 13:40

What is the query to get the list all index names, its column name and its table name of a postgresql database?

I have tried to get the list of all indexes in a db b

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 14:37

    For Non-Composite Indexes

    select  t.relname,i.relname ,
         STRING_AGG(pga.attname||'', ','order by i.relname,pga.attnum)   as columnName          
        from pg_class t inner join pg_index ix
                  on t.oid = ix.indrelid
                  inner join  pg_class i
                  on i.oid = ix.indexrelid
                   inner join  pg_attribute pga
                  on
                   pga.attrelid = i.oid 
                inner join pg_indexes pgidx
                    on pgidx.indexname=i.relname
                 where 
                   t.relkind = 'r' 
                and pgidx.schemaname='asit_cm'
                 and t.relname ='accessory'
                 group by  t.relname,i.relname having count(*)=1
    

    For Composite Indexes

    select  t.relname,i.relname ,
         STRING_AGG(pga.attname||'', ','order by i.relname,pga.attnum)   as columnName          
        from pg_class t inner join pg_index ix
                  on t.oid = ix.indrelid
                  inner join  pg_class i
                  on i.oid = ix.indexrelid
                   inner join  pg_attribute pga
                  on
                   pga.attrelid = i.oid 
                inner join pg_indexes pgidx
                    on pgidx.indexname=i.relname
                 where 
                   t.relkind = 'r' 
                and pgidx.schemaname='asit_cm'
                 and t.relname ='accessory'
                 group by  t.relname,i.relname having count(*)=1
    

提交回复
热议问题