I experienced a storage issue and then found that my database was taking up 300GB using:
SELECT pg_size_pretty(pg_database_size(current_database()))
Looking at the sum size of my schemas for this database, it only takes up 126 GB, which is 42%:
SELECT
pg_size_pretty(sum(total_size)::bigint) as "total",
pg_size_pretty(sum(relation_size_main)::bigint) as "main",
pg_size_pretty(sum(relation_size_fsm)::bigint) as "fsm",
pg_size_pretty(sum(relation_size_vm)::bigint) as "vm",
pg_size_pretty(sum(relation_size_init)::bigint) as "init",
pg_size_pretty(sum(table_size)::bigint) as "table",
pg_size_pretty(sum(external_size)::bigint) as "external_size",
(sum(total_size) / pg_database_size(current_database())) * 100 as percentage
FROM (
SELECT pg_catalog.pg_namespace.nspname as schema_name,
pg_total_relation_size(pg_catalog.pg_class.oid) as total_size,
pg_relation_size(pg_catalog.pg_class.oid, 'main') as relation_size_main,
pg_relation_size(pg_catalog.pg_class.oid, 'fsm') as relation_size_fsm,
pg_relation_size(pg_catalog.pg_class.oid, 'vm') as relation_size_vm,
pg_relation_size(pg_catalog.pg_class.oid, 'init') as relation_size_init,
pg_table_size(pg_catalog.pg_class.oid) as table_size,
pg_total_relation_size(pg_catalog.pg_class.oid) - pg_relation_size(pg_catalog.pg_class.oid) as external_size
FROM pg_catalog.pg_class
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
) t;
Which returns:
"total","main","fsm","vm","init","table","external_size","percentage"
"126 GB","97 GB","63 MB","17 MB","0 bytes","99 GB","29 GB",41.91348045761441753100
So what takes up the other 58%?
NOTE: Doing a pg_dump
, clearing the /var/lib/postgresql/data/
directory, restarting the server and reloading the SQL dump does solve this problem. But this is not a valid production solution.
Additional Info: I am using the Postgres Docker container and have stopped it multiple times in the past few months since it was crippling my laptop.
Credit to this post and this site for helping with table size extraction.