From a07bd696a0ed933ca95c33f736a3a7e7eccecf0f Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@heroku.com>
Date: Tue, 2 Sep 2014 18:55:05 -0700
Subject: [PATCH 5/7] Re-enable optimization on Darwin and 32-bit platforms

Add a macro to pg_config_manual.h that provides a relatively convenient
way to at least disable the optimization at compile time.
---
 src/backend/utils/adt/varlena.c | 39 ++++++++++++++-------------------------
 src/include/pg_config_manual.h  |  8 ++++++++
 2 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 062035f..92d9135 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -1751,24 +1751,13 @@ btsortsupport_worker(SortSupport ssup, Oid collid)
 
 	/*
 	 * On platforms where the abbreviated key for text optimization might have
-	 * bad worst case performance it is avoided entirely.  In order to ever
-	 * apply the optimization we require:
-	 *
-	 * 	* That the platform's strxfrm() meets a certain standard for
-	 * 	representing as much information as possible in leading bytes.
-	 *
-	 *	* That there are a full 8 bytes of storage per Datum on the platform,
-	 *	since we pack bytes into that representation.  Having only 4 bytes
-	 *	could make worse case performance drastically more likely.
-	 *
-	 * The standard applied for strxfrm() is that a significant amount of
-	 * entropy from the original string must be concentrated at the beginning
-	 * of returned blobs.  The Mac OS X implementation is known to produce
-	 * blobs with very little entropy;  it produces printable blobs with a
-	 * series of 24-bit weights, each one encoded into four bytes.  Multiple
-	 * "levels" of weights are separated by a weight of 0.  Typically only two
-	 * ASCII characters are represented in the first eight bytes.  Therefore,
-	 * the optimization is specially disabled.
+	 * bad worst case performance, it may be useful to avoid it entirely by
+	 * disabling it at compile time.  Having only 4 byte datums could make
+	 * worst-case performance drastically more likely, for example.  Moreover,
+	 * Darwin's strxfrm() implementations is known to not effectively
+	 * concentrate a significant amount of entropy from the original string in
+	 * earlier transformed blobs.  It's possible that other supported platforms
+	 * are similarly encumbered.
 	 *
 	 * Any reasonable implementation will pack primary weights into the start
 	 * of returned blobs.  The canonical algorithm's implementation is
@@ -1794,16 +1783,16 @@ btsortsupport_worker(SortSupport ssup, Oid collid)
 	 *
 	 * It is generally expected that most non-equal keys will have their
 	 * comparisons resolved at the primary level.  If enough comparisons can be
-	 * resolved with just 8 byte abbreviated keys, this optimization is very
-	 * effective (although if there are many tie-breakers that largely only
-	 * perform cheap memcmp() calls, that is also much faster than the
+	 * resolved with just 4 or 8 byte abbreviated keys, this optimization is
+	 * very effective (although if there are many tie-breakers that largely
+	 * only perform cheap memcmp() calls, that is also much faster than the
 	 * unoptimized case - see bttext_abbrev_abort()).
 	 *
-	 * There is no reason to not at least perform fmgr elision on platforms
-	 * where abbreviation isn't expected to be profitable, though.
+	 * There is no reason to not at least perform fmgr elision on builds where
+	 * abbreviation is disabled, though.
 	 */
-#if defined(__darwin__) || SIZEOF_DATUM != 8
-	ssup->type = sortKeyOther;
+#ifndef USE_ABBREV_KEYS
+	ssup->abbrev_state = ABBREVIATED_KEYS_NO;
 #endif
 
 	/*
diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h
index a27f5e3..02c43bf 100644
--- a/src/include/pg_config_manual.h
+++ b/src/include/pg_config_manual.h
@@ -304,6 +304,14 @@
 #define TRACE_SORT 1
 
 /*
+ * Enable "abbreviated key" optimization, for sortsupport B-Tree opclasses that
+ * additionally support abbreviated keys.  This is particularly relevant for
+ * sorting text, where platform-specific issues could limit the effectiveness
+ * of the optimization.
+ */
+#define USE_ABBREV_KEYS
+
+/*
  * Enable tracing of syncscan operations (see also the trace_syncscan GUC var).
  */
 /* #define TRACE_SYNCSCAN */
-- 
1.9.1

