Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
Beautiful Mind Avatar asked Sep 24 '13 22:09

Beautiful Mind


People also ask

Does DROP TABLE free up space Postgres?

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.

How do you force delete a DB in Postgres?

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.

Does DROP TABLE clear space?

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.

Does dropping a table drop the index Postgres?

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.


2 Answers

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:

  • http://wiki.postgresql.org/wiki/VACUUM_FULL

Postgres has dedicated object size functions to measure db size:

SELECT pg_size_pretty(pg_database_size(mydb));
like image 90
Erwin Brandstetter Avatar answered Sep 29 '22 20:09

Erwin Brandstetter


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

like image 26
MichaelStoner Avatar answered Sep 29 '22 21:09

MichaelStoner