查询表信息
使用系统表pg_tables查询数据库所有表的信息
select * from pg_tables;
-- 查看某个schema下的表信息
select* from pg_tables where schemaname = 'shemaname'
使用函数pg_get_tabledef查询表定义
select * from pg_get_tabledef('shemaname.tablename')
查询表是否做过表分析,执行如下命令会返回每个表最近一次做analyze的时间,没有返回时间的则表示没有做过analyze
select pg_stat_get_last_analyze_time(oid),relname from pg_class where relkind='r';
查询public下的表做表分析的时间
select pg_stat_get_last_analyze_time(c.oid),c.relname
from pg_class c
left join pg_namespace n on c.relnamespace = n.oid
where c.relkind='r' and n.nspname='public'
;
查询表大小
查询表的总大小(包含表的索引和数据)
select pg_size_pretty(pg_total_relation_size('schemaname.tablename'));
查询表的数据大小(不包括索引)
select pg_size_pretty(pg_relation_size('schemaname.tablename'));
查询系统中所有表占用空间大小排行
select table_schema || '.' || table_name as table_full_name
,pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) as size
from information_schema.tables
order by pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') desc
limit xx;
-- 查看某个schema下表占用空间大小
select table_schema || '.' || table_name as table_full_name
,pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) as size
from information_schema.tables
where table_schema = 'schemaname'
order by pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') desc
limit xx;
查询数据库
通过系统表pg_database查询数据库列表
select datname from pg_database;
查询数据库大小
查询数据库的大小
select datname,pg_size_pretty(pg_database_size(datname)) from pg_database;