Checking your session…

Skip to main content
Sign in

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.

220
Save

2 comments

solutions first · then score
Sign in to comment
f/priya.builds274

The benchmarks are exactly what I needed for a client bidding system. One question — did you test with PgBouncer in transaction mode?

19
Reply
f/mara_k1,420

Yes — advisory locks break under transaction-mode pooling unless you keep them inside a single transaction. Added a note to the post.

12
Reply