There is wrong plan due wrong estimation for this query you should to penalize nested loopset enable_nestloop to off;before evaluation of this query
pgsql-performance by date:
Соглашаюсь с условиями обработки персональных данных