diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c
index 9937ef9253..c7f2873e13 100644
--- a/contrib/pg_trgm/trgm_gist.c
+++ b/contrib/pg_trgm/trgm_gist.c
@@ -847,15 +847,22 @@ gtrgm_picksplit(PG_FUNCTION_ARGS)
 	v->spl_nleft = 0;
 	v->spl_nright = 0;
 
+	/*
+	 * We ignored the last entry in the list above, and did not populate its
+	 * cache entry yet, but if we were called with exactly 2 items then seed_2
+	 * now points to it, so we must fill in the cache entry before trying to
+	 * build datums from the seeds.
+	 */
+	maxoff = OffsetNumberNext(maxoff);
+	fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff),
+			  &cache_sign[siglen * maxoff], siglen);
+
 	/* form initial .. */
 	datum_l = gtrgm_alloc(cache[seed_1].allistrue, siglen, cache[seed_1].sign);
 	datum_r = gtrgm_alloc(cache[seed_2].allistrue, siglen, cache[seed_2].sign);
 
 	union_l = GETSIGN(datum_l);
 	union_r = GETSIGN(datum_r);
-	maxoff = OffsetNumberNext(maxoff);
-	fillcache(&cache[maxoff], GETENTRY(entryvec, maxoff),
-			  &cache_sign[siglen * maxoff], siglen);
 
 	/* sort before ... */
 	costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
