From 912bad2ec8c66ccd01cebf1f69233b004c633243 Mon Sep 17 00:00:00 2001 From: Kyotaro Horiguchi Date: Thu, 6 Feb 2020 19:09:09 +0900 Subject: [PATCH 2/2] crude fix --- src/backend/access/nbtree/nbtsearch.c | 43 +++++++++++++++++---------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c index 5cd97d8bb5..1f18b38ca5 100644 --- a/src/backend/access/nbtree/nbtsearch.c +++ b/src/backend/access/nbtree/nbtsearch.c @@ -1619,6 +1619,9 @@ _bt_skip(IndexScanDesc scan, ScanDirection dir, nextOffset = startOffset = ItemPointerGetOffsetNumber(&scan->xs_itup->t_tid); + if (nextOffset != startOffset) + LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); + while (nextOffset == startOffset) { IndexTuple itup; @@ -1653,7 +1656,7 @@ _bt_skip(IndexScanDesc scan, ScanDirection dir, offnum = OffsetNumberPrev(offnum); /* Check if _bt_readpage returns already found item */ - if (!_bt_readpage(scan, indexdir, offnum)) + if (!_bt_readpage(scan, dir, offnum)) { /* * There's no actually-matching data on this page. Try to @@ -1668,6 +1671,8 @@ _bt_skip(IndexScanDesc scan, ScanDirection dir, return false; } } + else + LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); currItem = &so->currPos.items[so->currPos.lastItem]; itup = (IndexTuple) (so->currTuples + currItem->tupleOffset); @@ -1721,24 +1726,30 @@ _bt_skip(IndexScanDesc scan, ScanDirection dir, } /* Now read the data */ - if (!_bt_readpage(scan, indexdir, offnum)) + if (!(ScanDirectionIsForward(dir) && + ScanDirectionIsBackward(indexdir)) || + scanstart) { - /* - * There's no actually-matching data on this page. Try to advance to - * the next page. Return false if there's no matching data at all. - */ - LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); - if (!_bt_steppage(scan, dir)) + if (!_bt_readpage(scan, dir, offnum)) { - pfree(so->skipScanKey); - so->skipScanKey = NULL; - return false; + /* + * There's no actually-matching data on this page. Try to advance + * to the next page. Return false if there's no matching data at + * all. + */ + LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); + if (!_bt_steppage(scan, dir)) + { + pfree(so->skipScanKey); + so->skipScanKey = NULL; + return false; + } + } + else + { + /* Drop the lock, and maybe the pin, on the current page */ + LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); } - } - else - { - /* Drop the lock, and maybe the pin, on the current page */ - LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK); } /* And set IndexTuple */ -- 2.18.2