Building a Multi-Generation Pedigree Tree in PostgreSQL
You have a record that points to two parent records. Each parent points to two more. You need to walk the tree up to N generations and return the full ancestor graph. If you have worked with org ch...

Source: DEV Community
You have a record that points to two parent records. Each parent points to two more. You need to walk the tree up to N generations and return the full ancestor graph. If you have worked with org charts, bill-of-materials structures, file systems, or category trees, you have seen one version of this problem. Pedigrees are the version where both parents matter, the graph doubles in width at every generation, and incorrect data has real consequences for the people who depend on it. I built ReptiDex, a mobile app that tracks lineage for animal breeders. Parent-offspring linking and multi-generation pedigree trees are in production, tracking real animals for real breeders. 50 paid subscribers and 200 animals tracked within 9 days of launch. This article covers the actual data model and the actual queries. Not a toy example. Here is what we will cover: the adjacency list model with self-referential foreign keys, recursive CTEs for ancestor and descendant traversal, sibling classification, th