Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 892a51c

Browse files
committed
Fix longstanding error in _bt_search(): should moveright at top of loop not
bottom. Otherwise we fail to moveright when the root page was split while we were "in flight" to it. This is not a significant problem when the root is above the leaf level, but if the root was also a leaf (ie, a single-page index just got split) we may return the wrong leaf page to the caller, resulting in failure to find a key that is in fact present. Bug has existed at least since 7.1, probably forever.
1 parent 5e3c09a commit 892a51c

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/backend/access/nbtree/nbtsearch.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.76 2003/07/28 00:09:14 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.77 2003/07/29 22:18:38 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -62,6 +62,13 @@ _bt_search(Relation rel, int keysz, ScanKey scankey,
6262
BlockNumber par_blkno;
6363
BTStack new_stack;
6464

65+
/*
66+
* Race -- the page we just grabbed may have split since we read
67+
* its pointer in the parent (or metapage). If it has, we may need
68+
* to move right to its new sibling. Do that.
69+
*/
70+
*bufP = _bt_moveright(rel, *bufP, keysz, scankey, BT_READ);
71+
6572
/* if this is a leaf page, we're done */
6673
page = BufferGetPage(*bufP);
6774
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
@@ -99,13 +106,6 @@ _bt_search(Relation rel, int keysz, ScanKey scankey,
99106
_bt_relbuf(rel, *bufP);
100107
*bufP = _bt_getbuf(rel, blkno, BT_READ);
101108

102-
/*
103-
* Race -- the page we just grabbed may have split since we read
104-
* its pointer in the parent. If it has, we may need to move
105-
* right to its new sibling. Do that.
106-
*/
107-
*bufP = _bt_moveright(rel, *bufP, keysz, scankey, BT_READ);
108-
109109
/* okay, all set to move down a level */
110110
stack_in = new_stack;
111111
}
@@ -599,8 +599,8 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
599599
/*
600600
* At this point we are positioned at the first item >= scan key, or
601601
* possibly at the end of a page on which all the existing items are
602-
* greater than the scan key and we know that everything on later
603-
* pages is less than or equal to scan key.
602+
* less than the scan key and we know that everything on later
603+
* pages is greater than or equal to scan key.
604604
*
605605
* We could step forward in the latter case, but that'd be a waste of
606606
* time if we want to scan backwards. So, it's now time to examine

0 commit comments

Comments
 (0)