Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gAnVp-0003ve-U2 for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Oct 2018 02:49:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gAnVo-0007vX-7G for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Oct 2018 02:49:12 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gAnVn-0007vO-Ar for pgsql-hackers@lists.postgresql.org; Fri, 12 Oct 2018 02:49:12 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gAnVk-0005N7-2S for pgsql-hackers@postgresql.org; Fri, 12 Oct 2018 02:49:10 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 5149A21EE9; Thu, 11 Oct 2018 22:49:07 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 11 Oct 2018 22:49:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=+zn+dO4khvzfc03ISiQPfFsJBhn ftpZDcCFsRJ8xxZM=; b=ECns2DLXw20Rm0iEdqDEMae6qYiDWgOgpJyuRmqmDzK oNpEH/si6grTubGIaAQ+GDFSqzHeD/wxbfxbotlY0J8rjPTWGq/LOhV2BdzA3jFA DsVqJdiJvr+pH3yYIryk9s1lq+7UhzFQFXvgUEhl3YNgfNzOKfbsy0ixmboCXINL xMjLOHAiPeodSGqQcUhwkYVoE4vUM+10mMAVTZ6heruIsFzhwkUJJZxIe7hf3aST BiizGBHWP0ZedK7a1Ki0Xyq2DlGd5x4dMPX5c3DqLO9r/+KxnCYSvKQTkfv18NP3 JVEplsBZrjEHikw/FKc4+9FWScg2qc6aAYCp6jrS+TA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=+zn+dO 4khvzfc03ISiQPfFsJBhnftpZDcCFsRJ8xxZM=; b=b3UFRMSM5jOMjFL598j3vn 2xcyDF3OrmS7C5r0zFh9DsF4Du5T3n3aR8Qdd4tmfwNLAvr8G3Lci11e2XFPUbim jNCa2spvTEsdipFGzgWJ4VYblYiq55LMJc9jD2KiM0sw3uVhfVO4cDFTO0J3OI8p +/PGF0CSq5QpY2BzEWDHn5vJR1mWzlR928hpHzAgKTERWfUqVgU+IstHKJf4iORc DUrU3/K5l91hePFyClDaFHD3uJYptfV2Vbemc+bkJ7OOtmv5YaOplTo+xRpYOdaW j9SaoPlbb87NkMc5NO+sQUaDNYrVxIjW5GbHRVFqJNboQxDvmWXa/QrgObLkh7dA == X-ME-Sender: X-ME-Proxy: Received: from paquier.xyz (c137162.net61215.cablenet.ne.jp [61.215.137.162]) by mail.messagingengine.com (Postfix) with ESMTPA id 47BEDE4439; Thu, 11 Oct 2018 22:49:04 -0400 (EDT) Date: Fri, 12 Oct 2018 11:49:01 +0900 From: Michael Paquier To: Konstantin Knizhnik Cc: Andres Freund , pgsql-hackers@lists.postgresql.org, PostgreSQL Hackers Subject: Re: out-of-order XID insertion in KnownAssignedXids Message-ID: <20181012024901.GC30064@paquier.xyz> References: <581a80f7-3992-f319-302d-7c9a384d64f7@postgrespro.ru> <2A819787-9E67-41EF-B89A-A906996924E5@anarazel.de> <20181008163049.nvaui5kjrsav2ojn@alap3.anarazel.de> <20181009055900.GH2137@paquier.xyz> <20181009075258.GI2137@paquier.xyz> <1e1d56e2-b056-5fa1-bc89-a6bb3836a1c8@postgrespro.ru> <20181010022245.GB2204@paquier.xyz> <20181011090611.GC23570@paquier.xyz> <54e55450-0c87-9dd4-c773-8d2f2fb6054c@postgrespro.ru> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="cPi+lWm09sJ+d57q" Content-Disposition: inline In-Reply-To: <54e55450-0c87-9dd4-c773-8d2f2fb6054c@postgrespro.ru> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --cPi+lWm09sJ+d57q Content-Type: multipart/mixed; boundary="NklN7DEeGtkPCoo3" Content-Disposition: inline --NklN7DEeGtkPCoo3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Oct 11, 2018 at 08:04:11PM +0300, Konstantin Knizhnik wrote: > Proposed patch is attached. The problem I have with this patch doing the duplication removal and qsort work in LogCurrentRunningXacts is that it would still lock ProcArrayLock until the WAL record has been written with wal_level = logical. With many sessions and 2PC transactions running around, this would be a performance impact for any deployments every time a checkpoint happens or every time the bgwriter decide to log a standby snapshot. Hence I would go instead with the attached, which does the legwork at recovery, which is a one-time code path as you mentioned. Okay, this makes the recovery a bit longer but that's way better than impacting all deployments of Postgres, even those not using 2PC when normally running. And as the sorting phase already happens we just need to do something like the attached. One thing that we could also do for HEAD is to add in RunningTransactionsData if the transaction comes from a 2PC entry, and allow recovery to do more sanity checks. This would require a WAL format change. The proposed patch needs to be back-patched down to 9.3. -- Michael --NklN7DEeGtkPCoo3 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="duplicated-xids-recovery.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/= procarray.c index bf2f4dbed2..6317b856a1 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -799,10 +799,20 @@ ProcArrayApplyRecoveryInfo(RunningTransactions runnin= g) qsort(xids, nxids, sizeof(TransactionId), xidComparator); =20 /* - * Add the sorted snapshot into KnownAssignedXids + * Add the sorted snapshot into KnownAssignedXids. The running-xacts + * snapshot may include duplicated xids because of prepared + * transactions, so ignore them. */ for (i =3D 0; i < nxids; i++) + { + if (i > 0 && TransactionIdEquals(xids[i - 1], xids[i])) + { + elog(DEBUG1, "found duplicated transaction %u from prepared transactio= n for KnownAssignedXids", + xids[i]); + continue; + } KnownAssignedXidsAdd(xids[i], xids[i], true); + } =20 KnownAssignedXidsDisplay(trace_recovery(DEBUG3)); } @@ -1898,7 +1908,8 @@ ProcArrayInstallRestoredXmin(TransactionId xmin, PGPR= OC *proc) * GetRunningTransactionData -- returns information about running transact= ions. * * Similar to GetSnapshotData but returns more information. We include - * all PGXACTs with an assigned TransactionId, even VACUUM processes. + * all PGXACTs with an assigned TransactionId, even VACUUM processes and + * prepared transactions. * * We acquire XidGenLock and ProcArrayLock, but the caller is responsible = for * releasing them. Acquiring XidGenLock ensures that no new XIDs enter the= proc @@ -1912,6 +1923,11 @@ ProcArrayInstallRestoredXmin(TransactionId xmin, PGP= ROC *proc) * This is never executed during recovery so there is no need to look at * KnownAssignedXids. * + * Dummy PGXACTs from prepared transaction are included, meaning that this + * may return entries with duplicated TransactionId values coming from + * transaction finishing to prepare. Nothing is done about duplicated + * entries here to not hold on ProcArrayLock more than necessary. + * * We don't worry about updating other counters, we want to keep this as * simple as possible and leave GetSnapshotData() as the primary code for * that bookkeeping. --NklN7DEeGtkPCoo3-- --cPi+lWm09sJ+d57q Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlvAC50ACgkQnvQgOdby QH3OHw//Z7eXuXg7RSIYrpa1jPN32ZVyFdnqLimpvuXUpZIH6N//+66oDicCJ2+k t75Ly9NSwXACXLAbeOd05izjiDmS9zh+Qxeo2o89fpKpHOYLd13bOFwy1QAUUSnx 8g30lMtkD+C8dFBfvRz61/FfulGLTGGT2PRk8tXeR5ogjNzw4dod3YPKJeFPS+XS 9Ors96rAvSNNncrKz+iqipFMha1ye4hnragyOX9lO772Wz+fLSliLE1BP0fGe37+ 7LP8TbUzhl4FWWMx3HwRC29NtpBuOxC/NUIn25Y05cseus+6Z8qILGCINROAjqB6 8b7I63bN1jqkdDVX5ahUnL3MpXGGp9Xm2L/HbyzzCXhuO417dNwgl66GYhes525j wnMGYZdW3gz9X6MRAXRe3X02qwEXw/jD8RikxsSGGOV90JqIb2jCGRFxV+N26zW3 fl4EoH7Lj1bp/I2VLdTPHXHv9/efd0zQ/RO6kxrd+LhUN7gOE07BP0eAGvogKjHc pbWtYLKRrI1IbcRMb4QSVadmD5xiGc89AHuW8f9nwmtE472Hjp3aR2vkwUO9BYdr LuPFLDY7xDC9mloi5AaV0IywHGJeVcMRq1siWJvzCa7MH/Oeiw34m2YflEpyO8VZ ZLOZjm+HawYlFg/GLJoWfWjzZJvY4Emb88HoaBS38H2JFw1MFWs= =hQK+ -----END PGP SIGNATURE----- --cPi+lWm09sJ+d57q--