Performance

更快的 ALTER COLUMN

  • July 10, 2020

有沒有一種方法可以在一張巨大的桌子上的 postgres 12 中執行 ALTER COLUMN 而無需等待一生?

我嘗試將欄位從 bigint 轉換為 smallint :

ALTER TABLE huge ALTER COLUMN result_code TYPE SMALLINT;

需要8個小時,有沒有更聰明的方法?該表具有序列、鍵和外鍵

Laurenz Albe在類似的文章中回答了這個問題

這是答案:

The table has to be rewritten, and you have to wait.

If you have several columns whose data type you want to change, you can use several ALTER COLUMN clauses in a single ALTER TABLE statement and save time that way.

An alternative idea would be to use logical replication: set up an empty copy of the database (pg_dump -s), where your large table is defined with smallint columns. Replicate your database to that database, and switch over as soon as replication has caught up.

是問題

引用自:https://dba.stackexchange.com/questions/270627