r/postgres•
bramley

Sequential Lock Acquisition?

Question

I'm learning about locking semantics and timeouts and I've run across something confusing. I have a table called demo_lock that has a single column target.

When I run these expressions, a strange thing happens:

BEGIN;
SET LOCAL lock_timeout=3000;
SELECT target FROM demo_lock WHERE target='foo' FOR UPDATE;

I run this in three parallel psql consoles.

The first successfully obtains the lock and sits there waiting for input.

The second waits for the timeout for 3 seconds, times out, and prints the error ERROR: canceling statement due to lock timeout

The third then waits a whole extra three seconds and then times out with the same error.

What I would have expected is that the second and third would time out at basically the same time (allowing for the time it takes for me to switch consoles and hit Enter). But they don't. The third waits an extra lock_timeout time and then fails. Why is that?

Interestingly, adding a 4th terminal is a little less deterministic, but still operates mostly the same.

Posted Aug 29, 2026, 3:10 AM UTC

10
Loading comments…