Seems the problem was with the custom aggregate function not being able to handle thousands of rows.
pgsql-performance by date:
Соглашаюсь с условиями обработки персональных данных