
Which ALTER TABLE Operations Lock Your PostgreSQL Table?
ALTER TABLE is not a single operation. PostgreSQL has dozens of ALTER TABLE sub-commands, and they acquire different lock levels. Some are instant and harmless. Others lock your entire table and block all traffic. This is the complete reference. ACCESS EXCLUSIVE Operations (Block Everything) These ALTER TABLE operations acquire ACCESS EXCLUSIVE, which blocks all reads and writes on the table. On a busy production table, these are the operations most likely to cause outages. ADD COLUMN with volatile DEFAULT -- Rewrites the entire table (all PostgreSQL versions) ALTER TABLE users ADD COLUMN request_id UUID DEFAULT gen_random_uuid(); -- Safe alternative: three-step pattern ALTER TABLE users ADD COLUMN request_id UUID; ALTER TABLE users ALTER COLUMN request_id SET DEFAULT gen_random_uuid(); -- Backfill in batches... On PostgreSQL 11+, non-volatile defaults (constants like 'active' , 0 , false ) are instant and do not rewrite the table. Volatile defaults ( now() , gen_random_uuid() , random
Continue reading on Dev.to
Opens in a new tab



