Anybody know a way to find out what was the last query executed on a specific backend? I have some connection leaks and I'm trying to find the cause. However, all I could find is a way to find the "currently running query".

So I was wondering if there is a way, given a PID, to find out what was the last query executed there.

I could make all queries go to the log and then grep it for the PID but I'm trying to find another approach as there are a lot of queries running and that has very bad impact.