I'm using pg_try_advisory_lock()
in Postgres.
Next two queries lock more than one records in table1
:
1)
SELECT a.id
FROM table1 a
JOIN table2 b ON a.table1_id = b.id
WHERE
table2.id = 1
AND
pg_try_advisory_lock('table1'::regclass::integer, a.id)
LIMIT 1;
but
SELECT a.id
FROM table1 a
JOIN table2 b ON a.table1_id = b.id
WHERE table2.id = 1
returns one record.
2)
SELECT a.id
FROM table1 a
JOIN table2 b ON a.table1_id = b.id
JOIN table3 c ON b.table2_id = c.id
WHERE
table3.id = 1
AND
pg_try_advisory_lock('table1'::regclass::integer, a.id)
LIMIT 1;
But I need pg_try_advisory_lock()
to lock only one record.
What's wrong?
UPD
But the strange thing is that when I run the following query
SELECT a.id
FROM table1 a
JOIN table2 b ON a.table1_id = b.id
WHERE
pg_try_advisory_lock('table1'::regclass::integer, a.id)
LIMIT 1;
Postgres locks only one row. So, Postgres scans the very first row then stops? I don't get it: it should scan all rows then limit the results to one row, or not?