diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index 520d7b24c5597d3059066dd440fce02179af6c35..06123c8fac39482458ddb18b98a9e01f9e84fc76 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -29,10 +29,16 @@
  * of size BLCKSZ.	Rather than assuming we know the exact amount of overhead
  * imposed by page headers, tuple headers, etc, we leave 100 bytes for that
  * (the actual overhead should be no more than 56 bytes at this writing, so
- * there is slop in this number).  The upshot is that the maximum safe prefix
- * length is this:
+ * there is slop in this number).  So we can safely create prefixes up to
+ * BLCKSZ - 256 * 16 - 100 bytes long.  Unfortunately, because 256 * 16 is
+ * already 4K, there is no safe prefix length when BLCKSZ is less than 8K;
+ * it is always possible to get "SPGiST inner tuple size exceeds maximum"
+ * if there are too many distinct next-byte values at a given place in the
+ * tree.  Since use of nonstandard block sizes appears to be negligible in
+ * the field, we just live with that fact for now, choosing a max prefix
+ * size of 32 bytes when BLCKSZ is configured smaller than default.
  */
-#define SPGIST_MAX_PREFIX_LENGTH	(BLCKSZ - 256 * 16 - 100)
+#define SPGIST_MAX_PREFIX_LENGTH	Max((int) (BLCKSZ - 256 * 16 - 100), 32)
 
 /* Struct for sorting values in picksplit */
 typedef struct spgNodePtr