Re: Race hazard deleting using CTID? - Mailing list pgsql-general

From Tom Lane
Subject Re: Race hazard deleting using CTID?
Date
Msg-id 28099.1250017252@sss.pgh.pa.us
Whole thread Raw
In response to Race hazard deleting using CTID?  ("Peter Headland" <pheadland@actuate.com>)
Responses Re: Race hazard deleting using CTID?  ("Peter Headland" <pheadland@actuate.com>)
List 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 by date:

Previous
From: "Peter Headland"
Date:
Subject: Race hazard deleting using CTID?
Next
From: Alvaro Herrera
Date:
Subject: Re: Does PERFORM hold a lock?