Skip to content

Commit 606c012

Browse files
Reduce btree scan overhead for < and > strategies
For <, <=, > and >= strategies, mark the first scan key as already matched if scanning in an appropriate direction. If index tuple contains no nulls we can skip the first re-check for each tuple. Author: Rajeev Rastogi Reviewer: Haribabu Kommi Rework of the code and comments by Simon Riggs
1 parent dedae6c commit 606c012

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/backend/access/nbtree/nbtsearch.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,33 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
996996
if (goback)
997997
offnum = OffsetNumberPrev(offnum);
998998

999+
/*
1000+
* By here the scan position is now set for the first key. If all
1001+
* further tuples are expected to match we set the SK_BT_MATCHED flag
1002+
* to avoid re-checking the scan key later. This is a big win for
1003+
* slow key matches though is still significant even for fast datatypes.
1004+
*/
1005+
switch (startKeys[0]->sk_strategy)
1006+
{
1007+
case BTEqualStrategyNumber:
1008+
break;
1009+
1010+
case BTGreaterEqualStrategyNumber:
1011+
case BTGreaterStrategyNumber:
1012+
if (ScanDirectionIsForward(dir))
1013+
startKeys[0]->sk_flags |= SK_BT_MATCHED;
1014+
break;
1015+
1016+
case BTLessEqualStrategyNumber:
1017+
case BTLessStrategyNumber:
1018+
if (ScanDirectionIsBackward(dir))
1019+
startKeys[0]->sk_flags |= SK_BT_MATCHED;
1020+
break;
1021+
1022+
default:
1023+
break;
1024+
}
1025+
9991026
/*
10001027
* Now load data from the first page of the scan.
10011028
*/

src/backend/access/nbtree/nbtutils.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,13 @@ _bt_checkkeys(IndexScanDesc scan,
14291429
bool isNull;
14301430
Datum test;
14311431

1432+
/*
1433+
* If the scan key has already matched we can skip this key, as
1434+
* long as the index tuple does not contain NULL values.
1435+
*/
1436+
if (key->sk_flags & SK_BT_MATCHED && !IndexTupleHasNulls(tuple))
1437+
continue;
1438+
14321439
/* row-comparison keys need special processing */
14331440
if (key->sk_flags & SK_ROW_HEADER)
14341441
{

src/include/access/nbtree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ typedef BTScanOpaqueData *BTScanOpaque;
618618
*/
619619
#define SK_BT_REQFWD 0x00010000 /* required to continue forward scan */
620620
#define SK_BT_REQBKWD 0x00020000 /* required to continue backward scan */
621+
#define SK_BT_MATCHED 0x00040000 /* required to skip further key match */
621622
#define SK_BT_INDOPTION_SHIFT 24 /* must clear the above bits */
622623
#define SK_BT_DESC (INDOPTION_DESC << SK_BT_INDOPTION_SHIFT)
623624
#define SK_BT_NULLS_FIRST (INDOPTION_NULLS_FIRST << SK_BT_INDOPTION_SHIFT)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy