Re: Race hazard deleting using CTID?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Race hazard deleting using CTID?
Дата
Msg-id 28099.1250017252@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Race hazard deleting using CTID?  ("Peter Headland" <pheadland@actuate.com>)
Ответы Re: Race hazard deleting using CTID?
Список pgsql-general
"Peter Headland" <pheadland@actuate.com> writes:
> My question is, does this code contain a race hazard, because the list from the SELECT might get changed by another
sessionbefore the DELETE uses it? 

>   delete from del where ctid = any(array(select ctid from del limit 10))

Well, the CTID of a row you can see can't be changed by another
transaction while your transaction is still live.  However, if someone
else does modify/delete one of those rows concurrently, it will fail the
outer WHERE check and thus silently not be deleted.  Net effect is that
you might delete fewer than 10 rows.  Not sure if you'd consider that a
race hazard or not.

> If so, am I correct to think that adding FOR UPDATE to create the version below would eliminate the hazard?

>   delete from del where ctid = any(array(select ctid from del limit 10 for update))

If you'd bothered to try that before asking the list, you'd know the
system won't take it --- FOR UPDATE is only supported at top level.
You could probably do something equivalent using a plpgsql loop, or
pulling the CTIDs back to the client side.

            regards, tom lane

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

Предыдущее
От: "Peter Headland"
Дата:
Сообщение: Race hazard deleting using CTID?
Следующее
От: Alvaro Herrera
Дата:
Сообщение: Re: Does PERFORM hold a lock?