pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Bruce Momjian <bruce@momjian.us>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8470: 9.3 locking/subtransaction performance regression
Date: Sat, 20 Jun 2015 01:36:50 -0300
Message-ID: <20150620043650.GX133018@postgresql.org> (raw)
In-Reply-To: <20150410161704.GH4369@alvh.no-ip.org>
References: <20131223155603.GI22570@eldon.alvh.no-ip.org>
	<20131223162903.GJ22570@eldon.alvh.no-ip.org>
	<20131223195300.GK22570@eldon.alvh.no-ip.org>
	<20131230181151.GG12910@alap2.anarazel.de>
	<20131231163418.GW22570@eldon.alvh.no-ip.org>
	<20131231202753.GY22570@eldon.alvh.no-ip.org>
	<20140114124305.GA28072@saarenmaa.fi>
	<20150105172318.GM1457@alvh.no-ip.org>
	<20150402005616.GB29404@momjian.us>
	<20150410161704.GH4369@alvh.no-ip.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Alvaro Herrera wrote:

> The main catch of the "simple" formulation of the patch is that we do
> the new GetMultiXactIdMembers call with the buffer lock held, which is a
> horrible idea from a concurrency point of view; it will make many cases
> where the optimization doesn't apply a lot slower.  I think with some
> extra contortions we could fix that problem, but it's already quite ugly
> that we have a duplicate check for the are-we-already-a-multixact-locker
> so I reject the idea that this seemingly simple patch is any good.  I
> much prefer the complex formulation, which is what I had to start with,
> and makes thing a bit less unclear(*).

Here's an updated version of this patch.  This applies to 9.3 and 9.4 and
restores performance to what's in master, while being much less invasive
than what was committed to master.  But more importantly, what it does
is reduce the typical size of multixacts.

Since the size of multixacts is so critical with regards to the problems
of members overrun, I think it is a good idea to reduce it.  In master
we already have an equivalent of this optimization.

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

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

0001-Optimize-locking-a-tuple-already-locked-by-another-s.patch (text/x-diff, 3.8K) download From 449df66162775adf27f5297f381f0ae199938064 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera <alvherre@alvh.no-ip.org> Date: Fri, 10 Apr 2015 12:46:56 -0300 Subject: [PATCH] Optimize locking a tuple already locked by another subxact This is equivalent to the fix in 27846f02c176eebe7e08ce51e, except less invasive: we don't change the API of HeapTupleSatisfiesUpdate here, but instead cope with the possibility by checking for multixacts separately. This patch, for 9.3 and 9.4, is much simpler and restores performance almost to what it was in 9.2 for the test case supplied by Oskari Saarenmaa in bug #8470. --- src/backend/access/heap/heapam.c | 70 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 2209db1..70a084c 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -4134,6 +4134,7 @@ heap_lock_tuple(Relation relation, HeapTuple tuple, new_infomask, new_infomask2; bool have_tuple_lock = false; + bool checked_our_multi = false; *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid)); LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); @@ -4535,9 +4536,8 @@ failed: /* * We might already hold the desired lock (or stronger), possibly under a * different subtransaction of the current top transaction. If so, there - * is no need to change state or issue a WAL record. We already handled - * the case where this is true for xmax being a MultiXactId, so now check - * for cases where it is a plain TransactionId. + * is no need to change state or issue a WAL record. First check + * for cases where xmax is a plain TransactionId. * * Note in particular that this covers the case where we already hold * exclusive lock on the tuple and the caller only wants key share or @@ -4564,6 +4564,70 @@ failed: } /* + * Now check the multixact case. It was already checked above in the + * HeapTupleBeingUpdated case, but it's possible to get + * HeapTupleMayBeUpdated from HTSU in some cases. + */ + if (!checked_our_multi && + (old_infomask & HEAP_XMAX_IS_MULTI)) + { + int nmembers; + MultiXactMember *members; + uint16 infomask; + TransactionId xwait; + + /* must copy state data before unlocking buffer */ + infomask = tuple->t_data->t_infomask; + xwait = HeapTupleHeaderGetRawXmax(tuple->t_data); + + LockBuffer(*buffer, BUFFER_LOCK_UNLOCK); + + nmembers = GetMultiXactIdMembers(xmax, &members, + !(infomask & HEAP_LOCK_MASK) && + HEAP_XMAX_IS_LOCKED_ONLY(infomask)); + if (nmembers > 0) + { + int i; + + for (i = 0; i < nmembers; i++) + { + if (TransactionIdIsCurrentTransactionId(members[i].xid)) + { + LockTupleMode membermode; + + membermode = TUPLOCK_from_mxstatus(members[i].status); + + if (membermode >= mode) + { + if (have_tuple_lock) + UnlockTupleTuplock(relation, tid, mode); + + pfree(members); + return HeapTupleMayBeUpdated; + } + } + } + + pfree(members); + } + + /* make sure to only do this once */ + checked_our_multi = true; + + /* + * If we reach this point, then we know the optimization doesn't apply + * and we need to fall through to lock the tuple. However, the xmax + * might have changed in the meantime, so recheck and start again if so. + */ + LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE); + if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) || + !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data), + xwait)) + goto l3; + + } + + /* * If this is the first possibly-multixact-able operation in the current * transaction, set my per-backend OldestMemberMXactId setting. We can be * certain that the transaction will never become a member of any older -- 2.1.4

view thread (26+ messages)

Message-ID: <20150620043650.GX133018@postgresql.org>
Permalink:  ../20150620043650.GX133018@postgresql.org/
Also on:    postgresql.org/message-id/20150620043650.GX133018@postgresql.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-bugs@postgresql.org
  Cc: alvherre@2ndquadrant.com, bruce@momjian.us
  Subject: Re: BUG #8470: 9.3 locking/subtransaction performance regression
  In-Reply-To: <20150620043650.GX133018@postgresql.org>
  References: <20131223155603.GI22570@eldon.alvh.no-ip.org> <20131223162903.GJ22570@eldon.alvh.no-ip.org> <20131223195300.GK22570@eldon.alvh.no-ip.org> <20131230181151.GG12910@alap2.anarazel.de> <20131231163418.GW22570@eldon.alvh.no-ip.org> <20131231202753.GY22570@eldon.alvh.no-ip.org> <20140114124305.GA28072@saarenmaa.fi> <20150105172318.GM1457@alvh.no-ip.org> <20150402005616.GB29404@momjian.us> <20150410161704.GH4369@alvh.no-ip.org> <20150620043650.GX133018@postgresql.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