On Mon, 2026-11-30 at 11:33 -0500, Justin wrote:
> On Mon, Nov 30, 2026 at 5:13 AM Laurenz Albe <[email protected]> wrote:
> > On Sun, 2026-11-29 at 23:28 -0500, Justin wrote:
> > > Use of explicit locking is highly discouraged.
> > 
> > I have to disagree with that.  I'd say it depends a lot on your use case.
> > If collisions are frequent, optimistic locking will lead to a lot of
> > transactions being repeated, which hurts performance and throughput and
> > creates more work for autovacuum.
> > As long as the transactions are short, pessimistic locking is a better
> > approach in that case.
> > 
> > 
> Why is the application repeating work that explicit locking would block?  
> This means
> the application has to be written in a manner to detect the explicit locks.
> 
> I would like a better explanation on what type of workload you're referring 
> to where
> explicit locking is a benefit. 

Here is a trivial example:

  BEGIN;

  SELECT value FROM tab WHERE id = 42 FOR NO KEY UPDATE;

  /* calculate a new value in the application */

  UPDATE tab SET value = 'new_value' WHERE is = 42;

  COMMIT;

There is no need to "detect" the lock.  Just wait for it to go away.
If the calculation takes longer, and there are a lot of processes
trying to modify the row in that fashion, an "optimistic lock" would
retry the operation (and the expensive calculation) frequently.

Yours,
Laurenz Albe


Reply via email to