Я, кажется, решил это следующим образом:
CREATE OR REPLACE FUNCTION validate_network_pricing_code() returns trigger as $$
DECLARE
tester int;
BEGIN
IF NEW.time_of_use = 'ANYTIME'
THEN tester := (SELECT COUNT(*) FROM network_pricing_category_code WHERE (
network_pricing_category_code.meter_register_code = NEW.meter_register_code AND
network_pricing_category_code.period_of_availability = NEW.period_of_availability AND
network_pricing_category_code.network_pricing_category_id = NEW.network_pricing_category_id AND
network_pricing_category_code.id != NEW.id
));
END IF;
IF NEW.time_of_use IS NOT NULL AND NEW.time_of_use != 'ANYTIME'
THEN tester := (SELECT COUNT(*) FROM network_pricing_category_code WHERE (
network_pricing_category_code.time_of_use = 'ANYTIME' AND
network_pricing_category_code.meter_register_code = NEW.meter_register_code AND
network_pricing_category_code.period_of_availability = NEW.period_of_availability AND
network_pricing_category_code.network_pricing_category_id = NEW.network_pricing_category_id AND
network_pricing_category_code.id != NEW.id
));
END IF;
IF (tester != 0) THEN
RAISE EXCEPTION 'ANYTIME rates cannot be combined with other types of rates - PEAK, SHOULDER, OFF-PEAK etc ';
END IF;
RETURN NEW;
END;
$$ language plpgsql;
CREATE TRIGGER validate_network_pricing_code_trigger BEFORE INSERT OR UPDATE ON network_pricing_category_code
FOR EACH ROW EXECUTE PROCEDURE validate_network_pricing_code();
Путем изменения записи я нашел здесь: Когда проверки недостаточно: триггеры PostgreSQL для целостности данных