SELECT * FROM pg_stat_activity WHERE state = 'active';
So you can identify the PID of the hanging query you want to terminate, run this:
SELECT pg_cancel_backend(PID);
A. Imani, A. Vakili, A. Montazer, and A. Shakery. Lecture Notes in Computer Science (including subseries Lecture Notes in Artificial Intelligence and Lecture Notes in Bioinformatics), (April 2019)
S. Panda, S. Mishra, and S. Mishra. International Journal of Computer Science, Engineering and Information Technology (IJCSEIT), 6 (2):
01-09(April 2016)