Are you then trying to process the whole data set at once? I'm pretty certain the issue is your app, not pgsql, running out of memory.
pgsql-performance by date:
Соглашаюсь с условиями обработки персональных данных