Skip to content

PostgreSQL error 40P01: deadlock detected

PostgreSQL detected a cycle of transactions waiting for each other’s locks and aborted one transaction to break it.

deadlock detected
SQLSTATE: 40P01

Each transaction in the cycle holds a lock another needs, so none can finish without intervention. PostgreSQL chooses a transaction to abort so the others can continue. An ordinary lock wait does not necessarily form a deadlock. See PostgreSQL’s deadlock explanation.

  • Transactions update the same rows in different orders.
  • An open transaction holds locks while waiting for application or user input.
  • Concurrent jobs change overlapping data or structures.
  1. Read the error details and transaction outcome.
  2. Run ROLLBACK if your explicit editor transaction remains aborted.
  3. Retry the complete transaction after competing work finishes.
  4. If the error repeats, compare the transactions’ lock acquisition order.
ROLLBACK;

Retry the whole transaction together. Earlier statements can be necessary to the operation’s correctness. For application code, use a consistent order when accessing shared rows and keep transactions short.

The server log can identify the statements and processes involved. Use the log source for your setup:

Setup Where to investigate
Local The log destination configured for that PostgreSQL server
Docker The container log, if PostgreSQL sends its logs there
Cloud The service’s database logs
Company server Ask the administrator for deadlock details at the recorded error time

Share only the necessary details through an approved channel; logged SQL can contain private values.

TableAI labels this error Deadlock Detected and includes PostgreSQL’s message and code in the details. Admin › Locks shows current activity, so the original cycle may already be gone when you inspect it. Use view locks for current blocking and server logs for the earlier deadlock.