From cdbf625683698918c7890ea572ec8e130b32d6a8 Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Sat, 20 Mar 2021 18:08:09 +1100
Subject: [PATCH v1] Fix for stream_cleanup_files HASH_REMOVE.

Fixed a bug where the code was de-referencing the hash entry returned by HASH_REMOVE.

Doing this is a violation of the API (see the hash_search function comment).
---
 src/backend/replication/logical/worker.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 0ddfd10..a045a7a 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -2991,14 +2991,14 @@ stream_cleanup_files(Oid subid, TransactionId xid)
 {
 	char		path[MAXPGPATH];
 	StreamXidHash *ent;
+	bool		found = false;
 
-	/* Remove the xid entry from the stream xid hash */
+	/* By this time we must have created the transaction entry */
 	ent = (StreamXidHash *) hash_search(xidhash,
 										(void *) &xid,
-										HASH_REMOVE,
-										NULL);
-	/* By this time we must have created the transaction entry */
-	Assert(ent != NULL);
+										HASH_FIND,
+										&found);
+	Assert(found);
 
 	/* Delete the change file and release the stream fileset memory */
 	changes_filename(path, subid, xid);
@@ -3014,6 +3014,9 @@ stream_cleanup_files(Oid subid, TransactionId xid)
 		pfree(ent->subxact_fileset);
 		ent->subxact_fileset = NULL;
 	}
+
+	/* Remove the xid entry from the stream xid hash */
+	hash_search(xidhash, (void *) &xid, HASH_REMOVE, NULL);
 }
 
 /*
-- 
1.8.3.1

