diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 1174e1a31c..12d1bf551b 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -436,7 +436,10 @@ struct Tuplesortstate /* * This variable is shared by the single-key MinimalTuple case and the - * Datum case (which both use qsort_ssup()). Otherwise it's NULL. + * Datum case (which both use qsort_ssup()). It is also used by various + * sort specialization functions when comparing the leading key in a + * tiebreak situation to determine if there are any subsequent keys to + * sort on. It's otherwise NULL. */ SortSupport onlyKey; @@ -698,7 +701,12 @@ qsort_tuple_unsigned_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state) compare = ApplyUnsignedSortComparator(a->datum1, a->isnull1, b->datum1, b->isnull1, &state->sortKeys[0]); - if (compare != 0) + + /* + * No need to call the tiebreak function when the datums differ or if this + * is the only key we're sorting on. + */ + if (compare != 0 || state->onlyKey != NULL) return compare; return state->comparetup(a, b, state); @@ -713,7 +721,12 @@ qsort_tuple_signed_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state) compare = ApplySignedSortComparator(a->datum1, a->isnull1, b->datum1, b->isnull1, &state->sortKeys[0]); - if (compare != 0) + + /* + * No need to call the tiebreak function when the datums differ or if this + * is the only key we're sorting on. + */ + if (compare != 0 || state->onlyKey != NULL) return compare; return state->comparetup(a, b, state); @@ -728,7 +741,12 @@ qsort_tuple_int32_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state) compare = ApplyInt32SortComparator(a->datum1, a->isnull1, b->datum1, b->isnull1, &state->sortKeys[0]); - if (compare != 0) + + /* + * No need to call the tiebreak function when the datums differ or if this + * is the only key we're sorting on. + */ + if (compare != 0 || state->onlyKey != NULL) return compare; return state->comparetup(a, b, state);