X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4D82BD1C4E0 for ; Fri, 1 Aug 2003 00:12:42 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 93265-09 for ; Thu, 31 Jul 2003 21:11:36 -0300 (ADT) Received: from megazone.bigpanda.com (45.mufb.snfc.sffca01r18.dsl.att.net [12.103.249.45]) by svr1.postgresql.org (Postfix) with ESMTP id 1C602D1C4DF for ; Thu, 31 Jul 2003 21:12:35 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id A0B52D6B5; Thu, 31 Jul 2003 17:12:35 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 924695C10; Thu, 31 Jul 2003 17:12:35 -0700 (PDT) Date: Thu, 31 Jul 2003 17:12:35 -0700 (PDT) From: Stephan Szabo To: Mauri Sahlberg Cc: , Subject: Re: Parallel transactions failing oddly In-Reply-To: <1059656469.4582.15.camel@taekwondo.intra.pretax.net> Message-ID: <20030731170748.L29530-100000@megazone.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/379 X-Sequence-Number: 9719 On 31 Jul 2003, Mauri Sahlberg wrote: > Either we have found a bug in Postgres (which I seriously doubt) or we > are being stupid clever enough way to not notice it. > > We have five complex "transactions" that are executed thru pq++/c++ > interface. If we run them one by one everything goes fine. But if I > run them in parallel - in separate processes - all but the first one > claiming the lock for "ryhmalaiset"-table will fail. And they will > fail as soon as the first one is finished by trying to insert > duplicate row in the shared table. Incidentally this row would also be > the very first row they are trying to insert. They all run the same code > but with different data. > > Each transaction fails in the following insert: > LOOP > SELECT * INTO r FROM kelaaryhma(aToimiala, aKielikoodi, pKoodi); > > SELECT INTO pRyhmalaiset * FROM ryhmalaiset > WHERE > toimiala = aToimiala AND > ryhma = pKoodi AND > kuukausi = aKuukausi AND > vuosi = aVuosi; > > IF NOT FOUND THEN > aMessage:= pKoodi::VARCHAR(16)||''/''||aKoodi::VARCHAR(16); > RAISE NOTICE ''ins-ryhmalaiset %'', aMessage; > INSERT INTO ryhmalaiset > (toimiala, ryhma, jasen, kuukausi, vuosi, ajettu) > VALUES > (aToimiala, pKoodi, aKoodi, aKuukausi, aVuosi, > ''now''::timestamp); > END IF; > ... > > Where (toimiala, ryhma, kuukausi, jasen, vuosi) are primary index > fields. The "IF NOT FOUND" should ensure that no duplicate inserts are > attempted but somehow it just fails when two or more processess are > run. The second transaction won't see the row inserted by the first transaction until it commits (at best). Both transactions can think there are no matching rows.