Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1atTyO-0005qr-K7 for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Apr 2016 05:49:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1atTyO-00056L-2z for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Apr 2016 05:49:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1atTyN-00056E-Bh for pgsql-bugs@postgresql.org; Fri, 22 Apr 2016 05:49:47 +0000 Received: from mail-wm0-x232.google.com ([2a00:1450:400c:c09::232]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1atTyK-00076K-4m for pgsql-bugs@postgresql.org; Fri, 22 Apr 2016 05:49:45 +0000 Received: by mail-wm0-x232.google.com with SMTP id u206so9500690wme.1 for ; Thu, 21 Apr 2016 22:49:43 -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=5vKd5CvcQk6sjT60D1XLKV4OrBMdaBVB8f7gmXpJPgw=; b=NKGRiD0ProrFVS7Kw536rNkb6GhUKokPwJQUiQugIbH4jp2CPRuLSyImxoftsllUFb 2g5kd0I5LvgFGJWBOVcIGepqzxckgVLpaFjRHoQ9TIUdaGI69AUPu98HrTP6ox/TWPKn FPh9Oa7VZ7aJihrm+AuzqiwBsHRBwjsuBHJWOOmxM2+suk1BCHT3XFruSCJNbR+pNXeB C/jHmjKl+wD+Bt6gfbMo6mp4k84xq5Nic1HQFykOa2a67+F5HDVVNv5kvpUqmkstvQ8e soyrRHEzHln8JLO5tuwckjpor6cOHRw27/8sERXanpvbsgNFpvW7mL9Kgl75lGb85+OJ c2vQ== 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=5vKd5CvcQk6sjT60D1XLKV4OrBMdaBVB8f7gmXpJPgw=; b=MrKX91nA/9X8V1egh+JkBqerCjffI33wNFilejh9hEXO62Rqi073dOzq46rh6/3F+T GH2tWDUaJk7PFMKw0mVOXEK1aikZdRuM/SuK+oN2P8Yc+1CTf+fpVnepU1FCOYGLTv6j KnVcKQlAz/6G1V7pDFWkpAkxypKndOoQ6cJQf+7mIya7LPMYj4d0ARCVdMqrIdUHM2fj hmMlOfeL9G9S0+T9QLfd6RKEl91gfx673PhXrIt7t5kMA4LpHMnz30MLk0Usr4EN6DcA XHIjmSDwFtL4r6g9ZmFQDjgf2+4cQduO3oaekqpSUVPWpjSJkyVWQxNdX2vWaiHzGnFA PHJw== X-Gm-Message-State: AOPr4FWfjOM4P4dZvownZ3zfrSwLCHqP2T+R8UZrCPPjpxyKLsJKU0l4/rZXZ5V2nMEzLA== X-Received: by 10.194.48.7 with SMTP id h7mr20226283wjn.81.1461304182381; Thu, 21 Apr 2016 22:49:42 -0700 (PDT) Received: from [192.168.1.144] (dsl-25-149.utaonline.at. [81.189.25.149]) by smtp.googlemail.com with ESMTPSA id 188sm1536956wmk.6.2016.04.21.22.49.41 for (version=TLSv1/SSLv3 cipher=OTHER); Thu, 21 Apr 2016 22:49:41 -0700 (PDT) Subject: Re: BUG #14107: Major query planner bug regarding subqueries and indices References: <20160421115638.22895.78902@wrigleys.postgresql.org> To: "pgsql-bugs@postgresql.org" From: Mathias Kunter Message-ID: <8a72e88e-819a-0d93-1d52-7b8134d7f86f@gmail.com> Date: Fri, 22 Apr 2016 07:49:40 +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: 8bit 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 The problem unfortunately persists in 9.5.2 as well, where the query plan is exactly the same as in 9.5.0. (Tested on Windows 7, but this presumably is a cross-platform bug.) From what I can tell, this affects all queries of the type SELECT ... WHERE OR IN (); As a workaround, you can use UNION, which works as expected: SELECT ... WHERE UNION SELECT ... WHERE IN (); However, as it currently stands, queries of the above form are de facto unusable with PostgreSQL. So this is pretty serious IMO. Am 21.04.2016 um 23:01 schrieb David G. Johnston: > On Thu, Apr 21, 2016 at 4:56 AM, >wrote: > > 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) > > > ​To lazy to research at the moment but I think this has been fixed and > released. You show 9.5.0 as your version. Update and you should be fine. > > David J​. > -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs