← all rules

alter-column-type

error

ALTER COLUMN ... TYPE

Why it's unsafe

ALTER COLUMN ... TYPE usually rewrites the whole table and rebuilds its indexes under an ACCESS EXCLUSIVE lock. Even a metadata-only change that does not rewrite (widening a varchar/numeric/timestamp precision, or varchar->text) changes the column's result type and breaks cached query plans and prepared statements in live sessions ('cached plan must not change result type') until they re-plan.

Safe rewrite

Use expand/contract for a rewriting change: add a new column, dual-write and backfill in batches, then swap (some changes, e.g. int->bigint, always rewrite). A no-rewrite change (e.g. varchar->text or widening a varchar) avoids the table rewrite but still invalidates cached plans, so recycle pooled connections or run DISCARD PLANS afterward, or apply it during a deploy window.

Example

Try it in the playground →

Unsafe

ALTER TABLE events ALTER COLUMN id TYPE bigint;

Related