Sub topic 10
Locking Timeouts in PostgreSQL
This chapter assumes you know basic SQL, transactions, and how to use two concurrent database sessions. It also assumes you understand that PostgreSQL can wait for a lock held by another transaction.
Timeouts limit how long a session waits or runs. They protect the database from stuck transactions, slow statements, and lock queues. Each timeout measures a different condition.
The three timeout settings
| Setting | Limits | Typical purpose |
|---|---|---|
| `lock_timeout` | Time spent waiting to acquire a lock | Fail quickly when another transaction blocks your work |
| `statement_timeout` | Total time spent executing one statement | Stop unexpectedly slow queries |
| `idle_in_transaction_session_timeout` | Time a session remains idle inside an open transaction | Terminate sessions that hold locks while doing nothing |
These settings accept units such as ms, s, min, and h. For example, 2s means two seconds.
`lock_timeout`
lock_timeout applies when a statement must wait for a lock. If the wait exceeds the limit, PostgreSQL cancels the statement and reports an error.
Use SET LOCAL inside a transaction when a timeout should apply only to one migration or unit of work:
BEGIN;
SET LOCAL lock_timeout = '2s';
ALTER TABLE accounts ADD COLUMN note text;
COMMIT;If another transaction holds a conflicting table lock for more than two seconds, the ALTER TABLE fails instead of waiting indefinitely. The transaction may then be in an aborted state, so the client must issue ROLLBACK before sending more commands.
lock_timeout does not make the operation itself faster. It only limits lock acquisition waits. A statement can acquire its locks quickly and then run for a long time.
`statement_timeout`
statement_timeout limits the total time allowed for a statement. The limit includes time spent waiting for locks and time spent executing the statement after it obtains them.
BEGIN;
SET LOCAL statement_timeout = '30s';
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
COMMIT;A statement can therefore fail because of either a lock wait or slow execution. Set lock_timeout shorter than statement_timeout when you want lock waits to fail separately:
SET LOCAL lock_timeout = '2s';
SET LOCAL statement_timeout = '30s';The first setting reports a lock-timeout failure if lock acquisition takes too long. The second still protects against slow execution after locks are acquired.
Timeout errors abort the current statement. In a transaction, the transaction itself becomes failed until you roll it back.
Idle transactions and held locks
A transaction can hold locks even when its client is not currently running a statement. For example:
BEGIN;
UPDATE accounts
SET balance = balance - 10
WHERE id = 1;
-- The session now waits for another client or application action.The session is idle in transaction, but the row lock remains held. A long pause here can block other transactions and increase the chance of deadlocks.
Set idle_in_transaction_session_timeout to terminate a session that stays idle inside a transaction too long:
SET idle_in_transaction_session_timeout = '60s';This setting measures idle time after a statement finishes while the transaction remains open. It does not limit an actively running query. Use statement_timeout for that case.
Choosing the right timeout
A practical policy often uses:
- A short
lock_timeoutfor deployments and schema changes. - A longer
statement_timeoutfor web requests or reports. - An
idle_in_transaction_session_timeoutlong enough for normal application pauses, but short enough to prevent abandoned transactions.
Set defaults for a role or database only after testing all clients. For one operation, prefer SET LOCAL, because it automatically ends with the transaction.
Exercise
A migration waits behind another transaction holding a table lock. You want the migration to fail after two seconds, but you want ordinary query execution to have up to one minute. Which settings should you use?
Show answer
Use SET LOCAL locktimeout = '2s' and SET LOCAL statementtimeout = '1min' inside the migration transaction.