Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1atiB9-0008Fb-Iq for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Apr 2016 20:59:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1atiB8-0002I3-WD for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Apr 2016 20:59:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1atiB8-0002Hp-Fb for pgsql-bugs@postgresql.org; Fri, 22 Apr 2016 20:59:54 +0000 Received: from mail-wm0-x22f.google.com ([2a00:1450:400c:c09::22f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1atiB5-0005Ax-67 for pgsql-bugs@postgresql.org; Fri, 22 Apr 2016 20:59:53 +0000 Received: by mail-wm0-x22f.google.com with SMTP id v188so34449896wme.1 for ; Fri, 22 Apr 2016 13:59:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:references:to:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=0/c8v2CzUC82RkcjWTvKa8makndM8kqP7DK0sGPZhao=; b=xvCyGi4AIctLN7JY0V2dLDXLWFvjibZw8UvkkkKm0vAX74PRSMxlNLwpOg+a8/144d Tdeq1Z7ZCqUPMrH9BqyKkwz83Qi2lEyDNWOQf7Q1dlDGGk4SvSRkIsXDsxF4LiaCUDWa gJPlH1lTq31TEj2V5PRXd/opwZVPK9dIpoiJ2q4LY/qe8zU9oWtbc7k7PcZBRpY2U0Kj plJM+YkkLPh2kjoLMvxMQ8V4NudMfGcoWJJgqYF5bds8oCLL+N9aT7ehIE6kB0g5YeJi RWplKa0cPpGnetOd0LtnjYTuqbSi84HeTZ85g2fXz3vyUfRpPFZqnn6G6S4qjYlIY6hG cN8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:references:to:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=0/c8v2CzUC82RkcjWTvKa8makndM8kqP7DK0sGPZhao=; b=LEA6JpmjE4XUzdUVg8JR/jJYysxJ+rVQoR2LSrzLUvzeKymSGOu6/GHjaAeNfi4IHw nUFqqIFLyNtISFwq7lTBLKGHgxSup0mwlIeewEu5hDm+2W2j2EcuPLduDuIFM/Fp/4Zs xof36/itfNkDTX8csxabjuLOP0Hy2DUVkJZ++2Kjy+UHfmDslFEjM2nR0upFiTu5o5t2 ko5NcPqaLxfQ6veGNadn6/izCcHOW0H8H3YjBDqTPnTJPPmT8t8TUZudkwD0a7HcoauO lYQvbPxRO8WQy605gb8vWODFsmmkA2OwKd+p/oXxqpZqGfpTFj0Usr9VY5HmVRB5Pt41 bmJA== X-Gm-Message-State: AOPr4FWcN/ioVVLNmhSyiLKZ4L1JCCM0pswVr5NWTO3EQgec0YQYRkjrnjWgu4s/ay9HSg== X-Received: by 10.194.105.42 with SMTP id gj10mr22412301wjb.49.1461358790398; Fri, 22 Apr 2016 13:59:50 -0700 (PDT) Received: from [192.168.1.144] (dsl-25-149.utaonline.at. [81.189.25.149]) by smtp.googlemail.com with ESMTPSA id q77sm5081192wmd.12.2016.04.22.13.59.49 for (version=TLSv1/SSLv3 cipher=OTHER); Fri, 22 Apr 2016 13:59:49 -0700 (PDT) Subject: Re: BUG #14107: Major query planner bug regarding subqueries and indices References: <20160421115638.22895.78902@wrigleys.postgresql.org> <69e376c7-6c2c-c96d-2ad6-3734affb59b7@gmail.com> To: "pgsql-bugs@postgresql.org" From: Mathias Kunter Message-ID: <44df2e58-87f5-282c-9871-c735240c55e4@gmail.com> Date: Fri, 22 Apr 2016 22:59:49 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org > Queries return different number of rows, meaning they're not fully equivalent. Well, I think in the given example they should actually be fully equivalent, since the unique id column is selected as well. Thus, UNION can't do any unwanted row eliminations. > Well, then `EXPLAIN (analyze, buffers)` is also wanted, together with > object definitions. So here are the exact SQL commands to reproduce the problem. -- Create table "book" CREATE TABLE book (id SERIAL NOT NULL, name VARCHAR, author INTEGER, CONSTRAINT book_pkey PRIMARY KEY (id)); CREATE INDEX book_name_index ON book (name); CREATE INDEX book_author_index ON book (author); -- Create table "author" CREATE TABLE author (id SERIAL NOT NULL, name VARCHAR, CONSTRAINT author_pkey PRIMARY KEY (id)); CREATE INDEX author_name_index ON author (name); -- Insert some test data so that the planner would never assume a sequential scan could be faster INSERT INTO book (id, name, author) SELECT generate_series(1, 100000), md5(random()::text), (generate_series(1, 100000) - 1) % 10000 + 1; INSERT INTO author (id, name) SELECT generate_series(1, 10000), md5(random()::text); ANALYZE book; ANALYZE author; -- Check query plan when using OR operator EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM book WHERE name = 'Harry Potter' OR author IN (SELECT id FROM author WHERE name = 'Rowling'); QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------- Seq Scan on book (cost=8.30..2443.30 rows=50001 width=41) (actual time=25.527..25.527 rows=0 loops=1) Filter: (((name)::text = 'Harry Potter'::text) OR (hashed SubPlan 1)) Rows Removed by Filter: 100000 Buffers: shared hit=937 SubPlan 1 -> Index Scan using author_name_index on author (cost=0.29..8.30 rows=1 width=4) (actual time=0.041..0.041 rows=0 loops=1) Index Cond: ((name)::text = 'Rowling'::text) Buffers: shared hit=2 Planning time: 0.237 ms Execution time: 25.603 ms -- Check query plan when using UNION operator EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM book WHERE name = 'Harry Potter' UNION SELECT * FROM book WHERE author IN (SELECT id FROM author WHERE name = 'Rowling'); QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=58.42..58.53 rows=11 width=41) (actual time=0.066..0.066 rows=0 loops=1) Group Key: book.id, book.name, book.author Buffers: shared hit=5 -> Append (cost=0.42..58.34 rows=11 width=41) (actual time=0.061..0.061 rows=0 loops=1) Buffers: shared hit=5 -> Index Scan using book_name_index on book (cost=0.42..8.44 rows=1 width=41) (actual time=0.035..0.035 rows=0 loops=1) Index Cond: ((name)::text = 'Harry Potter'::text) Buffers: shared hit=3 -> Nested Loop (cost=4.66..49.79 rows=10 width=41) (actual time=0.019..0.019 rows=0 loops=1) Buffers: shared hit=2 -> Index Scan using author_name_index on author (cost=0.29..8.30 rows=1 width=4) (actual time=0.015..0.015 rows=0 loops=1) Index Cond: ((name)::text = 'Rowling'::text) Buffers: shared hit=2 -> Bitmap Heap Scan on book book_1 (cost=4.37..41.39 rows=10 width=41) (never executed) Recheck Cond: (author = author.id) -> Bitmap Index Scan on book_author_index (cost=0.00..4.37 rows=10 width=0) (never executed) Index Cond: (author = author.id) Planning time: 0.669 ms Execution time: 0.183 ms -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs