Re: regression, deadlock in high frequency single-row UPDATE

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Mark Kirkwood <mark(dot)kirkwood(at)catalyst(dot)net(dot)nz>
Cc: Andrew Sackville-West <awest(at)janrain(dot)com>, pgsql-bugs(at)postgresql(dot)org, Paulo Tanimoto <paulo(at)janrain(dot)com>
Subject: Re: regression, deadlock in high frequency single-row UPDATE
Date: 2014-12-11 13:06:53
Message-ID: 20141211130653.GG1768@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Mark Kirkwood wrote:

> In the most glaringly obvious (and possibly not at all helpful) analysis -
> this is because we are using "FOR KEY SHARE" instead of "FOR SHARE" in the
> newer code. Reverting this (see attached very basic patch) gets rid of the
> deadlock (tested in 9.5devel). However this is obviously a less than
> desirable solution, as it loses any possible concurrency improvement - that
> we worked hard to gain in the patch that added this new lock mode! We really
> want to figure out how to keep "FOR KEY SHARE" and have it *not* deadlock.

Yeah. The problem is that the INSERT in the fktab table takes a FOR KEY
SHARE lock in the pktab table. This puts the inserting process' Xid
into the tuple's Xmax, but allows concurrent UPDATE while the inserter
transaction is still in progress. If you use a FOR SHARE lock, the
UPDATE is disallowed until the inserter transaction is completed.

Having the inserter be live is what causes the heap_lock_tuple and
heap_update calls in the updater process to take the heavyweight tuple
lock.

I think what we should do here is NOT take the HW lock unless we're
going to sleep in heap_lock_tuple and heap_update. Right now we always
take it if HeapTupleSatisfiesUpdate returns HeapTupleBeingUpdated, even
if we end up not sleeping to wait for something else.

I'm going to experiment with that idea and see if it leads to a
solution. I tried the other idea yesterday (to keep the HW tuple lock
we acquire in heap_lock_tuple until heap_update is done) but aside from
being very complicated and bug-prone, it doesn't solve the problem
anyway.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message prasanna 2014-12-11 13:36:48 BUG #12204: Getting wrong results from full text search
Previous Message wangzhipengkmust 2014-12-11 08:35:10 BUG #12203: No password dialog when access the postgresql server