diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c
index b17347b214..866925ac13 100644
--- a/src/backend/utils/sort/tuplesort.c
+++ b/src/backend/utils/sort/tuplesort.c
@@ -4355,6 +4355,16 @@ comparetup_index_hash(const SortTuple *a, const SortTuple *b,
 	else if (bucket1 < bucket2)
 		return -1;
 
+	/*
+	 * If bucket values are equal, sort by hash values. This allows us to
+	 * insert directly onto bucket/overflow pages, where the index tuples
+	 * are stored in hash order to allow fast binary search within each page.
+	 */
+	if (a->datum1 > b->datum1)
+		return 1;
+	else if (a->datum1 < b->datum1)
+		return -1;
+
 	/*
 	 * If hash values are equal, we sort on ItemPointer.  This does not affect
 	 * validity of the finished index, but it may be useful to have index
