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 366E55293B for ; Fri, 15 Jul 2005 12:52: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 73949-10 for ; Fri, 15 Jul 2005 15:52:05 +0000 (GMT) Received: from pd2mo2so.prod.shaw.ca (shawidc-mo1.cg.shawcable.net [24.71.223.10]) by svr1.postgresql.org (Postfix) with ESMTP id 9CEA452913 for ; Fri, 15 Jul 2005 12:52:04 -0300 (ADT) Received: from pd2mr1so.prod.shaw.ca (pd2mr1so-qfe3.prod.shaw.ca [10.0.141.110]) by l-daemon (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0IJO00G0XEQR0ZB0@l-daemon> for pgsql-sql@postgresql.org; Fri, 15 Jul 2005 09:52:03 -0600 (MDT) Received: from pn2ml6so.prod.shaw.ca ([10.0.121.150]) by pd2mr1so.prod.shaw.ca (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0IJO00I8UEQRKL90@pd2mr1so.prod.shaw.ca> for pgsql-sql@postgresql.org; Fri, 15 Jul 2005 09:52:03 -0600 (MDT) Received: from s01060050ba4264e9.ed.shawcable.net (S01060050ba4264e9.ed.shawcable.net [68.148.246.192]) by l-daemon (iPlanet Messaging Server 5.2 HotFix 1.18 (built Jul 28 2003)) with ESMTP id <0IJO0041BEQQG9@l-daemon> for pgsql-sql@postgresql.org; Fri, 15 Jul 2005 09:52:03 -0600 (MDT) Date: Fri, 15 Jul 2005 09:52:01 -0600 From: Luca Pireddu Subject: Re: problem (bug?) with "in (subquery)" In-reply-to: <20050715133404.GA30210@winnie.fuhr.org> To: pgsql-sql@postgresql.org Cc: Michael Fuhr , Tom Lane Message-id: <200507150952.01732.lucap@shaw.ca> MIME-version: 1.0 Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 7bit Content-disposition: inline References: <200507140134.21133.luca@cs.ualberta.ca> <20050715133404.GA30210@winnie.fuhr.org> User-Agent: KMail/1.7.2 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.362 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, DNS_FROM_RFC_POST, FORGED_RCVD_HELO X-Spam-Level: * X-Archive-Number: 200507/145 X-Sequence-Number: 22123 On July 15, 2005 07:34, Michael Fuhr wrote: > 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: Thanks for creating the reduced test case Michael. My apologies for not doing it myself. > > > > 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"? because I don't need to according to the specification of "in". However, it does generate the correct output. So does select distinct * from strains s where s.id in (select strain_id from pathway_strains); > > > 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 > ); With the "order by" it works as it should, not generating duplicate rows. > > CREATE TEMPORARY TABLE foo AS SELECT strain_id FROM pathway_strains; > SELECT * FROM strains WHERE id IN (SELECT strain_id FROM foo); This one's interesting. It only returns the unique rows. > > SET enable_hashjoin TO off; > SELECT * FROM strains WHERE id IN (SELECT strain_id FROM pathway_strains); With hashjoin off the query returns the correct output. On July 15, 2005 08:58, Tom Lane wrote: > Ah-hah: this one is the fault of create_unique_path, which quoth > > /* > * If the input is a subquery whose output must be unique already, we > * don't need to do anything. > */ > > Of course, that needs to read "... unique already, *and we are using all > of its output columns in our DISTINCT list*, we don't need to do > anything." > > regards, tom lane In any case, it looks like Tom has already found the problem :-) Thanks guys! Luca