From cfaf988364f75518389adc051b36b8b8dc5abc05 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Thu, 30 Aug 2018 14:35:40 +0900
Subject: [PATCH 2/2] Fix error message of gistSplit.

An error message of gistSplit looks wrong because it doesn't consider
fillfactor which the caller is considering. This patch corrects the
message by considering fillfactor.
---
 src/backend/access/gist/gist.c     | 7 ++++---
 src/backend/access/gist/gistutil.c | 5 ++---
 src/include/access/gist_private.h  | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index 33b9532bff..4132fe2990 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -1364,7 +1364,8 @@ gistSplit(GISTInsertState *istate,
 		ereport(ERROR,
 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 				 errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
-						IndexTupleSize(itup[0]), GiSTPageSize,
+						IndexTupleSize(itup[0]),
+						GiSTPageSize - istate->freespace,
 						RelationGetRelationName(istate->r))));
 
 	memset(v.spl_lisnull, true, sizeof(bool) * giststate->tupdesc->natts);
@@ -1382,7 +1383,7 @@ gistSplit(GISTInsertState *istate,
 		rvectup[i] = itup[v.splitVector.spl_right[i] - 1];
 
 	/* finalize splitting (may need another split) */
-	if (!gistfitpage(rvectup, v.splitVector.spl_nright))
+	if (!gistfitpage(rvectup, v.splitVector.spl_nright, istate->freespace))
 	{
 		res = gistSplit(istate, page, rvectup, v.splitVector.spl_nright);
 	}
@@ -1394,7 +1395,7 @@ gistSplit(GISTInsertState *istate,
 		res->itup = gistFormTuple(istate, v.spl_rattr, v.spl_risnull, false);
 	}
 
-	if (!gistfitpage(lvectup, v.splitVector.spl_nleft))
+	if (!gistfitpage(lvectup, v.splitVector.spl_nleft, istate->freespace))
 	{
 		SplitedPageLayout *resptr,
 				   *subres;
diff --git a/src/backend/access/gist/gistutil.c b/src/backend/access/gist/gistutil.c
index 20802f1adc..286068666c 100644
--- a/src/backend/access/gist/gistutil.c
+++ b/src/backend/access/gist/gistutil.c
@@ -74,7 +74,7 @@ gistnospace(Page page, IndexTuple *itvec, int len, OffsetNumber todelete, Size f
 }
 
 bool
-gistfitpage(IndexTuple *itvec, int len)
+gistfitpage(IndexTuple *itvec, int len, int freespace)
 {
 	int			i;
 	Size		size = 0;
@@ -82,8 +82,7 @@ gistfitpage(IndexTuple *itvec, int len)
 	for (i = 0; i < len; i++)
 		size += IndexTupleSize(itvec[i]) + sizeof(ItemIdData);
 
-	/* TODO: Consider fillfactor */
-	return (size <= GiSTPageSize);
+	return (size <= GiSTPageSize - freespace);
 }
 
 /*
diff --git a/src/include/access/gist_private.h b/src/include/access/gist_private.h
index 6818e9d70e..cf15173e6f 100644
--- a/src/include/access/gist_private.h
+++ b/src/include/access/gist_private.h
@@ -437,7 +437,7 @@ extern bytea *gistoptions(Datum reloptions, bool validate);
 extern bool gistproperty(Oid index_oid, int attno,
 			 IndexAMProperty prop, const char *propname,
 			 bool *res, bool *isnull);
-extern bool gistfitpage(IndexTuple *itvec, int len);
+extern bool gistfitpage(IndexTuple *itvec, int len, int freespace);
 extern bool gistnospace(Page page, IndexTuple *itvec, int len, OffsetNumber todelete, Size freespace);
 extern void gistcheckpage(Relation rel, Buffer buf);
 extern Buffer gistNewBuffer(Relation r);
-- 
2.16.3

