Re: transaction - Mailing list pgsql-sql

From Bruno Wolff III
Subject Re: transaction
Date
Msg-id 20040421180242.GB30712@wolff.to
Whole thread Raw
In response to transaction  ("H.J. Sanders" <hjs@worldonline.nl>)
List pgsql-sql
On Wed, Apr 21, 2004 at 12:58:56 +0530, denis@coralindia.com wrote:
> 
> The code looks like:
> 
>         update tempxitag set qty = qty + nqty where
>                 ccod = cccod
>         GET DIAGNOSTICS nFound = ROW_COUNT;
>         If nFound = 0 then
>             insert into tempxitag( ccod, qty)
>             values (cccod, nqty );
>         End if;

You still can get errors if two transactions try to refer to the same
nonexistant record at the same time. Postgres doesn't do predicate
locking so the update won't lock the to be inserted row and both
transactions may see the record as not existing and both try to do
an insert.

Updating, checking the count and then trying an insert if the count was
0 and retrying if the insert fails may be a better approach than locking
the table. However, since this is an existing application it may be hard
to make this complicated of a change.

If there is flexibility in how the task gets done, switching to something
based on sequences is probably the way to go.


pgsql-sql by date:

Previous
From:
Date:
Subject: Trigger calling a function HELP ME! (2)
Next
From: Heflin
Date:
Subject: Join issue on a maximum value