pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Haas <robertmhaas@gmail.com>
Cc: Maksym Boguk <maxim.boguk@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #6278: Index scans on '>' condition on field with many NULLS
Date: Mon, 31 Oct 2011 12:43:48 -0400
Message-ID: <28958.1320079428@sss.pgh.pa.us> (raw)
In-Reply-To: <26438.1320071858@sss.pgh.pa.us>
References: <201110310339.p9V3dtIb015254@wwwmaster.postgresql.org>
	<CA+TgmoaF9uU0BGgLsje8Mz0AdgJ1=1Znyqbq3xewvz-S0dEHYg@mail.gmail.com>
	<26438.1320071858@sss.pgh.pa.us>

I wrote:
> I poked at this a bit last night.  The reason it's happening is that the
> ">" key is only marked SK_BT_REQBKWD, not SK_BT_REQFWD, so _bt_checkkeys
> doesn't think it can stop when it hits the NULLs.  Right at the moment
> it seems like we could mark that key with both flags, which leads to the
> conclusion that two flags are unnecessary and we could get by with only
> one direction-independent flag.

OK, I swapped some of this knowledge back in.  The above idea would work
in simple cases with non-redundant scan keys, but in general not.
Consider an indexscan on x with

	WHERE x > 4

We'll start scanning at the x>4 boundary point and run forward.  The
scankey condition can only fail once we reach NULL index entries, at
which point we can stop, so in this case marking this key SK_BT_REQFWD
would be OK.  However, consider something like

	WHERE x > 4 AND x > 10

where _bt_preprocess_keys is unable to determine which key is more
restrictive.  (This could only happen if the comparison constants are of
different types and we don't have a suitable cross-type comparison
operator in the opfamily.  None of the standard opfamilies have such
omissions, which is why there's no regression test covering this.)
In such a case, _bt_first arbitrarily picks one of the keys to do the
initial positioning with.  If it picks x>4, then the x>10 scankey will
fail until we reach index entries > 10 ... but we can't abandon the
scan just because x>10 is failing.  This is why we have to have
direction-sensitive required-key flags.  (If we were to start backing
up, failure of either of the redundant keys would indeed be
justification for stopping.)

We could possibly do something with marking only non-redundant >/>=
scankeys as SK_BT_REQFWD, and conversely for </<= keys.  But this
looks like it'd be a pain to manage in _bt_preprocess_keys, and given
the difficulty of testing cases where it matters, I'm not excited about
going that direction.

What I'm currently thinking is that we could hack _bt_checkkeys to
stop the scan when it reaches NULLs if the current scankey is not an
ISNULL test and is marked *either* SK_BT_REQFWD or SK_BT_REQBKWD.
In such a case we know that the scankey cannot be satisfied by a NULL,
so it will fail until we reach the next range of this index column,
ie move to the next value of the next higher-order column (if any).
And that value can't pass the scankeys, because it must have an equality
constraint, else this key wouldn't be marked required.

BTW, I had thought that Maksym could work around this with something
like
	WHERE value > 0.9999 and value < 1.1
or
	WHERE value > 0.9999 and value is not null
but it turns out that neither of those work.  The second key will be
marked SK_BT_REQFWD, but because _bt_checkkeys stops testing as soon
as any key has failed, it doesn't reach the second key and doesn't
realize that there's a failing SK_BT_REQFWD key available.  That's
a little bit annoying, but the only clear fix would be to keep testing
keys after we already know the index entry has failed, and I think
that's probably going to be a net loss.  If we fix the NULL case to
accept either flag, then the only case where this will matter is
redundant scan keys, and that's not a case that I think we should
optimize at the cost of pessimizing normal cases.

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.

			regards, tom lane



view thread (7+ messages)  latest in thread

Message-ID: <28958.1320079428@sss.pgh.pa.us>
Permalink:  ../28958.1320079428@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/28958.1320079428@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, robertmhaas@gmail.com, maxim.boguk@gmail.com
  Subject: Re: BUG #6278: Index scans on '>' condition on field with many NULLS
  In-Reply-To: <28958.1320079428@sss.pgh.pa.us>
  References: <201110310339.p9V3dtIb015254@wwwmaster.postgresql.org> <CA+TgmoaF9uU0BGgLsje8Mz0AdgJ1=1Znyqbq3xewvz-S0dEHYg@mail.gmail.com> <26438.1320071858@sss.pgh.pa.us> <28958.1320079428@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox