Skip to content

Traverse from your application data

Download the complete SQL script, including dataset setup for a fresh database.

Find how much of the network is reachable from a person, then return their neighbours as ordinary application rows. Run the setup once, then execute this script in the same database.

-- Node ids are the original people.id values. Look up a start dynamically.
SELECT depth, count(*) AS people_reached
FROM warren.bfs('example_social', ARRAY(
SELECT id FROM warren_examples.people WHERE name = 'person-1'
), 10)
GROUP BY depth ORDER BY depth;
-- Return business rows, including a predecessor for each shortest route.
SELECT p.id, p.name, b.depth, previous.name AS reached_via
FROM warren.bfs('example_social', ARRAY(
SELECT id FROM warren_examples.people WHERE name = 'person-1'
), 3) b
JOIN warren_examples.people p ON p.id = b.node_id
LEFT JOIN warren_examples.people previous ON previous.id = b.via
ORDER BY b.depth, p.id;
SELECT warren.shortest_path('example_social', 1::bigint, 9999::bigint) AS path;

The start array comes from a name lookup. In an application, use a bound parameter for the name, email, or business identifier. The returned ids already belong to your table; there is no internal-id translation step.

The first query visits each person once at their shortest distance from the start. For person-1, its depth distribution is:

Depth People
0 1
1 5
2 25
3 124
4 599
5 2,425
6 4,912
7 1,825
8 80
9 4

All 10,000 people are reached within nine hops. The second query returns names and each node’s predecessor, limited to three hops. The last query returns a shortest path as an array of original ids.

The lookup can return several ids. BFS then measures the minimum distance from any start, with all starts at depth zero. A lookup returning no rows produces an empty start array and no traversal results.

Edges are followed outward. Use direction := 'in' to traverse against them or 'both' to ignore direction. A depth limit bounds hops, not the number of rows; max_results can additionally cap the number visited. See traversal for filters and truncation semantics.