Sphere
Log in Join
☆ 15

Adding a column with a default to a big table

ALTER TABLE events ADD COLUMN flagged boolean NOT NULL DEFAULT false on a 30M row table. Will this lock the table?

⚑ Report

3 replies

☆ 0
J Jordan ·2026-11-03#1

On Postgres 11+ a constant default is mostly metadata only, but older versions rewrite the table. A volatile default always rewrites.

⚑ Report
☆ 0
O Oskar ·2026-11-03#2

We are on Postgres 12 but the migration still seemed slow.

⚑ Report
☆ 0
J Jules R. ·2026-11-04#3

Check for a long-running transaction holding a lock on the table. Add the column as nullable first, backfill in batches, then set NOT NULL.

⚑ Report
Log in to reply.