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/2101:23:06 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.85 2003/12/2103:00:04 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -495,10 +495,9 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
495495BlockNumber blkno ;
496496StrategyNumber strat ;
497497bool res ;
498- int32 result ;
499498bool nextkey ;
500499bool continuescan ;
501- ScanKey scankeys = NULL ;
500+ ScanKey scankeys ;
502501ScanKey * startKeys = NULL ;
503502int keysCount = 0 ;
504503int i ;
@@ -695,8 +694,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
695694
696695pfree (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+
749748if (!BufferIsValid (buf ))
750749{
751750/* Only get here if index is completely empty */
@@ -765,6 +764,9 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
765764
766765ItemPointerSet (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 */
804806if (!_bt_step (scan ,& buf ,BackwardScanDirection ))
805- {
806- pfree (scankeys );
807807return false;
808- }
809808break ;
810809
811810case BTLessEqualStrategyNumber :
@@ -818,10 +817,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
818817 * that is always the correct starting position.)
819818 */
820819if (!_bt_step (scan ,& buf ,BackwardScanDirection ))
821- {
822- pfree (scankeys );
823820return false;
824- }
825821break ;
826822
827823case 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 */
840836if (!_bt_step (scan ,& buf ,BackwardScanDirection ))
841- {
842- pfree (scankeys );
843837return false;
844- }
845838}
846839else
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 */
855848if (offnum > PageGetMaxOffsetNumber (page ))
856849{
857850if (!_bt_step (scan ,& buf ,ForwardScanDirection ))
858- {
859- pfree (scankeys );
860851return 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- gotonomatches ;/* no equal items! */
871854break ;
872855
873856case BTGreaterEqualStrategyNumber :
@@ -879,10 +862,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
879862if (offnum > PageGetMaxOffsetNumber (page ))
880863{
881864if (!_bt_step (scan ,& buf ,ForwardScanDirection ))
882- {
883- pfree (scankeys );
884865return false;
885- }
886866}
887867break ;
888868
@@ -895,10 +875,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
895875if (offnum > PageGetMaxOffsetNumber (page ))
896876{
897877if (!_bt_step (scan ,& buf ,ForwardScanDirection ))
898- {
899- pfree (scankeys );
900878return false;
901- }
902879}
903880break ;
904881}
@@ -924,15 +901,12 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
924901else
925902{
926903/* no tuples in the index match this scan key */
927- nomatches :
928904ItemPointerSetInvalid (current );
929905so -> btso_curbuf = InvalidBuffer ;
930906_bt_relbuf (rel ,buf );
931907res = false;
932908}
933909
934- pfree (scankeys );
935-
936910return res ;
937911}
938912