Surely the biggest issue there is the way that application work is being done 
while holding any sort of lock. The transactions need to be short and not 
waiting for any app work, particularly if it's heavy or involved round trip 
latency.

Regards

Greg


Dr Greg Low
SQL Down Under Pty Ltd
+61 419201410

________________________________
From: Justin <[email protected]>
Sent: Tuesday, 01 December 2026 12:52:42
To: Laurenz Albe <[email protected]>
Cc: Muhammad Rehan Paracha <[email protected]>; 
[email protected] <[email protected]>
Subject: Re: Best practices for transaction-level authorization, row locking 
and least-privilege roles in PostgreSQL 17



On Mon, Nov 30, 2026 at 7:03 PM Laurenz Albe 
<[email protected]<mailto:[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]<mailto:[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