Trapped on a plane waiting to take off and reading the #postgres docs. We built half the good stuff in @metaplane with recursive queries (https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-RECURSIVE), but there are deeper bits that I didn't know about.
7.8. WITH Queries (Common Table Expressions)

7.8. WITH Queries (Common Table Expressions) # 7.8.1. SELECT in WITH 7.8.2. Recursive Queries 7.8.3. Common Table Expression Materialization 7.8.4. Data-Modifying …

PostgreSQL Documentation
The `UNION` takes care of cycles in a lot of cases, but there are examples of how to handle cycles manually when it doesn't... and again there's built-in cycle detection syntax too.
https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-CYCLE
7.8. WITH Queries (Common Table Expressions)

7.8. WITH Queries (Common Table Expressions) # 7.8.1. SELECT in WITH 7.8.2. Recursive Queries 7.8.3. Common Table Expression Materialization 7.8.4. Data-Modifying …

PostgreSQL Documentation