Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 812D39FC075 for ; Wed, 9 May 2007 10:24:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 25237-01 for ; Wed, 9 May 2007 10:24:23 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from oxford.xeocode.com (unknown [62.232.55.118]) by postgresql.org (Postfix) with ESMTP id 34F839FC058 for ; Wed, 9 May 2007 10:24:24 -0300 (ADT) Received: from localhost ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.67) (envelope-from ) id 1Hlm9W-0002P4-Mm for pgsql-sql@postgresql.org; Wed, 09 May 2007 14:24:22 +0100 From: Gregory Stark To: pgsql-sql@postgresql.org Subject: Re: query to select a linked list In-Reply-To: <20070509125520.GA17497@apartia.fr> (Louis-David Mitterrand's message of "Wed, 9 May 2007 14:55:20 +0200") Organization: EnterpriseDB References: <20070509125520.GA17497@apartia.fr> X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.sql" 117) Date: Wed, 09 May 2007 14:24:22 +0100 Message-ID: <878xbyi0zd.fsf@oxford.xeocode.com> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/64 X-Sequence-Number: 28345 "Louis-David Mitterrand" writes: > 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? You would need recursive queries which Postgres doesn't support. There is a patch out there to add support but I don't think it's up-to-date with 8.2 and in any case the resulting queries can be quite intense. I would recommend you look into the contrib module named "ltree". It's easy to use and works well with the gist indexes. It does require changing your data model denormalizing it slightly which makes it hard to "reparent" children, but if that isn't an operation you have to support I think it makes most other operations you might want to do much easier to support. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com