The problem is that your SELECT FOR UPDATE
should be wrapped in a transaction to be effective.
When typing this in a single line in psql
:
select * from TABLE_NAME for update; SELECT pg_sleep(30);
it gets broken down into two sql statements executed separately, so the locking effect of FOR UPDATE
is lost before the pg_sleep
even starts.
Try:
BEGIN; select * from TABLE_NAME for update; SELECT pg_sleep(30); END;
and in the other session:
select * from TABLE_NAME for update;
to see the second session being blocked for 30 seconds.