Find the bugs: long-running transactions
iv_sessions contains a snapshot of pg_stat_activity taken on February 1, 2026, at 12:00:00. Find sessions with transactions that have been open for more than 10 minutes. These transactions may hold locks and prevent VACUUM from cleaning up old row versions. List the oldest transactions first. Which lines of the query contain errors?
Find the errorsTransactions and locksMiddleBackendData Engineer
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) |
SELECT pid, state, xact_start, query
FROM iv_sessions
WHERE state = 'active'
AND query_start < '2026-02-01 11:50:00'
ORDER BY xact_start