Avoiding deadlocks on mass delete / update

Поиск
Список
Период
Сортировка
Искать

Avoiding deadlocks on mass delete / update

От:
Роман Маширов <mrj@nandu.ru>
Дата:
I've got a simple 'spool' table, one process 'worker' reads and updates 
this table, other 'stat' performs 'delete ... where ... returning *'. 
Sometimes I've got dedlocks on delete operation in 'stat', seems like at 
the moment of expiration of data by timeout some state changes arrived 
from worker. So the question, is it possible to somehow set order of row 
deletion in such bulk delete operation, to avoid deadlocks?

Thank you beforehand
--
MRJ

Re: Avoiding deadlocks on mass delete / update

От:
Craig Ringer <craig@postnewspapers.com.au>
Дата:
Роман Маширов wrote:
> I've got a simple 'spool' table, one process 'worker' reads and updates
> this table, other 'stat' performs 'delete ... where ... returning *'.
> Sometimes I've got dedlocks on delete operation in 'stat', seems like at
> the moment of expiration of data by timeout some state changes arrived
> from worker. So the question, is it possible to somehow set order of row
> deletion in such bulk delete operation, to avoid deadlocks?

OK, so for the sake of example, WORKER is UPDATEing rows that stat is
trying to DELETE at the same time, such that worker holds a lock on row
A and wants a lock on row B, but stat holds B and wants A?

In other words, the deadlock is an _interaction_ between 'stat' and
'worker'?

Can you post the queries?


One option is to SELECT ... FOR UPDATE NOWAIT before your UPDATE or DELETE.

http://www.postgresql.org/docs/8.4/static/sql-select.html#SQL-FOR-UPDATE-SHARE
http://www.postgresql.org/docs/8.4/static/explicit-locking.html#LOCKING-ROWS

Some kind of discussion of row level lock ordering might be in order for
the manual, actually. If there is one, it needs a link from the above
sections. The section on deadlocks:

http://www.postgresql.org/docs/8.4/static/explicit-locking.html#LOCKING-DEADLOCKS

doesn't mention how to avoid deadlock when multi-row updates/deletes are
being used concurrently.

--
Craig Ringer

Re: Avoiding deadlocks on mass delete / update

От:
Роман Маширов <mrj@nandu.ru>
Дата:
 Craig Ringer wrote: 
Роман Маширов wrote: 
I've got a simple 'spool' table, one process 'worker' reads and updates
this table, other 'stat' performs 'delete ... where ... returning *'.
Sometimes I've got dedlocks on delete operation in 'stat', seems like at
the moment of expiration of data by timeout some state changes arrived
from worker. So the question, is it possible to somehow set order of row
deletion in such bulk delete operation, to avoid deadlocks?   
OK, so for the sake of example, WORKER is UPDATEing rows that stat is
trying to DELETE at the same time, such that worker holds a lock on row
A and wants a lock on row B, but stat holds B and wants A?

In other words, the deadlock is an _interaction_ between 'stat' and
'worker'? 
yes
Can you post the queries? 
as dumb as possible :)

worker parses several thousand events and do
update queue set state=$1 where queue_id in (<id list>) and state in (<previous state list>)
for each target state, so it performs 1-4 update queries.

stat do
delete from queue where queue_stamp < now()-'1day'::interval or state in (<terminal state list>)
returning *

The main reason for such thing is to reduce amount of queries to perform, since this queue could work at about 50 objects per second with 4 state changes.
One option is to SELECT ... FOR UPDATE NOWAIT before your UPDATE or DELETE. 
Yep, thank you very much!

But, it would be good feature to somehow allow to explicitly set order of multi-row update / delete,
or to 'delete but locked', meaning delete all rows by given query parameters, except locked ones.

--
MRJ
FAQ