Additional info:
PostgreSQL 9.6.8 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
database size 220GB
System CentOS 7
I am not sure how to recognize the problem.
Does the server log have more info?
Can you reproduce if you run that query manually?
David J.