How do you find the disk size of a Postgres / PostgreSQL table and its indexes

前端 未结 9 2171
甜味超标
甜味超标 2020-12-02 03:42

I\'m coming to Postgres from Oracle and looking for a way to find the table and index size in terms of bytes/MB/GB/etc, or even better the size for all tables.

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 04:29

    Try the Database Object Size Functions. An example:

    SELECT pg_size_pretty(pg_total_relation_size('"".""'));
    

    For all tables, something along the lines of:

    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;
    

    Edit: Here's the query submitted by @phord, for convenience:

    SELECT
        table_name,
        pg_size_pretty(table_size) AS table_size,
        pg_size_pretty(indexes_size) AS indexes_size,
        pg_size_pretty(total_size) AS total_size
    FROM (
        SELECT
            table_name,
            pg_table_size(table_name) AS table_size,
            pg_indexes_size(table_name) AS indexes_size,
            pg_total_relation_size(table_name) AS total_size
        FROM (
            SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
            FROM information_schema.tables
        ) AS all_tables
        ORDER BY total_size DESC
    ) AS pretty_sizes;
    

    I've modified it slightly to use pg_table_size() to include metadata and make the sizes add up.

    提交回复
    热议问题