Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 0679E9FBFC0 for ; Wed, 9 May 2007 11:33:11 -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 46840-03 for ; Wed, 9 May 2007 11:33:07 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sargon.lncsa.com (sargon.lncsa.com [212.99.8.251]) by postgresql.org (Postfix) with ESMTP id 5DC889FC073 for ; Wed, 9 May 2007 11:33:07 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 60D481689B06 for ; Wed, 9 May 2007 16:33:05 +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 jxN9FeZUowPf for ; Wed, 9 May 2007 16:33:05 +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 2D35C16F7AFD for ; Wed, 9 May 2007 16:33:05 +0200 (CEST) Received: from trajan.apartia.fr (trajan.apartia.fr [10.0.3.121]) by zenon.apartia.fr (Postfix) with ESMTP id 7B15EF0A06B04 for ; Wed, 9 May 2007 16:33:04 +0200 (CEST) Received: by trajan.apartia.fr (Postfix, from userid 1000) id 560812CA7184; Wed, 9 May 2007 16:33:01 +0200 (CEST) Date: Wed, 9 May 2007 16:33:01 +0200 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: query to select a linked list Message-ID: <20070509143301.GA20327@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20070509125520.GA17497@apartia.fr> <20070509143021.GA20210@apartia.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070509143021.GA20210@apartia.fr> User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/69 X-Sequence-Number: 28349 On Wed, May 09, 2007 at 04:30:21PM +0200, Louis-David Mitterrand wrote: > On Wed, May 09, 2007 at 02:55:20PM +0200, Louis-David Mitterrand wrote: > > 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? > > I am trying to write a recursive pl/sql function to return all thread > children: > > create or replace function forum_children(integer) returns setof forum as $$ > declare > rec record; > begin > > for rec in select * from forum where $1 in (id_parent,id_forum) loop Oops, I meant : for rec in select * from forum where id_parent=$1 loop which works fine. Sorry, > select * from forum_children(rec.id_forum); > return next rec; > > end loop; > > return; > > end; > $$ language 'plpgsql'; > > > But it does not work as intended (infinite loop?). > > What did I miss? > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend