tags:

views:

36

answers:

2

Since Postgres can only add columns at the end of tables, I end up re-ordering by adding new columns at the end of the table, setting them equal to existing columns, and then dropping the original columns.

So, what does PostgreSQL do with the memory that's freed by dropped columns? Does it automatically re-use the memory, so a single record consumes the same amount of space as it did before? But that would require a re-write of the whole table, so to avoid that, does it just keep a bunch of blank space around in each record?

Thanks!

~S

+2  A: 

From the docs:

The DROP COLUMN form does not physically remove the column, but simply makes it invisible to SQL operations. Subsequent insert and update operations in the table will store a null value for the column. Thus, dropping a column is quick but it will not immediately reduce the on-disk size of your table, as the space occupied by the dropped column is not reclaimed. The space will be reclaimed over time as existing rows are updated.

You'll need to do a CLUSTER followed by a VACUUM FULL to reclaim the space.

Quassnoi
VACUUM FULL will only reclaim the space if every row is updated, it cleans up after completely dead rows--not dead columns in otherwise good rows. Since the UPDATE that sets the new column must happen before doing the DROP, the space taken up by the now obsolete column won't be reclaimed by it. Only the old, original copy of the row will be cleaned up.Besides a total UPDATE, you could execute CLUSTER (in PostgreSQL 8.3 or later) or create a whole new copy of the table (something like CREATE TABLE AS) and shuffle the names around to actually get rid of the space taken up by dead columns.
Greg Smith
@Greg: nice point.
Quassnoi
+2  A: 

Why do you "reorder" ? There is no order in SQL, it doesn't make sence. If you need a fixed order, tell your queries what order you need or use a view, that's what views are made for.

Diskspace will be used again after vacuum, auto_vacuum will do the job. Unless you disabled this process.

Your current approach will kill overall performance (table locks), indexes have to be recreated, statistics go down the toilet, etc. etc. And in the end, you end up with the same situation you allready had. So why the effort?

Frank Heikens
I was expecting this to come up. I like my columns to be in an order that makes sense to humans, like me. As long as it doesn't take up too much disk space (which it won't after a vacuum full). Just personal preference. :)
Summer
You shouldn't be relying on implicit column ordering, just like you shouldn't rely on implicit row ordering in queries without ORDER BY. Does this mean your applications use SELECT * and INSERT INTO table VALUES, without field names? Because that would be just asking for trouble. If you want a specific column order, create a view with that order.
MkV