Re: Do I need serializable for this query?

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: William Garrison <postgres(at)mobydisk(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Do I need serializable for this query?
Date: 2007-04-10 20:28:16
Message-ID: 1176236896.4152.84.camel@dogma.v10.wvs
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 2007-04-10 at 14:45 -0400, William Garrison wrote:
> I have a table that keeps running totals. It is possible that this
> would get called twice simultaneously for the same UserID. Do I need to
> put this in a serializable transaction? Since this gets called often,
> could it be a performance problem if I make it serializable?
>
> CREATE FUNCTION UpdateTotals(IN UserID int,
> IN AddToCount int,
> IN AddToSize bigint)
> RETURNS void AS $$
> BEGIN
> UPDATE
> Totals
> SET
> TotalSize = TotalSize + $2,
> TotalCount = TotalCount + $3
> WHERE
> UserID = $1;
> END IF;
> END
> $$ LANGUAGE 'plpgsql' VOLATILE;
>

You only have one command to execute; you don't need to do anything
special at all. If two UPDATEs happen to the same tuple at the same
time, one will block waiting for the other to commit.

Regards,
Jeff Davis

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2007-04-10 20:52:11 Re: Do I need serializable for this query?
Previous Message Florian G. Pflug 2007-04-10 20:18:01 Re: Do I need serializable for this query?