PostgreSQL查询索引及字段详情(终极版)-v2

-- PostgreSQL查询索引及字段详情(终极版)-v2
with idx as (
select tc.relname as tablename  -- 表名
      ,c.relname indexname     -- 对象名
      --,i.indkey
      --,array_length(i.indkey, 1) as array_length
      --,i.indkey[0] as field1
      ,attr1.attname as field1
      --,i.indkey[1] as field2
      ,attr2.attname as field2
      --,i.indkey[2] as field3
      ,attr3.attname as field3
      --,i.indkey[3] as field4
      ,attr4.attname as field4
      --,i.indkey[4] as field5
      ,attr5.attname as field5
      --,i.indkey[5] as field6
      ,attr6.attname as field6
      --,i.indkey[6] as field7
      ,attr7.attname as field7
      -- ,i.* 
  from pg_index i
  left join pg_class c
    on i.indexrelid = c.oid
  left join pg_class tc
    on i.indrelid = tc.oid
  left join pg_attribute attr1
    on attr1.attrelid = tc.oid
   and attr1.attnum = i.indkey[0]
  left join pg_attribute attr2
    on attr2.attrelid = tc.oid
   and attr2.attnum = i.indkey[1]
  left join pg_attribute attr3
    on attr3.attrelid = tc.oid
   and attr3.attnum = i.indkey[2]
  left join pg_attribute attr4
    on attr4.attrelid = tc.oid
   and attr4.attnum = i.indkey[3]
  left join pg_attribute attr5
    on attr5.attrelid = tc.oid
   and attr5.attnum = i.indkey[4]
  left join pg_attribute attr6
    on attr6.attrelid = tc.oid
   and attr6.attnum = i.indkey[5]
  left join pg_attribute attr7
    on attr7.attrelid = tc.oid
   and attr7.attnum = i.indkey[6]
 where not tc.relname ~ '[0-9]{4}$'
   and tc.relname not like '%act\_%'
   and tc.relname not like '%\_other'
   and tc.relname not like '%\_others'
   and tc.relname not like '%\_old'
   and (tc.relname like '%\_t' or 
        tc.relname like '%\_ti' or 
        tc.relname like '%\_tmp')
   and tc.relowner = (select usesysid from pg_user where usename = CURRENT_SCHEMA)
   and indisunique = 't'  -- 唯一索引
   and indisprimary = 'f' -- 索引是唯一一个字段,是否是表的主键
   and c.relname not like '%\_pkey'
order by tablename,indexname
)
,idxs as (
select * from pg_indexes
 where schemaname = CURRENT_SCHEMA
   and indexdef like '%UNIQUE%'
   and indexname not like '%\_pkey'
   and not tablename ~ '[0-9]{4}$'
   and tablename not like 'act\_%'
   and tablename not like '%\_other'
   and tablename not like '%\_others'
   and tablename not like '%\_old'
   and (tablename like '%\_t' or 
        tablename like '%\_ti' or 
        tablename like '%\_tmp')
)
,index_info as (
select idx.tablename
      ,idx.indexname
      ,idx.field1,idx.field2,idx.field3,idx.field4,idx.field5,idx.field6,idx.field7
      ,case 
         when position(' WHERE ' in idxs.indexdef)>0 
           then replace(replace(replace(
                  substring(idxs.indexdef from (position(' WHERE ' in idxs.indexdef)+7) for length(idxs.indexdef))
                ,'(',''),')',''),'::text','')
         else null
       end as where_condition
      ,idxs.indexdef
  from idx
  left join idxs
    on idx.indexname = idxs.indexname
)
,table_info as (
select tablename
  from pg_tables 
 where schemaname = CURRENT_SCHEMA
   and not tablename ~ '[0-9]{4}$'
   and tablename not like 'act\_%'
   and tablename not like '%\_other'
   and tablename not like '%\_others'
   and tablename not like '%\_old'
   and (tablename like '%\_t' or 
        tablename like '%\_ti' or 
        tablename like '%\_tmp')
   and tablename not in ('accmc_company_account_t','tmc_ins_apwf_invoice_map_tmp')
 order by tablename
)
select * from index_info
union all 
select t.tablename,'','','','','','','','','',''
  from table_info t
 where not exists (
   select 1 from index_info i
    where t.tablename = i.tablename)
order by 1
;

你可能感兴趣的:(PostgreSQL,postgresql,数据库)