Blocking Pattern (Avoid in Production)

ALTER TABLE payment_ledger ADD CONSTRAINT fk_ledger_cust
  FOREIGN KEY (customer_id) REFERENCES customers (id);

Zero-Downtime Safe Rewrite

CREATE INDEX idx_ledger_cust_fk ON payment_ledger (customer_id) ONLINE;
ALTER TABLE payment_ledger ADD CONSTRAINT fk_ledger_cust
  FOREIGN KEY (customer_id) REFERENCES customers (id) ENABLE NOVALIDATE;
ALTER TABLE payment_ledger MODIFY CONSTRAINT fk_ledger_cust VALIDATE;

Production Rollout Notes

  • The FK columns must be the leading columns of the child index.
  • ENABLE NOVALIDATE enforces the rule for all new DML immediately without scanning existing rows under an exclusive lock.