X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 86D18D700E for ; Wed, 24 Aug 2005 07:03:46 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 54571-04 for ; Wed, 24 Aug 2005 10:03:38 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.204]) by svr1.postgresql.org (Postfix) with ESMTP id 70836D6ECE for ; Wed, 24 Aug 2005 07:03:37 -0300 (ADT) Received: by wproxy.gmail.com with SMTP id i4so70388wra for ; Wed, 24 Aug 2005 03:03:40 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:user-agent:x-accept-language:mime-version:to:subject:references:in-reply-to:content-type:content-transfer-encoding; b=E0pRoA5mPUR4hhBJ03sJdQe6N36V86qkuFBJglMuknE1AHD2+qbRk4u2hzn3sXNcOrzuv+NUv5E4QXcsBo5FYYOWrEN4qGQHjSWd0v8G84krG5CEAamZCCRcto6icm0Evpy3PUwZB3oVfKwjkGQ3HOJ47MhZ4maDmq/Di0kHkGU= Received: by 10.54.42.27 with SMTP id p27mr415766wrp; Wed, 24 Aug 2005 03:03:40 -0700 (PDT) Received: from ?192.168.3.4? ( [81.182.248.121]) by mx.gmail.com with ESMTP id g12sm751006wra.2005.08.24.03.03.38; Wed, 24 Aug 2005 03:03:40 -0700 (PDT) Message-ID: <430C45E2.8080103@gmail.com> Date: Wed, 24 Aug 2005 12:03:14 +0200 From: =?ISO-8859-2?Q?Sz=FBcs_G=E1bor?= User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: hu-hu, hu, en-us, en MIME-Version: 1.0 To: "pgsql-sql@postgresql.org" Subject: Re: Tuple insert missing query in ongoing transaction References: <430B3C36.3060909@gmail.com> <75184185984.20050823182602@ugyvitelszolgaltato.hu> In-Reply-To: <75184185984.20050823182602@ugyvitelszolgaltato.hu> Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.452 required=5 tests=[AWL=-0.042, RCVD_BY_IP=0.024, TO_ADDRESS_EQ_REAL=0.47] X-Spam-Level: X-Archive-Number: 200508/211 X-Sequence-Number: 22439 Dear DAQ, Thanks for the native answer ;) For those who have concerns reading Hungarian, here is an excerpt including my answer. You adviced me to issue an ACCESS EXCLUSIVE lock on table "shift" in transaction B, before issuing the actual insert, so that the insert will wait till the end of transaction S. Actually, you pointed me to the right way of the PostgreSQL doc and gave me the inspiration to actually read about lock modes :) As far as I understood things: 1. Shouldn't it be enough to lock in EXCLUSIVE mode, thus allowing others to access (select only) table "shift"? What's more, since any ins/upd/del on a table aquires ROW EXCLUSIVE mode, shouldn't it be enough to just acquire SHARE mode? It wouldn't even conflict with concurrent SHARE locks of other clients inserting into table B. 2. Am I reading right that acquiring a lock in a BEFORE trigger is too late? (not mentioning that trigger firing order is undefined) If so, is there another way to provide a server-side automatic lock before the insert, say, writing an ON INSERT rule on table "barcode"? 3. I can't see why shouldn't this work with updates and deletes (on any of the two tables), but since I'm new to this topic, it's better to ask: should I have any further concerns? TIA, -- G. On 2005.08.23. 18:26, daq wrote: > SG> Dear Gurus, > > SG> I know this is the typical case of transaction use, I just seem to lack the > SG> appropriate education of what exactly happens and whether I may be able to > SG> detect it. > > SG> I have two queries, one affecting the other. > > SG> 1. INSERT INTO barcode. > SG> A BEFORE INSERT/UPDATE trigger checks if there's an appropriate tuple in > SG> table "shift" for this tuple (matching day, shift-of-the-day and workplace) > SG> and denormalizes fields. > > SG> 2. INSERT INTO shift. > SG> An AFTER INSERT/UPDATE/DELETE trigger updates rows in table barcode, forcing > SG> the abovementioned check for shift. > > SG> The second one is a long process, taking about 20 sec to finish. Imagine the > SG> following scenario: > > SG> x:xx:00 INSERT INTO shift. > SG> Transaction "S" begins. > SG> It updates several rows, but not the not-yet-inserted row. > SG> x:xx:10 INSERT INTO barcode (... appropriate for above-inserted shift ...) > SG> Transaction "B" begins. > SG> It checks but does not find the corresponding shift. > SG> x:xx:11 Transaction "B" ends. > SG> x:xx:20 Transaction "S" ends. > > SG> In such scenarios, sometimes we get "Deadlock detected." That's OK since the > SG> transactions actually cross each other's way. > > SG> But not always. In about 100 inserts, now we have the first case that did > SG> not show any trace of that something went wrong. > > SG> 1. Is there a way to detect such "crossing" transactions? > SG> 2. Is there a thorough article on deadlocks, how and when do they happen? > SG> 3. Maybe a section of the postgresql doc clarifying when do tuples get locked? > > A helyzet elkerulesere talan tudok egy modszert. A barcode tablaba > insertalast szervezd tranzakcioba, es access exclusive moddal lockold > az elejen(mindenkeppen meg az insert elott) a shift tablat. Igy mig "S" tranzakcio folyamatban van, > addig a "B" nem indulhat el az exclusiv lock igenye miatt, es mig a > "B" var a sorara, addig ujabb "S"-t sem enged elindulni. Igy > elkerulheted az egymast keresztbe vero tranzakciokat, ami a > deadlockhoz szokott vezetni. > > DAQ > >