Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 5742FB5DC4E for ; Mon, 31 Oct 2011 16:40:06 -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 09864-10 for ; Mon, 31 Oct 2011 19:39:59 +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 8798AB5DC4F for ; Mon, 31 Oct 2011 16:39:59 -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 p9VJdvVU006646; Mon, 31 Oct 2011 15:39:57 -0400 (EDT) To: Robert Haas cc: Maksym Boguk , pgsql-bugs@postgresql.org Subject: Re: BUG #6278: Index scans on '>' condition on field with many NULLS In-reply-to: References: <201110310339.p9V3dtIb015254@wwwmaster.postgresql.org> <26438.1320071858@sss.pgh.pa.us> <28958.1320079428@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Mon, 31 Oct 2011 14:41:00 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <6620.1320089926.0@sss.pgh.pa.us> Date: Mon, 31 Oct 2011 15:39:57 -0400 Message-ID: <6645.1320089997@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: 201110/325 X-Sequence-Number: 31595 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="iso-8859-1" Content-ID: <6620.1320089926.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Robert Haas writes: > On Mon, Oct 31, 2011 at 12:43 PM, Tom Lane wrote: >> A patch along these lines should be pretty localized. Has anyone >> got an opinion on whether to back-patch or not? This seems like a >> performance bug, but given the lack of prior complaints, maybe we >> shouldn't take any risks for it. > I think my vote would be to just fix it in master. After researching the issue, my patch looks like the attached. This seems to apply cleanly back to about 8.3, so I'm inclined to back-patch that far. It's pretty simple, and I think the reason we (I) got it wrong to begin with is just lack of thought about the NULLs case. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-patch; charset="us-ascii" Content-ID: <6620.1320089926.2@sss.pgh.pa.us> Content-Description: stop-at-nulls.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbt= ree/nbtutils.c index 134abe6d596413a1ad54c0c7f378248251295d4a..c00255567116e3b6f7bd8426eb= 32b2398c8d95d5 100644 *** a/src/backend/access/nbtree/nbtutils.c --- b/src/backend/access/nbtree/nbtutils.c *************** _bt_checkkeys(IndexScanDesc scan, *** 1384,1398 **** } = /* ! * Tuple fails this qual. If it's a required qual for the current ! * scan direction, then we can conclude no further tuples will ! * pass, either. */ ! if ((key->sk_flags & SK_BT_REQFWD) && ! ScanDirectionIsForward(dir)) ! *continuescan =3D false; ! else if ((key->sk_flags & SK_BT_REQBKWD) && ! ScanDirectionIsBackward(dir)) *continuescan =3D false; = /* --- 1394,1409 ---- } = /* ! * Tuple fails this qual. If it's a required qual, then we can ! * conclude no further tuples will pass, either. We can stop ! * regardless of the scan direction, because we know that NULLs ! * sort to one end or the other of the range of values. If this ! * tuple doesn't pass, then no future ones will either, until we ! * reach the next set of values of the higher-order index attrs ! * (if any) ... and those attrs must have equality quals, else ! * this one wouldn't be marked required. */ ! if (key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) *continuescan =3D false; = /* *************** _bt_checkkeys(IndexScanDesc scan, *** 1403,1434 **** = if (isNull) { ! if (key->sk_flags & SK_BT_NULLS_FIRST) ! { ! /* ! * Since NULLs are sorted before non-NULLs, we know we have ! * reached the lower limit of the range of values for this ! * index attr. On a backward scan, we can stop if this qual ! * is one of the "must match" subset. On a forward scan, ! * however, we should keep going. ! */ ! if ((key->sk_flags & SK_BT_REQBKWD) && ! ScanDirectionIsBackward(dir)) ! *continuescan =3D false; ! } ! else ! { ! /* ! * Since NULLs are sorted after non-NULLs, we know we have ! * reached the upper limit of the range of values for this ! * index attr. On a forward scan, we can stop if this qual is ! * one of the "must match" subset. On a backward scan, ! * however, we should keep going. ! */ ! if ((key->sk_flags & SK_BT_REQFWD) && ! ScanDirectionIsForward(dir)) ! *continuescan =3D false; ! } = /* * In any case, this indextuple doesn't match the qual. --- 1414,1428 ---- = if (isNull) { ! /* ! * The index entry is NULL, so it must fail this qual (we assume ! * all btree operators are strict). Furthermore, we know that ! * all remaining entries with the same higher-order index attr ! * values must be NULLs too. So, just as above, we can stop the ! * scan regardless of direction, if the qual is required. ! */ ! if (key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) ! *continuescan =3D false; = /* * In any case, this indextuple doesn't match the qual. *************** _bt_check_rowcompare(ScanKey skey, Index *** 1508,1539 **** = if (isNull) { ! if (subkey->sk_flags & SK_BT_NULLS_FIRST) ! { ! /* ! * Since NULLs are sorted before non-NULLs, we know we have ! * reached the lower limit of the range of values for this ! * index attr. On a backward scan, we can stop if this qual is ! * one of the "must match" subset. On a forward scan, ! * however, we should keep going. ! */ ! if ((subkey->sk_flags & SK_BT_REQBKWD) && ! ScanDirectionIsBackward(dir)) ! *continuescan =3D false; ! } ! else ! { ! /* ! * Since NULLs are sorted after non-NULLs, we know we have ! * reached the upper limit of the range of values for this ! * index attr. On a forward scan, we can stop if this qual is ! * one of the "must match" subset. On a backward scan, ! * however, we should keep going. ! */ ! if ((subkey->sk_flags & SK_BT_REQFWD) && ! ScanDirectionIsForward(dir)) ! *continuescan =3D false; ! } = /* * In any case, this indextuple doesn't match the qual. --- 1502,1516 ---- = if (isNull) { ! /* ! * The index entry is NULL, so it must fail this qual (we assume ! * all btree operators are strict). Furthermore, we know that ! * all remaining entries with the same higher-order index attr ! * values must be NULLs too. So, just as above, we can stop the ! * scan regardless of direction, if the qual is required. ! */ ! if (subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) ! *continuescan =3D false; = /* * In any case, this indextuple doesn't match the qual. ------- =_aaaaaaaaaa0--