10

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?

Matt
  • 121
  • 1
  • 5
Beautiful Mind
  • 5,828
  • 4
  • 23
  • 42

3 Answers3

9

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));
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • 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." https://www.postgresql.org/docs/9.6/app-vacuumdb.html – Paul Holden Dec 20 '21 at 20:41
  • 1
    @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. – Erwin Brandstetter Dec 21 '21 at 11:01
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

MichaelStoner
  • 889
  • 10
  • 26
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.

jjanes
  • 37,812
  • 5
  • 27
  • 34
  • 1
    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. – Beautiful Mind Sep 24 '13 at 23:24
  • 6
    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. – jjanes Sep 25 '13 at 02:54