Re: [NOVICE] WHERE clause not used when index is used

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Petr Jelinek <petr(at)2ndquadrant(dot)com>
Cc: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, Tobias Florek <postgres(at)ibotty(dot)net>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Simon Riggs <simon(at)2ndquadrant(dot)com>
Subject: Re: [NOVICE] WHERE clause not used when index is used
Date: 2016-03-01 21:20:33
Message-ID: 26257.1456867233@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-novice

I wrote:
> I believe the way to fix this would be to stop regarding SK_BT_MATCHED
> as state, and instead treat it as a scankey property identified during
> _bt_preprocess_keys, analogously to SK_BT_REQFWD/SK_BT_REQBKWD --- and,
> like those, you'd need two flags not one since the properties will be
> determined independently of knowing which direction you'll be going in.

BTW, the analogy to SK_BT_REQFWD/SK_BT_REQBKWD exposes another way in
which the patch leaves money on the table: if the leading key is "=" then
MATCHED behavior can't apply to it, but it might apply to a later key.

I'm imagining a specification like this (in the comments for
_bt_preprocess_keys, after the para starting "The output keys are marked
with flags SK_BT_REQFWD and/or SK_BT_REQBKWD ..."):

* Another property of the first attribute without an "=" key is that it may
* not be necessary to recheck its value at each index entry as we scan
* through the index. Again considering "x = 1 AND y < 4 AND z < 5", once we
* have positioned to an entry satisfying those keys, it is unnecessary to
* recheck "y < 4" as we scan forward, at least so long as the index's y
* value is not NULL. Every later row with x=1 must have y>=4; though we
* can't make any similar statement about z. Similarly, a key like "y > 4"
* need not be rechecked in a backwards scan. We mark appropriate keys with
* flags SK_BT_NORECHECK_FWD or SK_BT_NORECHECK_BKWD to indicate that _bt_next
* can skip checking those keys (at non-null index entries) when scanning in
* the indicated direction.

I'm also wondering whether it'd be worth taking more care about the
handling of index entries containing some null columns. Right now,
the presence of any nulls disables the MATCH improvement, but it would
still apply if the null(s) are in lower-order columns. I'm not sure
if that case comes up often enough to justify checking the flag bit
twice per iteration, but it might.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jim Nasby 2016-03-01 21:23:27 Re: Improve error handling in pltcl
Previous Message David Steele 2016-03-01 21:15:32 Re: 2016-03 Commitfest Manager

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2016-03-01 21:44:52 Re: [NOVICE] WHERE clause not used when index is used
Previous Message Tom Lane 2016-03-01 20:03:39 Re: [NOVICE] WHERE clause not used when index is used