Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WBsE8-0004JM-68 for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Feb 2014 20:40:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WBsE7-0000V9-Bd for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Feb 2014 20:40:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WBsE6-0000V2-Dy for pgsql-bugs@postgresql.org; Fri, 07 Feb 2014 20:40:42 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WBsE4-0003TL-9o for pgsql-bugs@postgresql.org; Fri, 07 Feb 2014 20:40:42 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s17KeOd3001918; Fri, 7 Feb 2014 15:40:24 -0500 From: Tom Lane To: maxim.boguk@gmail.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #9135: PostgreSQL doesn't want use index scan instead of (index scan+sort+limit) In-reply-to: <20140207045518.13610.2870@wrigleys.postgresql.org> References: <20140207045518.13610.2870@wrigleys.postgresql.org> Comments: In-reply-to maxim.boguk@gmail.com message dated "Fri, 07 Feb 2014 04:55:18 +0000" Date: Fri, 07 Feb 2014 15:40:24 -0500 Message-ID: <1917.1391805624@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) 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 maxim.boguk@gmail.com writes: > Index on the last three fields defined as: > "i_liexwebmasterproducts_2" btree (lwpwebsiteid, lwpnotforsale, lwpcreatedate) > Target query and plan: > select * > from liexWebmasterProducts this_ > where > this_.lwpWebsiteId=5935 > and this_.lwpnotForSale=FALSE > order by this_.lwpCreateDate desc limit 1; > Limit (cost=122.18..122.19 rows=1 width=902) (actual time=13.505..13.506 > rows=1 loops=1) > -> Sort (cost=122.18..124.57 rows=953 width=902) (actual > time=13.503..13.503 rows=1 loops=1) > Sort Key: lwpcreatedate > Sort Method: top-N heapsort Memory: 27kB > -> Index Scan using i_liexwebmasterproducts_2 on > liexwebmasterproducts this_ (cost=0.43..117.42 rows=953 width=902) (actual > time=0.171..10.429 rows=1674 loops=1) > Index Cond: ((lwpwebsiteid = 5935) AND (lwpnotforsale = > false)) > Filter: (NOT lwpnotforsale) > Total runtime: 13.626 ms As a workaround you could do select * from liexWebmasterProducts this_ where this_.lwpWebsiteId=5935 and this_.lwpnotForSale=FALSE order by this_.lwpnotForSale desc, this_.lwpCreateDate desc limit 1; The problem is that "this_.lwpnotForSale=FALSE" is canonicalized into "NOT this_.lwpnotForSale", and then the ORDER BY machinery fails to realize that that makes the index column a no-op for ordering purposes. It does work as you're expecting for index columns of non-boolean types. I'll see about fixing this, but considering that it's worked like that since about 8.1 without complaints, I don't think I'll risk back-patching the change. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs