v4-0001-Sortsupport-for-sorting-GiST-build-for-gist_btree.patch

application/octet-stream

Filename: v4-0001-Sortsupport-for-sorting-GiST-build-for-gist_btree.patch
Type: application/octet-stream
Part: 0
Message: Re: Yet another fast GiST build

Patch

Format: format-patch
Series: patch v4-0001
Subject: Sortsupport for sorting GiST build for gist_btree types
File+
contrib/btree_gist/btree_bit.c 24 0
contrib/btree_gist/btree_bytea.c 25 1
contrib/btree_gist/btree_cash.c 75 0
contrib/btree_gist/btree_date.c 28 0
contrib/btree_gist/btree_enum.c 70 0
contrib/btree_gist/btree_float4.c 71 0
contrib/btree_gist/btree_float8.c 80 0
contrib/btree_gist/btree_gist--1.6--1.7.sql 182 0
contrib/btree_gist/btree_gist.control 1 1
contrib/btree_gist/btree_gist.h 1 0
contrib/btree_gist/btree_inet.c 81 0
contrib/btree_gist/btree_int2.c 70 0
contrib/btree_gist/btree_int4.c 70 0
contrib/btree_gist/btree_int8.c 74 1
contrib/btree_gist/btree_interval.c 25 0
contrib/btree_gist/btree_macaddr8.c 71 0
contrib/btree_gist/btree_macaddr.c 71 0
contrib/btree_gist/btree_numeric.c 24 0
contrib/btree_gist/btree_oid.c 71 0
contrib/btree_gist/btree_text.c 26 0
contrib/btree_gist/btree_time.c 27 0
contrib/btree_gist/btree_ts.c 26 0
contrib/btree_gist/btree_uuid.c 26 0
contrib/btree_gist/expected/bit.out 7 0
contrib/btree_gist/expected/bytea.out 7 0
contrib/btree_gist/expected/cash.out 7 0
contrib/btree_gist/expected/char.out 7 0
contrib/btree_gist/expected/cidr.out 7 0
contrib/btree_gist/expected/date.out 7 0
contrib/btree_gist/expected/enum.out 7 0
contrib/btree_gist/expected/float4.out 7 0
contrib/btree_gist/expected/float8.out 7 0
contrib/btree_gist/expected/inet.out 7 0
contrib/btree_gist/expected/int2.out 7 0
contrib/btree_gist/expected/int4.out 7 0
contrib/btree_gist/expected/int8.out 7 0
contrib/btree_gist/expected/interval.out 7 0
contrib/btree_gist/expected/macaddr8.out 7 0
contrib/btree_gist/expected/macaddr.out 7 0
contrib/btree_gist/expected/numeric.out 7 0
contrib/btree_gist/expected/oid.out 7 0
contrib/btree_gist/expected/text.out 7 0
contrib/btree_gist/expected/time.out 7 0
contrib/btree_gist/expected/timestamp.out 7 0
contrib/btree_gist/expected/timestamptz.out 7 0
contrib/btree_gist/expected/timetz.out 7 0
contrib/btree_gist/expected/uuid.out 7 0
contrib/btree_gist/expected/varbit.out 7 0
contrib/btree_gist/expected/varchar.out 7 0
contrib/btree_gist/Makefile 1 1
contrib/btree_gist/sql/bit.sql 4 0
contrib/btree_gist/sql/bytea.sql 4 0
contrib/btree_gist/sql/cash.sql 4 0
contrib/btree_gist/sql/char.sql 4 0
contrib/btree_gist/sql/cidr.sql 4 0
contrib/btree_gist/sql/date.sql 4 0
contrib/btree_gist/sql/enum.sql 4 0
contrib/btree_gist/sql/float4.sql 4 0
contrib/btree_gist/sql/float8.sql 4 0
contrib/btree_gist/sql/inet.sql 4 0
contrib/btree_gist/sql/int2.sql 4 0
contrib/btree_gist/sql/int4.sql 4 0
contrib/btree_gist/sql/int8.sql 4 0
contrib/btree_gist/sql/interval.sql 4 0
contrib/btree_gist/sql/macaddr8.sql 4 0
contrib/btree_gist/sql/macaddr.sql 4 0
contrib/btree_gist/sql/numeric.sql 4 0
contrib/btree_gist/sql/oid.sql 4 0
contrib/btree_gist/sql/text.sql 4 0
contrib/btree_gist/sql/time.sql 4 0
contrib/btree_gist/sql/timestamp.sql 4 0
contrib/btree_gist/sql/timestamptz.sql 4 0
contrib/btree_gist/sql/timetz.sql 4 0
contrib/btree_gist/sql/uuid.sql 4 0
contrib/btree_gist/sql/varbit.sql 4 0
contrib/btree_gist/sql/varchar.sql 4 0
src/backend/access/gist/gistbuild.c 1 0
From 5360dac5b4c15a01b55a93dab6048b71df1b5dbb Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Sat, 3 Oct 2020 21:08:54 +0500
Subject: [PATCH v4] Sortsupport for sorting GiST build for gist_btree types

---
 contrib/btree_gist/Makefile                 |   2 +-
 contrib/btree_gist/btree_bit.c              |  24 +++
 contrib/btree_gist/btree_bytea.c            |  26 ++-
 contrib/btree_gist/btree_cash.c             |  75 ++++++++
 contrib/btree_gist/btree_date.c             |  28 +++
 contrib/btree_gist/btree_enum.c             |  70 ++++++++
 contrib/btree_gist/btree_float4.c           |  71 ++++++++
 contrib/btree_gist/btree_float8.c           |  80 +++++++++
 contrib/btree_gist/btree_gist--1.6--1.7.sql | 182 ++++++++++++++++++++
 contrib/btree_gist/btree_gist.control       |   2 +-
 contrib/btree_gist/btree_gist.h             |   1 +
 contrib/btree_gist/btree_inet.c             |  81 +++++++++
 contrib/btree_gist/btree_int2.c             |  70 ++++++++
 contrib/btree_gist/btree_int4.c             |  70 ++++++++
 contrib/btree_gist/btree_int8.c             |  75 +++++++-
 contrib/btree_gist/btree_interval.c         |  25 +++
 contrib/btree_gist/btree_macaddr.c          |  71 ++++++++
 contrib/btree_gist/btree_macaddr8.c         |  71 ++++++++
 contrib/btree_gist/btree_numeric.c          |  24 +++
 contrib/btree_gist/btree_oid.c              |  71 ++++++++
 contrib/btree_gist/btree_text.c             |  26 +++
 contrib/btree_gist/btree_time.c             |  27 +++
 contrib/btree_gist/btree_ts.c               |  26 +++
 contrib/btree_gist/btree_uuid.c             |  26 +++
 contrib/btree_gist/expected/bit.out         |   7 +
 contrib/btree_gist/expected/bytea.out       |   7 +
 contrib/btree_gist/expected/cash.out        |   7 +
 contrib/btree_gist/expected/char.out        |   7 +
 contrib/btree_gist/expected/cidr.out        |   7 +
 contrib/btree_gist/expected/date.out        |   7 +
 contrib/btree_gist/expected/enum.out        |   7 +
 contrib/btree_gist/expected/float4.out      |   7 +
 contrib/btree_gist/expected/float8.out      |   7 +
 contrib/btree_gist/expected/inet.out        |   7 +
 contrib/btree_gist/expected/int2.out        |   7 +
 contrib/btree_gist/expected/int4.out        |   7 +
 contrib/btree_gist/expected/int8.out        |   7 +
 contrib/btree_gist/expected/interval.out    |   7 +
 contrib/btree_gist/expected/macaddr.out     |   7 +
 contrib/btree_gist/expected/macaddr8.out    |   7 +
 contrib/btree_gist/expected/numeric.out     |   7 +
 contrib/btree_gist/expected/oid.out         |   7 +
 contrib/btree_gist/expected/text.out        |   7 +
 contrib/btree_gist/expected/time.out        |   7 +
 contrib/btree_gist/expected/timestamp.out   |   7 +
 contrib/btree_gist/expected/timestamptz.out |   7 +
 contrib/btree_gist/expected/timetz.out      |   7 +
 contrib/btree_gist/expected/uuid.out        |   7 +
 contrib/btree_gist/expected/varbit.out      |   7 +
 contrib/btree_gist/expected/varchar.out     |   7 +
 contrib/btree_gist/sql/bit.sql              |   4 +
 contrib/btree_gist/sql/bytea.sql            |   4 +
 contrib/btree_gist/sql/cash.sql             |   4 +
 contrib/btree_gist/sql/char.sql             |   4 +
 contrib/btree_gist/sql/cidr.sql             |   4 +
 contrib/btree_gist/sql/date.sql             |   4 +
 contrib/btree_gist/sql/enum.sql             |   4 +
 contrib/btree_gist/sql/float4.sql           |   4 +
 contrib/btree_gist/sql/float8.sql           |   4 +
 contrib/btree_gist/sql/inet.sql             |   4 +
 contrib/btree_gist/sql/int2.sql             |   4 +
 contrib/btree_gist/sql/int4.sql             |   4 +
 contrib/btree_gist/sql/int8.sql             |   4 +
 contrib/btree_gist/sql/interval.sql         |   4 +
 contrib/btree_gist/sql/macaddr.sql          |   4 +
 contrib/btree_gist/sql/macaddr8.sql         |   4 +
 contrib/btree_gist/sql/numeric.sql          |   4 +
 contrib/btree_gist/sql/oid.sql              |   4 +
 contrib/btree_gist/sql/text.sql             |   4 +
 contrib/btree_gist/sql/time.sql             |   4 +
 contrib/btree_gist/sql/timestamp.sql        |   4 +
 contrib/btree_gist/sql/timestamptz.sql      |   4 +
 contrib/btree_gist/sql/timetz.sql           |   4 +
 contrib/btree_gist/sql/uuid.sql             |   4 +
 contrib/btree_gist/sql/varbit.sql           |   4 +
 contrib/btree_gist/sql/varchar.sql          |   4 +
 src/backend/access/gist/gistbuild.c         |   1 +
 77 files changed, 1507 insertions(+), 4 deletions(-)
 create mode 100644 contrib/btree_gist/btree_gist--1.6--1.7.sql

