Optimize a postgresql database view query
It seems you need in something similar to:

WITH cte AS ( SELECT *, 
                     RANK() OVER (PARTITION BY clients.id 
                                  ORDER BY stages.position DESC) rnk
              FROM clients
              INNER JOIN deals ON deals.client_id = clients.id
              INNER JOIN deal_stages ON deal_stages.deal_id = deals.id
              INNER JOIN stages ON stages.id = deal_stages.stage_id
              WHERE deal_stages.current = true AND deals.is_active = true )
SELECT *
FROM cte 
WHERE rnk=1


PS. Posted as an answer because it's too long for a comment.

PPS. I'm not sure because WHERE exists in CTE and not exists in main query.