r/foojay • u/FrankCodeWriter • 6d ago
When Postgres is the wrong place to traverse the graph
We at Foojay published an article by Andrey Minogin on deep graph searches, like finding level-N friends or tier-N parts in a bill of materials. Recursive CTEs in Postgres hit the O(b^D) wall as branching factor and depth grow, and Postgres cannot do a true depth-first search, so the frontier keeps growing in memory.
The article explains moving the traversal into application memory: a depth-first search over an in-memory graph using primitive collections (HPPC) instead of boxed Java types, which can cut memory use up to 10 times. It also covers early filtering, visited-sets, and concurrent updates.
https://foojay.io/today/when-postgres-is-the-wrong-place-to-traverse-the-graph/