Which sessions are idle with an open transaction?
The iv_sessions table contains a snapshot of pg_stat_activity. Find the sessions that aren't currently running a query but still have an open transaction. Include transactions that have already encountered an error. Which rows will the query return?
Predict the resultTransactions and locksJuniorBackendData EngineerQA
Input data · iv_sessions
| pid | state | xact_start | query_start | query |
|---|---|---|---|---|
| 101 | active | 2026-02-01 11:59:58 | 2026-02-01 11:59:58 | SELECT * FROM iv_orders ORDER BY created_at DESC LIMIT 20 |
| 102 | idle in transaction | 2026-02-01 11:05:00 | 2026-02-01 11:05:02 | UPDATE iv_orders SET total = total - 10 WHERE customer_id = 2 |
| 103 | active | 2026-02-01 11:20:00 | 2026-02-01 11:59:30 | DELETE FROM iv_events WHERE user_id IS NULL |
| 104 | idle | NULL | 2026-02-01 09:00:00 | COMMIT |
| 105 | active | 2026-02-01 11:40:00 | 2026-02-01 11:40:00 | SELECT customer_id, SUM(total) FROM iv_orders GROUP BY customer_id |
| 106 | idle in transaction (aborted) | 2026-02-01 11:57:00 | 2026-02-01 11:57:10 | INSERT INTO iv_events (event_type) VALUES (NULL) |
Query
SELECT pid, state
FROM iv_sessions
WHERE state LIKE 'idle in transaction%'
ORDER BY pid