X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id EE766475843 for ; Sun, 2 Mar 2003 09:19:08 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 30204-09 for ; Sun, 2 Mar 2003 09:19:06 -0500 (EST) Received: from sabre.velocet.net (sabre.velocet.net [216.138.209.205]) by postgresql.org (Postfix) with ESMTP id 6DF2E4753A1 for ; Sun, 2 Mar 2003 09:19:06 -0500 (EST) Received: from stark.dyndns.tv (H162.C233.tor.velocet.net [216.138.233.162]) by sabre.velocet.net (Postfix) with ESMTP id BF9471380D2; Sun, 2 Mar 2003 09:19:08 -0500 (EST) Received: from localhost ([127.0.0.1] helo=stark.dyndns.tv ident=foobar) by stark.dyndns.tv with smtp (Exim 3.36 #1 (Debian)) id 18pUJ2-0000ny-00; Sun, 02 Mar 2003 09:19:08 -0500 To: Achilleus Mantzios Cc: Bruno Wolff III , Victor Yegorov , Subject: Re: Dynamic SELECT condition References: In-Reply-To: From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 02 Mar 2003 09:19:08 -0500 Message-ID: <87smu5kg0z.fsf@stark.dyndns.tv> Lines: 39 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new X-Archive-Number: 200303/49 X-Sequence-Number: 12089 Achilleus Mantzios writes: > On Sat, 1 Mar 2003, Bruno Wolff III wrote: > > > On Mon, Feb 24, 2003 at 19:53:00 +0200, > > Victor Yegorov wrote: > > > > > > I mean, I know the id of a root object, it's 3. If I'll extract with > > > condition parent_id = 3, then I'll get only 2 rows. I'd like to have a > > > recursive result set - while there are records in the table for which > > > count(next_level_parent_id == this_level_child_id) > 0, select parent_id, child_id. You might want to look into the "Nested Sets" cookbook page too: http://www.brasileiro.net:8080/postgres/cookbook/view-recipes.adp?section_id=310&format=long This representation of hierarchies has a lot of nice properties including being able to look up a whole subtree quickly. It makes it a bit of a pain to modify the tree though. > If you want to know the direct kids (4,5 in the example) of id=3, then it is > select from table where itoar(3) ~ parents and level(parents)=2 (itoar and > level are simple C functions that convert an int4 to its corresponding 1x1 > array and calculate the length of the array , respectively) In the "int_agg" directory in contrib there are operators that do this, you can say "WHERE parents *= 3". I prefer to keep level in a separate column though. I've find the features in the "int_agg" and "array" directories in the contrib directory to be extremely useful. The only disadvantage is that the optimizer doesn't have good basis for guessing the selectivity of the *= type operators. GiST indexing of arrays is nice but depends on knowing which element of the array you're looking for. If you're looking for 3 anywhere in the hierarchy I don't think you can use the index. -- greg