diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index dbb10c7b00..0fc3a14765 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -844,6 +844,14 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
 
 	Assert(!MyReplicationSlot);
 
+	/*
+	 * If WAL sender is shutting down, prevent CREATE_REPLICATION_SLOT as it
+	 * could result in the generation of new WAL data.
+	 */
+	if (walsender_ready_to_stop)
+		ereport(ERROR,
+				(errmsg("CREATE_REPLICATION_SLOT cannot be called during WAL sender shutdown")));
+
 	parseCreateReplSlotOptions(cmd, &reserve_wal, &snapshot_action);
 
 	/* setup state for XLogReadPage */
@@ -1019,6 +1027,14 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd)
 static void
 DropReplicationSlot(DropReplicationSlotCmd *cmd)
 {
+	/*
+	 * If WAL sender is shutting down, prevent DROP_REPLICATION_SLOT as it
+	 * could result in the generation of new WAL data.
+	 */
+	if (walsender_ready_to_stop)
+		ereport(ERROR,
+				(errmsg("DROP_REPLICATION_SLOT cannot be called during WAL sender shutdown")));
+
 	ReplicationSlotDrop(cmd->slotname);
 	EndCommand("DROP_REPLICATION_SLOT", DestRemote);
 }
@@ -2840,6 +2856,8 @@ WalSndLastCycleHandler(SIGNAL_ARGS)
 {
 	int			save_errno = errno;
 
+	walsender_ready_to_stop = true;
+
 	/*
 	 * If replication has not yet started, die like with SIGTERM. If
 	 * replication is active, only set a flag and wake up the main loop. It
@@ -2849,7 +2867,6 @@ WalSndLastCycleHandler(SIGNAL_ARGS)
 	if (!replication_active)
 		kill(MyProcPid, SIGTERM);
 
-	walsender_ready_to_stop = true;
 	SetLatch(MyLatch);
 
 	errno = save_errno;
