"What could help is running 'analyze' locally against the foreign table.
That can cause other problems, though."
Yes, this really helped - ANALYZE on foreign table took ~45 minutes but
after that I was able to get results and memory was OK too. Only several
hundreds of MB used...