diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index 748e568..7073146 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -1902,8 +1902,28 @@ spgdoinsert(Relation index, SpGistState *state,
 	 * cycles in the loop below.
 	 */
 	if (!isnull)
+	{
 		procinfo = index_getprocinfo(index, 1, SPGIST_CHOOSE_PROC);
 
+		/* Compress the data if the corresponding support function exists. */
+		if (OidIsValid(index_getprocid(index, 1, SPGIST_COMPRESS_PROC)))
+		{
+			spgCompressIn in;
+			spgCompressIn out;
+			FmgrInfo   *compress = index_getprocinfo(index, 1,
+													 SPGIST_COMPRESS_PROC);
+
+			in.datum = datum;
+
+			FunctionCall2Coll(compress,
+							  index->rd_indcollation[0],
+							  PointerGetDatum(&in),
+							  PointerGetDatum(&out));
+
+			datum = out.datum;
+		}
+	}
+
 	/*
 	 * Since we don't use index_form_tuple in this AM, we have to make sure
 	 * value to be inserted is not toasted; FormIndexDatum doesn't guarantee
diff --git a/src/backend/access/spgist/spgvalidate.c b/src/backend/access/spgist/spgvalidate.c
index 0a3eeb6..02cc908 100644
--- a/src/backend/access/spgist/spgvalidate.c
+++ b/src/backend/access/spgist/spgvalidate.c
@@ -26,6 +26,7 @@
 #include "utils/regproc.h"
 #include "utils/syscache.h"
 
+#define SPGIST_OPTIONAL_PROCS_MASK (((uint64) 1) << SPGIST_COMPRESS_PROC)
 
 /*
  * Validator for an SP-GiST opclass.
@@ -104,6 +105,7 @@ spgvalidate(Oid opclassoid)
 			case SPGIST_CHOOSE_PROC:
 			case SPGIST_PICKSPLIT_PROC:
 			case SPGIST_INNER_CONSISTENT_PROC:
+			case SPGIST_COMPRESS_PROC:
 				ok = check_amproc_signature(procform->amproc, VOIDOID, true,
 											2, 2, INTERNALOID, INTERNALOID);
 				break;
@@ -224,6 +226,8 @@ spgvalidate(Oid opclassoid)
 		{
 			if ((thisgroup->functionset & (((uint64) 1) << i)) != 0)
 				continue;		/* got it */
+			if ((SPGIST_OPTIONAL_PROCS_MASK & (((uint64) 1) << i)) != 0)
+				continue;		/* support function is optional */
 			ereport(INFO,
 					(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
 					 errmsg("spgist operator family \"%s\" is missing support function %d for type %s",
diff --git a/src/include/access/spgist.h b/src/include/access/spgist.h
index 23ed9bb..2927605 100644
--- a/src/include/access/spgist.h
+++ b/src/include/access/spgist.h
@@ -30,7 +30,8 @@
 #define SPGIST_PICKSPLIT_PROC			3
 #define SPGIST_INNER_CONSISTENT_PROC	4
 #define SPGIST_LEAF_CONSISTENT_PROC		5
-#define SPGISTNProc						5
+#define SPGIST_COMPRESS_PROC			6
+#define SPGISTNProc						6
 
 /*
  * Argument structs for spg_config method
@@ -189,6 +190,19 @@ typedef struct spgLeafConsistentOut
 	double	   *distances;		/* associated distances */
 } spgLeafConsistentOut;
 
+/*
+ * Argument structs for spg_compress method
+ */
+typedef struct spgCompressIn
+{
+	Datum		datum;			/* data to be compressed for storage,
+								 * can be toasted */
+} spgCompressIn;
+
+typedef struct spgCompressOut
+{
+	Datum		datum;			/* compressed data to be stored at leaf */
+} spgCompressOut;
 
 /* spgutils.c */
 extern bytea *spgoptions(Datum reloptions, bool validate);
