Question

Is there a method to do an ALTER COLUMN in postgres 12 on an huge table without waiting a lifetime?

I try to convert a field from bigint to smallint :

ALTER TABLE huge ALTER COLUMN result_code TYPE SMALLINT;

It takes 8 hours, is there a smarter method? The table has sequences, keys and foreign keys

Was it helpful?

Solution

Laurenz Albe answered this question on a similar post

this is the answer:

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.

here is the question

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top