Implementing Breath First Search algorithm in Postgres for fast cycle detection