Some operations must not run at the same time. A lock lets the second session wait (or fail fast) until the first one commits or rolls back. In PostgreSQL locks are held until the end of the transaction, and a cycle of waits becomes a deadlock.