PostgreSQL's SELECT FOR UPDATE creates unnecessarily strong row locks that can harm database concurrency. Most UPDATE operations don't actually need FOR UPDATE locks since they don't modify key columns that could conflict with foreign key references. Instead, SELECT FOR NO KEY UPDATE should be used in most cases as it prevents lost updates without blocking concurrent INSERTs on referencing tables. The distinction exists because PostgreSQL uses different lock modes to maintain referential integrity - FOR KEY SHARE protects referenced rows from deletion while allowing non-key updates to proceed.

6m read timeFrom cybertec-postgresql.com
Post cover image
Table of contents
The motivation behind SELECT FOR UPDATE : avoiding lost updatesHow PostgreSQL uses locks to maintain foreign key consistencyThe row locks taken by UPDATE and DELETEThe problem with SELECT FOR UPDATE in PostgreSQLWhat? SELECT FOR UPDATE is not the correct row lock for an UPDATE ?Conclusion

Sort: