From e1418b6655386eb34574c4f930d8dc66f16e69a1 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Wed, 5 Sep 2018 12:15:13 +0900
Subject: [PATCH 2/2] More friendly error message for rootsplit failure of GiST

When tuple insertion failed during page split of GiST index, we have
an internal error that complains that just "failed to add item to
index page". However, things are a bit different in the rootsplit
case. It is rather a user side error that the user provided too long
data for the index so emit a more friendly error message in the case
like gistSplit does.
---
 src/backend/access/gist/gist.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index 5915ab2cf2..4590e88a77 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -412,7 +412,34 @@ gistplacetopage(Relation rel, GISTSTATE *giststate,
 				IndexTuple	thistup = (IndexTuple) data;
 
 				if (PageAddItem(ptr->page, (Item) data, IndexTupleSize(thistup), i + FirstOffsetNumber, false, false) == InvalidOffsetNumber)
+				{
+					/*
+					 * Emit user error message for root split failure since
+					 * this is rather a user error than internal one. We could
+					 * this earlier before preparing new buffers, but we don't
+					 * bother prechecking for such a corner case.
+					 */
+					if (is_rootsplit && ptr == dist)
+					{
+						size_t	total_tupsize;
+
+						/* count size of all tuples to be inserted */
+						for (; i < ptr->block.num ; i++)
+							data += IndexTupleSize((IndexTuple) data);
+
+						/*  total_tupsize is including item id */
+						total_tupsize =
+							(char *)data - (char *) (ptr->list)
+							+ sizeof(ItemIdData) * ptr->block.num;
+						ereport(ERROR,
+								(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+								 (errmsg("size of %d index rows (%zu bytes) exceeds maximum %zu of the root page for the index \"%s\"",
+										 ptr->block.num, total_tupsize,
+										 GiSTPageSize,
+										 RelationGetRelationName(rel)))));
+					}
 					elog(ERROR, "failed to add item to index page in \"%s\"", RelationGetRelationName(rel));
+				}
 
 				/*
 				 * If this is the first inserted/updated tuple, let the caller
-- 
2.16.3

