Skip to content

Lock Timeout

Error message

                canceling statement due to lock timeout
              

Quick summary

Your transaction tried to acquire a lock but another transaction held it for longer than the timeout period.

Why this happens

  • Another long-running transaction is holding a lock on the needed resource

  • The lock timeout is set too low for your workload

  • A transaction forgot to commit or roll back, holding locks indefinitely

Minimal example

✗ Broken code
-- Transaction 1 holds a lock and never commits
BEGIN;
UPDATE users SET name = 'Ada' WHERE id = 1;
-- forgot to COMMIT

-- Transaction 2 tries to update the same row → lock timeout
✓ Fixed code
-- Transaction 1 commits promptly
BEGIN;
UPDATE users SET name = 'Ada' WHERE id = 1;
COMMIT;

-- Transaction 2 can now proceed

The first transaction held a lock by not committing. Committing releases the lock so the second transaction can proceed.

How to diagnose

  • Check for long-running or idle transactions holding locks: SELECT * FROM pg_locks

  • Check the lock_timeout setting

  • Identify which transaction is holding the lock and why

How to fix

  • Wait and retry the query

  • Kill or roll back the transaction holding the lock

  • Increase the lock_timeout setting if appropriate

  • Optimize the long-running transaction to hold locks for less time

How to prevent

  • Keep transactions short to minimize lock holding time

  • Always commit or roll back transactions — never leave them open

  • Monitor for idle-in-transaction connections and clean them up

Related Resources

Related Glossary

Related Lessons

← Back to language