How to optimize PostgreSQL query with multiple joins and subqueries and slow grouping/sorting?

Published --