From b2fbc7f197be725c6e75b248bf66905ebe252961 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 4 Feb 2020 22:16:43 +1300
Subject: [PATCH 2/3] Fix parallel hash join rescan on DSM exhaustion.

If there is no DSM segment, there is nothing to reinitialize in
ExecHashJoinReInitializeDSM().  Return early, rather than allowing
shm_toc() to report an error.

Back-patch to 11.
---
 src/backend/executor/nodeHashjoin.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/backend/executor/nodeHashjoin.c b/src/backend/executor/nodeHashjoin.c
index c901a80923..5c9f385ba6 100644
--- a/src/backend/executor/nodeHashjoin.c
+++ b/src/backend/executor/nodeHashjoin.c
@@ -1496,8 +1496,13 @@ void
 ExecHashJoinReInitializeDSM(HashJoinState *state, ParallelContext *cxt)
 {
 	int			plan_node_id = state->js.ps.plan->plan_node_id;
-	ParallelHashJoinState *pstate =
-	shm_toc_lookup(cxt->toc, plan_node_id, false);
+	ParallelHashJoinState *pstate;
+
+	/* If there's no segment, there's nothing to reinitialize. */
+	if (cxt->seg == NULL)
+		return;
+
+	pstate = shm_toc_lookup(cxt->toc, plan_node_id, false);
 
 	/*
 	 * It would be possible to reuse the shared hash table in single-batch
-- 
2.23.0

