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
pidstatexact_startquery_startquery
101active2026-02-01 11:59:582026-02-01 11:59:58SELECT * FROM iv_orders ORDER BY created_at DESC LIMIT 20
102idle in transaction2026-02-01 11:05:002026-02-01 11:05:02UPDATE iv_orders SET total = total - 10 WHERE customer_id = 2
103active2026-02-01 11:20:002026-02-01 11:59:30DELETE FROM iv_events WHERE user_id IS NULL
104idleNULL2026-02-01 09:00:00COMMIT
105active2026-02-01 11:40:002026-02-01 11:40:00SELECT customer_id, SUM(total) FROM iv_orders GROUP BY customer_id
106idle in transaction (aborted)2026-02-01 11:57:002026-02-01 11:57:10INSERT INTO iv_events (event_type) VALUES (NULL)

Query

SELECT pid, state
FROM iv_sessions
WHERE state LIKE 'idle in transaction%'
ORDER BY pid