Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A reporting session started BEGIN ISOLATION LEVEL REPEATABLE READ at 09:00 and has been idle in transaction for six hours. Explain what it does to the rest of the database and how you would find and prevent it.

The session's snapshot still says 09:00, so the database-wide xmin horizon is stuck at 09:00 and VACUUM cannot remove any row version deleted or updated since then, in any table. Tables and indexes bloat, scans slow down, index-only scans stop helping and transaction ID age keeps growing. We find it in pg_stat_activity by state, xact_start and backend_xmin (and check replication slots and prepared transactions too), end it, and prevent it with idle_in_transaction_session_timeout, other timeouts, and application code that never holds a transaction open around slow work.

Why this question is asked

A session that does nothing should cost nothing. In PostgreSQL that is true for an idle connection, but not for a connection that is idle inside an open transaction, especially one holding a REPEATABLE READ snapshot. Interviewers ask this question because the damage is silent: no errors, no locks reported, just a database that gets slower all afternoon. In this answer, we will recap the xmin horizon, see exactly what this one session does to every table, find it, end it safely, and prevent it from happening again, including the Spring pattern that most often causes it.

1A Quick Recap: Snapshots and the xmin Horizon

Under MVCC, an UPDATE or DELETE leaves the old row version in place, because some transaction might still need to see it. VACUUM removes old versions only once no snapshot can see them. To decide that, PostgreSQL computes the xmin horizon: the oldest transaction id that any open snapshot in the database still treats as running. Versions deleted by transactions newer than that horizon must be kept.

A REPEATABLE READ transaction takes one snapshot at its first statement and keeps it until it ends. So our reporting session, which ran its first query at 09:00, holds a 09:00 snapshot. Being idle does not release it; only COMMIT or ROLLBACK does.

2What One Idle Session Does to the Whole Database

Because the horizon is database-wide, it does not matter which tables the report read. From 09:00 onwards, vacuum can still run, but for every table in that database it may only remove versions that died before 09:00. Every row updated or deleted since then stays, in orders, in sessions, in the job queue.

After six hours, a busy table may carry millions of extra dead versions. Its indexes also hold entries for them. Sequential scans read more pages, index scans visit more dead tuples, and index-only scans lose their advantage, because the visibility map cannot mark pages all-visible while recent dead tuples remain on them. Vacuum also cannot freeze rows newer than the horizon, so transaction ID age grows towards wraparound pressure.

Step throughThe idle session holds back cleanup for everyone
Report sessionDatabaseOrder serviceAutovacuum
09:00 BEGIN REPEATABLE READ; first SELECT
idle in transaction for 6 hours
update and delete millions of rows
vacuum orders
only versions dead before 09:00 removable
dead tuples stay; tables and indexes bloat

6 messages in order. Play them, or step through them one at a time.

The report reads none of these tables, yet it decides what vacuum may remove in all of them.

3Finding the Session

pg_stat_activity has a row per backend. We look for sessions whose state is idle in transaction and sort by how long their transaction has been open. backend_xmin shows the snapshot horizon each session holds. Sessions are not the only possible culprits, so we check two more places: replication slots, whose xmin or catalog_xmin also hold the horizon, and prepared transactions from two-phase commit, which survive even if their client disconnects.

