Postgresql – Postgres – how much space is required to perform a VACUUM

performancepostgresqlpostgresql-performance

I know that doing a VACUUM FULL on a table will free up disk space and return it to the OS.

There's been a couple of occasions where my OS (windows) has warned me that it's running low on disc space. My response has been to quickly shut down everything I can and run a VACUUM FULL on one of the more active tables.

I've noticed that VACUUM FULL appears to require a least a few hundred Mbytes free to actually work. If I attempt a VACCUM FULL on a 1Gbyte table when I've only got say 50 Mbytes of disk space then this quickly disappears and my computer grounds to a halt pretty quickly and the VACUUM FULL won't work.

How do I free OS space in a POSTGRES database when I know there is unused space available, but there's not enough space to run a VACUUM?

(I know the correct answer is – 'don't be stupid enough to let it happen in the first place', but sh1t happens occasionally!)

Best Answer

how much space is required to perform a VACUUM

At worst pg_total_relation_size(table) * 2. Usually lots less because the new table and indexes are significantly more compact.

How do I free OS space in a POSTGRES database when I know there is unused space available, but there's not enough space to run a VACUUM?

Clean up unrelated files on the volume. Drop some tables or indexes you don't need. Move tables or indexes to other tablespaces on other storage. Expand the underlying volume.

There isn't really a good way to compact in-place and free space to the OS without first needing to allocate more space. I think most people agree this is a flaw, but nobody seems to care enough to develop any improvement for it.