Remove BTScanOpaqueData.firstPage
authorAlexander Korotkov <akorotkov@postgresql.org>
Wed, 27 Dec 2023 12:21:49 +0000 (14:21 +0200)
committerAlexander Korotkov <akorotkov@postgresql.org>
Wed, 27 Dec 2023 12:21:49 +0000 (14:21 +0200)
It's not necessary to keep the firstPage flag as a field of BTScanOpaqueData.
This commit makes it an argument of the _bt_readpage() function.  We can easily
distinguish first-time and repeated calls (within the scan) of this function.

Reported-by: Peter Geoghegan
Discussion: https://postgr.es/m/CAH2-Wzk4SOsw%2BtHuTFiz8U9Jqj-R77rYPkhWKODCBb1mdHACXA%40mail.gmail.com
Reviewed-by: Pavel Borisov
src/backend/access/nbtree/nbtree.c
src/backend/access/nbtree/nbtsearch.c
src/include/access/nbtree.h

index 6c8cd93fa0a50fc238a5063128fa4630934a607d..dd6dc0971bcf5e5c7631a781159995298de1786b 100644 (file)
@@ -409,7 +409,6 @@ btrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
 
        so->markItemIndex = -1;
        so->arrayKeyCount = 0;
-       so->firstPage = false;
        BTScanPosUnpinIfPinned(so->markPos);
        BTScanPosInvalidate(so->markPos);
 
index be61b3868f42fbe5a87c2f4cbf31f251f812af58..b0c65a42d6d6af9e67174c84c5ed0d178740811c 100644 (file)
@@ -30,7 +30,7 @@ static OffsetNumber _bt_binsrch(Relation rel, BTScanInsert key, Buffer buf);
 static int     _bt_binsrch_posting(BTScanInsert key, Page page,
                                                                OffsetNumber offnum);
 static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir,
-                                                OffsetNumber offnum);
+                                                OffsetNumber offnum, bool firstPage);
 static void _bt_saveitem(BTScanOpaque so, int itemIndex,
                                                 OffsetNumber offnum, IndexTuple itup);
 static int     _bt_setuppostingitems(BTScanOpaque so, int itemIndex,
@@ -1395,7 +1395,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
        offnum = _bt_binsrch(rel, &inskey, buf);
        Assert(!BTScanPosIsValid(so->currPos));
        so->currPos.buf = buf;
-       so->firstPage = true;
 
        /*
         * Now load data from the first page of the scan.
@@ -1416,7 +1415,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
         * for the page.  For example, when inskey is both < the leaf page's high
         * key and > all of its non-pivot tuples, offnum will be "maxoff + 1".
         */
-       if (!_bt_readpage(scan, dir, offnum))
+       if (!_bt_readpage(scan, dir, offnum, true))
        {
                /*
                 * There's no actually-matching data on this page.  Try to advance to
@@ -1520,7 +1519,8 @@ _bt_next(IndexScanDesc scan, ScanDirection dir)
  * Returns true if any matching items found on the page, false if none.
  */
 static bool
-_bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
+_bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum,
+                        bool firstPage)
 {
        BTScanOpaque so = (BTScanOpaque) scan->opaque;
        Page            page;
@@ -1601,7 +1601,7 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
         * We skip this for the first page in the scan to evade the possible
         * slowdown of the point queries.
         */
-       if (!so->firstPage && minoff < maxoff)
+       if (!firstPage && minoff < maxoff)
        {
                ItemId          iid;
                IndexTuple      itup;
@@ -1620,7 +1620,6 @@ _bt_readpage(IndexScanDesc scan, ScanDirection dir, OffsetNumber offnum)
        }
        else
        {
-               so->firstPage = false;
                requiredMatchedByPrecheck = false;
        }
 
@@ -2079,7 +2078,7 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
                                PredicateLockPage(rel, blkno, scan->xs_snapshot);
                                /* see if there are any matches on this page */
                                /* note that this will clear moreRight if we can stop */
-                               if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque)))
+                               if (_bt_readpage(scan, dir, P_FIRSTDATAKEY(opaque), false))
                                        break;
                        }
                        else if (scan->parallel_scan != NULL)
@@ -2170,7 +2169,7 @@ _bt_readnextpage(IndexScanDesc scan, BlockNumber blkno, ScanDirection dir)
                                PredicateLockPage(rel, BufferGetBlockNumber(so->currPos.buf), scan->xs_snapshot);
                                /* see if there are any matches on this page */
                                /* note that this will clear moreLeft if we can stop */
-                               if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page)))
+                               if (_bt_readpage(scan, dir, PageGetMaxOffsetNumber(page), false))
                                        break;
                        }
                        else if (scan->parallel_scan != NULL)
@@ -2487,14 +2486,13 @@ _bt_endpoint(IndexScanDesc scan, ScanDirection dir)
 
        /* remember which buffer we have pinned */
        so->currPos.buf = buf;
-       so->firstPage = true;
 
        _bt_initialize_more_data(so, dir);
 
        /*
         * Now load data from the first page of the scan.
         */
-       if (!_bt_readpage(scan, dir, start))
+       if (!_bt_readpage(scan, dir, start, false))
        {
                /*
                 * There's no actually-matching data on this page.  Try to advance to
index 5e083591a62e40cc0602340e545cb1061e4643e8..dab84c90baedc0d8aa4484476a8dadc6136d8206 100644 (file)
@@ -1051,9 +1051,6 @@ typedef struct BTScanOpaqueData
        int                *killedItems;        /* currPos.items indexes of killed items */
        int                     numKilled;              /* number of currently stored items */
 
-       /* flag indicating the first page in the scan */
-       bool            firstPage;
-
        /*
         * If we are doing an index-only scan, these are the tuple storage
         * workspaces for the currPos and markPos respectively.  Each is of size