While running PostgreSQL 13.12 (occurs in several versions of PG11/PG13) using SQLAlchemy 1.3, we are occasionally hitting issues where increased concurrency leaves certain transactions (and their nested transactions) in the "Idle in Transaction" state with the RELEASE SAVEPOINT ...
query.
Looking at the currently running queries, it is not clear why transactions have stopped moving forward. I have also observed this behavior without any hanging locks:
pid | datname | duration | query | state | application_name | wait_event_type |
---|---|---|---|---|---|---|
27662 | app | 22:22:37.429569 | select pg_advisory_xact_lock(resource_id) from resource where uuid = '018afcac-a5ab-7a5c-9eb4-2d8b0be4b556' |
active | http.api.1 | Lock |
25830 | app | 22:22:29.236398 | RELEASE SAVEPOINT sa_savepoint_5 |
idle in transaction | http.api.0 | Client |
21490 | app | 22:22:29.015862 | select pg_advisory_xact_lock(resource_id) |
active | http.api.0 | Lock |
27674 | app | 22:22:27.780581 | RELEASE SAVEPOINT sa_savepoint_3 |
idle in transaction | http.api.2 | Client |
29120 | app | 22:22:26.053851 | select pg_advisory_xact_lock(resource_id) |
active | http.api.2 | Lock |
Any way to debug this? This API call generally works, with the session being cleaned up and does not fail consistently. We are using SQL Alchemy's connection pooling to manage the connections - closed sessions will return the connection to the pool and issue a rollback to clean up the connection (when commit should have committed all other statements).