Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 897319FBAA8 for ; Wed, 9 May 2007 10:32:07 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 07157-06-2 for ; Wed, 9 May 2007 10:32:02 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 1A6EF9FBEA7 for ; Wed, 9 May 2007 10:32:03 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from matrix.gatewaynet.com (host2.dynacom.ondsl.gr [62.103.35.210]) by svr4.postgresql.org (Postfix) with ESMTP id 953635AF882 for ; Wed, 9 May 2007 13:32:01 +0000 (GMT) Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=ip216.internal.net) by matrix.gatewaynet.com with esmtp (Exim 4.62) (envelope-from ) id 1HlmGh-0001x0-6E for pgsql-sql@postgresql.org; Wed, 09 May 2007 16:31:47 +0300 From: Achilleas Mantzios Organization: Dynacom Tankers Mgmt To: pgsql-sql@postgresql.org Subject: Re: query to select a linked list Date: Wed, 9 May 2007 16:31:46 +0300 User-Agent: KMail/1.9.3 References: <20070509125520.GA17497@apartia.fr> In-Reply-To: <20070509125520.GA17497@apartia.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200705091631.46660.achill@matrix.gatewaynet.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.066 tagged_above=0 required=5 tests=AWL=-0.070, BAYES_50=0.001, FORGED_RCVD_HELO=0.135 X-Spam-Level: X-Archive-Number: 200705/66 X-Sequence-Number: 28347 =CE=A3=CF=84=CE=B9=CF=82 =CE=A4=CE=B5=CF=84=CE=AC=CF=81=CF=84=CE=B7 09 =CE= =9C=CE=AC=CE=B9=CE=BF=CF=82 2007 15:55, =CE=BF/=CE=B7 Louis-David Mitterran= d =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: > Hi, > > To build a threaded forum application I came up the following schema: > > forum > ------ > id_forum | integer| not null default > nextval('forum_id_forum_seq'::regclass) id_parent| integer| > subject | text | not null > message | text | > > Each message a unique id_forum and an id_parent pointing to the replied > post (empty if first post). > > How can I build an elegant query to select all messages in a thread? > Your question is about storing hierarchies in postgresql. One way is to use the genealogical approach, where we store for any node the path to its root. I have used this technique to store description of tanker vessels machinery (over 1M items) and the plan maintenance on them, and the performance is ve= ry=20 good, while the representation is highly intuitive and flexible, unlike some wierd approcahes i have hit on. When i did a small research on the complexity/index usage on various operations (UPDATE, INSERT, DELETE, SELECT), the performance was at least as good as the "nested pair" approch that many seemed to promote. You add a column "parents" (rather than just the parentid) as an integer[]. =46or every node you store the path to the root node starting from the most immediate ancestor. Then you just make an index on this column using the intarray contrib packa= ge. Then you can easily query for nodes under a specific node, or for nodes jus= t=20 one level below a specific node, nodes with no descendents (leaf nodes)=20 etc... Of course you could do smth simpler, but in the long run, representing data in the correct way will certainly pay off. > Thanks, > > ---------------------------(end of broadcast)--------------------------- > TIP 1: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly =2D-=20 Achilleas Mantzios