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?
The DROP TABLE operation removes the table definition and data as well as the indexes, constraints, and triggers related to the table. This command frees the memory space. No triggers are fired when executing DROP TABLE . This operation cannot be rolled back in MySQL, but it can in Oracle, SQL Server, and PostgreSQL.
Using the option -f or –force with dropdb command or FORCE with DROP DATABASE to drop the database, it will terminate all existing connections with the database. Similarly, DROP DATABASE FORCE will do the same. In the first terminal, create a test database and a database test, and connect to the database.
Dropping a table will free up the space within the database, but will not release the space back to Windows. That requires shrinking the database file.
DROP TABLE removes tables from the database. Only the table owner, the schema owner, and superuser can drop a table. To empty a table of rows without destroying the table, use DELETE or TRUNCATE . DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table.
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));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With