postgresql列表和按大小sorting表

有没有简单的方法来列出PostgreSQL数据库中的所有表格,并按大小sorting?

伪代码

SELECT * FROM tables ORDER by tables.size 

我正在使用PostgreSQL 9.3.2

 select table_name, pg_relation_size(quote_ident(table_name)) from information_schema.tables where table_schema = 'public' order by 2 

如果您有多个模式,这将向您显示架构public中所有表的大小,您可能需要使用:

 select table_schema, table_name, pg_relation_size('"'||table_schema||'"."'||table_name||'"') from information_schema.tables order by 3 

SQLFiddle示例: http ://sqlfiddle.com/#!15/13157/3

手册中所有对象尺寸函数的列表:
https://www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE

这将显示模式名称,表名称,大小漂亮和大小(sorting所需)。

 SELECT schema_name, relname, pg_size_pretty(table_size) AS size, table_size FROM ( SELECT pg_catalog.pg_namespace.nspname AS schema_name, relname, pg_relation_size(pg_catalog.pg_class.oid) AS table_size FROM pg_catalog.pg_class JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid ) t WHERE schema_name NOT LIKE 'pg_%' ORDER BY table_size DESC; 

我build立这个基于从这里的解决scheme列表的大小(相对和绝对)在PostgreSQL数据库中的解决scheme

 select table_name,n_live_tup, pg_size_pretty(pg_relation_size(table_name)) from information_schema.tables inner join pg_stat_user_tables on table_name=relname where table_schema = 'public' order by 2 desc 

另一种select

  select uv.a tablename, pg_size_pretty(uv.b) sizepretty from (select tb.tablename a, pg_table_size('schemaname.'||tb.tablename::text) b from pg_tables tb where tb.schemaname ilike 'schemaname' order by 2 desc) uv 
 SELECT relname as "Table", pg_size_pretty(pg_total_relation_size(relid)) As "Size", pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as "External Size" FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC; 

取自https://wiki-bsse.ethz.ch/display/ITDOC/Check+size+of+tables+and+objects+in+PostgreSQL+database