Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id DB3B19FB891 for ; Thu, 19 Apr 2007 11:29:01 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 65760-05 for ; Thu, 19 Apr 2007 11:28:47 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mail.ecircle.de (mail.ecircle.de [195.140.186.200]) by postgresql.org (Postfix) with ESMTP id 12A569FA4B4 for ; Thu, 19 Apr 2007 11:28:53 -0300 (ADT) Received: from einstein.muc.ecircle.de (einstein.muc.ecircle.de [192.168.1.7]) by mail.ecircle.de (READY) with ESMTP id 7E00055C015; Thu, 19 Apr 2007 16:28:51 +0200 (CEST) Received: from [192.168.1.91] ([192.168.1.91]) by einstein.muc.ecircle.de with Microsoft SMTPSVC(6.0.3790.1830); Thu, 19 Apr 2007 16:23:42 +0200 Subject: Re: Foreign Key Deadlocking From: Csaba Nagy To: Alvaro Herrera Cc: Dave Cramer , Steven Flatt , postgres performance list In-Reply-To: <20070419141442.GE4572@alvh.no-ip.org> References: <357fa7590704180807j1906d42u1f0f539ac561dc3e@mail.gmail.com> <1176910597.16187.75.camel@coppola.muc.ecircle.de> <049552E8-8EBB-4B44-8D65-6108456A2421@fastcrypt.com> <20070419141442.GE4572@alvh.no-ip.org> Content-Type: text/plain Message-Id: <1176992622.16187.97.camel@coppola.muc.ecircle.de> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Thu, 19 Apr 2007 16:23:42 +0200 Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 19 Apr 2007 14:23:42.0938 (UTC) FILETIME=[552DCBA0:01C7828E] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/346 X-Sequence-Number: 24223 > A frequently mentioned approach to avoid the point of contention is to > have a "totals" record and have the triggers insert "deltas" records; to > get the sum, add them all. Periodically, take the deltas and apply them > to the totals. This is what we do here too. There is only one exception to this rule, in one case we actually need to have the inserted records and the updated parent in one transaction for data consistency, in that case the delta approach won't work... we didn't find any other solution to that except patching postgres not to lock the parent keys at all, which has it's own problems too (occasional breakage of the foreign key relationship when the parent is deleted and a child still slips in, but this is very rare in our case not to cause problems which cannot be cleaned up with relative ease - not to mention that there could be other problems we didn't discover yet or our usage patterns are avoiding). Cheers, Csaba.