[Dbix-class] Accumulating totals in a column

Bill Moseley moseley at hank.org
Wed Jun 13 20:24:30 GMT 2012


Say I have a table "task" in a Postgresql database that has an integer
column "total_seconds" which is a total number of seconds one or more
actors (people, worker processes, etc.) spent working on the task.   So,
these various actors need to add some value to an existing column.

What is the recommended way to to add to an existing integer column like
this and avoid race condition?

I can see in the past I've used a customer result set:

    UPDATE task SET total_seconds =3D total_seconds + ? WHERE id =3D ?

but, I also have used  SELECT .. FOR UPDATE  ( i.e. { for =3D> 'update' } )
in a transaction.

Is there any advantage of one over the other?


-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20120613/b22=
33291/attachment.htm


More information about the DBIx-Class mailing list