with recursive "nodes" ("node", "depth", "path", "cycle") as (
select 10, 0, ARRAY[10], false
union all
select "to", "depth" + 1, path || "to", "to" = ANY(path)
from "edges", "nodes"
where "from" = "node" and not "cycle"
) select * from (select "node", min("depth"), "path", "cycle"
from "nodes" group by "node", "path", "cycle") as n
where not "cycle";
该查询将返回您期望的数据