HAF crashes when new application is created concurently to execution of hive.set_irreversible

The problem is with hive.remove_unecessary_events and read committed isolation level. When a new application is adding its context it gets events_id=0 what may lead to breach constraint by query from hive.remove_unecessary_events:

DELETE FROM hive.events_queue heq
    USING ( SELECT MIN( hc.events_id) as id FROM hive.contexts hc ) as min_event
    WHERE ( heq.id < __upper_bound_events_id OR __upper_bound_events_id IS NULL )  AND ( heq.id < min_event.id OR min_event.id IS NULL ) AND heq.id != 0;

Explanation on the picture below: bug_remove_events_and_create_new_application_negative_only