Quick revision
The short answer
The analyst's open transaction still holds a row lock on the customer, so every order request that updates that row waits, holding a pooled connection while it waits, until the pool runs dry. We find the session with pg_stat_activity and pg_blocking_pids(), confirm what it is doing, end it with pg_terminate_backend(), and then prevent a repeat with idle_in_transaction_session_timeout for human roles, read-only access on replicas, and lock_timeout and statement_timeout on the application side.
What to remember
In this answer, we saw how a single uncommitted UPDATE holds a row lock that makes order requests wait, and how those waiting requests hold connections until the pool is exhausted. We found the blocker with pg_stat_activity and pg_blocking_pids(), confirmed its owner and last statement, and released it with pg_terminate_backend(). To prevent a repeat, we moved analysts to read-only roles on replicas, set idle and transaction timeouts for human roles, gave the application lock_timeout and statement_timeout so it fails fast, and added alerts on old transactions and lock waits.
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.
The analyst's open transaction still holds a row lock on the customer, so every order request that updates that row waits, holding a pooled connection while it waits, until the pool runs dry. We find the session with pg_stat_activity and pg_blocking_pids(), confirm what it is doing, end it with pg_terminate_backend(), and then prevent a repeat with idle_in_transaction_session_timeout for human roles, read-only access on replicas, and lock_timeout and statement_timeout on the application side.
Why this question is asked
This incident is common because nothing about it looks dangerous at the start: a person runs one UPDATE in a desktop SQL tool that has autocommit turned off, and then walks away. Interviewers ask it because the answer shows whether we can connect a database lock to an application symptom (timeouts and an exhausted connection pool), act calmly under pressure, and then fix the system rather than blame the person. In this answer, we will recap why an uncommitted UPDATE blocks other sessions, find the blocker step by step, resolve it safely, and then add the settings and alerts that stop it from happening again.
1A Quick Recap: Why One Forgotten UPDATE Blocks Everyone
When a session runs UPDATE customer SET ... WHERE id = 42 inside a transaction, PostgreSQL takes a row-level lock on that row. The lock lasts until the transaction ends with COMMIT or ROLLBACK, not until the statement finishes. Many SQL tools start a transaction implicitly and wait for the user to press commit, so the analyst may not even know a transaction is open.
While that lock is held, any other session that tries to update or lock the same row (UPDATE, DELETE, SELECT ... FOR UPDATE) must wait. Plain SELECTs still work, because PostgreSQL readers do not block on writers. So the damage is selective: only requests that write customer 42 hang, which is exactly why the order API times out for that one customer.
The pool exhaustion is the second half of the story. Each waiting request holds a connection from HikariCP, the connection pool Spring Boot uses by default. As retries and new orders for that customer arrive, waiting requests take every connection, and then requests for all other customers fail too. A lock on one row has become an outage for the whole service.
2Finding the Blocker
In pg_stat_activity, a session with an open transaction that is doing nothing has the state idle in transaction, and xact_start says when that transaction began.
6 messages in order. Play them, or step through them one at a time.
The lock affects one row, but waiting requests keep their connections and starve everyone else.
For an idle session, query shows the last statement it ran: here, an UPDATE of a customer row. pg_blocking_pids() returns the process ids holding the locks a session waits for, and every waiting order request points at pid 81234.
We check both because chains form: C waits for B, which waits for A. The session at the root, blocked by nobody, is the one we need.
3Resolving It Safely
Before ending anything, we confirm whose session it is from usename, client_addr and application_name. Asking the analyst to roll back is gentlest, but during an outage we usually cannot wait.
pg_cancel_backend() is not enough here. It cancels the statement that is currently running, and an idle session is not running one. pg_terminate_backend() closes the whole connection, and PostgreSQL rolls back the open transaction, which releases its locks at once.
The analyst loses an uncommitted change, which was never part of the database anyway, and we tell them. Waiting requests now complete, but some had already timed out and been retried by clients, so we check for duplicate orders.
4Preventing It: Limit Human Sessions
The strongest prevention is that people do not hold write transactions on the production primary at all. Analysts get a read-only role, and their tools point at a replica, a copy of the database that follows the primary and accepts only reads. A replica also keeps heavy analytical queries away from the order traffic.
For the human roles that do need the primary, PostgreSQL can end forgotten transactions automatically. idle_in_transaction_session_timeout terminates a session that has been idle inside a transaction for longer than the limit. PostgreSQL 17 also added transaction_timeout, which limits the total length of a transaction whether it is idle or not.
Notice that these settings are attached to roles, not to the whole server, so the application role keeps its own limits.
5Protecting the Application: Fail Fast Instead of Waiting
Even with good rules for humans, some lock will eventually be held too long, by a batch job, a migration or a bug. The application should then fail a request quickly rather than wait and hold a connection. Two PostgreSQL settings do this. lock_timeout aborts a statement that waits longer than the limit to acquire a lock. statement_timeout aborts any statement that runs longer than its limit, for whatever reason.
On the Spring side, the HikariCP connectionTimeout decides how long a request waits for a free connection (30 seconds by default), and the transaction timeout bounds the whole unit of work.
With these limits, a request for customer 42 fails after two seconds with a clear lock timeout error, its connection returns to the pool, and orders for every other customer keep working.
6Alerting Before Customers Notice
The last step is to see the next one coming. We alert on the age of the oldest open transaction and on lock waits, both of which come straight from pg_stat_activity, and we watch Hikari's pending-connection metric in the application.
Setting log_lock_waits = on also logs every lock wait longer than deadlock_timeout (one second by default) with the blocking process, so the on-call engineer would have seen this long before the pool ran out.
7Follow-up questions
Why did plain SELECTs on that customer still work?
Because PostgreSQL uses multiversion concurrency control: readers see the last committed version of a row and never wait for a writer's row lock. Only statements that want to change or lock the same row wait. That is why the symptom was limited to order writes for one customer until the pool filled up.
Would pg_cancel_backend have been enough?
No. It cancels the statement a session is currently executing, and an idle-in-transaction session is not executing anything, so the transaction and its locks stay. pg_terminate_backend closes the connection, which rolls back the transaction and releases the locks.
8Conclusion
In this answer, we saw how a single uncommitted UPDATE holds a row lock that makes order requests wait, and how those waiting requests hold connections until the pool is exhausted. We found the blocker with pg_stat_activity and pg_blocking_pids(), confirmed its owner and last statement, and released it with pg_terminate_backend(). To prevent a repeat, we moved analysts to read-only roles on replicas, set idle and transaction timeouts for human roles, gave the application lock_timeout and statement_timeout so it fails fast, and added alerts on old transactions and lock waits.
Open with
First I would confirm it is a lock problem and not a slow query. I query pg_stat_activity for sessions in the state idle in transaction, ordered by xact_start, and I use pg_blocking_pids on the waiting sessions to see who they wait for.
Then, if they want more
That shows the analyst's session at the root, with its last UPDATE on the customer table. After checking whose session it is, I run pg_terminate_backend on that pid. Cancel is not enough because the session is idle; terminating rolls back the transaction and releases the row lock, and the waiting requests then complete.
After that I check for duplicate orders from client retries. To prevent it, analysts get a read-only role on a replica, and any human role on the primary gets idle_in_transaction_session_timeout, and on PostgreSQL 17 transaction_timeout as well.
The application role gets lock_timeout of about two seconds and a statement_timeout, and Hikari gets a short connection timeout, so one locked row fails a few requests quickly instead of draining the pool.
Finally, I add alerts on the age of the oldest transaction, on lock waits, and on pending Hikari connections, and I turn on log_lock_waits so the next incident names the blocker in the logs.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 17, PostgreSQL 14
- 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, MySQL reference manual
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 "An analyst's session opened a transaction in a SQL tool, ran an UPDATE on a customer...": https://scrivio-interview-library.netlify.app/databases/database-locking/3203c6f4dd/