Quick revision
The short answer
Sessions that stay idle in transaction for hours mean the Spring service began a transaction and never finished it: a leaked connection, a code path that waits on something slow while holding a transaction, or a long-running process that keeps one open. An open transaction stops vacuum from removing dead rows, which causes the bloat and slow queries, and it may also hold locks. We find the code through application_name, the client address and HikariCP leak detection, then fix it and add backstops: idle_in_transaction_session_timeout in PostgreSQL and transaction timeouts in Spring.
What to remember
In this answer, we saw that idle in transaction sessions come from Spring code that began a transaction and never ended it, through slow waits, leaked connections or long processes. An open transaction holds back the horizon below which vacuum may remove dead rows, so tables bloat and queries slow down, and it can hold locks. We found the code with application_name, the client address and Hikari leak detection, fixed it, and added backstops in PostgreSQL and Spring.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Sessions that stay idle in transaction for hours mean the Spring service began a transaction and never finished it: a leaked connection, a code path that waits on something slow while holding a transaction, or a long-running process that keeps one open. An open transaction stops vacuum from removing dead rows, which causes the bloat and slow queries, and it may also hold locks. We find the code through application_name, the client address and HikariCP leak detection, then fix it and add backstops: idle_in_transaction_session_timeout in PostgreSQL and transaction timeouts in Spring.
Why this question is asked
This is a real production problem that looks like a database problem but is almost always an application bug. Interviewers ask it because solving it needs both sides: how PostgreSQL keeps old row versions and why open transactions matter to vacuum, and how a Spring service can leave a transaction open. In this answer, we will recap what idle in transaction means, explain why it causes bloat, trace the session back to the code, look at the usual causes, and then put the fixes and backstops in place.
1What Idle in Transaction Means
pg_stat_activity is a PostgreSQL view with one row per connection. Its state column says what the connection is doing: active means a query is running, idle means the connection is free, and idle in transaction means a transaction was started with BEGIN, or with autocommit turned off, and the client has not sent COMMIT or ROLLBACK, nor any new query. The database is waiting for the client. For a few milliseconds between statements this is normal; for hours it is a bug.
The query column shows the last statement the session ran, not a running one. That is often the best clue to which code path opened the transaction.
2Why One Open Transaction Bloats the Database
PostgreSQL never updates a row in place. An UPDATE writes a new version of the row and leaves the old one as a dead tuple, and DELETE marks the row dead. Vacuum later removes dead tuples, but only those that no open transaction could still need to see.
An open transaction that holds a snapshot, as every REPEATABLE READ transaction does, or that has written anything and so has its own transaction id, sets a horizon. Every row version that became dead after that transaction started must be kept, in every table of the database, because the old transaction might in theory still read it. After three hours on a busy system, that is millions of dead tuples that vacuum cannot remove. Tables and indexes grow, queries scan more pages, and everything slows down. The backend_xmin and backend_xid columns in pg_stat_activity show which session holds the horizon back.
It can also hold locks. Any row locked by an UPDATE or SELECT FOR UPDATE stays locked, and even a plain read keeps a table-level lock that blocks ALTER TABLE, which in turn blocks every query queued behind it.
- Code begins a transactionThen waits, or leaks the connection
- Session sits idle in transactionSnapshot or transaction id still held
- Other sessions update and delete rowsOld versions become dead tuples
- Vacuum runsMust keep everything newer than the horizon
- Tables and indexes bloatQueries read more pages and slow down
5 steps in order. Play them, or step through them one at a time.
Ending the transaction lets the next vacuum clean up, but space already bloated may need VACUUM FULL or pg_repack.
3Tracing the Session Back to the Code
First we make sessions identifiable. The PostgreSQL JDBC driver sets application_name from the ApplicationName URL parameter, such as jdbc:postgresql://db/orders?ApplicationName=order-service, so each service shows its own name. client_addr then narrows it to one pod or host.
Inside the service, HikariCP's leak detection points at the exact line. With spring.datasource.hikari.leak-detection-threshold=60000, Hikari logs a warning with the stack trace of the code that borrowed any connection held for more than 60 seconds. That stack trace usually names the method that opened the transaction.
4The Usual Causes
The first cause is a transaction that waits. A @Transactional method calls a remote service with no timeout, waits on a queue, or streams a large result to a slow client with a repository method that returns Stream<Order>, which keeps the transaction open until the stream is consumed. If the remote side hangs, the transaction hangs with it.
The second is a real leak: code that takes a connection directly from the DataSource, calls setAutoCommit(false), and returns early or throws before closing it, so the connection never goes back to the pool and the transaction is never ended.
The third is a long-running process, such as a scheduled export or a consumer loop, annotated with @Transactional as a whole, so one transaction lasts as long as the process. Open Session in View can make the effect wider, because it keeps the persistence context and often its connection for the whole web request, and with autocommit disabled in the pool a lazy load in the view can open a transaction that lives until the request ends.
5The Fixes and the Backstops
We fix the code first: move remote calls out of transactions, close connections with try-with-resources, break long processes into short transactions, and turn off Open Session in View with spring.jpa.open-in-view=false. Then we add limits on both sides so the next bug cannot run for hours.
Notice the difference between the two limits. Spring's transaction timeout is checked when the transaction issues statements, so it stops a transaction that keeps querying for too long, but it cannot interrupt a thread that sits idle waiting for a remote call. The PostgreSQL setting catches that case: after 60 seconds idle in a transaction, the server ends the session, the transaction rolls back, and the application gets an error it can log. Setting it per role means each service gets limits that suit it.
6Follow-up questions
Can I just kill the sessions?
As an emergency step, yes: select pg_terminate_backend(pid) ends the session and rolls back its transaction, and the next vacuum can clean up. But the service will open new ones, so it only buys time to find the code.
Does idle_in_transaction_session_timeout break long transactions that are working?
No. It only counts time spent idle inside a transaction, between statements. A transaction running a long query is active, not idle. The limit for those is statement_timeout, and PostgreSQL 17 also adds transaction_timeout for the total duration.
Will the bloat go away after the fix?
Dead tuples are removed by the next vacuum, and the space is reused for new rows. The files on disk do not shrink, though; if a table grew far beyond its normal size, VACUUM FULL, which locks the table, or an online tool such as pg_repack is needed to return the space.
7Conclusion
In this answer, we saw that idle in transaction sessions come from Spring code that began a transaction and never ended it, through slow waits, leaked connections or long processes. An open transaction holds back the horizon below which vacuum may remove dead rows, so tables bloat and queries slow down, and it can hold locks. We found the code with application_name, the client address and Hikari leak detection, fixed it, and added backstops in PostgreSQL and Spring.
Open with
Idle in transaction means the service started a transaction and stopped sending anything, without commit or rollback. For hours, that is an application bug. It causes bloat because PostgreSQL keeps old row versions until no open transaction could need them; one long-open transaction holds back that horizon for the whole database, so vacuum cannot remove dead tuples, tables and indexes grow, and queries slow down.
Then, if they want more
It may also hold row locks or block DDL. To find the code, I would make sure each service sets ApplicationName in the JDBC URL, use client_addr to find the pod, and look at the query column, which shows the last statement. In the service, Hikari's leak-detection-threshold logs the stack trace of whoever held a connection too long.
The usual causes are a @Transactional method waiting on a remote call or a slow stream, a connection taken manually and not closed on an error path, or a long job that is one big transaction, and Open Session in View can stretch things too.
I would fix the code, then add backstops: idle_in_transaction_session_timeout and statement_timeout on the service's database role, and Spring transaction timeouts. I would also terminate the stuck sessions right away so vacuum can catch up.
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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 PostgreSQL database shows table bloat and slow queries, and pg_stat_activity shows...": https://scrivio-interview-library.netlify.app/databases/transactions/0c1ecd0851/