X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 989AC52A80 for ; Fri, 15 Jul 2005 10:34:13 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 46117-02 for ; Fri, 15 Jul 2005 13:34:06 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 0ED1852B63 for ; Fri, 15 Jul 2005 10:34:05 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j6FDY5LF008994 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 15 Jul 2005 07:34:08 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j6FDY5r9030293; Fri, 15 Jul 2005 07:34:05 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j6FDY4md030292; Fri, 15 Jul 2005 07:34:04 -0600 (MDT) (envelope-from mfuhr) Date: Fri, 15 Jul 2005 07:34:04 -0600 From: Michael Fuhr To: Luca Pireddu Cc: pgsql-sql@postgresql.org Subject: Re: problem (bug?) with "in (subquery)" Message-ID: <20050715133404.GA30210@winnie.fuhr.org> References: <200507140134.21133.luca@cs.ualberta.ca> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200507140134.21133.luca@cs.ualberta.ca> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/140 X-Sequence-Number: 22118 On Thu, Jul 14, 2005 at 01:34:21AM -0600, Luca Pireddu wrote: > I have the following query that isn't behaving like I would expect: > > select * from strains s where s.id in (select strain_id from pathway_strains); Any reason the subquery isn't doing "SELECT DISTINCT strain_id"? > I would expect each strain record to appear only once. Instead I get output > like this, where the same strain id appears many times: > > id | name | organism > -------+--------------+---------- > 83 | common | 82 > 83 | common | 82 > 83 | common | 82 What happens when you try each of the following? Do they give the expected results? I did some tests and I'm wondering if the planner's hash join is responsible for the duplicate rows. SELECT * FROM strains WHERE id IN ( SELECT strain_id FROM pathway_strains ORDER BY strain_id ); CREATE TEMPORARY TABLE foo AS SELECT strain_id FROM pathway_strains; SELECT * FROM strains WHERE id IN (SELECT strain_id FROM foo); SET enable_hashjoin TO off; SELECT * FROM strains WHERE id IN (SELECT strain_id FROM pathway_strains); -- Michael Fuhr http://www.fuhr.org/~mfuhr/