Is there a way to add a non-null type column to a Postgresql table without doubling its size on disk? If, for example, I have a table with specific columns and I want to add a column, I would do the following:
alter table my_table add new_column int
update table my_table set new_column = 0
alter table my_table alter column new_column set not null
This, in fact, doubles the space that is allocated for the table due to how Postgresql works. Updates create new tuples that will be marked for reuse after completion of this transaction, and the vacuum does its job. If the table is large (i.e., several million rows) but grows very slowly or is almost constant in size, these rows will never be reused, and only “full vacuum” or full database backup and restore will free up space on disk. Is there a way to automatically add a column with some default value, but without this behavior? For example, if there was a way to lock the table and perform an update, then in this case there would be no need for MVCC.
source
share