Performance improvement by using postgres_ext gem and a recursive query to avoid the slow 'DISTINCT' feature.