//
Busca en todas las páginas de la documentación
Hierarchies and graph walks with cycle guards. Production-oriented PostgreSQL guidance.
WITH RECURSIVE tree AS (
SELECT id, parent_id, 1 AS depth FROM app.nodes WHERE parent_id IS NULL
UNION ALL
SELECT n.id, n.parent_id, t.depth + 1 FROM app.nodes n JOIN tree t ON n.parent_id = t.id
)
SELECT * FROM tree;When to reach for this: You implement or review SQL using this concept in PostgreSQL 18.
CREATE TABLE IF NOT EXISTS app.nodes (id int PRIMARY KEY, parent_id int REFERENCES app.nodes(id));What this demonstrates:
| Alternative | Use When | Don't Use When |
|---|---|---|
| Application-level logic | Simple CRUD | Integrity spread across services |
| Materialized view | Heavy repeated reads | Refresh complexity |
| External stream processor | Cross-system events | Extra operational surface |
Read-only replicas execute SELECT; writes go to primary.
Transaction pooling affects session-level features; use session mode when needed.
Stack versions: This page was written for PostgreSQL 18.4 (stable 18, maintenance 17), pgvector 0.8+, PgBouncer 1.x, Patroni 3.x, and PostGIS 3.5+.