Optimization in postgreSQL that detects and removes redundant DISTINCT clause and improves query performance