Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 477CF9FC073 for ; Wed, 9 May 2007 11:30:56 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 94311-04-2 for ; Wed, 9 May 2007 11:30:26 -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 D2A769FC04A for ; Wed, 9 May 2007 11:30:28 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by sargon.lncsa.com (Postfix) with ESMTP id 7F52316F7B0C for ; Wed, 9 May 2007 16:30:26 +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 fnzH6LHTEv0K for ; Wed, 9 May 2007 16:30:26 +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 18F6616F7AD8 for ; Wed, 9 May 2007 16:30:26 +0200 (CEST) Received: from trajan.apartia.fr (trajan.apartia.fr [10.0.3.121]) by zenon.apartia.fr (Postfix) with ESMTP id A5E07F0A06B04 for ; Wed, 9 May 2007 16:30:24 +0200 (CEST) Received: by trajan.apartia.fr (Postfix, from userid 1000) id 7C1062CA7184; Wed, 9 May 2007 16:30:21 +0200 (CEST) Date: Wed, 9 May 2007 16:30:21 +0200 From: Louis-David Mitterrand To: pgsql-sql@postgresql.org Subject: Re: query to select a linked list Message-ID: <20070509143021.GA20210@apartia.fr> Mail-Followup-To: pgsql-sql@postgresql.org References: <20070509125520.GA17497@apartia.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20070509125520.GA17497@apartia.fr> User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/68 X-Sequence-Number: 28348 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 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?