Skip to content

Commit 2a0caef

Browse files
committed
Previous change exposed some opportunities for further simplification
in _bt_first().
1 parent 569659a commit 2a0caef

File tree

1 file changed

+11
-37
lines changed

1 file changed

+11
-37
lines changed

src/backend/access/nbtree/nbtsearch.c

+11-37
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-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.84 2003/12/21 01:23:06 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.85 2003/12/21 03:00:04 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -495,10 +495,9 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
495495
BlockNumber blkno;
496496
StrategyNumber strat;
497497
bool res;
498-
int32 result;
499498
bool nextkey;
500499
bool continuescan;
501-
ScanKey scankeys = NULL;
500+
ScanKey scankeys;
502501
ScanKey *startKeys = NULL;
503502
int keysCount = 0;
504503
int i;
@@ -695,8 +694,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
695694

696695
pfree(startKeys);
697696

698-
current = &(scan->currentItemData);
699-
700697
/*
701698
* We want to locate either the first item >= boundary point, or
702699
* first item > boundary point, depending on the initial-positioning
@@ -746,6 +743,8 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
746743
/* don't need to keep the stack around... */
747744
_bt_freestack(stack);
748745

746+
current = &(scan->currentItemData);
747+
749748
if (!BufferIsValid(buf))
750749
{
751750
/* Only get here if index is completely empty */
@@ -765,6 +764,9 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
765764

766765
ItemPointerSet(current, blkno, offnum);
767766

767+
/* done with manufactured scankey, now */
768+
pfree(scankeys);
769+
768770
/*
769771
* It's now time to examine the initial-positioning strategy to find the
770772
* exact place to start the scan.
@@ -802,10 +804,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
802804
* that is always the correct starting position.)
803805
*/
804806
if (!_bt_step(scan, &buf, BackwardScanDirection))
805-
{
806-
pfree(scankeys);
807807
return false;
808-
}
809808
break;
810809

811810
case BTLessEqualStrategyNumber:
@@ -818,10 +817,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
818817
* that is always the correct starting position.)
819818
*/
820819
if (!_bt_step(scan, &buf, BackwardScanDirection))
821-
{
822-
pfree(scankeys);
823820
return false;
824-
}
825821
break;
826822

827823
case BTEqualStrategyNumber:
@@ -834,40 +830,27 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
834830
/*
835831
* We are on first item > scankey.
836832
*
837-
* Back up one to arrive at last item <= scankey, then
838-
* check to see if it is equal to scankey.
833+
* Back up one to arrive at last item <= scankey.
834+
* We will check below to see if it is equal to scankey.
839835
*/
840836
if (!_bt_step(scan, &buf, BackwardScanDirection))
841-
{
842-
pfree(scankeys);
843837
return false;
844-
}
845838
}
846839
else
847840
{
848841
/*
849842
* We are on first item >= scankey.
850843
*
851844
* Make sure we are on a real item; might have to
852-
* step forward if currently at end of page. Then check
853-
* to see if it is equal to scankey.
845+
* step forward if currently at end of page.
846+
* We will check below to see if it is equal to scankey.
854847
*/
855848
if (offnum > PageGetMaxOffsetNumber(page))
856849
{
857850
if (!_bt_step(scan, &buf, ForwardScanDirection))
858-
{
859-
pfree(scankeys);
860851
return false;
861-
}
862852
}
863853
}
864-
865-
/* If we are not now on an equal item, then there ain't any. */
866-
offnum = ItemPointerGetOffsetNumber(current);
867-
page = BufferGetPage(buf);
868-
result = _bt_compare(rel, keysCount, scankeys, page, offnum);
869-
if (result != 0)
870-
goto nomatches; /* no equal items! */
871854
break;
872855

873856
case BTGreaterEqualStrategyNumber:
@@ -879,10 +862,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
879862
if (offnum > PageGetMaxOffsetNumber(page))
880863
{
881864
if (!_bt_step(scan, &buf, ForwardScanDirection))
882-
{
883-
pfree(scankeys);
884865
return false;
885-
}
886866
}
887867
break;
888868

@@ -895,10 +875,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
895875
if (offnum > PageGetMaxOffsetNumber(page))
896876
{
897877
if (!_bt_step(scan, &buf, ForwardScanDirection))
898-
{
899-
pfree(scankeys);
900878
return false;
901-
}
902879
}
903880
break;
904881
}
@@ -924,15 +901,12 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
924901
else
925902
{
926903
/* no tuples in the index match this scan key */
927-
nomatches:
928904
ItemPointerSetInvalid(current);
929905
so->btso_curbuf = InvalidBuffer;
930906
_bt_relbuf(rel, buf);
931907
res = false;
932908
}
933909

934-
pfree(scankeys);
935-
936910
return res;
937911
}
938912

0 commit comments

Comments
 (0)