Re: Delete from locking ordering differences

Поиск
Список
Период
Сортировка
От Peter Hendriks
Тема Re: Delete from locking ordering differences
Дата
Msg-id CAFhXkLH2u+xSsRpym3m1s_dXsXvVgn1Lcgd3Brzcq1ASLDdADw@mail.gmail.com
обсуждение исходный текст
Ответ на Delete from locking ordering differences  (Peter Hendriks <peter@mindloops.nl>)
Список pgsql-sql
Yes, we do not really care about order, but to prevent locking issues. Multiple transactions may run this query at the same time. It should never contend because of the skip locked, so maybe we should try this without an order by too. We can not get this query to fail in test so far, just in production, wo we are bit hesitant to change now that it is finally working... Our guess is to why this makes such a difference is that the delete statement in postgres does not guarantee ordering, so maybe the optimizer makes different choices than expected. Asking the question here, so maybe someone with more understanding can explain why we need the CTE.

Op vr 23 sep. 2022 om 09:31 schreef Peter Hendriks <peter@mindloops.nl>:
We are wondering if anyone can explain the difference we are having in production with the following queries:

DELETE FROM store
WHERE id IN (
  SELECT id FROM store 
  FOR UPDATE SKIP LOCKED 
  ORDER BY ID
  LIMIT 1000
)
RETURNING id, payload

This query is sometimes executed with high concurrency, and then can hang indefinitely, we assume because of a locking problem that postgresql is not detecting as a deadlock.

This alternative query does not have the hanging problem:

WITH store_ids AS (
  SELECT id FROM store 
  FOR UPDATE SKIP LOCKED 
  ORDER BY ID
  LIMIT 1000
)
DELETE FROM store s
USING store_ids si 
WHERE s.id = si.id
RETURNING s.id, s.payload

Can anyone explain why the first query is expected to fail (hang), and the second query does not have this problem? We would be interested in more understanding on this. Thanks! 


--
Met vriendelijke groet,

Peter Hendriks
Mindloops B.V.

T. +31 (0)6 37 23 26 73
E. peter@mindloops.nl
KvK: 85061921

В списке pgsql-sql по дате отправления:

Предыдущее
От: Rob Sargent
Дата:
Сообщение: Re: Delete from locking ordering differences
Следующее
От: Shaozhong SHI
Дата:
Сообщение: Can we generate a series of tables in a loop and then use another loop to union all?