-- V13 — Unique partial index: at most one open alert_instance per rule
-- Prevents duplicate FIRING rows in multi-replica deployments.
-- The Java save() path catches DuplicateKeyException and log-and-skips the losing insert.
CREATE UNIQUE INDEX alert_instances_open_rule_uq
  ON alert_instances (rule_id)
  WHERE rule_id IS NOT NULL
    AND state IN ('PENDING','FIRING','ACKNOWLEDGED');
