转: postgresql 查看单个表大小

分享到:

原文链接:https://blog.csdn.net/kmust20093211/article/details/47616345

方法一 ,查某个表

1select pg_size_pretty(pg_relation_size('table_name'));

方法二 ,查出所有表并按大小排序

1SELECT 
2table_schema || '.' || table_name 
3AS table_full_name, pg_size_pretty(pg_total_relation_size('"' ||table_schema || '"."' || table_name || '"')) AS size
4FROM 
5information_schema.tables
6ORDER BY
7    pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')
8DESC limit 20;

方法三,查出所有表按大小排序并分离data与index

 1SELECT
 2    table_name,
 3    pg_size_pretty(table_size) AS table_size,
 4    pg_size_pretty(indexes_size) AS indexes_size,
 5    pg_size_pretty(total_size) AS total_size
 6FROM (
 7    SELECT
 8        table_name,
 9        pg_table_size(table_name) AS table_size,
10        pg_indexes_size(table_name) AS indexes_size,
11        pg_total_relation_size(table_name) AS total_size
12    FROM (
13        SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
14        FROM information_schema.tables
15    ) AS all_tables
16    ORDER BY total_size DESC
17) AS pretty_sizes;