Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku Postgres database size doesn't go down after deleting rows

I'm using a Dev level database on Heroku that was about 63GB and approaching about 9.9 million rows (close to the limit of 10 million for this tier). I ran a script that deleted about 5 million rows I didn't need, and now (few days later) in the Postgres control panel/using pginfo:table-size it shows roughly 4.7 million rows but it's still at 63GB. 64 is the limit for he next tier so I need to reduce the size.

I've tried vacuuming but pginfo:bloat said the bloat was only about 3GB. Any idea what's happening here?

like image 443
nathan Avatar asked Oct 15 '25 22:10

nathan


1 Answers

If you have [vacuum][1]ed the table, don't worry about the size one disk still remaining unchanged. The space has been marked as reusable by new rows. So you can easily add another 4.7 million rows and the size on disk wont grow.

The standard form of VACUUM removes dead row versions in tables and indexes and marks the space available for future reuse. However, it will not return the space to the operating system, except in the special case where one or more pages at the end of a table become entirely free and an exclusive table lock can be easily obtained. In contrast, VACUUM FULL actively compacts tables by writing a complete new version of the table file with no dead space. This minimizes the size of the table, but can take a long time. It also requires extra disk space for the new copy of the table, until the operation completes.

If you want to shrink it on disk, you will need to VACUUM FULL which locks the tables and needs as much extra space as the size of the tables when the operation is in progress. So you will have to check your quota before you try this and your site will be unresponsive.

Update:
You can get a rough idea about the size of your data on disk by querying the pg_class table like this:

SELECT SUM(relpages*8192) from pg_class

Another method is a query of this nature:

SELECT pg_database_size('yourdbname');

This link: https://www.postgresql.org/docs/9.5/static/disk-usage.html provides additional information on disk usage.

like image 172
e4c5 Avatar answered Oct 17 '25 12:10

e4c5