33장. 실제 무중단 Migration
OrderPulse에 risk_score NOT NULL column을 추가한다. 1단계는 nullable column 추가와 새 코드 dual compatibility, 2단계는 작은 batch backfill, 3단계는 NOT VALID constraint와 validate, 4단계는 read 전환, 5단계는 old path 제거다.
set lock_timeout='2s';
alter table orders add column risk_score smallint;
alter table orders add constraint orders_risk_score_ck
check (risk_score between 0 and 100) not valid;
-- keyset batch로 잠금과 WAL을 제어한다.
update orders set risk_score=0
where id in (select id from orders where risk_score is null order by id limit 5000);
alter table orders validate constraint orders_risk_score_ck;
배포 순서는 DB와 app의 backward/forward compatibility matrix로 검증한다. rollback이 코드만 되는지, schema·data도 돌릴 수 있는지 구분한다. 파괴적 drop은 최소 한 release 뒤에 한다.