From 231b57b75bf900235a1192f900ece1d86fdc4552 Mon Sep 17 00:00:00 2001
From: Arseniy Mukhin <arseniy.mukhin.dev@gmail.com>
Date: Wed, 13 Aug 2025 18:26:40 +0300
Subject: [PATCH v1] Adds check of index tuple size during gist insert.

---
 src/backend/access/gist/gist.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index 7b24380c978..3c8661249e8 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -260,6 +260,16 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
 
 	*splitinfo = NIL;
 
+	/* Check that index tuples are not too large */
+	for (int i = 0; i < ntup; i++)
+	{
+		if (IndexTupleSize(itup[i]) > GISTMaxIndexTupleSize)
+		{
+			elog(ERROR, "itup exceed maximum size: %lu, max: %lu",
+				 IndexTupleSize(itup[i]), GISTMaxIndexTupleSize);
+		}
+	}
+
 	/*
 	 * if isupdate, remove old key: This node's key has been modified, either
 	 * because a child split occurred or because we needed to adjust our key
@@ -320,6 +330,16 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
 		npage = 0;
 		for (ptr = dist; ptr; ptr = ptr->next)
 			npage++;
+
+		/* Check that new downlinks are not too large */
+		for (ptr = dist; ptr; ptr = ptr->next)
+		{
+			if (IndexTupleSize(ptr->itup) > GISTMaxIndexTupleSize)
+			{
+				elog(ERROR, "itup exceed maximum size: %lu, max: %lu",
+					 IndexTupleSize(ptr->itup), GISTMaxIndexTupleSize);
+			}
+		}
 		/* in a root split, we'll add one more page to the list below */
 		if (is_rootsplit)
 			npage++;
-- 
2.43.0

