python - postgres locking with UPDATE FOR and foreign keys -


my postgres tables:

performers

  • id
  • rank
  • group_id references groups

performances

  • id
  • pay
  • performer_id references performers

groups

  • id

i need select performers group , update ranks. leads deadlocks since i'm doing select ... update select , other threads inserting @ same time.

example of error see lot (in python sqlalchemy):

dbapierror: (transactionrollbackerror) deadlock detected detail:  process 83182 waits sharelock on transaction 14282922; blocked process 83171. process 83171 waits sharelock on transaction 14282925; blocked process 83182. hint:  see server log query details.  'select performers.id performers_id, performers.rank performers_rank, performers.group_id performers_group_id \nfrom performers \nwhere performers.group_id = %(group_id_1)s update' {'group_id_1': 2} 

i've found few examples of behavior around well.

how can fix this? can switch different transaction locking level? i'd rather not abort , have retry - want database take care of contention me.

there must way fix - want quite simple.

you can avoid deadlocks, if concurrent write operations go in same unique order. add order by locking statement, , use same sort order everywhere. like:

select ... performers ... order performers.id update

where id primary key (or other stable, unambiguous combination of columns).

also, if operations involve multiple columns, locking in strictly same sequence across tables.

more details in this thread in pgsql-general list.
or in manual.

keep triggers (and foreign key constraints) necessary minimum. either way, first sort out for update locks. might solve problems. if not, consider different transaction isolation level. serializable should trick. have to prepared retry transactions until succeed.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -