I'm debugging lots of "idle in transactions" queries in my database. I was able to trace some of them back to the source code and fix the runaway sessions, but for others I can't easily tell where the query originates in the source code to fix it.
I think would be possible if I started logging stacks for all of the queries like here, but probably there's an easier way.
My query to find the long transactions look like this:
select query_start, application_name, state, query,*
from pg_stat_activity
and state != 'idle'
order by query_start