举例说:
UPDATE queue_messages
SET status = leased
WHERE id = ANY(
SELECT id FROM queue_messages
WHERE status = pending
ORDER BY id ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
)
我与EXPLAIN进行了核对,发现在应用LMIT 1之前,分局结果被锁定:
-> Nested Loop
-> Seq Scan on queue_messages
-> Subquery Scan on sub [...] loops=11
-> Limit [...] rows=2
-> LockRows
-> Sort
-> Seq Scan on queue_messages
Does that mean there are more than one row locked? How can I make sure that I lock only one row?
我对此进行了尝试,但我没有在EXPLAIN计划中看到
WITH _queue_ids AS (
SELECT id FROM queue_messages
WHERE status = pending
ORDER BY id ASC
LIMIT 1
) UPDATE queue_messages
SET status = leased
WHERE id = ANY(SELECT id FROM _queue_ids FOR UPDATE SKIP LOCKED)