diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c
index 028c5b71c27..5185d8403d5 100644
--- a/src/backend/utils/sort/tuplesort.c
+++ b/src/backend/utils/sort/tuplesort.c
@@ -2834,6 +2834,13 @@ radix_sort_tuple(SortTuple *begin, size_t n_elems, int level, Tuplesortstate *st
 		{
 			uint8		idx = remaining_partitions[i];
 
+			/* count sorted partitions */
+			if (partitions[idx].offset == partitions[idx].next_offset)
+			{
+				num_remaining--;
+				continue;
+			}
+
 			for (SortTuple *st = begin + partitions[idx].offset;
 				 st < begin + partitions[idx].next_offset;
 				 st++)
@@ -2841,6 +2848,9 @@ radix_sort_tuple(SortTuple *begin, size_t n_elems, int level, Tuplesortstate *st
 				size_t		offset = partitions[st->current_byte].offset++;
 				SortTuple	tmp;
 
+				//if (offset == (size_t) (st - begin))
+				//	continue;	/* already in correct position */
+
 				/* swap current tuple with destination position */
 				Assert(offset < n_elems);
 				tmp = *st;
@@ -2849,10 +2859,6 @@ radix_sort_tuple(SortTuple *begin, size_t n_elems, int level, Tuplesortstate *st
 
 				CHECK_FOR_INTERRUPTS();
 			};
-
-			/* count sorted partitions */
-			if (partitions[idx].offset == partitions[idx].next_offset)
-				num_remaining--;
 		}
 	}
 
