[snip] I think I know where issue is.The PostgreSQL planner unable pass join conditions into subquery with aggregate functions (it's well known limitation).[snip]
pgsql-performance by date:
Соглашаюсь с условиями обработки персональных данных