postgresql list and order tables by size

2019-01-21 15:43发布

问题:

Is there an easy way to list all tables from PostgreSQL database and order them by size?

Pseudo-code

SELECT * FROM tables
ORDER by tables.size

I am using PostgreSQL 9.3.2.

回答1:

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

This shows you the size of all tables in the schema public if you have multiple schemas, you might want to use:

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

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

List of all object size functions in the manual:
https://www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADMIN-DBSIZE



回答2:

This will show you the schema name, table name, size pretty and size (needed for sort).

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;

I build this based on the solutions from here list of schema with sizes (relative and absolute) in a PostgreSQL database



回答3:

This will be more clear.

pg_size_pretty(<numeric_value>) - converts no.of bytes to human-readable format.

pg_database_size(<db_name>) - gets database size in bytes.

pg_total_relation_size(<relation_name>) - gets total size of table and its index in bytes.

pg_relation_size(<relation_name>) - gets relation size in bytes.

pg_index_size(<relation_name>) - gets index size of the relation in bytes.

current_database() - gets the currently used database on which this query is being performed.

Query:

select current_database() as database,
       pg_size_pretty(total_database_size) as total_database_size,
       table_name,
       pg_size_pretty(total_table_size) as total_table_size,
       pg_size_pretty(table_size) as table_size,
       pg_size_pretty(index_size) as index_size
       from ( select table_name,
                pg_database_size('vigneshdb') as total_database_size,
                pg_total_relation_size(table_name) as total_table_size,
                pg_relation_size(table_name) as table_size,
                pg_indexes_size(table_name) as index_size
                from information_schema.tables where table_schema='vigneshschema'
            ) as sizes
       order by table_name;

Result:

database   | total_database_size |    table_name   | total_table_size | table_size | index_size
-----------+---------------------+-----------------+------------------+------------+------------
 vigneshdb | 1586 MB             | aaaaaaaaaaaaaaa | 16 kB            | 0 bytes    | 8192 bytes
 vigneshdb | 1586 MB             | bbbbbbbbbbbbbbb | 24 kB            | 0 bytes    | 16 kB
 vigneshdb | 1586 MB             | ccccccccccccccc | 640 kB           | 112 kB     | 488 kB
 vigneshdb | 1586 MB             | ddddddddddddddd | 9760 kB          | 3152 kB    | 6568 kB
 vigneshdb | 1586 MB             | eeeeeeeeeeeeeee | 1120 MB          | 311 MB     | 808 MB

The humanized format is represent in bytes, kB, MB, GB, and TB.

bytes to kB - begins from 10240 bytes

bytes to MB - begins from 10485248 bytes = 10239.5 kB ~ 10 MB

bytes to GB - begins from 10736893952 bytes = 10239.5 MB ~ 10 BG

bytes to TB - begins from 10994579406848 bytes = 10239.5 GB ~ 10 TB

All unit conversions starts from 10 + <unit>.

For reference - Postgres Official Documentation



回答4:

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;

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



回答5:

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

Another alternative



回答6:

 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