From e89b00fb9bbc26608086e92b382e5451e20adf50 Mon Sep 17 00:00:00 2001
From: Amit Kapila <akapila@postgresql.org>
Date: Tue, 16 Mar 2021 12:07:46 +0530
Subject: [PATCH v1] Prevent cancel/die interrupts while creating tablesync
 slot.

---
 src/backend/replication/logical/tablesync.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index feb634e7ac0..53788685398 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -1051,9 +1051,16 @@ LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
 	 * Create a new permanent logical decoding slot. This slot will be used
 	 * for the catchup phase after COPY is done, so tell it to use the
 	 * snapshot to make the final data consistent.
+	 *
+	 * Prevent cancel/die interrupts while creating slot here because it is
+	 * possible that before the server finishes this command a concurrent drop
+	 * subscrition happens which would complete without removing this slot
+	 * leading to a dangling slot on the server.
 	 */
+	HOLD_INTERRUPTS();
 	walrcv_create_slot(wrconn, slotname, false /* permanent */ ,
 					   CRS_USE_SNAPSHOT, origin_startpos);
+	RESUME_INTERRUPTS();
 
 	/*
 	 * Setup replication origin tracking. The purpose of doing this before the
-- 
2.28.0.windows.1

