On Mon, Nov 30, 2026 at 7:03 PM Laurenz Albe <[email protected]>
wrote:

> 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
>

The use of
FOR NO KEY UPDATE
FOR UPDATE
FOR SHARE
FOR KEY SHARE

I already acknowledged that these lock modes are critical when the order of
operations matters or when parallelizing large updates / long-running
transactions.

They are not necessary for the bulk of ordinary updates and deletes. In the
normal MVCC path, readers do not block writers and writers do not block
readers; FOR UPDATE / FOR NO KEY UPDATE deliberately change that.
However I doubt that is what OP is talking about which is my concern.  The
question seem to be coming from someone not familiar with MVCC

Reply via email to