A deadlock occurs when transactions wait on one another in a cycle. PostgreSQL detects the cycle and aborts one transaction. The application must retry the aborted unit of work; a longer lock timeout doesn't remove the cycle.
Consistent lock ordering prevents a common account-transfer deadlock. If every transaction locks account IDs in ascending order, two transfers avoid acquiring the same pair in opposite orders. Keep unrelated network calls outside the locked transaction. A smaller critical section gives other sessions less time to collide.
Retries need a limit, delay and an idempotency strategy. SQLSTATE 40P01 identifies a deadlock; 40001 identifies serialization failure. A client can't safely repeat an external effect merely because a database transaction aborted. These exercises combine a real ordered lock query with production decisions. The browser doesn't run PostgreSQL's multi-process deadlock detector.
Lock the required pair in the shared ascending order.