delete rows and get space postgresql delete rows and get space postgresql postgresql postgresql

delete rows and get space postgresql


autovacuum should be running in any modern Postgres database unless you know exactly what you are doing. But autovacuum is not going to shrink tables except for rare corner cases. It schedules periodic VACUUM and ANALYZE jobs, but never VACUUM FULL, which acquires an exclusive lock on processed tables and indexes. VACUUM helps to avoid table and index bloat, though, by cleaning up dead tuples (along with various other good deeds) and thereby making space available to future write operations on the same object - not returning space to the OS, yet.

You write:

tried to execute 'VACUUM FULL;' but that consumed my whole disk space.

You mean temporarily? Because that's what VACUUM FULL does. But when it's done, tables and associated indexes are reduced to their minimum size - which is not generally desirable, unless you urgently need the disk space or rows are never updated.

So VACUUM FULL is the right tool for you, unless you are out of disk space already - which should best not occur to begin with. So you need to create some wiggle room to let it work its magic.

There is also the community tool pg_repack, which can do the same as VACUUM FULL without exclusive locks. But it also needs some free disk space to work. Related answer with more details:


To delete all rows of a table and immediately release disk space, use TRUNCATE. It effectively writes a new file on disk and deletes the old. That's also why it typically can't be used with concurrent load on a DB.

TRUNCATE table_name;


VACUUM FULL works by creating a new version of each table and copying all the data. An old table isn't deleted until the new table is complete, so a VACUUM FULL operation will temporarily increase disk space while it is processing. See this note in the PostgreSQL documentation (emphasis mine):

Tip: Plain VACUUM may not be satisfactory when a table contains large numbers of dead row versions as a result of massive update or delete activity. If you have such a table and you need to reclaim the excess disk space it occupies, you will need to use VACUUM FULL, or alternatively CLUSTER or one of the table-rewriting variants of ALTER TABLE. These commands rewrite an entire new copy of the table and build new indexes for it. All these options require exclusive lock. Note that they also temporarily use extra disk space approximately equal to the size of the table, since the old copies of the table and indexes can't be released until the new ones are complete.

A regular VACUUM, on the other hand, just prunes deleted rows and index entries from the database file.

You may have more success if you first run a regular VACUUM and then try running a VACUUM FULL. You may also have more success if you try running VACUUM FULL on specific tables. If you run them in the right order, you might be able to recover enough disk space that the remaining tables in the database can run VACUUM FULL. Otherwise, you will need to find some way of increasing the available disk space so VACUUM FULL can complete.