diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 589b2f1..d71b8b9 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -39,6 +39,7 @@
 
 
 static void ExecHashIncreaseNumBatches(HashJoinTable hashtable);
+static void ExecHashIncreaseNumBuckets(HashJoinTable hashtable);
 static void ExecHashBuildSkewHash(HashJoinTable hashtable, Hash *node,
 					  int mcvsToUse);
 static void ExecHashSkewTableInsert(HashJoinTable hashtable,
@@ -682,6 +683,77 @@ ExecHashIncreaseNumBatches(HashJoinTable hashtable)
 }
 
 /*
+ * ExecHashIncreaseNumBuckets
+ *		increase the original number of buckets in order to reduce
+ *		number of tuples per bucket
+ */
+static void
+ExecHashIncreaseNumBuckets(HashJoinTable hashtable)
+{
+	int			oldnbuckets = hashtable->nbuckets;
+	int			i;
+    HashJoinTuple * oldbuckets = hashtable->buckets;
+
+    /* TODO Maybe it'd be better to resize the buckets in place (should be possible,
+     * but when I tried it I always ended up with a strange infinite loop). */
+
+	MemoryContext oldcxt;
+
+	/* FIXME do nothing if we've decided to shut off bucket count growth */
+
+    /* FIXME safety checks here */
+
+    /* update the hashtable info, so that we can compute buckets etc. */
+    hashtable->log2_nbuckets += 1; /* we've multiplied by 2 */
+	hashtable->nbuckets *= 2;
+
+	Assert(hashtable->nbuckets > 1);
+    Assert(hashtable->nbuckets == (1 << hashtable->log2_nbuckets));
+
+    /* grow the bucket list */
+    oldcxt = MemoryContextSwitchTo(hashtable->batchCxt);
+    
+	hashtable->buckets = (HashJoinTuple *) palloc0(hashtable->nbuckets * sizeof(HashJoinTuple));
+    
+    MemoryContextSwitchTo(oldcxt);
+
+    /* walk through the old buckets, check if tuples are in the right bucket */
+	for (i = 0; i < oldnbuckets; i++)
+	{
+		HashJoinTuple tuple;
+
+		tuple = oldbuckets[i];
+
+		while (tuple != NULL)
+		{
+			/* save link in case we delete */
+			HashJoinTuple nexttuple = tuple->next;
+			int			bucketno;
+			int			batchno;
+
+			ExecHashGetBucketAndBatch(hashtable, tuple->hashvalue,
+									  &bucketno, &batchno);
+
+            /* move it to the correct bucket */
+            tuple->next = hashtable->buckets[bucketno];
+            hashtable->buckets[bucketno] = tuple;
+
+            /* process the next tuple */
+			tuple = nexttuple;
+
+		}
+
+	}
+    
+    pfree(oldbuckets);
+
+    /* TODO disable growth if nbuckets * NTUP_PER_BUCKET reaches work_mem (or something
+     * like that) */
+
+}
+
+
+/*
  * ExecHashTableInsert
  *		insert a tuple into the hash table depending on the hash value
  *		it may just go to a temp file for later batches
@@ -740,6 +812,17 @@ ExecHashTableInsert(HashJoinTable hashtable,
 			hashtable->spacePeak = hashtable->spaceUsed;
 		if (hashtable->spaceUsed > hashtable->spaceAllowed)
 			ExecHashIncreaseNumBatches(hashtable);
+            
+        /* check average number of tuples per bucket, increase (2*nbuckets) if needed */
+        if (hashtable->spaceUsed / hashTupleSize / hashtable->nbuckets > 1.5 * NTUP_PER_BUCKET) {
+
+#ifdef HJDEBUG
+	printf("Increasing nbucket to %d because average per bucket = %d\n",
+		   nbuckets,  hashtable->spaceUsed / hashTupleSize / hashtable->nbuckets);
+#endif
+            ExecHashIncreaseNumBuckets(hashtable);
+        }
+
 	}
 	else
 	{
