When you run a query like that it is run as a single transaction. So, you need enough heap memory to be able to complete that operation.
What is your system memory available?
If the system is crashing when you have a complex query it could be due to same reason, not enough heap memory available.
There are 3 options here.
modify the query to run in smaller batches.
Increase the heap memory.
For stability purposes we can add a query guard for memory.
By default the heap allocated to a query is unlimited.
Please take a look at these configurations
By setting dbms.memory.transaction.max_size you are limiting how much memory a single transaction can use. if it exceeds that size the query will be killed.