S M Shamimul Hasan
S M Shamimul Hasan

Reputation: 6694

PostgreSQL drop table command is not reducing DB size

I dropped a couple of tables from my Postgres database. However, before dropping tables the size of the database was 6586kB, and after dropping the table size of the database remains same. I think size should be reduced.

What do I need to do to get the actual size?

I know about the VACUUM command. Do I need to use that? And how?

Upvotes: 12

Views: 18379

Answers (3)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 659367

VACUUM (or VACUUM FULL) is hardly useful in this case, since it only reclaims space from within tables. Neither is required after dropping a table. Each table (and index) is stored as separate file in the operating system, which is deleted along with the table. So space is reclaimed almost instantly.

Well, there are entries in catalog tables that would leave dead tuples behind after dropping a table. So the database can occupy slightly more space after a table has been created and dropped again.

To get a db down to minimum size again, run (as privileged user):

VACUUM FULL;

Without a list of tables, all accessible tables are rewritten to pristine condition. (Not advisable for a big database with concurrent load, as it takes exclusive locks!)

Or the client tool vacuumdb with the --full option:

vacuumdb -f mydb

This also rewrites system catalogs (also tables) and indices in pristine condition. Details in the Postgres Wiki:

Postgres has dedicated object size functions to measure db size:

SELECT pg_size_pretty(pg_database_size(mydb));

Upvotes: 10

Michael Stoner
Michael Stoner

Reputation: 902

Use truncate, understand the warning about MVCC first

TRUNCATE quickly removes all rows from a set of tables. It has the same effect as an unqualified DELETE on each table, but since it does not actually scan the tables it is faster. Furthermore, it reclaims disk space immediately, rather than requiring a subsequent VACUUM operation. This is most useful on large tables.

http://www.postgresql.org/docs/9.1/static/sql-truncate.html

Upvotes: 7

jjanes
jjanes

Reputation: 44423

6586KB is about the size of an "empty" (freshly created) database. If the tables you dropped were very small or empty, dropping them will not reduce the size by much if any.

When I drop a populated large table, the reduction in database size (as seen by psql's "\l+" command) is reflected near instantaneously, with no need for a vacuum or a checkpoint.

Upvotes: 4

Related Questions