Seems the problem was with the custom aggregate function not being able to handle thousands of rows.