Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id B84D7B5DC5E for ; Wed, 2 Nov 2011 13:38:19 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 37701-09 for ; Wed, 2 Nov 2011 16:38:13 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 25844B5DC5C for ; Wed, 2 Nov 2011 13:38:13 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id pA2Gc3kl002260; Wed, 2 Nov 2011 12:38:04 -0400 (EDT) To: =?iso-2022-jp?B?GyRCMEJAPiEhRD5MaRsoQg==?= cc: Simon Riggs , pgsql-bugs@postgresql.org, =?iso-2022-jp?B?GyRCNGRAdSEhOThPOhsoQg==?= Subject: Re: BUG #6283: About the behavior of indexscan in case there are some NULL values. In-reply-to: <11CC99435D0E8Canzai-naoya@mxu.nes.nec.co.jp> References: <201111020444.pA24iN5a031950@wwwmaster.postgresql.org> <11CC99435D0E8Canzai-naoya@mxu.nes.nec.co.jp> Comments: In-reply-to =?iso-2022-jp?B?GyRCMEJAPiEhRD5MaRsoQg==?= message dated "Wed, 02 Nov 2011 18:39:48 +0900" Date: Wed, 02 Nov 2011 12:38:03 -0400 Message-ID: <2259.1320251883@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-3.101 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-1.201 X-Spam-Level: X-Archive-Number: 201111/21 X-Sequence-Number: 31616 =?iso-2022-jp?B?GyRCMEJAPiEhRD5MaRsoQg==?= writes: > I have checked latest source code. > But, backward scan doesn't work correctly... [ pokes at that... ] Hmm, the patches I applied a couple days ago assumed that we are stepping forward or back from a place where the WHERE clauses are satisfied. But in this example, the system just applies _bt_endpoint to descend to the right-hand end of the index, since there is no upper-bound qual with which to do anything different. So we start from a place where the clauses aren't satisfied. That also means that we haven't really fixed the original performance complaint: there could be lots of nulls to be stepped over before we reach the first matching row. I think that the right fix for this is probably to make _bt_preprocess_keys explicitly generate the "id is not null" qual that's implied by "id > 0", so that it will have what amounts to a range condition on the index contents (since for NULLS LAST, "id is not null" amounts to "id is less than null", as it were). Then, instead of applying _bt_endpoint, it will use the less-than key to descend the btree to the last non-null entry, and we'll be good for both correctness and performance. I don't see any big problem in doing this in HEAD, but it's getting past what seems like a sane back-patch. So probably we should revert the back-branch versions of the prior patch, and just say that the performance problem is only going to be addressed in HEAD. regards, tom lane