Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 71BD8B5DBD6 for ; Thu, 15 Sep 2011 04:25:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 30522-06 for ; Thu, 15 Sep 2011 07:25:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from exprod7og124.obsmtp.com (exprod7og124.obsmtp.com [64.18.2.26]) by mail.postgresql.org (Postfix) with ESMTP id 4AE8DB5D91F for ; Thu, 15 Sep 2011 04:25:47 -0300 (ADT) Received: from mail-fx0-f53.google.com ([209.85.161.53]) (using TLSv1) by exprod7ob124.postini.com ([64.18.6.12]) with SMTP ID DSNKTnGoeiAog9Xihj70+K5Pp/BCyuRuibgQ@postini.com; Thu, 15 Sep 2011 00:25:47 PDT Received: by mail-fx0-f53.google.com with SMTP id 2so388050fxh.12 for ; Thu, 15 Sep 2011 00:25:46 -0700 (PDT) Received: by 10.223.48.214 with SMTP id s22mr511894faf.104.1316071546540; Thu, 15 Sep 2011 00:25:46 -0700 (PDT) Received: from [192.168.1.183] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi. [88.195.122.240]) by mx.google.com with ESMTPS id n1sm5995352fad.20.2011.09.15.00.25.44 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 15 Sep 2011 00:25:44 -0700 (PDT) Message-ID: <4E71A876.7000507@enterprisedb.com> Date: Thu, 15 Sep 2011 10:25:42 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.2.21) Gecko/20110831 Icedove/3.1.13 MIME-Version: 1.0 To: Eduardo Piombino CC: pgsql-bugs@postgresql.org Subject: Re: BUG #6207: fali to get lock on parent table after two consecutive updates to the same row in child table References: <201109150355.p8F3tioI087016@wwwmaster.postgresql.org> In-Reply-To: <201109150355.p8F3tioI087016@wwwmaster.postgresql.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201109/89 X-Sequence-Number: 31106 On 15.09.2011 06:55, Eduardo Piombino wrote: > Problem came when I tried to update the child table a, which holds a fk to > b, after doing two updates to the same record in a. The other session will > fail to obtain the lock after the second update (but won't fail immediately > after the first one). > > Tom Lane suggested that it may have something to do to some optimization in > the management of the foreign keys after two updates to the same row in the > child table, but I got no concrete answer as if this behavior is what is > expected from the engine or not. > > ... > > Following is a test case with which you can very easily reproduce the > issue. > Open two pg sessions to the database. > Statements should be executed in the following order. > -- session T1 means that the following lines should be executed from pg > session 1. > -- session T2 means that the following lines should be executed from pg > session 2. > > create table b ( > id bigint not null, > x double precision, > constraint pk_b primary key (id)); > > create table a ( > id bigint not null, > id_b bigint, > x double precision, > constraint pk_a primary key (id), > constraint fk_b foreign key (id_b) references b (id)); > > insert into b (id, x) values (1, 0); > insert into a (id, id_b, x) values (1, 1, 0); > > [updates using two sessions] Those first updates on the tables are unnecessary, this test case can be simplified into just: -- session T1 begin transaction; update a set x = x + 1 where id = 1; update a set x = x + 1 where id = 1; (now switch to the second terminal) -- session T2 select * from b where id = 1 for update nowait; As Tom suggested, this an artifact of the way foreign key triggers are queued. The first update takes advantage of this optimization (in AfterTriggerSaveEvent function): > /* > * If this is an UPDATE of a PK table or FK table that does not change > * the PK or FK respectively, we can skip queuing the event: there is > * no need to fire the trigger. > */ The second UPDATE, however, falls into this exception later in that function: > /* > * If this is an UPDATE of a PK table or FK table that does not change > * the PK or FK respectively, we can skip queuing the event: there is > * no need to fire the trigger. > */ > ... > /* > * Update on FK table > * > * There is one exception when updating FK tables: if the > * updated row was inserted by our own transaction and the > * FK is deferred, we still need to fire the trigger. This > * is because our UPDATE will invalidate the INSERT so the > * end-of-transaction INSERT RI trigger will not do > * anything, so we have to do the check for the UPDATE > * anyway. > */ We can't distinguish a row that was UPDATEd earlier in the same transaction from a row that was INSERTed earlier in the same transaction. So on the second UPDATE, the foreign-key trigger is fired, and the trigger locks the tuple in table b. That's not ideal, of course, but I don't see any easy way to fix that. Perhaps we could add some extra checks there, like whether any INSERT-triggers have actually been queued. But it would just narrow the issue, I don't see a way to completely eliminate it. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com