BEGIN; CREATE TABLE external_dependency_probe_state ( service_name varchar(32) NOT NULL CHECK (service_name ~ '^[a-z][a-z0-9-]{1,31}$'), dependency varchar(64) NOT NULL CHECK (dependency ~ '^[a-z][a-z0-9_]{1,63}$'), status varchar(16) NOT NULL CHECK (status IN ('AVAILABLE','UNAVAILABLE')), checked_at timestamptz NOT NULL, last_success_at timestamptz, last_failure_at timestamptz, last_error_code varchar(80), latency_ms bigint NOT NULL CHECK (latency_ms >= 0), probe_interval_ms bigint NOT NULL CHECK (probe_interval_ms > 0), consecutive_failures integer NOT NULL DEFAULT 0 CHECK (consecutive_failures >= 0), updated_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (service_name, dependency), CONSTRAINT external_probe_success_check CHECK ( (status='AVAILABLE' AND last_success_at IS NOT NULL AND last_error_code IS NULL AND consecutive_failures=0) OR (status='UNAVAILABLE' AND last_failure_at IS NOT NULL AND last_error_code IS NOT NULL AND consecutive_failures>0) ) ); CREATE INDEX external_dependency_probe_status_idx ON external_dependency_probe_state(status, checked_at DESC); COMMIT;