Re: problem (bug?) with "in (subquery)" - Mailing list pgsql-sql

From Tom Lane
Subject Re: problem (bug?) with "in (subquery)"
Date
Msg-id 11560.1121439505@sss.pgh.pa.us
Whole thread Raw
In response to Re: problem (bug?) with "in (subquery)"  (Michael Fuhr <mike@fuhr.org>)
List pgsql-sql
Michael Fuhr <mike@fuhr.org> writes:
> I've been reverse-engineering and simplifying this.  Here's something
> that I think is close:

> CREATE TABLE foo (id integer);
> CREATE TABLE bar (id1 integer, id2 integer);

> INSERT INTO foo VALUES (1);

> INSERT INTO bar VALUES (1, 1);
> INSERT INTO bar VALUES (2, 2);
> INSERT INTO bar VALUES (3, 1);

> SELECT *
> FROM foo
> WHERE id IN (SELECT id2 FROM (SELECT DISTINCT id1, id2 FROM bar) AS s);
>  id 
> ----
>   1
>   1
> (2 rows)

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


pgsql-sql by date:

Previous
From: "Dinesh Pandey"
Date:
Subject: Postgres for Fedora Core 2 OS ****************
Next
From: Luca Pireddu
Date:
Subject: Re: problem (bug?) with "in (subquery)"