RE: Row locks, SKIP LOCKED, and transactions

From: Steven Winfield <Steven(dot)Winfield(at)cantabcapital(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: RE: Row locks, SKIP LOCKED, and transactions
Date: 2019-12-19 15:30:32
Message-ID: E9FA92C2921F31408041863B74EE4C2001E766933B@CCPMAILDAG03.cantab.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> (Or you could use serializable mode, but that feels like using a hammer to swat a fly.)

Do you mean the serializable transaction isolation level? Because that doesn't work either. Here (finally) is a tiny repro case. You'll need 2 psql sessions (S1, S2):

S1: CREATE TABLE t (id integer):
S1: INSERT INTO t VALUES (1);
S1: BEGIN;
S1: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
S1: SELECT id FROM t FOR UPDATE;

(So now there is a single, globally visible row that S1 has a lock on)

S2: BEGIN;
S2: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
S2: SELECT id FROM t; -- returns 1 row, as expected
S2: SELECT id FROM t FOR UPDATE SKIP LOCKED; -- returns 0 rows, as expected

S1: ROLLBACK; -- S1's row lock is released

S2: SELECT id FROM t FOR UPDATE SKIP LOCKED; -- returns 1 row

...i.e. the row appears unlocked to S2 despite its transaction's snapshot being taken before the lock was released.

I'm going to use the suggestions made by you and others previously in this thread, so (for me at least) this is now just academic, but I'm still interested to know if the above behaviour is expected, and if I should have been able to deduce it from the docs. The best I could find is:

https://www.postgresql.org/docs/11/sql-select.html
"With SKIP LOCKED, any selected rows that cannot be immediately locked are skipped. Skipping locked rows provides an inconsistent view of the data, so this is not suitable for general purpose work, but can be used to avoid lock contention with multiple consumers accessing a queue-like table."

Thanks for your (and everyone else's) help,

Steve.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron 2019-12-19 15:32:26 Re: Experiencing error during restore - found unexpected block ID (0)
Previous Message Sar 2019-12-19 15:24:47 Re: Experiencing error during restore - found unexpected block ID (0)