Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z6AWY-0000kl-IS for pgsql-bugs@arkaria.postgresql.org; Sat, 20 Jun 2015 04:36:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Z6AWX-0005TA-Q9 for pgsql-bugs@arkaria.postgresql.org; Sat, 20 Jun 2015 04:36:57 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Z6AWV-0005T1-LP for pgsql-bugs@postgresql.org; Sat, 20 Jun 2015 04:36:55 +0000 Received: from smtp-auth.no-ip.com ([8.23.224.61] helo=out.smtp-auth.no-ip.com) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Z6AWS-0005jv-F2 for pgsql-bugs@postgresql.org; Sat, 20 Jun 2015 04:36:54 +0000 X-No-IP: alvh.no-ip.org@noip-smtp X-Report-Spam-To: abuse@no-ip.com Received: from alvin.alvh.no-ip.org (unknown [216.13.52.59]) (Authenticated sender: alvh.no-ip.org@noip-smtp) by smtp-auth.no-ip.com (Postfix) with ESMTPA id 7A476400714; Fri, 19 Jun 2015 21:36:51 -0700 (PDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 15E42595F; Sat, 20 Jun 2015 01:36:50 -0300 (CLT) Date: Sat, 20 Jun 2015 01:36:50 -0300 From: Alvaro Herrera To: Bruce Momjian Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8470: 9.3 locking/subtransaction performance regression Message-ID: <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> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="RMedoP2+Pr6Rq0N2" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20150410161704.GH4369@alvh.no-ip.org> User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --RMedoP2+Pr6Rq0N2 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 --RMedoP2+Pr6Rq0N2 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Optimize-locking-a-tuple-already-locked-by-another-s.patch" From 449df66162775adf27f5297f381f0ae199938064 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera 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 --RMedoP2+Pr6Rq0N2 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --RMedoP2+Pr6Rq0N2--