From 37e5a60ffc6b63a5a273cc742ae6444ca6c6751a Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Thu, 25 May 2017 16:16:46 +0500
Subject: [PATCH] Allow uncompressed GiST

---
 src/backend/access/gist/gist.c     | 18 +++++++++++++++---
 src/backend/access/gist/gistutil.c | 10 ++++++----
 src/include/access/gist_private.h  |  2 ++
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index 6593771..b3deeaf 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -1453,12 +1453,24 @@ initGISTstate(Relation index)
 		fmgr_info_copy(&(giststate->unionFn[i]),
 					   index_getprocinfo(index, i + 1, GIST_UNION_PROC),
 					   scanCxt);
-		fmgr_info_copy(&(giststate->compressFn[i]),
+		if (OidIsValid(index_getprocid(index, i + 1, GIST_COMPRESS_PROC))
+				|| OidIsValid(index_getprocid(index, i + 1, GIST_DECOMPRESS_PROC)))
+		{
+			fmgr_info_copy(&(giststate->compressFn[i]),
 					   index_getprocinfo(index, i + 1, GIST_COMPRESS_PROC),
-					   scanCxt);
-		fmgr_info_copy(&(giststate->decompressFn[i]),
+					   	   scanCxt);
+			fmgr_info_copy(&(giststate->decompressFn[i]),
 					   index_getprocinfo(index, i + 1, GIST_DECOMPRESS_PROC),
 					   scanCxt);
+			giststate->compressed[i] = true;
+		}
+		else
+		{
+			giststate->compressFn[i].fn_oid = InvalidOid;
+			giststate->decompressFn[i].fn_oid = InvalidOid;
+			giststate->compressed[i] = false;
+		}
+
 		fmgr_info_copy(&(giststate->penaltyFn[i]),
 					   index_getprocinfo(index, i + 1, GIST_PENALTY_PROC),
 					   scanCxt);
diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c
index cbdaec9..c018e77 100644
--- a/src/backend/access/gist/gistutil.c
+++ b/src/backend/access/gist/gistutil.c
@@ -550,6 +550,8 @@ gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e,
 		GISTENTRY  *dep;
 
 		gistentryinit(*e, k, r, pg, o, l);
+		if(!giststate->compressed[nkey])
+			return;
 		dep = (GISTENTRY *)
 			DatumGetPointer(FunctionCall1Coll(&giststate->decompressFn[nkey],
 										   giststate->supportCollation[nkey],
@@ -585,10 +587,10 @@ gistFormTuple(GISTSTATE *giststate, Relation r,
 
 			gistentryinit(centry, attdata[i], r, NULL, (OffsetNumber) 0,
 						  isleaf);
-			cep = (GISTENTRY *)
-				DatumGetPointer(FunctionCall1Coll(&giststate->compressFn[i],
-											  giststate->supportCollation[i],
-												  PointerGetDatum(&centry)));
+			cep = giststate->compressed[i] ?
+								(GISTENTRY *) DatumGetPointer(FunctionCall1Coll(&giststate->compressFn[i],
+														  giststate->supportCollation[i], PointerGetDatum(&centry)))
+								: &centry;
 			compatt[i] = cep->key;
 		}
 	}
diff --git a/src/include/access/gist_private.h b/src/include/access/gist_private.h
index 1ad4ed6..66dd30a 100644
--- a/src/include/access/gist_private.h
+++ b/src/include/access/gist_private.h
@@ -94,6 +94,8 @@ typedef struct GISTSTATE
 
 	/* Collations to pass to the support functions */
 	Oid			supportCollation[INDEX_MAX_KEYS];
+
+	bool		compressed[INDEX_MAX_KEYS];
 } GISTSTATE;
 
 
-- 
2.7.4

