diff --git a/src/backend/access/hash/hashsearch.c b/src/backend/access/hash/hashsearch.c
index 995498e48d..4bc4360173 100644
--- a/src/backend/access/hash/hashsearch.c
+++ b/src/backend/access/hash/hashsearch.c
@@ -457,6 +457,7 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
 	Page		page;
 	HashPageOpaque opaque;
 	OffsetNumber offnum;
+	BlockNumber blkno;
 	uint16		itemIndex;
 
 	buf = *bufP;
@@ -470,8 +471,6 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
 
 	if (ScanDirectionIsForward(dir))
 	{
-		BlockNumber prev_blkno = InvalidBlockNumber;
-
 		for (;;)
 		{
 			/* new page, locate starting position by binary search */
@@ -496,9 +495,9 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
 			 */
 			if (so->currPos.buf == so->hashso_bucket_buf ||
 				so->currPos.buf == so->hashso_split_bucket_buf)
-				prev_blkno = InvalidBlockNumber;
+				blkno = InvalidBlockNumber;
 			else
-				prev_blkno = opaque->hasho_prevblkno;
+				blkno = opaque->hasho_prevblkno;
 
 			_hash_readnext(scan, &buf, &page, &opaque);
 			if (BufferIsValid(buf))
@@ -516,7 +515,7 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
 				 * _hash_kill_items to be called for the old page after this
 				 * function returns.
 				 */
-				so->currPos.prevPage = prev_blkno;
+				so->currPos.prevPage = blkno;
 				so->currPos.nextPage = InvalidBlockNumber;
 				so->currPos.buf = buf;
 				return false;
@@ -529,8 +528,6 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
 	}
 	else
 	{
-		BlockNumber next_blkno = InvalidBlockNumber;
-
 		for (;;)
 		{
 			/* new page, locate starting position by binary search */
@@ -551,7 +548,9 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
 
 			if (so->currPos.buf == so->hashso_bucket_buf ||
 				so->currPos.buf == so->hashso_split_bucket_buf)
-				next_blkno = opaque->hasho_nextblkno;
+				blkno = opaque->hasho_nextblkno;
+			else
+				blkno = InvalidBlockNumber;
 
 			_hash_readprev(scan, &buf, &page, &opaque);
 			if (BufferIsValid(buf))
@@ -570,7 +569,7 @@ _hash_readpage(IndexScanDesc scan, Buffer *bufP, ScanDirection dir)
 				 * function returns.
 				 */
 				so->currPos.prevPage = InvalidBlockNumber;
-				so->currPos.nextPage = next_blkno;
+				so->currPos.nextPage = blkno;
 				so->currPos.buf = buf;
 				return false;
 			}