From 566b160cbe2acd276b173e18679ccc34cf00d116 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@heroku.com>
Date: Tue, 2 Sep 2014 18:38:10 -0700
Subject: [PATCH 4/5] Fix auxiliary state for bounded heap sorts

Top-N heap sorts do not require that we "change direction" within
reversedirection_heap() (nor any other reversedirection* routine), since
they aren't subject to abbreviated key optimization under any
circumstances.
---
 src/backend/utils/sort/tuplesort.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c
index d4a314d..ea1252c 100644
--- a/src/backend/utils/sort/tuplesort.c
+++ b/src/backend/utils/sort/tuplesort.c
@@ -927,6 +927,11 @@ tuplesort_set_bound(Tuplesortstate *state, int64 bound)
 	/*
 	 * Bounded sorts are not an effective target for abbreviated key
 	 * optimization.  Disable.
+	 *
+	 * If we were ever to change our minds about this, then
+	 * reversedirection_heap(), and perhaps other reversedirection routines
+	 * would be obligated to invert the nested, authoritative sortsupport
+	 * state.
 	 */
 	if (state->sortKeys && state->sortKeys->abbrev_converter)
 	{
@@ -3095,15 +3100,6 @@ reversedirection_heap(Tuplesortstate *state)
 		sortKey->ssup_reverse = !sortKey->ssup_reverse;
 		sortKey->ssup_nulls_first = !sortKey->ssup_nulls_first;
 	}
-
-	if (state->sortKeys->abbrev_tiebreak)
-	{
-		sortKey = state->sortKeys->abbrev_tiebreak;
-
-		Assert(sortKey->abbreviate_state == ABBREVIATED_KEYS_TIE);
-		sortKey->ssup_reverse = !sortKey->ssup_reverse;
-		sortKey->ssup_nulls_first = !sortKey->ssup_nulls_first;
-	}
 }
 
 
-- 
1.9.1

