Postgresql Concurrency Postgresql Concurrency postgresql postgresql

Postgresql Concurrency


Triggers don't handle concurrency, and PostgreSQL should do the right thing whether or not you use explicit transactions.

PostgreSQL uses optimistic locking which means the first person to actually update the row gets a lock on that row. If a second person tries to update the row, their update statement waits to see if the first commits their change or rolls back.

If the first person commits, the second person gets an error, rather than their change going through and obliterating a change that might have been interesting to them.

If the first person rolls back, the second person's update un-blocks, and goes through normally, because now it's not going to overwrite anything.

The second person can also use the NOWAIT option, which makes the error happen immediately instead of blocking, if their update conflicts with an unresolved change.