From 24e096dc232f9f0aa1f1accd6a53ddafedbb19b6 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@enterprisedb.com>
Date: Tue, 26 Dec 2017 22:44:00 +1300
Subject: [PATCH] Fix rare assertion failure in parallel hash join.

When a backend runs out of inner tuples to hash, it should detach from
grow_batch_barrier only after it has flushed all batches to disk and merged
counters, not before.  Otherwise a concurrent backend in
ExecParallelHashIncreaseNumBatches() could stop waiting for this backend and
try to read tuples before they have been written.  This commit reorders those
operations and should fix the assertion failures seen occasionally on the
build farm since commit 1804284042e659e7d16904e7bbb0ad546394b6a3.

Author: Thomas Munro
Discussion: https://postgr.es/m/E1eRwXy-0004IK-TO%40gemulon.postgresql.org
---
 src/backend/executor/nodeHash.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/backend/executor/nodeHash.c b/src/backend/executor/nodeHash.c
index 0a519fae313..04eb3650aa3 100644
--- a/src/backend/executor/nodeHash.c
+++ b/src/backend/executor/nodeHash.c
@@ -288,8 +288,6 @@ MultiExecParallelHash(HashState *node)
 					ExecParallelHashTableInsert(hashtable, slot, hashvalue);
 				hashtable->partialTuples++;
 			}
-			BarrierDetach(&pstate->grow_buckets_barrier);
-			BarrierDetach(&pstate->grow_batches_barrier);
 
 			/*
 			 * Make sure that any tuples we wrote to disk are visible to
@@ -304,6 +302,9 @@ MultiExecParallelHash(HashState *node)
 			 */
 			ExecParallelHashMergeCounters(hashtable);
 
+			BarrierDetach(&pstate->grow_buckets_barrier);
+			BarrierDetach(&pstate->grow_batches_barrier);
+
 			/*
 			 * Wait for everyone to finish building and flushing files and
 			 * counters.
-- 
2.15.0

