
foojay · 4 min readBackend & APIs
When Postgres is the wrong place to traverse the graph
Deep graph traversal in Postgres hits an exponential O(b^D) wall, making deep searches impractical. Moving the traversal logic to application memory and using depth‑first search with primitive collections drastically reduces memory and latency. The article explains why and how to implement this shift.
- Use depth‑first search to keep only the current path in memory.
- Load the graph into memory with unboxed primitive collections (e.g., HPPC) for 10× less memory use.
- Filter subgraphs early and use a visited set to collapse exponential growth to linear when only distinct nodes matter.