12

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?

3
  • 1
    What's with all the tags? Commented Sep 24, 2013 at 22:33
  • 2
    What's your actual version? Please fix the tags. How do you measure size? Provide more details please. Commented Sep 24, 2013 at 23:28
  • I measure the size through select pg_size_pretty(pg_database_size(databaseName)); command Commented Sep 24, 2013 at 23:38

3 Answers 3

10

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));
Sign up to request clarification or add additional context in comments.

2 Comments

I believe vacuumdb is effectively the same as running vacuum by other means. From the documentation: "vacuumdb is a wrapper around the SQL command VACUUM. There is no effective difference between vacuuming and analyzing databases via this utility and via other methods for accessing the server." postgresql.org/docs/9.6/app-vacuumdb.html
@PaulHolden: Yes, all true. My point was that the only lasting effect on database size could be entries in system catalogs. vacuumdb -f (or just VACUUM FULL) cleans that up - as opposed to just VACUUM or even VACUUM FULL on the table. That's how you get the db down to minimum size, the actual objective of the OP. I clarified.
7

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

1 Comment

The OP did not DELETE FROM, but DROP the table. TRUNCATE cannot help to reclaim more space there.
4

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.

2 Comments

6586kB is not empty DB size. Empty DB size is 6562kB. I need to know the data growth. That's why I need original size after dropping tables.
The exact size depends on what options it was compiled with, what hardware, etc. Is 34kb really something to worry about? It is a rounding error. If you are trying to figure out what is going on, use a table large enough to matter. If you try to learn based on the smallest possible data size, you will find your knowledge does not extrapolate to reality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.