Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id D500A9FB46D for ; Thu, 10 May 2007 05:05:21 -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 32014-04 for ; Thu, 10 May 2007 05:05:18 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sargon.lncsa.com (sargon.lncsa.com [212.99.8.251]) by postgresql.org (Postfix) with ESMTP id 33A9C9FB44C for ; Thu, 10 May 2007 05:05:18 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 527D415B50BD for ; Thu, 10 May 2007 10:05:16 +0200 (CEST) Received: from sargon.lncsa.com ([127.0.0.1]) by localhost (sargon.lncsa.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xPZH0A81dWw8 for ; Thu, 10 May 2007 10:05:16 +0200 (CEST) Received: from zenon.apartia.fr (zenon.apartia.fr [10.0.3.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "zenon.apartia.fr", Issuer "ca.apartia.fr" (verified OK)) by sargon.lncsa.com (Postfix) with ESMTP id E4F7514AFC93 for ; Thu, 10 May 2007 10:05:15 +0200 (CEST) Received: from trajan.apartia.fr (trajan.apartia.fr [10.0.3.121]) by zenon.apartia.fr (Postfix) with ESMTP id 0B877F0A4731F for ; Thu, 10 May 2007 10:05:15 +0200 (CEST) Received: by trajan.apartia.fr (Postfix, from userid 1000) id D6072254CAE0; Thu, 10 May 2007 10:05:09 +0200 (CEST) Date: Thu, 10 May 2007 10:05:09 +0200 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: query to select a linked list Message-ID: <20070510080509.GA17255@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20070509125520.GA17497@apartia.fr> <46425E0C.8080804@cs.anu.edu.au> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <46425E0C.8080804@cs.anu.edu.au> User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/78 X-Sequence-Number: 28358 On Thu, May 10, 2007 at 09:49:32AM +1000, Robert Edwards wrote: > > Hi Louis-David, > > I also have written a forum application using PostgreSQL. > > My schema has a "threadid" for each posting, which is actually also the > "messageid" of the first posting in the thread, but that is irrelevant. > > I can then just select all messages belonging to that thread. The actual > hierarchy of messages (which posting is in response to which) is dealt > with by a "parentid", identifying the messageid of the post being > responded to. Sorting that out is done by the middleware (PHP in this > case) - the SQL query simply returns all messages in the thread in a > single query. Because our database is somewhat busy, I have opted to > keep the queries to the database simple and let the middleware sort > out the heirarchical structure (which it is quite good at). > > I hope this helps. This helps a lot, thanks. I just wrote a little pl/sql function to compensate for the absence of a threadid in my schema: create or replace function forum_children(integer) returns setof forum as $$ declare rec record; subrec record; begin for rec in select * from forum where id_parent=$1 loop return next rec; for subrec in select * from forum_children(rec.id_forum) loop return next subrec; end loop; end loop; return; end; $$ language 'plpgsql'; But in the end it might just be more convenient and clear to have that threadid column as you did. Sorting in middleware (perl in my case) also seems like good compromise. Cheers,