From 8c0c25fc894de49054fb6711af96cbcf61cc2b51 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@pgaddict.com>
Date: Wed, 30 Dec 2015 14:02:26 +0100
Subject: [PATCH 2/5] always start in un-batched mode

---
 src/backend/executor/nodeHash.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 7708581..e7d4b5f 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -290,7 +290,7 @@ ExecHashTableCreate(Hash *node, List *hashOperators, bool keepNulls)
 	hashtable->skewBucketLen = 0;
 	hashtable->nSkewBuckets = 0;
 	hashtable->skewBucketNums = NULL;
-	hashtable->nbatch = nbatch;
+	hashtable->nbatch = 1;
 	hashtable->curbatch = 0;
 	hashtable->nbatch_original = nbatch;
 	hashtable->nbatch_outstart = nbatch;
@@ -600,7 +600,15 @@ ExecHashIncreaseNumBatches(HashJoinTable hashtable)
 	if (oldnbatch > Min(INT_MAX / 2, MaxAllocSize / (sizeof(void *) * 2)))
 		return;
 
-	nbatch = oldnbatch * 2;
+	/*
+	 * If we're incrementing the number of batches for the first time,
+	 * let's see if we should start with nbatch_original.
+	 */
+	if ((oldnbatch == 1) && (hashtable->nbatch_original > 1))
+		nbatch = hashtable->nbatch_original;
+	else
+		nbatch = oldnbatch * 2;
+
 	Assert(nbatch > 1);
 
 #ifdef HJDEBUG
-- 
2.1.0

