9.6
with tmp_table as (
select pc.relowner,
pu.usename as relownername,
pc.relnamespace,
pn.nspname as relnamespacename,
pc.reltablespace,
pts.spcname as reltablespacename,
pc.oid as taboid,
pc.relname,
pc.relfilenode,
pc.relpages,
pc.reltuples,
pc.reltoastrelid
from pg_class pc
left outer join pg_user pu
on pc.relowner = pu.usesysid
left outer join pg_namespace pn
on pc.relnamespace = pn.oid
left outer join pg_tablespace pts
on pc.reltablespace = pts.oid
where 1=1
and pc.relkind in ('r')
-- r = 普通表,i = 索引, S = 序列,v = 视图, m = 物化视图, c = 组合类型,t = TOAST表, f = 外部表
),tmp_index as (
select pc.relowner,
pu.usename as relownername,
pc.relnamespace,
pn.nspname as relnamespacename,
pc.reltablespace,
pts.spcname as reltablespacename,
pin.indrelid as taboid,
--pc.oid as idxoid,
pin.indexrelid as idxoid,
pin.indisvalid,
pa.oid as amoid,
pa.amname,
pa.amhandler,
pa.amtype,
pc.relname,
pc.relfilenode,
pc.relpages,
pc.reltuples
from pg_index pin
left outer join pg_class pc
on pc.oid=pin.indexrelid
left outer join pg_am pa
on pc.relam=pa.oid
left outer join pg_user pu
on pc.relowner = pu.usesysid
left outer join pg_namespace pn
on pc.relnamespace = pn.oid
left outer join pg_tablespace pts
on pc.reltablespace = pts.oid
where 1=1
and pc.relkind = 'i'
),tmp_database as (
select pdb.datdba,pu.usename,
pdb.oid as datid,pdb.datname,
pts.oid as default_spcid ,pts.spcname as default_spcname,
case when pdb.datname = current_database() then 0
else pts.oid
end default_spcid2
from pg_database pdb
left outer join pg_tablespace pts
on pdb.dattablespace= pts.oid
left outer join pg_user pu
on pdb.datdba = pu.usesysid
where 1=1
and pdb.datname in ( select current_database() )
)
select td.datdba as database_owner_id,
td.usename as database_owner_name,
td.datid as database_id,
td.datname as database_name,
td.default_spcid as database_default_spcid,
td.default_spcname as database_default_spcname,
pg_database_size(td.datid) as database_size,
round(pg_database_size(td.datid)/1024/1024.0,2) as database_size_mb,
'###############'::varchar as flag_database_table,
--tt.*,
tt.relowner as table_owner_id,
tt.relownername as table_owner_name,
tt.relnamespace as table_namespace_id,
tt.relnamespacename as table_namespace_name,
tt.taboid as table_id,
tt.relname as table_name,
tt.reltablespace as table_tablespace_id,
tt.reltablespacename as table_tablespace_name,
tt.relfilenode as table_file_node,
pg_relation_filepath(tt.taboid) as table_file_path,
tt.relpages as table_pages,
tt.reltuples as table_typles,
pg_relation_size(tt.taboid) as table_size,
round(pg_relation_size(tt.taboid)/1024/1024.0,2) as table_size_mb,
'###############'::varchar as flag_table_index,
ti.relowner as index_owner_id,
ti.relownername as index_owner_name,
ti.relnamespace as index_namespace_id,
ti.relnamespacename as index_namespace_name,
ti.idxoid as index_id,
ti.relname as index_name,
ti.reltablespace as index_tablespace_id,
ti.reltablespacename as index_tablespace_name,
ti.indisvalid as index_is_valid,
ti.amname as index_type,
tt.relfilenode as index_file_node,
pg_relation_filepath(ti.idxoid) as index_file_path,
tt.relpages as index_pages,
tt.reltuples as index_typles,
pg_get_indexdef(ti.idxoid) as index_def,
pg_relation_size(ti.idxoid) as index_size,
round(pg_relation_size(ti.idxoid)/1024/1024.0,2) as index_size_mb
from tmp_database td
left outer join tmp_table tt
on 1=1
left outer join tmp_index ti
on tt.taboid=ti.taboid
where 1=1
and tt.relnamespacename not in ('pg_catalog','information_schema')
and tt.relnamespacename not like 'pg_toast%'
order by tt.relnamespace,
tt.relpages desc,
ti.relpages desc
;