From 7c2c580e16d3d32db4a622e99100cb8ff4ba6b7c Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Tue, 30 Apr 2019 10:55:52 -0700
Subject: [PATCH] Tentative fix for nbtsort.c space bug.

---
 src/backend/access/nbtree/nbtsort.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c
index 9ac4c1e1c0..f00e9a6e30 100644
--- a/src/backend/access/nbtree/nbtsort.c
+++ b/src/backend/access/nbtree/nbtsort.c
@@ -879,7 +879,8 @@ _bt_buildadd(BTWriteState *wstate, BTPageState *state, IndexTuple itup)
 	 * fillfactor.  However, we must put at least two items on each page, so
 	 * disregard fillfactor if we don't have that many.
 	 */
-	if (pgspc < itupsz || (pgspc < state->btps_full && last_off > P_FIRSTKEY))
+	if (pgspc < itupsz + MAXALIGN(sizeof(ItemPointerData)) ||
+		(pgspc < state->btps_full && last_off > P_FIRSTKEY))
 	{
 		/*
 		 * Finish off the page and write it out.
-- 
2.17.1

