Re: Race hazard deleting using CTID?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Peter Headland" <pheadland(at)actuate(dot)com>
Cc: pgsql-general(at)postgresql(dot)org, "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
Subject: Re: Race hazard deleting using CTID?
Date: 2009-08-11 19:00:52
Message-ID: 28099.1250017252@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

"Peter Headland" <pheadland(at)actuate(dot)com> writes:
> My question is, does this code contain a race hazard, because the list from the SELECT might get changed by another session before 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2009-08-11 20:10:48 Re: Does PERFORM hold a lock?
Previous Message Peter Headland 2009-08-11 18:29:26 Race hazard deleting using CTID?

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Headland 2009-08-11 20:13:05 Re: Race hazard deleting using CTID?
Previous Message Peter Headland 2009-08-11 18:29:26 Race hazard deleting using CTID?