I am tracing the Postgis topological network with Postgresql via: WITH RECURSIVE path AS ( SELECT edge_id,start_node, end_node FROM simulation.edge_data WHERE start_node = 1 OR end_node = 1 OR start_node = 8 OR end_node = 8 UNION SELECT e.edge_id, e.start_node, e.end_node FROM simulation.edge_data e INNER JOIN path s ON (s.start_node = e.start_node OR s.start_node = e.end_node OR s.end_node = e.end_node OR s.end_node = e.start_node) ) SELECT * FROM path; I want to improve the query in only visiting every node once. Also this network is full of gas pipelines. If the node is a valve that is closed i want to not further track in that direction. This returns me all nodes that have access to gas but not all edges. What am I doing wrong? I do this with: WITH RECURSIVE path AS ( SELECT edge_id,start_node, end_node FROM simulation.edge_data WHERE start_node = 1 OR end_node = 1 OR start_node = 8 OR end_node = 8 UNION SELECT e.edge_id, e.start_node, e.end_node FROM simulation.edge_data e INNER JOIN path s ON (SELECT * FROM topology.validateAttribute('topology','valve','status','closed',e.start_node,1)) = false AND (s.start_node = e.start_node OR s.start_node = e.end_node OR s.end_node = e.end_node OR s.end_node = e.start_node) ) SELECT * FROM path; This is my graph, where purple represents the gas sources. The yellow marked are closed valves. The result of the above query is missing the two edges 2 and 3, but includes the correct nodes: [![graph][1]][1] [1]: https://i.sstatic.net/bKuYi.png