Permalink
Fetching contributors…
Cannot retrieve contributors at this time
21 lines (16 sloc) 503 Bytes
WITH RECURSIVE search_path(id, link, depth, route, cycle) AS (
SELECT k.k_person1id, k.k_person2id, 1,
ARRAY[k.k_person1id],
false
FROM knows k where k.k_person1id = @Person1@
UNION ALL
SELECT k.k_person1id, k.k_person2id, sp.depth+1,
route || k.k_person1id,
k.k_person1id = ANY(route)
FROM knows k, search_path sp
WHERE k.k_person1id = sp.link AND NOT cycle AND sp.depth < 4
)
SELECT sp.depth AS route
FROM search_path AS sp
WHERE link = @Person2@ AND NOT cycle
LIMIT 1;