diff --git a/contrib/btree_gist/Makefile b/contrib/btree_gist/Makefile
index e92d974a1a..a1f818f71e 100644
--- a/contrib/btree_gist/Makefile
+++ b/contrib/btree_gist/Makefile
@@ -32,7 +32,7 @@ EXTENSION = btree_gist
 DATA = btree_gist--1.0--1.1.sql \
        btree_gist--1.1--1.2.sql btree_gist--1.2.sql btree_gist--1.2--1.3.sql \
        btree_gist--1.3--1.4.sql btree_gist--1.4--1.5.sql \
-       btree_gist--1.5--1.6.sql
+       btree_gist--1.5--1.6.sql btree_gist--1.6--1.7.sql
 PGFILEDESC = "btree_gist - B-tree equivalent GiST operator classes"
 
 REGRESS = init int2 int4 int8 float4 float8 cash oid timestamp timestamptz \
diff --git a/contrib/btree_gist/btree_bit.c b/contrib/btree_gist/btree_bit.c
index 2225244ded..e5d3ac6a24 100644
--- a/contrib/btree_gist/btree_bit.c
+++ b/contrib/btree_gist/btree_bit.c
@@ -19,6 +19,7 @@ PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
 PG_FUNCTION_INFO_V1(gbt_bit_consistent);
 PG_FUNCTION_INFO_V1(gbt_bit_penalty);
 PG_FUNCTION_INFO_V1(gbt_bit_same);
+PG_FUNCTION_INFO_V1(gbt_bit_sortsupport);
 
 
 /* define for comparison */
@@ -209,3 +210,26 @@ gbt_bit_penalty(PG_FUNCTION_ARGS)
 	PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
 									  &tinfo, fcinfo->flinfo));
 }
+
+static int
+gbt_bit_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	return DatumGetInt32(DirectFunctionCall2(byteacmp,
+											 PointerGetDatum(a),
+											 PointerGetDatum(b)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_bit_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_bit_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_bytea.c b/contrib/btree_gist/btree_bytea.c
index 6b005f0157..a2abfb7d7c 100644
--- a/contrib/btree_gist/btree_bytea.c
+++ b/contrib/btree_gist/btree_bytea.c
@@ -18,6 +18,7 @@ PG_FUNCTION_INFO_V1(gbt_bytea_picksplit);
 PG_FUNCTION_INFO_V1(gbt_bytea_consistent);
 PG_FUNCTION_INFO_V1(gbt_bytea_penalty);
 PG_FUNCTION_INFO_V1(gbt_bytea_same);
+PG_FUNCTION_INFO_V1(gbt_bytea_sortsupport);
 
 
 /* define for comparison */
@@ -87,7 +88,7 @@ static const gbtree_vinfo tinfo =
 
 
 /**************************************************
- * Text ops
+ * Bytea ops
  **************************************************/
 
 
@@ -168,3 +169,26 @@ gbt_bytea_penalty(PG_FUNCTION_ARGS)
 	PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
 									  &tinfo, fcinfo->flinfo));
 }