SQL
SELECT pid, usename, application_name, state,
now() - xact_start AS xact_age, backend_xmin, left(query, 50) AS last_query
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL
ORDER BY xact_start;
-- 21490 | reporting | Metabase | idle in transaction | 06:02:11 | 912004 | SELECT region, sum(...
SELECT slot_name, active, xmin, catalog_xmin FROM pg_replication_slots;
SELECT gid, prepared, owner FROM pg_prepared_xacts;

Notice the last_query column: it shows the last statement the session ran, not what it is doing now. Here it points at the reporting tool, which gives us the owner to talk to.

4Ending It Safely

Ending a read-only reporting session is safe: it has written nothing (its backend_xid column is empty), so terminating it loses no work. If backend_xid were set, we would ask the owner first, because its writes would be rolled back. pg_cancel_backend() only cancels a running statement, which does nothing to an idle session, so we use pg_terminate_backend(), which closes the connection and rolls back its transaction.

SQL
SELECT pg_terminate_backend(21490); -- true: the snapshot is released
-- the next autovacuum runs can now remove six hours of dead tuples

The horizon moves forward immediately, and autovacuum catches up over the following minutes or hours. The bloat it removes becomes reusable space, but table files do not shrink; badly affected tables may need pg_repack later.

5Preventing It

The server-side guardrail is idle_in_transaction_session_timeout, which terminates any session that sits idle inside a transaction for longer than the limit. statement_timeout stops individual statements, and since PostgreSQL 17 transaction_timeout bounds the whole transaction. We set them per role, for example 60 seconds idle for services and a few minutes for reporting tools.

SQL
ALTER ROLE reporting SET idle_in_transaction_session_timeout = '5min';
ALTER ROLE svc_orders SET idle_in_transaction_session_timeout = '60s';
ALTER ROLE svc_orders SET transaction_timeout = '2min'; -- PostgreSQL 17 and later

The real fix is in the application. Transactions should wrap database work only. Opening one, then calling a slow HTTP service, waiting for a user, or processing a large file before committing, leaves the session idle in transaction for the whole wait.

6On a Standby, the Same Thing Moves to the Primary

Running the report on a hot standby does not escape the problem if hot_standby_feedback is on. With feedback, the standby tells the primary the oldest xmin its queries need, and the primary's vacuum respects it. A six-hour idle transaction on the standby then holds back vacuum on the primary exactly as if it ran there. Without feedback, the standby's query would instead risk being cancelled by recovery conflicts.

7Follow-up questions

Which Spring pattern most often causes idle in transaction sessions?

A @Transactional method that does slow non-database work inside it: calling a remote API, sending email, or processing a file between two repository calls. The transaction and its connection stay open, idle, for the whole call. A related pattern is Open Session in View, which Spring Boot enables by default (spring.jpa.open-in-view=true): it keeps the persistence context and usually its connection for the whole web request. After the transaction commits that connection normally shows as idle rather than idle in transaction, but it ties up connections and hides lazy loading. The fix is to keep transactions short, move remote calls outside them, and turn open-in-view off.

Does READ COMMITTED avoid the problem?

Only for read-only transactions. In READ COMMITTED, each statement takes a new snapshot, so between statements an idle read-only transaction holds no snapshot at all. But if the transaction has written anything, it has its own transaction id, which stays running until it ends, and that running id holds the horizon just as firmly. So READ COMMITTED reduces the damage in some cases, but any idle transaction that has written is still a problem, and the timeouts are needed either way.

Does the idle session hold locks too?

It holds whatever locks its statements took until it ends, at least ACCESS SHARE on every table it read. That blocks DDL such as ALTER TABLE, which then blocks everyone queued behind it.

8Conclusion

In this answer, we saw that a REPEATABLE READ session idle in transaction since 09:00 keeps the database's xmin horizon at 09:00, so vacuum cannot remove any version deleted or updated since then in any table. That causes bloat, slower scans, weaker index-only scans and growing transaction ID age. We found it with pg_stat_activity, also checking replication slots and pg_prepared_xacts, ended it with pg_terminate_backend, and prevented recurrence with idle_in_transaction_session_timeout, statement and transaction timeouts, and short transactions in the application, remembering that hot_standby_feedback moves the same effect to the primary.

What to say in the interviewAbout 80 seconds aloud

Open with

That session's snapshot was taken at 09:00, and because it is REPEATABLE READ it keeps that snapshot until it commits or rolls back, idle or not. The xmin horizon is database-wide, so for six hours vacuum has not been able to remove any row version deleted or updated after 09:00, in any table, even tables the report never touched.

Then, if they want more

The effects are table and index bloat, slower scans, index-only scans that have to visit the heap because pages cannot be marked all-visible, and transaction ID age growing because rows cannot be frozen. To find it, I query pg_stat_activity for state idle in transaction ordered by xact_start, and look at backend_xmin and the last query to identify the owner.

I also check pg_replication_slots and pg_prepared_xacts, because slots and orphaned prepared transactions hold the horizon the same way. I end it with pg_terminate_backend, since cancel does nothing to an idle session.

To prevent it, I set idle_in_transaction_session_timeout per role, plus statement_timeout and, on PostgreSQL 17, transaction_timeout, and fix application code that keeps a transaction open around slow work like remote calls. And on a standby with hot_standby_feedback on, the same session would hold back vacuum on the primary.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 17
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
PostgreSQL documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A reporting session started BEGIN ISOLATION LEVEL REPEATABLE READ at 09:00 and has been...": https://scrivio-interview-library.netlify.app/databases/postgresql/5e39f97435/