In terms of preventing this happening again, you might consider setting some reasonable temp_file_limit as default. Alternatively or additionally, you can set up another volume and direct temp files to use that to avoid the server crashing if excessive temp files are used in a moment's time. Then only that one query fails that used too much space.
It always bears mentioning that 9.4 is very old and upgrading is recommended to at least v11 that has been out for more than a year.
"PostgreSQL 9.4 will stop receiving fixes on February 13, 2020, which is the next planned cumulative update release."