且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在大型数据集上的Postgres中删除列

更新时间:2023-01-31 10:08:01

ALTER TABLE DROP COLUMN仅禁用系统表中的列。速度非常快,但不会从堆文件中删除数据。您稍后必须进行VACUUM FULL压缩分配的文件空间。因此,ALTER TABLE DROP COLUMN非常快。而要压缩文件,您必须调用速度较慢(带有独占锁定)的VACUUM FULL。


So I have a table with a large dataset and this table has a three columns that I would like to drop.
The question is: how will Postgres deal with it?

Will it walk through every entry or will it just update mapping info without much overhead? Can I just make an ALTER TABLE or should I use swap-table in this particular case?

And, if it makes any difference, all three columns have fixed length (two integers and one numeric).

I'm sorry if it's been asked already, but Google couldn't find any related questions / articles ...

ALTER TABLE DROP COLUMN does just only disabling columns in system tables. It is very fast, but it doesn't remove data from heap files. You have to do VACUUM FULL later to compact allocated file space. So ALTER TABLE DROP COLUMN is very fast. And to compact files, you have to call the slower (with exclusive LOCK) VACUUM FULL.