diff --git a/src/backend/access/hash/hashsort.c b/src/backend/access/hash/hashsort.c
index 0e0f393..18a788f 100644
--- a/src/backend/access/hash/hashsort.c
+++ b/src/backend/access/hash/hashsort.c
@@ -37,7 +37,7 @@ struct HSpool
 {
 	Tuplesortstate *sortstate;	/* state data for tuplesort.c */
 	Relation	index;
-	uint32		hash_mask;		/* bitmask for hash codes */
+	uint32		hash_mod;		/* mask for hash codes */
 };
 
 
@@ -52,15 +52,12 @@ _h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
 	hspool->index = index;
 
 	/*
-	 * Determine the bitmask for hash code values.  Since there are currently
-	 * num_buckets buckets in the index, the appropriate mask can be computed
-	 * as follows.
-	 *
-	 * Note: at present, the passed-in num_buckets is always a power of 2, so
-	 * we could just compute num_buckets - 1.  We prefer not to assume that
-	 * here, though.
+	 * At this point max_buckets in hash index is num_buckets - 1.
+	 * The "hash key mod num_buckets" will indicate which bucket does the
+	 * hash key belongs to, and will be used to sort the index tuples based on
+	 * their bucket.
 	 */
-	hspool->hash_mask = (((uint32) 1) << _hash_log2(num_buckets)) - 1;
+	hspool->hash_mod = num_buckets;
 
 	/*
 	 * We size the sort area as maintenance_work_mem rather than work_mem to
@@ -69,7 +66,7 @@ _h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
 	 */
 	hspool->sortstate = tuplesort_begin_index_hash(heap,
 												   index,
-												   hspool->hash_mask,
+												   hspool->hash_mod,
 												   maintenance_work_mem,
 												   false);
 
@@ -122,7 +119,7 @@ _h_indexbuild(HSpool *hspool, Relation heapRel)
 #ifdef USE_ASSERT_CHECKING
 		uint32		lasthashkey = hashkey;
 
-		hashkey = _hash_get_indextuple_hashkey(itup) & hspool->hash_mask;
+		hashkey = _hash_get_indextuple_hashkey(itup) % hspool->hash_mod;
 		Assert(hashkey >= lasthashkey);
 #endif
 
diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c
index e1e692d..8ff50a1 100644
--- a/src/backend/utils/sort/tuplesort.c
+++ b/src/backend/utils/sort/tuplesort.c
@@ -473,7 +473,7 @@ struct Tuplesortstate
 	bool		enforceUnique;	/* complain if we find duplicate tuples */
 
 	/* These are specific to the index_hash subcase: */
-	uint32		hash_mask;		/* mask for sortable part of hash code */
+	uint32		hash_mod;		/* mask for sortable part of hash code */
 
 	/*
 	 * These variables are specific to the Datum case; they are set by
@@ -991,7 +991,7 @@ tuplesort_begin_index_btree(Relation heapRel,
 Tuplesortstate *
 tuplesort_begin_index_hash(Relation heapRel,
 						   Relation indexRel,
-						   uint32 hash_mask,
+						   uint32 hash_mod,
 						   int workMem, bool randomAccess)
 {
 	Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
@@ -1002,8 +1002,8 @@ tuplesort_begin_index_hash(Relation heapRel,
 #ifdef TRACE_SORT
 	if (trace_sort)
 		elog(LOG,
-		"begin index sort: hash_mask = 0x%x, workMem = %d, randomAccess = %c",
-			 hash_mask,
+		"begin index sort: hash_mod = 0x%x, workMem = %d, randomAccess = %c",
+			 hash_mod,
 			 workMem, randomAccess ? 't' : 'f');
 #endif
 
@@ -1017,7 +1017,7 @@ tuplesort_begin_index_hash(Relation heapRel,
 	state->heapRel = heapRel;
 	state->indexRel = indexRel;
 
-	state->hash_mask = hash_mask;
+	state->hash_mod = hash_mod;
 
 	MemoryContextSwitchTo(oldcontext);
 
@@ -4167,9 +4167,9 @@ comparetup_index_hash(const SortTuple *a, const SortTuple *b,
 	 * that the first column of the index tuple is the hash key.
 	 */
 	Assert(!a->isnull1);
-	hash1 = DatumGetUInt32(a->datum1) & state->hash_mask;
+	hash1 = DatumGetUInt32(a->datum1) % state->hash_mod;
 	Assert(!b->isnull1);
-	hash2 = DatumGetUInt32(b->datum1) & state->hash_mask;
+	hash2 = DatumGetUInt32(b->datum1) % state->hash_mod;
 
 	if (hash1 > hash2)
 		return 1;
diff --git a/src/include/utils/tuplesort.h b/src/include/utils/tuplesort.h
index 5b3f475..03594e7 100644
--- a/src/include/utils/tuplesort.h
+++ b/src/include/utils/tuplesort.h
@@ -72,7 +72,7 @@ extern Tuplesortstate *tuplesort_begin_index_btree(Relation heapRel,
 							int workMem, bool randomAccess);
 extern Tuplesortstate *tuplesort_begin_index_hash(Relation heapRel,
 						   Relation indexRel,
-						   uint32 hash_mask,
+						   uint32 hash_mod,
 						   int workMem, bool randomAccess);
 extern Tuplesortstate *tuplesort_begin_datum(Oid datumType,
 					  Oid sortOperator, Oid sortCollation,
