Blocking Pattern (Avoid in Production)

ALTER TABLE billing.orders ALTER COLUMN region_code SET NOT NULL;

Zero-Downtime Safe Rewrite

SET lock_timeout = '2s';
ALTER TABLE billing.orders ADD CONSTRAINT chk_orders_region_nn CHECK (region_code IS NOT NULL) NOT VALID;
ALTER TABLE billing.orders VALIDATE CONSTRAINT chk_orders_region_nn;
ALTER TABLE billing.orders ALTER COLUMN region_code SET NOT NULL;
ALTER TABLE billing.orders DROP CONSTRAINT chk_orders_region_nn;

Production Rollout Notes

  • This pattern prevents multi-minute read/write outages on multi-gigabyte tables.
  • Always wrap the brief ACCESS EXCLUSIVE steps in SET lock_timeout.