title: postgres "idle in transaction" for 13 hours
body: <p>We recently saw a few queries "idle in transaction" for quite some time</p>
  
  <pre><code> pid  | usename |        state        | duration | application_name | wait_event | wait_event_type
  ------+---------+---------------------+----------+------------------+------------+----------------
  31620 | results | idle in transaction | 12:52:23 | bin/rails        |            |                
  </code></pre>
  
  <p>That is almost 13 hours idle in transaction.</p>
  
  <p>Any idea what causes them to get stuck in idle, or how to dig deeper? We did notice some OOM errors for background jobs.</p>
  
  <p>There are also a lot of "idle" queries, but thanks for the comments, those seem to be fine:
  <a href="https://i.stack.imgur.com/gioiJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gioiJ.png" alt="idle transactions in postgres"></a></p>
  
  <p>In <a href="https://stackoverflow.com/questions/44908010/postgresql-idle-in-transaction-with-all-locks-granted/44908655">postgresql &quot;idle in transaction&quot; with all locks granted</a> @LaurenzAlbe was pointing out the idle session timeout configuration option as a band-aid, but I'd rather understand this issue than hide it.</p>
  
  <p>thanks!</p>
  
  <p>PS: our application is ruby on rails and we use a mix of active record and custom SQL</p>
  
  <p>EDIT: original title was "idle in transaction", the queries are actually just idle most of the time and not in transaction, sorry about that</p>
  
  <p>EDIT #2: found the 13 hour idle in transaction process</p>