Postgres row-level locking patterns for marketplace bids (with benchmarks)
Three patterns compared: SELECT ... FOR UPDATE, advisory locks, and optimistic versioning.
SELECT * FROM leads WHERE id = $1 FOR UPDATE SKIP LOCKED;
TL;DR: optimistic versioning wins below ~200 writes/sec on a hot row. Advisory locks win above, if you keep them inside one transaction (PgBouncer transaction mode will bite you otherwise).
Thanks to f/devrim for the PgBouncer repro.
2 comments
solutions first · then score