+
+static int
+gbt_bytea_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	return DatumGetInt32(DirectFunctionCall2(byteacmp,
+											 PointerGetDatum(a),
+											 PointerGetDatum(b)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_bytea_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_bytea_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_cash.c b/contrib/btree_gist/btree_cash.c
index dfa23224b6..bba5c51a8e 100644
--- a/contrib/btree_gist/btree_cash.c
+++ b/contrib/btree_gist/btree_cash.c
@@ -25,6 +25,7 @@ PG_FUNCTION_INFO_V1(gbt_cash_consistent);
 PG_FUNCTION_INFO_V1(gbt_cash_distance);
 PG_FUNCTION_INFO_V1(gbt_cash_penalty);
 PG_FUNCTION_INFO_V1(gbt_cash_same);
+PG_FUNCTION_INFO_V1(gbt_cash_sortsupport);
 
 static bool
 gbt_cashgt(const void *a, const void *b, FmgrInfo *flinfo)
@@ -216,3 +217,77 @@ gbt_cash_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_cash_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	cashKEY   *ia = (cashKEY *) DatumGetPointer(a);
+	cashKEY   *ib = (cashKEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_cash_abbrev_convert(Datum original, SortSupport ssup)
+{
+	cashKEY   *b1 = (cashKEY *) DatumGetPointer(original);
+	int64		z = b1->lower;
+
+#if SIZEOF_DATUM == 8
+	return (Datum) z;
+#else
+	return (Datum) (z >> 32);
+#endif
+}
+
+static int
+gbt_cash_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+/*
+ * We never consider aborting the abbreviation.
+ */
+static bool
+gbt_cash_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_cash_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_cash_cmp_abbrev;
+		ssup->abbrev_converter = gbt_cash_abbrev_convert;
+		ssup->abbrev_abort = gbt_cash_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_cash_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_cash_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
+
diff --git a/contrib/btree_gist/btree_date.c b/contrib/btree_gist/btree_date.c
index 455a265a49..7d6759b101 100644
--- a/contrib/btree_gist/btree_date.c
+++ b/contrib/btree_gist/btree_date.c
@@ -25,6 +25,7 @@ PG_FUNCTION_INFO_V1(gbt_date_consistent);
 PG_FUNCTION_INFO_V1(gbt_date_distance);
 PG_FUNCTION_INFO_V1(gbt_date_penalty);
 PG_FUNCTION_INFO_V1(gbt_date_same);
+PG_FUNCTION_INFO_V1(gbt_date_sortsupport);
 
 static bool
 gbt_dategt(const void *a, const void *b, FmgrInfo *flinfo)
@@ -257,3 +258,30 @@ gbt_date_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_date_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	dateKEY    *ia = (dateKEY *) PointerGetDatum(a);
+	dateKEY    *ib = (dateKEY *) PointerGetDatum(b);
+
+	return DatumGetInt32(DirectFunctionCall2(date_cmp,
+											DateADTGetDatum(ia->lower),
+											DateADTGetDatum(ib->lower)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_date_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_date_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
+
diff --git a/contrib/btree_gist/btree_enum.c b/contrib/btree_gist/btree_enum.c
index d4dc38a38e..41b487747e 100644
--- a/contrib/btree_gist/btree_enum.c
+++ b/contrib/btree_gist/btree_enum.c
@@ -26,6 +26,7 @@ PG_FUNCTION_INFO_V1(gbt_enum_picksplit);
 PG_FUNCTION_INFO_V1(gbt_enum_consistent);
 PG_FUNCTION_INFO_V1(gbt_enum_penalty);
 PG_FUNCTION_INFO_V1(gbt_enum_same);
+PG_FUNCTION_INFO_V1(gbt_enum_sortsupport);
 
 
 static bool
@@ -183,3 +184,72 @@ gbt_enum_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_enum_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	oidKEY   *ia = (oidKEY *) DatumGetPointer(a);
+	oidKEY   *ib = (oidKEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_enum_abbrev_convert(Datum original, SortSupport ssup)
+{
+	oidKEY   *b1 = (oidKEY *) DatumGetPointer(original);
+	Oid		z = b1->lower;
+
+	return (Datum) z;
+}
+
+static int
+gbt_enum_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+/*
+ * We never consider aborting the abbreviation.
+ */
+static bool
+gbt_enum_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_enum_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_enum_cmp_abbrev;
+		ssup->abbrev_converter = gbt_enum_abbrev_convert;
+		ssup->abbrev_abort = gbt_enum_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_enum_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_enum_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_float4.c b/contrib/btree_gist/btree_float4.c
index 3604c73313..751e526b0a 100644
--- a/contrib/btree_gist/btree_float4.c
+++ b/contrib/btree_gist/btree_float4.c
@@ -23,6 +23,7 @@ PG_FUNCTION_INFO_V1(gbt_float4_consistent);
 PG_FUNCTION_INFO_V1(gbt_float4_distance);
 PG_FUNCTION_INFO_V1(gbt_float4_penalty);
 PG_FUNCTION_INFO_V1(gbt_float4_same);
+PG_FUNCTION_INFO_V1(gbt_float4_sortsupport);
 
 static bool
 gbt_float4gt(const void *a, const void *b, FmgrInfo *flinfo)
@@ -209,3 +210,73 @@ gbt_float4_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+
+static int
+gbt_float4_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	float4KEY   *ia = (float4KEY *) DatumGetPointer(a);
+	float4KEY   *ib = (float4KEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_float4_abbrev_convert(Datum original, SortSupport ssup)
+{
+	float4KEY   *b1 = (float4KEY *) DatumGetPointer(original);
+	float4		z = b1->lower;
+
+	return (Datum) Float4GetDatum(z);
+}
+
+static int
+gbt_float4_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (DatumGetFloat4(z1) > DatumGetFloat4(z2))
+		return 1;
+	else if (DatumGetFloat4(z1) < DatumGetFloat4(z2))
+		return -1;
+	else
+		return 0;
+}
+
+/*
+ * We never consider aborting the abbreviation.
+ */
+static bool
+gbt_float4_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_float4_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_float4_cmp_abbrev;
+		ssup->abbrev_converter = gbt_float4_abbrev_convert;
+		ssup->abbrev_abort = gbt_float4_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_float4_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_float4_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_float8.c b/contrib/btree_gist/btree_float8.c
index 10a5262aaa..b4e6a75361 100644
--- a/contrib/btree_gist/btree_float8.c
+++ b/contrib/btree_gist/btree_float8.c
@@ -23,6 +23,7 @@ PG_FUNCTION_INFO_V1(gbt_float8_consistent);
 PG_FUNCTION_INFO_V1(gbt_float8_distance);
 PG_FUNCTION_INFO_V1(gbt_float8_penalty);
 PG_FUNCTION_INFO_V1(gbt_float8_same);
+PG_FUNCTION_INFO_V1(gbt_float8_sortsupport);
 
 
 static bool
@@ -216,3 +217,82 @@ gbt_float8_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_float8_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	float8KEY   *ia = (float8KEY *) DatumGetPointer(a);
+	float8KEY   *ib = (float8KEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_float8_abbrev_convert(Datum original, SortSupport ssup)
+{
+	float8KEY   *b1 = (float8KEY *) DatumGetPointer(original);
+	float8		z = b1->lower;
+
+#if SIZEOF_DATUM == 8
+	return (Datum) Float8GetDatum(z);
+#else
+	return (Datum) Float4GetDatum((float)z);
+#endif
+}
+
+static int
+gbt_float8_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+#if SIZEOF_DATUM == 8
+	if (DatumGetFloat8(z1) > DatumGetFloat8(z2))
+		return 1;
+	else if (DatumGetFloat8(z1) < DatumGetFloat8(z2))
+		return -1;
+	else
+		return 0;
+#else
+	if (DatumGetFloat4(z1) > DatumGetFloat4(z2))
+		return 1;
+	else if (DatumGetFloat4(z1) < DatumGetFloat4(z2))
+		return -1;
+	else
+		return 0;
+#endif
+}
+
+static bool
+gbt_float8_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_float8_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_float8_cmp_abbrev;
+		ssup->abbrev_converter = gbt_float8_abbrev_convert;
+		ssup->abbrev_abort = gbt_float8_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_float8_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_float8_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_gist--1.6--1.7.sql b/contrib/btree_gist/btree_gist--1.6--1.7.sql
new file mode 100644
index 0000000000..abb5b8b0f4
--- /dev/null
+++ b/contrib/btree_gist/btree_gist--1.6--1.7.sql
@@ -0,0 +1,182 @@
+/* contrib/btree_gist/btree_gist--1.6--1.7.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION btree_gist UPDATE TO '1.7'" to load this file. \quit
+
+
+CREATE FUNCTION gbt_int8_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_int8_ops USING gist ADD
+	FUNCTION	11	(int8, int8) gbt_int8_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_int4_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_int4_ops USING gist ADD
+	FUNCTION	11	(int4, int4) gbt_int4_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_int2_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_int2_ops USING gist ADD
+	FUNCTION	11	(int2, int2) gbt_int2_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_float8_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_float8_ops USING gist ADD
+	FUNCTION	11	(float8, float8) gbt_float8_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_float4_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_float4_ops USING gist ADD
+	FUNCTION	11	(float4, float4) gbt_float4_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_enum_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_enum_ops USING gist ADD
+	FUNCTION	11	(anyenum, anyenum) gbt_enum_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_oid_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_oid_ops USING gist ADD
+	FUNCTION	11	(oid, oid) gbt_oid_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_cash_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_cash_ops USING gist ADD
+	FUNCTION	11	(money, money) gbt_cash_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_inet_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_inet_ops USING gist ADD
+	FUNCTION	11	(inet, inet) gbt_inet_sortsupport (internal) ;
+
+ALTER OPERATOR FAMILY gist_cidr_ops USING gist ADD
+	FUNCTION	11	(cidr, cidr) gbt_inet_sortsupport (internal) ;
+
+
+CREATE FUNCTION gbt_macad_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_macaddr_ops USING gist ADD
+	FUNCTION	11	(macaddr, macaddr) gbt_macad_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_macad8_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_macaddr8_ops USING gist ADD
+	FUNCTION	11	(macaddr8, macaddr8) gbt_macad8_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_numeric_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_numeric_ops USING gist ADD
+	FUNCTION	11	(numeric, numeric) gbt_numeric_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_uuid_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_uuid_ops USING gist ADD
+	FUNCTION	11	(uuid, uuid) gbt_uuid_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_ts_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_timestamp_ops USING gist ADD
+	FUNCTION	11	(timestamp, timestamp) gbt_ts_sortsupport (internal) ;
+
+ALTER OPERATOR FAMILY gist_timestamptz_ops USING gist ADD
+	FUNCTION	11	(timestamptz, timestamptz) gbt_ts_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_text_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_text_ops USING gist ADD
+	FUNCTION	11	(text, text) gbt_text_sortsupport (internal) ;
+
+ALTER OPERATOR FAMILY gist_bpchar_ops USING gist ADD
+	FUNCTION	11	(bpchar, bpchar) gbt_text_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_time_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_time_ops USING gist ADD
+	FUNCTION	11	(time, time) gbt_time_sortsupport (internal) ;
+
+ALTER OPERATOR FAMILY gist_timetz_ops USING gist ADD
+	FUNCTION	11	(timetz, timetz) gbt_time_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_bytea_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_bytea_ops USING gist ADD
+	FUNCTION	11	(bytea, bytea) gbt_bytea_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_date_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_date_ops USING gist ADD
+	FUNCTION	11	(date, date) gbt_date_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_bit_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_bit_ops USING gist ADD
+	FUNCTION	11	(bit, bit) gbt_bit_sortsupport (internal) ;
+
+ALTER OPERATOR FAMILY gist_vbit_ops USING gist ADD
+	FUNCTION	11	(varbit, varbit) gbt_bit_sortsupport (internal) ;
+
+CREATE FUNCTION gbt_intv_sortsupport(internal)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT;
+
+ALTER OPERATOR FAMILY gist_interval_ops USING gist ADD
+	FUNCTION	11	(interval, interval) gbt_intv_sortsupport (internal) ;
+
diff --git a/contrib/btree_gist/btree_gist.control b/contrib/btree_gist/btree_gist.control
index e5c41fe8f3..fa9171a80a 100644
--- a/contrib/btree_gist/btree_gist.control
+++ b/contrib/btree_gist/btree_gist.control
@@ -1,6 +1,6 @@
 # btree_gist extension
 comment = 'support for indexing common datatypes in GiST'
-default_version = '1.6'
+default_version = '1.7'
 module_pathname = '$libdir/btree_gist'
 relocatable = true
 trusted = true
diff --git a/contrib/btree_gist/btree_gist.h b/contrib/btree_gist/btree_gist.h
index 14c7c8ee19..35ad287ed3 100644
--- a/contrib/btree_gist/btree_gist.h
+++ b/contrib/btree_gist/btree_gist.h
@@ -6,6 +6,7 @@
 
 #include "access/nbtree.h"
 #include "fmgr.h"
+#include "utils/sortsupport.h"
 
 #define BtreeGistNotEqualStrategyNumber 6
 
diff --git a/contrib/btree_gist/btree_inet.c b/contrib/btree_gist/btree_inet.c
index e4b3a946b2..d3188f65c3 100644
--- a/contrib/btree_gist/btree_inet.c
+++ b/contrib/btree_gist/btree_inet.c
@@ -24,6 +24,7 @@ PG_FUNCTION_INFO_V1(gbt_inet_picksplit);
 PG_FUNCTION_INFO_V1(gbt_inet_consistent);
 PG_FUNCTION_INFO_V1(gbt_inet_penalty);
 PG_FUNCTION_INFO_V1(gbt_inet_same);
+PG_FUNCTION_INFO_V1(gbt_inet_sortsupport);
 
 
 static bool
@@ -186,3 +187,83 @@ gbt_inet_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_inet_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	inetKEY   *ia = (inetKEY *) DatumGetPointer(a);
+	inetKEY   *ib = (inetKEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_inet_abbrev_convert(Datum original, SortSupport ssup)
+{
+	inetKEY   *b1 = (inetKEY *) DatumGetPointer(original);
+	double		z = b1->lower;
+
+#if SIZEOF_DATUM == 8
+	return (Datum) Float8GetDatum(z);
+#else
+	return (Datum) Float4GetDatum((float) z);
+#endif
+}
+
+static int
+gbt_inet_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+#if SIZEOF_DATUM == 8
+	if (DatumGetFloat8(z1) > DatumGetFloat8(z2))
+		return 1;
+	else if (DatumGetFloat8(z1) < DatumGetFloat8(z2))
+		return -1;
+	else
+		return 0;
+#else
+	if (DatumGetFloat4(z1) > DatumGetFloat4(z2))
+		return 1;
+	else if (DatumGetFloat4(z1) < DatumGetFloat4(z2))
+		return -1;
+	else
+		return 0;
+#endif
+}
+
+static bool
+gbt_inet_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_inet_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_inet_cmp_abbrev;
+		ssup->abbrev_converter = gbt_inet_abbrev_convert;
+		ssup->abbrev_abort = gbt_inet_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_inet_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_inet_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
+
diff --git a/contrib/btree_gist/btree_int2.c b/contrib/btree_gist/btree_int2.c
index a91b95ff39..968c6950ca 100644
--- a/contrib/btree_gist/btree_int2.c
+++ b/contrib/btree_gist/btree_int2.c
@@ -24,6 +24,7 @@ PG_FUNCTION_INFO_V1(gbt_int2_consistent);
 PG_FUNCTION_INFO_V1(gbt_int2_distance);
 PG_FUNCTION_INFO_V1(gbt_int2_penalty);
 PG_FUNCTION_INFO_V1(gbt_int2_same);
+PG_FUNCTION_INFO_V1(gbt_int2_sortsupport);
 
 static bool
 gbt_int2gt(const void *a, const void *b, FmgrInfo *flinfo)
@@ -214,3 +215,72 @@ gbt_int2_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_int2_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	int16KEY   *ia = (int16KEY *) DatumGetPointer(a);
+	int16KEY   *ib = (int16KEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_int2_abbrev_convert(Datum original, SortSupport ssup)
+{
+	int16KEY   *b1 = (int16KEY *) DatumGetPointer(original);
+	int16		z = b1->lower;
+
+	return (Datum) z;
+}
+
+static int
+gbt_int2_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+/*
+ * We never consider aborting the abbreviation.
+ */
+static bool
+gbt_int2_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_int2_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_int2_cmp_abbrev;
+		ssup->abbrev_converter = gbt_int2_abbrev_convert;
+		ssup->abbrev_abort = gbt_int2_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_int2_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_int2_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_int4.c b/contrib/btree_gist/btree_int4.c
index 7ea98c478c..9f6f77ce43 100644
--- a/contrib/btree_gist/btree_int4.c
+++ b/contrib/btree_gist/btree_int4.c
@@ -24,6 +24,7 @@ PG_FUNCTION_INFO_V1(gbt_int4_consistent);
 PG_FUNCTION_INFO_V1(gbt_int4_distance);
 PG_FUNCTION_INFO_V1(gbt_int4_penalty);
 PG_FUNCTION_INFO_V1(gbt_int4_same);
+PG_FUNCTION_INFO_V1(gbt_int4_sortsupport);
 
 
 static bool
@@ -215,3 +216,72 @@ gbt_int4_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_int4_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	int32KEY   *ia = (int32KEY *) DatumGetPointer(a);
+	int32KEY   *ib = (int32KEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_int4_abbrev_convert(Datum original, SortSupport ssup)
+{
+	int32KEY   *b1 = (int32KEY *) DatumGetPointer(original);
+	int32		z = b1->lower;
+
+	return (Datum) z;
+}
+
+static int
+gbt_int4_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+/*
+ * We never consider aborting the abbreviation.
+ */
+static bool
+gbt_int4_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_int4_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_int4_cmp_abbrev;
+		ssup->abbrev_converter = gbt_int4_abbrev_convert;
+		ssup->abbrev_abort = gbt_int4_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_int4_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_int4_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_int8.c b/contrib/btree_gist/btree_int8.c
index df2b0d174b..18be500ab1 100644
--- a/contrib/btree_gist/btree_int8.c
+++ b/contrib/btree_gist/btree_int8.c
@@ -24,7 +24,7 @@ PG_FUNCTION_INFO_V1(gbt_int8_consistent);
 PG_FUNCTION_INFO_V1(gbt_int8_distance);
 PG_FUNCTION_INFO_V1(gbt_int8_penalty);
 PG_FUNCTION_INFO_V1(gbt_int8_same);
-
+PG_FUNCTION_INFO_V1(gbt_int8_sortsupport);
 
 static bool
 gbt_int8gt(const void *a, const void *b, FmgrInfo *flinfo)
@@ -215,3 +215,76 @@ gbt_int8_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_int8_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	int64KEY   *ia = (int64KEY *) DatumGetPointer(a);
+	int64KEY   *ib = (int64KEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_int8_abbrev_convert(Datum original, SortSupport ssup)
+{
+	int64KEY   *b1 = (int64KEY *) DatumGetPointer(original);
+	int64		z = b1->lower;
+
+#if SIZEOF_DATUM == 8
+	return (Datum) z;
+#else
+	return (Datum) (z >> 32);
+#endif
+}
+
+static int
+gbt_int8_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+/*
+ * We never consider aborting the abbreviation.
+ */
+static bool
+gbt_int8_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_int8_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_int8_cmp_abbrev;
+		ssup->abbrev_converter = gbt_int8_abbrev_convert;
+		ssup->abbrev_abort = gbt_int8_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_int8_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_int8_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_interval.c b/contrib/btree_gist/btree_interval.c
index a4b3b2b1e6..2657213ec7 100644
--- a/contrib/btree_gist/btree_interval.c
+++ b/contrib/btree_gist/btree_interval.c
@@ -27,6 +27,7 @@ PG_FUNCTION_INFO_V1(gbt_intv_consistent);
 PG_FUNCTION_INFO_V1(gbt_intv_distance);
 PG_FUNCTION_INFO_V1(gbt_intv_penalty);
 PG_FUNCTION_INFO_V1(gbt_intv_same);
+PG_FUNCTION_INFO_V1(gbt_intv_sortsupport);
 
 
 static bool
@@ -297,3 +298,27 @@ gbt_intv_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_intv_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	intvKEY   *ia = (intvKEY *) DatumGetPointer(a);
+	intvKEY   *ib = (intvKEY *) DatumGetPointer(b);
+
+	return DatumGetInt32(DirectFunctionCall2(interval_cmp, IntervalPGetDatum(&ia->lower), IntervalPGetDatum(&ib->lower)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_intv_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_intv_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_macaddr.c b/contrib/btree_gist/btree_macaddr.c
index 7f0e9e9c91..f43edad74e 100644
--- a/contrib/btree_gist/btree_macaddr.c
+++ b/contrib/btree_gist/btree_macaddr.c
@@ -25,6 +25,7 @@ PG_FUNCTION_INFO_V1(gbt_macad_picksplit);
 PG_FUNCTION_INFO_V1(gbt_macad_consistent);
 PG_FUNCTION_INFO_V1(gbt_macad_penalty);
 PG_FUNCTION_INFO_V1(gbt_macad_same);
+PG_FUNCTION_INFO_V1(gbt_macad_sortsupport);
 
 
 static bool
@@ -195,3 +196,73 @@ gbt_macad_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_macad_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	macKEY   *ma = (macKEY *) DatumGetPointer(a);
+	macKEY   *mb = (macKEY *) DatumGetPointer(b);
+	uint64    ia  = mac_2_uint64(&ma->lower);
+	uint64    ib  = mac_2_uint64(&mb->lower);
+
+	/* for leaf items we expect lower == upper */
+
+	if (ia == ib)
+	{
+		return 0;
+	}
+
+	return (ia > ib) ? 1 : -1;
+}
+
+static Datum
+gbt_macad_abbrev_convert(Datum original, SortSupport ssup)
+{
+	macKEY   *b1 = (macKEY *) DatumGetPointer(original);
+	uint64    z  = mac_2_uint64(&b1->lower);
+
+#if SIZEOF_DATUM == 8
+	return (Datum) z;
+#else
+	return (Datum) z>>32;
+#endif
+}
+
+static int
+gbt_macad_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+static bool
+gbt_macad_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_macad_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_macad_cmp_abbrev;
+		ssup->abbrev_converter = gbt_macad_abbrev_convert;
+		ssup->abbrev_abort = gbt_macad_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_macad_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_macad_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_macaddr8.c b/contrib/btree_gist/btree_macaddr8.c
index ab4bca5d50..458c0bf6f7 100644
--- a/contrib/btree_gist/btree_macaddr8.c
+++ b/contrib/btree_gist/btree_macaddr8.c
@@ -25,6 +25,7 @@ PG_FUNCTION_INFO_V1(gbt_macad8_picksplit);
 PG_FUNCTION_INFO_V1(gbt_macad8_consistent);
 PG_FUNCTION_INFO_V1(gbt_macad8_penalty);
 PG_FUNCTION_INFO_V1(gbt_macad8_same);
+PG_FUNCTION_INFO_V1(gbt_macad8_sortsupport);
 
 
 static bool
@@ -195,3 +196,73 @@ gbt_macad8_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_macad8_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	mac8KEY   *ma = (mac8KEY *) DatumGetPointer(a);
+	mac8KEY   *mb = (mac8KEY *) DatumGetPointer(b);
+	uint64    ia  = mac8_2_uint64(&ma->lower);
+	uint64    ib  = mac8_2_uint64(&mb->lower);
+
+	/* for leaf items we expect lower == upper */
+
+	if (ia == ib)
+	{
+		return 0;
+	}
+
+	return (ia > ib) ? 1 : -1;
+}
+
+static Datum
+gbt_macad8_abbrev_convert(Datum original, SortSupport ssup)
+{
+	mac8KEY   *b1 = (mac8KEY *) DatumGetPointer(original);
+	uint64    z  = mac8_2_uint64(&b1->lower);
+
+#if SIZEOF_DATUM == 8
+	return (Datum) z;
+#else
+	return (Datum) z>>32;
+#endif
+}
+
+static int
+gbt_macad8_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+static bool
+gbt_macad8_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_macad8_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_macad8_cmp_abbrev;
+		ssup->abbrev_converter = gbt_macad8_abbrev_convert;
+		ssup->abbrev_abort = gbt_macad8_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_macad8_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_macad8_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_numeric.c b/contrib/btree_gist/btree_numeric.c
index 35e466cdd9..ed5305397a 100644
--- a/contrib/btree_gist/btree_numeric.c
+++ b/contrib/btree_gist/btree_numeric.c
@@ -21,6 +21,7 @@ PG_FUNCTION_INFO_V1(gbt_numeric_picksplit);
 PG_FUNCTION_INFO_V1(gbt_numeric_consistent);
 PG_FUNCTION_INFO_V1(gbt_numeric_penalty);
 PG_FUNCTION_INFO_V1(gbt_numeric_same);
+PG_FUNCTION_INFO_V1(gbt_numeric_sortsupport);
 
 
 /* define for comparison */
@@ -227,3 +228,26 @@ gbt_numeric_picksplit(PG_FUNCTION_ARGS)
 					  &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(v);
 }
+
+static int
+gbt_numeric_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	return DatumGetInt32(DirectFunctionCall2(numeric_cmp,
+											 PointerGetDatum(a),
+											 PointerGetDatum(b)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_numeric_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_numeric_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
diff --git a/contrib/btree_gist/btree_oid.c b/contrib/btree_gist/btree_oid.c
index 3cc7d4245d..7cad25ba38 100644
--- a/contrib/btree_gist/btree_oid.c
+++ b/contrib/btree_gist/btree_oid.c
@@ -23,6 +23,7 @@ PG_FUNCTION_INFO_V1(gbt_oid_consistent);
 PG_FUNCTION_INFO_V1(gbt_oid_distance);
 PG_FUNCTION_INFO_V1(gbt_oid_penalty);
 PG_FUNCTION_INFO_V1(gbt_oid_same);
+PG_FUNCTION_INFO_V1(gbt_oid_sortsupport);
 
 
 static bool
@@ -215,3 +216,73 @@ gbt_oid_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_oid_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	oidKEY   *ia = (oidKEY *) DatumGetPointer(a);
+	oidKEY   *ib = (oidKEY *) DatumGetPointer(b);
+
+	/* for leaf items we expect lower == upper */
+	Assert(ia->lower == ia->upper);
+	Assert(ib->lower == ib->upper);
+
+	if (ia->lower == ib->lower)
+	{
+		return 0;
+	}
+
+	return (ia->lower > ib->lower) ? 1 : -1;
+}
+
+static Datum
+gbt_oid_abbrev_convert(Datum original, SortSupport ssup)
+{
+	oidKEY   *b1 = (oidKEY *) DatumGetPointer(original);
+	Oid		z = b1->lower;
+
+	return (Datum) z;
+}
+
+static int
+gbt_oid_cmp_abbrev(Datum z1, Datum z2, SortSupport ssup)
+{
+	if (z1 > z2)
+		return 1;
+	else if (z1 < z2)
+		return -1;
+	else
+		return 0;
+}
+
+/*
+ * We never consider aborting the abbreviation.
+ */
+static bool
+gbt_oid_abbrev_abort(int memtupcount, SortSupport ssup)
+{
+	return false;
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_oid_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	if (ssup->abbreviate)
+	{
+		ssup->comparator = gbt_oid_cmp_abbrev;
+		ssup->abbrev_converter = gbt_oid_abbrev_convert;
+		ssup->abbrev_abort = gbt_oid_abbrev_abort;
+		ssup->abbrev_full_comparator = gbt_oid_sort_build_cmp;
+	}
+	else
+	{
+		ssup->comparator = gbt_oid_sort_build_cmp;
+	}
+	PG_RETURN_VOID();
+}
+
diff --git a/contrib/btree_gist/btree_text.c b/contrib/btree_gist/btree_text.c
index 8019d11281..1a3fa2a39a 100644
--- a/contrib/btree_gist/btree_text.c
+++ b/contrib/btree_gist/btree_text.c
@@ -18,6 +18,7 @@ PG_FUNCTION_INFO_V1(gbt_text_consistent);
 PG_FUNCTION_INFO_V1(gbt_bpchar_consistent);
 PG_FUNCTION_INFO_V1(gbt_text_penalty);
 PG_FUNCTION_INFO_V1(gbt_text_same);
+PG_FUNCTION_INFO_V1(gbt_text_sortsupport);
 
 
 /* define for comparison */
@@ -239,3 +240,28 @@ gbt_text_penalty(PG_FUNCTION_ARGS)
 	PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
 									  &tinfo, fcinfo->flinfo));
 }
+
+static int
+gbt_text_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	return DatumGetInt32(DirectFunctionCall2Coll(bttextcmp,
+											 ssup->ssup_collation,
+											 PointerGetDatum(a),
+											 PointerGetDatum(b)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_text_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_text_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
+
diff --git a/contrib/btree_gist/btree_time.c b/contrib/btree_gist/btree_time.c
index fd8774a2f0..7d6009150b 100644
--- a/contrib/btree_gist/btree_time.c
+++ b/contrib/btree_gist/btree_time.c
@@ -28,6 +28,7 @@ PG_FUNCTION_INFO_V1(gbt_time_distance);
 PG_FUNCTION_INFO_V1(gbt_timetz_consistent);
 PG_FUNCTION_INFO_V1(gbt_time_penalty);
 PG_FUNCTION_INFO_V1(gbt_time_same);
+PG_FUNCTION_INFO_V1(gbt_time_sortsupport);
 
 
 #ifdef USE_FLOAT8_BYVAL
@@ -332,3 +333,29 @@ gbt_time_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_time_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	timeKEY    *ia = (timeKEY *) DatumGetPointer(a);
+	timeKEY    *ib = (timeKEY *) DatumGetPointer(b);
+
+	return DatumGetInt32(DirectFunctionCall2(time_cmp, TimeADTGetDatumFast(ia->lower), TimeADTGetDatumFast(ib->lower)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_time_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_time_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
+
+
diff --git a/contrib/btree_gist/btree_ts.c b/contrib/btree_gist/btree_ts.c
index 2671ba961c..31b2024c32 100644
--- a/contrib/btree_gist/btree_ts.c
+++ b/contrib/btree_gist/btree_ts.c
@@ -31,6 +31,7 @@ PG_FUNCTION_INFO_V1(gbt_tstz_consistent);
 PG_FUNCTION_INFO_V1(gbt_tstz_distance);
 PG_FUNCTION_INFO_V1(gbt_ts_penalty);
 PG_FUNCTION_INFO_V1(gbt_ts_same);
+PG_FUNCTION_INFO_V1(gbt_ts_sortsupport);
 
 
 #ifdef USE_FLOAT8_BYVAL
@@ -399,3 +400,28 @@ gbt_ts_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_ts_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	tsKEY   *ia = (tsKEY *) DatumGetPointer(a);
+	tsKEY   *ib = (tsKEY *) DatumGetPointer(b);
+
+	return DatumGetInt32(DirectFunctionCall2(timestamp_cmp, TimestampGetDatumFast(ia->lower), TimestampGetDatumFast(ib->lower)));
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_ts_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_ts_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
+
diff --git a/contrib/btree_gist/btree_uuid.c b/contrib/btree_gist/btree_uuid.c
index b81875979a..43a8cd896a 100644
--- a/contrib/btree_gist/btree_uuid.c
+++ b/contrib/btree_gist/btree_uuid.c
@@ -25,6 +25,7 @@ PG_FUNCTION_INFO_V1(gbt_uuid_picksplit);
 PG_FUNCTION_INFO_V1(gbt_uuid_consistent);
 PG_FUNCTION_INFO_V1(gbt_uuid_penalty);
 PG_FUNCTION_INFO_V1(gbt_uuid_same);
+PG_FUNCTION_INFO_V1(gbt_uuid_sortsupport);
 
 
 static int
@@ -233,3 +234,28 @@ gbt_uuid_same(PG_FUNCTION_ARGS)
 	*result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
 	PG_RETURN_POINTER(result);
 }
+
+static int
+gbt_uuid_sort_build_cmp(Datum a, Datum b, SortSupport ssup)
+{
+	uuidKEY   *ua = (uuidKEY *) DatumGetPointer(a);
+	uuidKEY   *ub = (uuidKEY *) DatumGetPointer(b);
+
+	return uuid_internal_cmp(&ua->lower, &ub->lower);
+}
+
+/*
+ * Sort support routine for fast GiST index build by sorting.
+ */
+Datum
+gbt_uuid_sortsupport(PG_FUNCTION_ARGS)
+{
+	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
+
+	ssup->comparator = gbt_uuid_sort_build_cmp;
+	ssup->abbrev_converter = NULL;
+	ssup->abbrev_abort = NULL;
+	ssup->abbrev_full_comparator = NULL;
+	PG_RETURN_VOID();
+}
+
diff --git a/contrib/btree_gist/expected/bit.out b/contrib/btree_gist/expected/bit.out
index e57871f310..fca3a20eec 100644
--- a/contrib/btree_gist/expected/bit.out
+++ b/contrib/btree_gist/expected/bit.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM bittmp WHERE a >   '011011000100010111011000110000100';
    350
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX bitidx ON bittmp USING GIST ( a );
+DEBUG:  building index "bitidx" on table "bittmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX bitidx_b ON bittmp USING GIST ( a ) WITH (buffering=on);
+DEBUG:  building index "bitidx_b" on table "bittmp" serially
+DROP INDEX bitidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM bittmp WHERE a <   '011011000100010111011000110000100';
  count 
diff --git a/contrib/btree_gist/expected/bytea.out b/contrib/btree_gist/expected/bytea.out
index b9efa73c08..d40974025e 100644
--- a/contrib/btree_gist/expected/bytea.out
+++ b/contrib/btree_gist/expected/bytea.out
@@ -33,7 +33,14 @@ SELECT count(*) FROM byteatmp WHERE a >   '31b0';
    400
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX byteaidx ON byteatmp USING GIST ( a );
+DEBUG:  building index "byteaidx" on table "byteatmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX byteaidx_b ON byteatmp USING GIST ( a ) WITH (buffering=on);
+DEBUG:  building index "byteaidx_b" on table "byteatmp" serially
+DROP INDEX byteaidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM byteatmp WHERE a <   '31b0'::bytea;
  count 
diff --git a/contrib/btree_gist/expected/cash.out b/contrib/btree_gist/expected/cash.out
index 7fbc735592..650a5b8635 100644
--- a/contrib/btree_gist/expected/cash.out
+++ b/contrib/btree_gist/expected/cash.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
  $21,915.01 |  $442.22
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX moneyidx ON moneytmp USING gist ( a );
+DEBUG:  building index "moneyidx" on table "moneytmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX moneyidx_b ON moneytmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "moneyidx_b" on table "moneytmp" serially
+DROP INDEX moneyidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM moneytmp WHERE a <  '22649.64'::money;
  count 
diff --git a/contrib/btree_gist/expected/char.out b/contrib/btree_gist/expected/char.out
index d715c045cc..9ead7907cf 100644
--- a/contrib/btree_gist/expected/char.out
+++ b/contrib/btree_gist/expected/char.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM chartmp WHERE a >   '31b0'::char(32);
    400
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX charidx ON chartmp USING GIST ( a );
+DEBUG:  building index "charidx" on table "chartmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX charidx_b ON chartmp USING GIST ( a ) WITH (buffering=on);
+DEBUG:  building index "charidx_b" on table "chartmp" serially
+DROP INDEX charidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM chartmp WHERE a <   '31b0'::char(32);
  count 
diff --git a/contrib/btree_gist/expected/cidr.out b/contrib/btree_gist/expected/cidr.out
index 6d0995add6..fd6b3d9657 100644
--- a/contrib/btree_gist/expected/cidr.out
+++ b/contrib/btree_gist/expected/cidr.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM cidrtmp WHERE a >  '121.111.63.82';
    309
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX cidridx ON cidrtmp USING gist ( a );
+DEBUG:  building index "cidridx" on table "cidrtmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX cidridx_b ON cidrtmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "cidridx_b" on table "cidrtmp" serially
+DROP INDEX cidridx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM cidrtmp WHERE a <  '121.111.63.82'::cidr;
  count 
diff --git a/contrib/btree_gist/expected/date.out b/contrib/btree_gist/expected/date.out
index 5db864bb82..df7e1b3d3e 100644
--- a/contrib/btree_gist/expected/date.out
+++ b/contrib/btree_gist/expected/date.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
  03-24-2001 |       39
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX dateidx ON datetmp USING gist ( a );
+DEBUG:  building index "dateidx" on table "datetmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX dateidx_b ON datetmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "dateidx_b" on table "datetmp" serially
+DROP INDEX dateidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM datetmp WHERE a <  '2001-02-13'::date;
  count 
diff --git a/contrib/btree_gist/expected/enum.out b/contrib/btree_gist/expected/enum.out
index c4b769dd4b..15cd580c8e 100644
--- a/contrib/btree_gist/expected/enum.out
+++ b/contrib/btree_gist/expected/enum.out
@@ -46,7 +46,14 @@ SELECT count(*) FROM enumtmp WHERE a >  'g'::rainbow;
    230
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX enumidx ON enumtmp USING gist ( a );
+DEBUG:  building index "enumidx" on table "enumtmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX enumidx_b ON enumtmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "enumidx_b" on table "enumtmp" serially
+DROP INDEX enumidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM enumtmp WHERE a <  'g'::rainbow;
  count 
diff --git a/contrib/btree_gist/expected/float4.out b/contrib/btree_gist/expected/float4.out
index dfe732049e..b3cfa72dba 100644
--- a/contrib/btree_gist/expected/float4.out
+++ b/contrib/btree_gist/expected/float4.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
  -158.17741 | 20.822586
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX float4idx ON float4tmp USING gist ( a );
+DEBUG:  building index "float4idx" on table "float4tmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX float4idx_b ON float4tmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "float4idx_b" on table "float4tmp" serially
+DROP INDEX float4idx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM float4tmp WHERE a <  -179.0::float4;
  count 
diff --git a/contrib/btree_gist/expected/float8.out b/contrib/btree_gist/expected/float8.out
index ebd0ef3d68..e90a061f68 100644
--- a/contrib/btree_gist/expected/float8.out
+++ b/contrib/btree_gist/expected/float8.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
   -1769.73634 | 120.26366000000007
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX float8idx ON float8tmp USING gist ( a );
+DEBUG:  building index "float8idx" on table "float8tmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX float8idx_b ON float8tmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "float8idx_b" on table "float8tmp" serially
+DROP INDEX float8idx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM float8tmp WHERE a <  -1890.0::float8;
  count 
diff --git a/contrib/btree_gist/expected/inet.out b/contrib/btree_gist/expected/inet.out
index c323d903da..e11d83a955 100644
--- a/contrib/btree_gist/expected/inet.out
+++ b/contrib/btree_gist/expected/inet.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM inettmp WHERE a >  '89.225.196.191';
    386
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX inetidx ON inettmp USING gist ( a );
+DEBUG:  building index "inetidx" on table "inettmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX inetidx_b ON inettmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "inetidx_b" on table "inettmp" serially
+DROP INDEX inetidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM inettmp WHERE a <  '89.225.196.191'::inet;
  count 
diff --git a/contrib/btree_gist/expected/int2.out b/contrib/btree_gist/expected/int2.out
index 50a332939b..c695767bd2 100644
--- a/contrib/btree_gist/expected/int2.out
+++ b/contrib/btree_gist/expected/int2.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
  228 |        9
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX int2idx ON int2tmp USING gist ( a );
+DEBUG:  building index "int2idx" on table "int2tmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX int2idx_b ON int2tmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "int2idx_b" on table "int2tmp" serially
+DROP INDEX int2idx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM int2tmp WHERE a <  237::int2;
  count 
diff --git a/contrib/btree_gist/expected/int4.out b/contrib/btree_gist/expected/int4.out
index 6bbdc7c3f4..3a45937ca1 100644
--- a/contrib/btree_gist/expected/int4.out
+++ b/contrib/btree_gist/expected/int4.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
  228 |        9
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX int4idx ON int4tmp USING gist ( a );
+DEBUG:  building index "int4idx" on table "int4tmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX int4idx_b ON int4tmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "int4idx_b" on table "int4tmp" serially
+DROP INDEX int4idx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM int4tmp WHERE a <  237::int4;
  count 
diff --git a/contrib/btree_gist/expected/int8.out b/contrib/btree_gist/expected/int8.out
index eff77c26b5..ee7bbae0fc 100644
--- a/contrib/btree_gist/expected/int8.out
+++ b/contrib/btree_gist/expected/int8.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841'
  478227196042750 | 13655904687909
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX int8idx ON int8tmp USING gist ( a );
+DEBUG:  building index "int8idx" on table "int8tmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX int8idx_b ON int8tmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "int8idx_b" on table "int8tmp" serially
+DROP INDEX int8idx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM int8tmp WHERE a <  464571291354841::int8;
  count 
diff --git a/contrib/btree_gist/expected/interval.out b/contrib/btree_gist/expected/interval.out
index 4c3d494e4a..d720ffbccc 100644
--- a/contrib/btree_gist/expected/interval.out
+++ b/contrib/btree_gist/expected/interval.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21
  @ 220 days 19 hours 5 mins 42 secs  | @ 21 days -2 hours -15 mins -41 secs
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX intervalidx ON intervaltmp USING gist ( a );
+DEBUG:  building index "intervalidx" on table "intervaltmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX intervalidx_b ON intervaltmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "intervalidx_b" on table "intervaltmp" serially
+DROP INDEX intervalidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM intervaltmp WHERE a <  '199 days 21:21:23'::interval;
  count 
diff --git a/contrib/btree_gist/expected/macaddr.out b/contrib/btree_gist/expected/macaddr.out
index c0a4c6287f..81fe01eaa4 100644
--- a/contrib/btree_gist/expected/macaddr.out
+++ b/contrib/btree_gist/expected/macaddr.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM macaddrtmp WHERE a >  '22:00:5c:e5:9b:0d';
    540
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX macaddridx ON macaddrtmp USING gist ( a );
+DEBUG:  building index "macaddridx" on table "macaddrtmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX macaddridx_b ON macaddrtmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "macaddridx_b" on table "macaddrtmp" serially
+DROP INDEX macaddridx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM macaddrtmp WHERE a <  '22:00:5c:e5:9b:0d'::macaddr;
  count 
diff --git a/contrib/btree_gist/expected/macaddr8.out b/contrib/btree_gist/expected/macaddr8.out
index e5ec6a5dea..30afd94380 100644
--- a/contrib/btree_gist/expected/macaddr8.out
+++ b/contrib/btree_gist/expected/macaddr8.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM macaddr8tmp WHERE a >  '22:00:5c:e5:9b:0d';
    540
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX macaddr8idx ON macaddr8tmp USING gist ( a );
+DEBUG:  building index "macaddr8idx" on table "macaddr8tmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX macaddr8idx_b ON macaddr8tmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "macaddr8idx_b" on table "macaddr8tmp" serially
+DROP INDEX macaddr8idx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM macaddr8tmp WHERE a <  '22:00:5c:e5:9b:0d'::macaddr8;
  count 
diff --git a/contrib/btree_gist/expected/numeric.out b/contrib/btree_gist/expected/numeric.out
index ae839b8ec8..0b0d491a40 100644
--- a/contrib/btree_gist/expected/numeric.out
+++ b/contrib/btree_gist/expected/numeric.out
@@ -94,7 +94,14 @@ SELECT count(*) FROM numerictmp WHERE a >  0 ;
    576
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX numericidx ON numerictmp USING gist ( a );
+DEBUG:  building index "numericidx" on table "numerictmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX numericidx_b ON numerictmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "numericidx_b" on table "numerictmp" serially
+DROP INDEX numericidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM numerictmp WHERE a <  -1890.0;
  count 
diff --git a/contrib/btree_gist/expected/oid.out b/contrib/btree_gist/expected/oid.out
index 776bbb1026..3c8746483f 100644
--- a/contrib/btree_gist/expected/oid.out
+++ b/contrib/btree_gist/expected/oid.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM oidtmp WHERE oid >  17;
    983
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX oididx ON oidtmp USING gist ( oid );
+DEBUG:  building index "oididx" on table "oidtmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX oididx_b ON oidtmp USING gist ( oid ) WITH (buffering=on);
+DEBUG:  building index "oididx_b" on table "oidtmp" serially
+DROP INDEX oididx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM oidtmp WHERE oid <  17;
  count 
diff --git a/contrib/btree_gist/expected/text.out b/contrib/btree_gist/expected/text.out
index bb4e2e62d1..4f17ac413f 100644
--- a/contrib/btree_gist/expected/text.out
+++ b/contrib/btree_gist/expected/text.out
@@ -33,7 +33,14 @@ SELECT count(*) FROM texttmp WHERE a >   '31b0';
    400
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX textidx ON texttmp USING GIST ( a );
+DEBUG:  building index "textidx" on table "texttmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX textidx_b ON texttmp USING GIST ( a ) WITH (buffering=on);
+DEBUG:  building index "textidx_b" on table "texttmp" serially
+DROP INDEX textidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM texttmp WHERE a <   '31b0'::text;
  count 
diff --git a/contrib/btree_gist/expected/time.out b/contrib/btree_gist/expected/time.out
index ec95ef77c5..a71dfc980a 100644
--- a/contrib/btree_gist/expected/time.out
+++ b/contrib/btree_gist/expected/time.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
  10:55:32 | @ 1 min 39 secs
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX timeidx ON timetmp USING gist ( a );
+DEBUG:  building index "timeidx" on table "timetmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX timeidx_b ON timetmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "timeidx_b" on table "timetmp" serially
+DROP INDEX timeidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM timetmp WHERE a <  '10:57:11'::time;
  count 
diff --git a/contrib/btree_gist/expected/timestamp.out b/contrib/btree_gist/expected/timestamp.out
index 0d94f2f245..b96d5ade8c 100644
--- a/contrib/btree_gist/expected/timestamp.out
+++ b/contrib/btree_gist/expected/timestamp.out
@@ -40,7 +40,14 @@ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-
  Mon Nov 29 20:12:43 2004 | @ 34 days 11 hours 17 mins 35 secs
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX timestampidx ON timestamptmp USING gist ( a );
+DEBUG:  building index "timestampidx" on table "timestamptmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX timestampidx_b ON timestamptmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "timestampidx_b" on table "timestamptmp" serially
+DROP INDEX timestampidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM timestamptmp WHERE a <  '2004-10-26 08:55:08'::timestamp;
  count 
diff --git a/contrib/btree_gist/expected/timestamptz.out b/contrib/btree_gist/expected/timestamptz.out
index 75a15a4256..72f466fd7c 100644
--- a/contrib/btree_gist/expected/timestamptz.out
+++ b/contrib/btree_gist/expected/timestamptz.out
@@ -100,7 +100,14 @@ SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '
  Thu Jan 24 12:28:12 2019 PST | @ 37 days 7 hours 28 mins 18 secs
 (3 rows)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX timestamptzidx ON timestamptztmp USING gist ( a );
+DEBUG:  building index "timestamptzidx" on table "timestamptztmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX timestamptzidx_b ON timestamptztmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "timestamptzidx_b" on table "timestamptztmp" serially
+DROP INDEX timestamptzidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM timestamptztmp WHERE a <  '2018-12-18 10:59:54 GMT+3'::timestamptz;
  count 
diff --git a/contrib/btree_gist/expected/timetz.out b/contrib/btree_gist/expected/timetz.out
index 7f73e44797..2eb70af8b1 100644
--- a/contrib/btree_gist/expected/timetz.out
+++ b/contrib/btree_gist/expected/timetz.out
@@ -18,7 +18,14 @@ INSERT INTO timetzcmp (r_id,a) SELECT 22,count(*) FROM timetztmp WHERE a <= '07:
 INSERT INTO timetzcmp (r_id,a) SELECT 23,count(*) FROM timetztmp WHERE a  = '07:46:45 GMT+4';
 INSERT INTO timetzcmp (r_id,a) SELECT 24,count(*) FROM timetztmp WHERE a >= '07:46:45 GMT+4';
 INSERT INTO timetzcmp (r_id,a) SELECT 25,count(*) FROM timetztmp WHERE a >  '07:46:45 GMT+4';
+SET client_min_messages = DEBUG1;
 CREATE INDEX timetzidx ON timetztmp USING gist ( a );
+DEBUG:  building index "timetzidx" on table "timetztmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX timetzidx_b ON timetztmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "timetzidx_b" on table "timetztmp" serially
+DROP INDEX timetzidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 UPDATE timetzcmp SET b=c FROM ( SELECT count(*) AS c FROM timetztmp WHERE a <  '07:46:45 GMT+3'::timetz ) q WHERE r_id=1 ;
 UPDATE timetzcmp SET b=c FROM ( SELECT count(*) AS c FROM timetztmp WHERE a <= '07:46:45 GMT+3'::timetz ) q WHERE r_id=2 ;
diff --git a/contrib/btree_gist/expected/uuid.out b/contrib/btree_gist/expected/uuid.out
index a34b024603..112d70055a 100644
--- a/contrib/btree_gist/expected/uuid.out
+++ b/contrib/btree_gist/expected/uuid.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM uuidtmp WHERE a >  '55e65ca2-4136-4a4b-ba78-cd3fe4678203';
    375
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX uuididx ON uuidtmp USING gist ( a );
+DEBUG:  building index "uuididx" on table "uuidtmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX uuididx_b ON uuidtmp USING gist ( a ) WITH (buffering=on);
+DEBUG:  building index "uuididx_b" on table "uuidtmp" serially
+DROP INDEX uuididx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM uuidtmp WHERE a <  '55e65ca2-4136-4a4b-ba78-cd3fe4678203'::uuid;
  count 
diff --git a/contrib/btree_gist/expected/varbit.out b/contrib/btree_gist/expected/varbit.out
index ede36bc3ea..d64d0ee6b4 100644
--- a/contrib/btree_gist/expected/varbit.out
+++ b/contrib/btree_gist/expected/varbit.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM varbittmp WHERE a >   '1110100111010';
     50
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX varbitidx ON varbittmp USING GIST ( a );
+DEBUG:  building index "varbitidx" on table "varbittmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX varbitidx_b ON varbittmp USING GIST ( a ) WITH (buffering=on);
+DEBUG:  building index "varbitidx_b" on table "varbittmp" serially
+DROP INDEX varbitidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM varbittmp WHERE a <   '1110100111010'::varbit;
  count 
diff --git a/contrib/btree_gist/expected/varchar.out b/contrib/btree_gist/expected/varchar.out
index d071d714cd..9f4f95616a 100644
--- a/contrib/btree_gist/expected/varchar.out
+++ b/contrib/btree_gist/expected/varchar.out
@@ -32,7 +32,14 @@ SELECT count(*) FROM vchartmp WHERE a >   '31b0'::varchar(32);
    400
 (1 row)
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX vcharidx ON vchartmp USING GIST ( text(a) );
+DEBUG:  building index "vcharidx" on table "vchartmp" serially
+DEBUG:  Using sorted GiST build
+CREATE INDEX vcharidx_b ON vchartmp USING GIST ( text(a) ) WITH (buffering=on);
+DEBUG:  building index "vcharidx_b" on table "vchartmp" serially
+DROP INDEX vcharidx_b;
+RESET client_min_messages;
 SET enable_seqscan=off;
 SELECT count(*) FROM vchartmp WHERE a <   '31b0'::varchar(32);
  count 
diff --git a/contrib/btree_gist/sql/bit.sql b/contrib/btree_gist/sql/bit.sql
index a733042023..53c67cf77a 100644
--- a/contrib/btree_gist/sql/bit.sql
+++ b/contrib/btree_gist/sql/bit.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM bittmp WHERE a >=  '011011000100010111011000110000100';
 
 SELECT count(*) FROM bittmp WHERE a >   '011011000100010111011000110000100';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX bitidx ON bittmp USING GIST ( a );
+CREATE INDEX bitidx_b ON bittmp USING GIST ( a ) WITH (buffering=on);
+DROP INDEX bitidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/bytea.sql b/contrib/btree_gist/sql/bytea.sql
index 6885f5e56d..fdfa0c345b 100644
--- a/contrib/btree_gist/sql/bytea.sql
+++ b/contrib/btree_gist/sql/bytea.sql
@@ -17,7 +17,11 @@ SELECT count(*) FROM byteatmp WHERE a >=  '31b0';
 
 SELECT count(*) FROM byteatmp WHERE a >   '31b0';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX byteaidx ON byteatmp USING GIST ( a );
+CREATE INDEX byteaidx_b ON byteatmp USING GIST ( a ) WITH (buffering=on);
+DROP INDEX byteaidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/cash.sql b/contrib/btree_gist/sql/cash.sql
index 4526cc4f0a..0581b3593e 100644
--- a/contrib/btree_gist/sql/cash.sql
+++ b/contrib/btree_gist/sql/cash.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM moneytmp WHERE a >  '22649.64';
 
 SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX moneyidx ON moneytmp USING gist ( a );
+CREATE INDEX moneyidx_b ON moneytmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX moneyidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/char.sql b/contrib/btree_gist/sql/char.sql
index f6eb52e672..234eabee3b 100644
--- a/contrib/btree_gist/sql/char.sql
+++ b/contrib/btree_gist/sql/char.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM chartmp WHERE a >=  '31b0'::char(32);
 
 SELECT count(*) FROM chartmp WHERE a >   '31b0'::char(32);
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX charidx ON chartmp USING GIST ( a );
+CREATE INDEX charidx_b ON chartmp USING GIST ( a ) WITH (buffering=on);
+DROP INDEX charidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/cidr.sql b/contrib/btree_gist/sql/cidr.sql
index 9bd77185b9..be2d22b079 100644
--- a/contrib/btree_gist/sql/cidr.sql
+++ b/contrib/btree_gist/sql/cidr.sql
@@ -15,7 +15,11 @@ SELECT count(*) FROM cidrtmp WHERE a >= '121.111.63.82';
 
 SELECT count(*) FROM cidrtmp WHERE a >  '121.111.63.82';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX cidridx ON cidrtmp USING gist ( a );
+CREATE INDEX cidridx_b ON cidrtmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX cidridx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/date.sql b/contrib/btree_gist/sql/date.sql
index f969ef0a08..f007402bac 100644
--- a/contrib/btree_gist/sql/date.sql
+++ b/contrib/btree_gist/sql/date.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM datetmp WHERE a >  '2001-02-13';
 
 SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX dateidx ON datetmp USING gist ( a );
+CREATE INDEX dateidx_b ON datetmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX dateidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/enum.sql b/contrib/btree_gist/sql/enum.sql
index 476211e979..d6dbcb4239 100644
--- a/contrib/btree_gist/sql/enum.sql
+++ b/contrib/btree_gist/sql/enum.sql
@@ -20,7 +20,11 @@ SELECT count(*) FROM enumtmp WHERE a >= 'g'::rainbow;
 
 SELECT count(*) FROM enumtmp WHERE a >  'g'::rainbow;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX enumidx ON enumtmp USING gist ( a );
+CREATE INDEX enumidx_b ON enumtmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX enumidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/float4.sql b/contrib/btree_gist/sql/float4.sql
index 3da1ce953c..0e3eb49343 100644
--- a/contrib/btree_gist/sql/float4.sql
+++ b/contrib/btree_gist/sql/float4.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM float4tmp WHERE a >  -179.0;
 
 SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX float4idx ON float4tmp USING gist ( a );
+CREATE INDEX float4idx_b ON float4tmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX float4idx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/float8.sql b/contrib/btree_gist/sql/float8.sql
index e1e819b37f..6a216dd606 100644
--- a/contrib/btree_gist/sql/float8.sql
+++ b/contrib/btree_gist/sql/float8.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM float8tmp WHERE a >  -1890.0;
 
 SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX float8idx ON float8tmp USING gist ( a );
+CREATE INDEX float8idx_b ON float8tmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX float8idx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/inet.sql b/contrib/btree_gist/sql/inet.sql
index 4b8d354b00..0339c853d3 100644
--- a/contrib/btree_gist/sql/inet.sql
+++ b/contrib/btree_gist/sql/inet.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM inettmp WHERE a >= '89.225.196.191';
 
 SELECT count(*) FROM inettmp WHERE a >  '89.225.196.191';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX inetidx ON inettmp USING gist ( a );
+CREATE INDEX inetidx_b ON inettmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX inetidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/int2.sql b/contrib/btree_gist/sql/int2.sql
index 988518795f..bf98ac65f8 100644
--- a/contrib/btree_gist/sql/int2.sql
+++ b/contrib/btree_gist/sql/int2.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM int2tmp WHERE a >  237;
 
 SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX int2idx ON int2tmp USING gist ( a );
+CREATE INDEX int2idx_b ON int2tmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX int2idx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/int4.sql b/contrib/btree_gist/sql/int4.sql
index 659ab5ee24..214993314a 100644
--- a/contrib/btree_gist/sql/int4.sql
+++ b/contrib/btree_gist/sql/int4.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM int4tmp WHERE a >  237;
 
 SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX int4idx ON int4tmp USING gist ( a );
+CREATE INDEX int4idx_b ON int4tmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX int4idx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/int8.sql b/contrib/btree_gist/sql/int8.sql
index 51e55e9c14..8a6c2a4bfd 100644
--- a/contrib/btree_gist/sql/int8.sql
+++ b/contrib/btree_gist/sql/int8.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM int8tmp WHERE a >  464571291354841;
 
 SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX int8idx ON int8tmp USING gist ( a );
+CREATE INDEX int8idx_b ON int8tmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX int8idx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/interval.sql b/contrib/btree_gist/sql/interval.sql
index 346d6adcb5..6f9b1d4a39 100644
--- a/contrib/btree_gist/sql/interval.sql
+++ b/contrib/btree_gist/sql/interval.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM intervaltmp WHERE a >  '199 days 21:21:23';
 
 SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX intervalidx ON intervaltmp USING gist ( a );
+CREATE INDEX intervalidx_b ON intervaltmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX intervalidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/macaddr.sql b/contrib/btree_gist/sql/macaddr.sql
index 85c271f7ce..bccfc820ca 100644
--- a/contrib/btree_gist/sql/macaddr.sql
+++ b/contrib/btree_gist/sql/macaddr.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM macaddrtmp WHERE a >= '22:00:5c:e5:9b:0d';
 
 SELECT count(*) FROM macaddrtmp WHERE a >  '22:00:5c:e5:9b:0d';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX macaddridx ON macaddrtmp USING gist ( a );
+CREATE INDEX macaddridx_b ON macaddrtmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX macaddridx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/macaddr8.sql b/contrib/btree_gist/sql/macaddr8.sql
index 61e7d7af40..2d0447a777 100644
--- a/contrib/btree_gist/sql/macaddr8.sql
+++ b/contrib/btree_gist/sql/macaddr8.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM macaddr8tmp WHERE a >= '22:00:5c:e5:9b:0d';
 
 SELECT count(*) FROM macaddr8tmp WHERE a >  '22:00:5c:e5:9b:0d';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX macaddr8idx ON macaddr8tmp USING gist ( a );
+CREATE INDEX macaddr8idx_b ON macaddr8tmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX macaddr8idx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/numeric.sql b/contrib/btree_gist/sql/numeric.sql
index dbb2f2f183..55ecbcdadc 100644
--- a/contrib/btree_gist/sql/numeric.sql
+++ b/contrib/btree_gist/sql/numeric.sql
@@ -40,7 +40,11 @@ SELECT count(*) FROM numerictmp WHERE a >= 0 ;
 SELECT count(*) FROM numerictmp WHERE a >  0 ;
 
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX numericidx ON numerictmp USING gist ( a );
+CREATE INDEX numericidx_b ON numerictmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX numericidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/oid.sql b/contrib/btree_gist/sql/oid.sql
index c9358234ce..bc9ee0cba3 100644
--- a/contrib/btree_gist/sql/oid.sql
+++ b/contrib/btree_gist/sql/oid.sql
@@ -15,7 +15,11 @@ SELECT count(*) FROM oidtmp WHERE oid >= 17;
 
 SELECT count(*) FROM oidtmp WHERE oid >  17;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX oididx ON oidtmp USING gist ( oid );
+CREATE INDEX oididx_b ON oidtmp USING gist ( oid ) WITH (buffering=on);
+DROP INDEX oididx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/text.sql b/contrib/btree_gist/sql/text.sql
index 46597e731d..52705a216d 100644
--- a/contrib/btree_gist/sql/text.sql
+++ b/contrib/btree_gist/sql/text.sql
@@ -17,7 +17,11 @@ SELECT count(*) FROM texttmp WHERE a >=  '31b0';
 
 SELECT count(*) FROM texttmp WHERE a >   '31b0';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX textidx ON texttmp USING GIST ( a );
+CREATE INDEX textidx_b ON texttmp USING GIST ( a ) WITH (buffering=on);
+DROP INDEX textidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/time.sql b/contrib/btree_gist/sql/time.sql
index 6104e7f61c..6123945213 100644
--- a/contrib/btree_gist/sql/time.sql
+++ b/contrib/btree_gist/sql/time.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM timetmp WHERE a >  '10:57:11';
 
 SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX timeidx ON timetmp USING gist ( a );
+CREATE INDEX timeidx_b ON timetmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX timeidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/timestamp.sql b/contrib/btree_gist/sql/timestamp.sql
index 95effebfc4..66a14f5ae5 100644
--- a/contrib/btree_gist/sql/timestamp.sql
+++ b/contrib/btree_gist/sql/timestamp.sql
@@ -18,7 +18,11 @@ SELECT count(*) FROM timestamptmp WHERE a >  '2004-10-26 08:55:08';
 
 SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX timestampidx ON timestamptmp USING gist ( a );
+CREATE INDEX timestampidx_b ON timestamptmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX timestampidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/timestamptz.sql b/contrib/btree_gist/sql/timestamptz.sql
index f70caa4a64..2a92f63fc4 100644
--- a/contrib/btree_gist/sql/timestamptz.sql
+++ b/contrib/btree_gist/sql/timestamptz.sql
@@ -39,7 +39,11 @@ SELECT count(*) FROM timestamptztmp WHERE a >  '2018-12-18 10:59:54 GMT+4';
 
 SELECT a, a <-> '2018-12-18 10:59:54 GMT+2' FROM timestamptztmp ORDER BY a <-> '2018-12-18 10:59:54 GMT+2' LIMIT 3;
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX timestamptzidx ON timestamptztmp USING gist ( a );
+CREATE INDEX timestamptzidx_b ON timestamptztmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX timestamptzidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/timetz.sql b/contrib/btree_gist/sql/timetz.sql
index 2fb725db74..bc79d134b8 100644
--- a/contrib/btree_gist/sql/timetz.sql
+++ b/contrib/btree_gist/sql/timetz.sql
@@ -42,7 +42,11 @@ INSERT INTO timetzcmp (r_id,a) SELECT 25,count(*) FROM timetztmp WHERE a >  '07:
 
 
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX timetzidx ON timetztmp USING gist ( a );
+CREATE INDEX timetzidx_b ON timetztmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX timetzidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/uuid.sql b/contrib/btree_gist/sql/uuid.sql
index 3f7ad764e2..7771bc0d82 100644
--- a/contrib/btree_gist/sql/uuid.sql
+++ b/contrib/btree_gist/sql/uuid.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM uuidtmp WHERE a >= '55e65ca2-4136-4a4b-ba78-cd3fe4678203';
 
 SELECT count(*) FROM uuidtmp WHERE a >  '55e65ca2-4136-4a4b-ba78-cd3fe4678203';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX uuididx ON uuidtmp USING gist ( a );
+CREATE INDEX uuididx_b ON uuidtmp USING gist ( a ) WITH (buffering=on);
+DROP INDEX uuididx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/varbit.sql b/contrib/btree_gist/sql/varbit.sql
index e2a33b5a1b..6d8243572b 100644
--- a/contrib/btree_gist/sql/varbit.sql
+++ b/contrib/btree_gist/sql/varbit.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM varbittmp WHERE a >=  '1110100111010';
 
 SELECT count(*) FROM varbittmp WHERE a >   '1110100111010';
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX varbitidx ON varbittmp USING GIST ( a );
+CREATE INDEX varbitidx_b ON varbittmp USING GIST ( a ) WITH (buffering=on);
+DROP INDEX varbitidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/contrib/btree_gist/sql/varchar.sql b/contrib/btree_gist/sql/varchar.sql
index 8087a17704..59b77e0983 100644
--- a/contrib/btree_gist/sql/varchar.sql
+++ b/contrib/btree_gist/sql/varchar.sql
@@ -16,7 +16,11 @@ SELECT count(*) FROM vchartmp WHERE a >=  '31b0'::varchar(32);
 
 SELECT count(*) FROM vchartmp WHERE a >   '31b0'::varchar(32);
 
+SET client_min_messages = DEBUG1;
 CREATE INDEX vcharidx ON vchartmp USING GIST ( text(a) );
+CREATE INDEX vcharidx_b ON vchartmp USING GIST ( text(a) ) WITH (buffering=on);
+DROP INDEX vcharidx_b;
+RESET client_min_messages;
 
 SET enable_seqscan=off;
 
diff --git a/src/backend/access/gist/gistbuild.c b/src/backend/access/gist/gistbuild.c
index 9d3fa9c3b7..a1950bfc1e 100644
--- a/src/backend/access/gist/gistbuild.c
+++ b/src/backend/access/gist/gistbuild.c
@@ -257,6 +257,7 @@ gistbuild(Relation heap, Relation index, IndexInfo *indexInfo)
 
 	if (buildstate.buildMode == GIST_SORTED_BUILD)
 	{
+		elog(DEBUG1, "Using sorted GiST build");
 		/*
 		 * Sort all data, build the index from bottom up.
 		 */
-- 
2.24.3 (Apple Git-128)