pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@commandprompt.com>
To: Dave Cramer <pg@fastcrypt.com>
Cc: Csaba Nagy <nagy@ecircle-ag.com>
Cc: Steven Flatt <steven.flatt@gmail.com>
Cc: postgres performance list <pgsql-performance@postgresql.org>
Subject: Re: Foreign Key Deadlocking
Date: Thu, 19 Apr 2007 10:14:42 -0400
Message-ID: <20070419141442.GE4572@alvh.no-ip.org> (raw)
In-Reply-To: <049552E8-8EBB-4B44-8D65-6108456A2421@fastcrypt.com>
References: <357fa7590704180807j1906d42u1f0f539ac561dc3e@mail.gmail.com>
	<1176910597.16187.75.camel@coppola.muc.ecircle.de>
	<049552E8-8EBB-4B44-8D65-6108456A2421@fastcrypt.com>

Dave Cramer escribió:
> Hi Csaba,
> 
> I have a similar problem.
> 
> In an attempt to avoid the overhead of select count(*) from mailbox  
> where uid = somuid I've implemented triggers on insert and delete.
> 
> So there is a
> 
> user table which refers to to an inbox table,
> 
> so when people insert into the inbox there is an RI trigger grabbing  
> the shared lock, then the count triggers try to grab an exclusive  
> lock resulting in a deadlock.
> 
> Can we safely remove the shared locks ?
> 
> Is there a right way to implement the count triggers. I've tried  
> before triggers, and after triggers, both result in different kinds  
> of deadlocks.

Would it be possible for the triggers to lock the records, before
starting the actual operation, in well known orders, to avoid the
deadlocks?

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.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



view thread (8+ messages)  latest in thread

Message-ID: <20070419141442.GE4572@alvh.no-ip.org>
Permalink:  ../20070419141442.GE4572@alvh.no-ip.org/
Also on:    postgresql.org/message-id/20070419141442.GE4572@alvh.no-ip.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: alvherre@commandprompt.com, pg@fastcrypt.com, nagy@ecircle-ag.com, steven.flatt@gmail.com
  Subject: Re: Foreign Key Deadlocking
  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>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox