Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 15389475D8C for ; Fri, 9 Aug 2002 17:38:11 -0400 (EDT) Received: from ferda.ied.com (xtreme10-98.aci.on.ca [209.50.81.98]) by postgresql.org (Postfix) with ESMTP id 4B4E8475D3F for ; Fri, 9 Aug 2002 17:38:10 -0400 (EDT) Received: from [127.0.0.2] (localhost.localdomain [127.0.0.1]) by ferda.ied.com (8.11.6/8.11.6) with ESMTP id g79LaaR05383 for ; Fri, 9 Aug 2002 17:36:37 -0400 Date: Fri, 9 Aug 2002 17:36:35 -0400 (EDT) From: h012@ied.com To: pgsql-sql@postgresql.org Subject: retrieving all rows from a "tree" in one select - how ? Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/168 X-Sequence-Number: 9014 Hi, I realize that a relational database may not be ideal for storing (and retrieving) tree-like strucutres, but it looks like you guys are doing with PostgreSQL the impossible anyway. Having table t of all nodes: CREATE SEQUENCE nodeIDseq START 1; CREATE TABLE t( id int PRIMARY KEY DEFAULT NEXTVAL('nodeIDseq'), parent int REFERENCES t, mydata int4 ); INSERT INTO t VALUES (0,0); I was wondering whether there is a known (and perhaps working) way to do things like: -- select a tree starting with node 1234 and all its descendants: SELECT * FROM t WHERE id=1234 OR ANCESTOR(t.parent) IS 1234; and -- select the path from tree node 2345 to the root SELECT * FROM t WHERE id=2345 OR DESCENTANT(t.parent) IS 2345; (I've seen some terse soutions at http://www.brasileiro.net/postgres/cookbook/view-recipes.adp?section_id=2&format=long but they don't seem to be complete.) (Also I've looket at ltrees from GiST, but "ltree" seems to require that the ID attribute contains all ancestors.) Thanks, John -- -- Gospel of Jesus is the saving power of God for all who believe -- ## To some, nothing is impossible. ## http://Honza.Vicherek.com/