Measuring how long a query is IDLE IN TRANSACTION (postgresql + Django app)
A session that is "idle in transaction" can't have waiting = true because otherwise it wouldn't be "idle".

The difference between current_timestamp and  the timestamp in state_change should give you the time that the session was in "idle in transaction".

In general, a session that is just "idle" is not a problem. A session in "idle in transaction" might very well be a problem, as it prevents vacuum of cleaning up the rows that the transaction was using. 

A short (a few seconds) "idle in transaction" is not a problem, only very long running  (minutes, hours) transactions that are not ended properly (and thus stick in "idle in transaction") are a problem. 



With 9.6 you can force the server to disconnect long running "idle in transaction" sessions using the property idle_in_transaction_session_timeout