Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1atLKc-0002R0-3j for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Apr 2016 20:36:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1atLKb-0005aI-N4 for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Apr 2016 20:36:09 +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 1atDEH-0005ZE-H2 for pgsql-bugs@postgresql.org; Thu, 21 Apr 2016 11:57:05 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1atDEE-0002Zu-Ki for pgsql-bugs@postgresql.org; Thu, 21 Apr 2016 11:57:04 +0000 MIME-Version: 1.0 Subject: BUG #14107: Major query planner bug regarding subqueries and indices To: pgsql-bugs@postgresql.org From: mathiaskunter@gmail.com Date: Thu, 21 Apr 2016 11:56:38 +0000 Message-ID: <20160421115638.22895.78902@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.3 (/) Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" 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 The following bug has been logged on the website: Bug reference: 14107 Logged by: Mathias Kunter Email address: mathiaskunter@gmail.com PostgreSQL version: 9.5.0 Operating system: Windows 7 Description: The query planner doesn't use an index although it could, causing an unneccessary sequential table scan. Step by step instructions to reproduce the problem are given below. Step 1 - just create a simple test table with an indexed id column: CREATE TABLE test (id serial NOT NULL, CONSTRAINT pkey PRIMARY KEY (id)); Step 2 - note that the index is used for the following query as expected: EXPLAIN SELECT * FROM test WHERE id = 1 OR id IN (2); QUERY PLAN ------------------------------------------------------------------------- Bitmap Heap Scan on test (cost=8.33..13.67 rows=2 width=4) Recheck Cond: ((id = 1) OR (id = 2)) -> BitmapOr (cost=8.33..8.33 rows=2 width=0) -> Bitmap Index Scan on pkey (cost=0.00..4.16 rows=1 width=0) Index Cond: (id = 1) -> Bitmap Index Scan on pkey (cost=0.00..4.16 rows=1 width=0) Index Cond: (id = 2) Step 3 - note that the index is NOT used for the following query: EXPLAIN SELECT * FROM test WHERE id = 1 OR id IN (SELECT id FROM test WHERE id = 2); QUERY PLAN ------------------------------------------------------------------------------------- Seq Scan on test (cost=8.17..56.42 rows=1275 width=4) Filter: ((id = 1) OR (hashed SubPlan 1)) SubPlan 1 -> Index Only Scan using pkey on test test_1 (cost=0.16..8.17 rows=1 width=4) Index Cond: (id = 2) -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs