X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id D22FDD1B482 for ; Wed, 21 Apr 2004 14:58:45 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 62500-07 for ; Wed, 21 Apr 2004 14:58:45 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 7A5EFD1D1D5 for ; Wed, 21 Apr 2004 14:58:41 -0300 (ADT) Received: (qmail 30852 invoked by uid 500); 21 Apr 2004 18:02:42 -0000 Date: Wed, 21 Apr 2004 13:02:42 -0500 From: Bruno Wolff III To: denis@coralindia.com Cc: "H.J. Sanders" , pgsql-sql@postgresql.org Subject: Re: transaction Message-ID: <20040421180242.GB30712@wolff.to> Mail-Followup-To: denis@coralindia.com, "H.J. Sanders" , pgsql-sql@postgresql.org References: <20040421020010.GA19910@wolff.to> <003d01c42772$4fc3bf00$3332a8c0@DENIS> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <003d01c42772$4fc3bf00$3332a8c0@DENIS> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200404/209 X-Sequence-Number: 17584 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.