Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gI6Cl-00025i-Bh for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Nov 2018 06:11:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gI6Ab-0004Tz-6w for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Nov 2018 06:09:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gI6AZ-0004Ts-AJ for pgsql-hackers@lists.postgresql.org; Thu, 01 Nov 2018 06:09:29 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gI6AR-0006mQ-1T for pgsql-hackers@lists.postgresql.org; Thu, 01 Nov 2018 06:09:26 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 5E49F22480; Thu, 1 Nov 2018 02:09:17 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 01 Nov 2018 02:09:17 -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=fm3; bh=hcN1qt0nORiOWpMOQo7cd/6ejog Mj6I1sJ/MTPUjfJc=; b=x1357c5qCeJUvGRiVn8EEpBxR7jVYk4XVuMADjK5jI+ mUYQ76zW5+vj3TvCJ4LLIY2/ERMnqxV+xOsBxSTqVhpEQsInhy5GeWEx2R76QCG2 KLyE47aivHhFnd43vQHKSjptzoHP+h/Tp1nLjGZf4yjt17pdHICesK0fl7q+k8uV Bm9/mt5JSs+BT3pcCwPnXkC5z4+rOrg/PicVCA5p+c5+TguLQRvcs0e2VpioejxF NGAOu5mGhiHGKVfpXpF5ryMZ88FHpzkHDKtnCXBCkP374GgdGeIu8ohigg0sS1NM V3p/wOanHNYYz90tzILtxk/cACSqpbXG7IcY73sD9Fg== 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=hcN1qt 0nORiOWpMOQo7cd/6ejogMj6I1sJ/MTPUjfJc=; b=i1ORHRaglLefb4jWBG8RZ4 UZ4uSfwSJjzSiwjM3MdR7zBs4zW5errYd9pkSgoKW9TVXDW9VFsjqdvwPRdEEMq3 utV9v4evQ7t8VA2bo++n5zBWCFq6dgKQTVEDAJNQYE5X6FCiNvuFZ+QqFjkRED/a VZm9KJwgoGS19zHYJU1kiHGh0rHFYDrMT0AQJV704zQqmc6YkABLON9X5qNtY627 0NHVl9JWsBpvoGBjrFyir7FJWQqd1fhVY3Zm1UZdra6W+gLhCxkp7+5pj5Uo7GB9 SZ95zlfB2lD1VFz2ge+YaujnIcn3De+fCKFp9Lt3skfFCxnVCRG2zMP2wiodnd0A == 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 F0524E4430; Thu, 1 Nov 2018 02:09:14 -0400 (EDT) Date: Thu, 1 Nov 2018 15:09:11 +0900 From: Michael Paquier To: Alvaro Herrera Cc: Andres Freund , pgsql-hackers@lists.postgresql.org Subject: Re: pgsql: Avoid duplicate XIDs at recovery when building initial snapshot Message-ID: <20181101060911.GG1727@paquier.xyz> References: <20181022174155.wxxu2qt3bcrbrqy5@alap3.anarazel.de> <20181022221538.d73yxdypbmmzmi2l@alvherre.pgsql> <20181023014338.GA1658@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="X+8siUETKMkW99st" Content-Disposition: inline In-Reply-To: <20181023014338.GA1658@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --X+8siUETKMkW99st Content-Type: multipart/mixed; boundary="1giRMj6yz/+FOIRq" Content-Disposition: inline --1giRMj6yz/+FOIRq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Oct 23, 2018 at 10:43:38AM +0900, Michael Paquier wrote: > Well, following the same kind of thoughts, txid_current_snapshot() uses > sort_snapshot() to remove all the duplicates after fetching its data > from GetSnapshotData(), so wouldn't we want to do something about > removal of duplicates if dummy PGXACT entries are found while scanning > the ProcArray also in this case? What I would think we should do is not > only to patch GetRunningTransactionData() but also GetSnapshotData() so > as we don't have duplicates also in this case, and do things in such a > way that both code paths use the same logic, and that we don't need to > have sort_snapshot() anymore. That would be more costly though... My apologies it took a bit longer than I thought. I got a patch on my desk for a couple of days, and finally took the time to finish something which would address the concerns raised here. As long as we don't reach more than hundreds of thousands of entries, there is not going to be any performance impact. So what I do in the attached is to revert 1df21ddb, and then have GetRunningTransactionData() sort the XIDs in the snapshot and remove duplicates only if at least one dummy proc entry is found while scanning, for xids and subxids. This way, there is no need to impact most of the instance deployments with the extra sort/removal phase as most don't use two-phase transactions. The sorting done at recovery when initializing the standby snapshot still needs to happen of course. The patch is added to the upcoming CF for review. Thanks, -- Michael --1giRMj6yz/+FOIRq Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="duplicate-xid-snapshot-v1.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/= procarray.c index 908f62d37e..625c8ddf48 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -799,21 +799,10 @@ ProcArrayApplyRecoveryInfo(RunningTransactions runnin= g) qsort(xids, nxids, sizeof(TransactionId), xidComparator); =20 /* - * Add the sorted snapshot into KnownAssignedXids. The running-xacts - * snapshot may include duplicated xids because of prepared - * transactions, so ignore them. + * Add the sorted snapshot into KnownAssignedXids. */ for (i =3D 0; i < nxids; i++) - { - if (i > 0 && TransactionIdEquals(xids[i - 1], xids[i])) - { - elog(DEBUG1, - "found duplicated transaction %u for KnownAssignedXids insertion", - xids[i]); - continue; - } KnownAssignedXidsAdd(xids[i], xids[i], true); - } =20 KnownAssignedXidsDisplay(trace_recovery(DEBUG3)); } @@ -1924,10 +1913,9 @@ 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. + * If dummy entries for prepared transactions are found in the proc array + * make sure to discard any duplicates as a transaction finishing to + * prepare may cause two entries with the same TransactionId to be found. * * 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 @@ -1951,6 +1939,7 @@ GetRunningTransactionData(void) int count; int subcount; bool suboverflowed; + bool found_dummy =3D false; =20 Assert(!RecoveryInProgress()); =20 @@ -1999,6 +1988,7 @@ GetRunningTransactionData(void) { int pgprocno =3D arrayP->pgprocnos[index]; volatile PGXACT *pgxact =3D &allPgXact[pgprocno]; + volatile PGPROC *proc =3D &allProcs[pgprocno]; TransactionId xid; =20 /* Fetch xid just once - see GetNewTransactionId */ @@ -2022,6 +2012,13 @@ GetRunningTransactionData(void) if (pgxact->overflowed) suboverflowed =3D true; =20 + /* + * Mark that a dummy entry has been found, and that it is necessary to + * check for duplicates entries. + */ + if (proc->pid =3D=3D 0) + found_dummy =3D true; + /* * If we wished to exclude xids this would be the right place for it. * Procs with the PROC_IN_VACUUM flag set don't usually assign xids, @@ -2033,6 +2030,34 @@ GetRunningTransactionData(void) xids[count++] =3D xid; } =20 + /* + * If some dummy entries have been found, it is possible that a two-phase + * transaction just finishing to prepare has a duplicated entry with still + * the active transaction entry in the procArray. There is no need to + * remove any duplicates if no dummy entries have been found. + */ + if (found_dummy && count > 1) + { + TransactionId last =3D 0; + int idx1, + idx2; + int former_count; + + qsort(xids, count, sizeof(TransactionId), xidComparator); + + /* remove duplicate xids */ + former_count =3D count; + idx1 =3D idx2 =3D 0; + while (idx1 < former_count) + { + if (TransactionIdEquals(xids[idx1], last)) + last =3D xids[idx2++] =3D xids[idx1]; + else + count--; + idx1++; + } + } + /* * Spin over procArray collecting all subxids, but only if there hasn't * been a suboverflow. @@ -2065,6 +2090,40 @@ GetRunningTransactionData(void) */ } } + + /* + * Per the first array scanning, we know that there are dummy proc + * array entries, so sort and scan again the result, removing this + * time all subxids. + */ + if (found_dummy && subcount > 1) + { + TransactionId last =3D 0; + int idx1, + idx2; + int former_count; + + qsort(&xids[count], count - subcount, + sizeof(TransactionId), xidComparator); + + /* + * Remove duplicate subxids, beginning from the position they have + * been added. + */ + former_count =3D count; + idx1 =3D idx2 =3D count - subcount; + while (idx1 < former_count) + { + if (TransactionIdEquals(xids[idx1], last)) + last =3D xids[idx2++] =3D xids[idx1]; + else + { + count--; + subcount--; + } + idx1++; + } + } } =20 /* --1giRMj6yz/+FOIRq-- --X+8siUETKMkW99st Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlvamIcACgkQnvQgOdby QH1tkA//Xepq7LdmW3fo4u1ICXtSFg4JB9NwXoR1yWFETLOQ9zyE0xxHmi9Z5glH kIFdskU4enaCmw3PcYFmaZdnoCXgYq5mf1aHTXSPqJyvvJzSPbXTnkZkARm0Z3eK fm5ctRGUtFBCW+uGdCrZTngfRDaNLOlyVVGjUwttI0Ge1z3lZR6o9+5P9BKjt8pn 7FMT+S1LEbEwlx/WhnVhm83UJ474nZ0JtDtbrmhh2b0dEOO/Z9au5L2QeJlaOBk+ txNostGEdGP7kMbwzQ3XoPdi616qd35vI+gR1xT4RF+18QS+440vyo80+oJxy/gl TehFpn6LBgYYpCm8ClxH0MXerFhJYiMQqDdaGSmX7jAmfQnPMNWCXDacIzBQJQ9j 7YHzG1HxobHAQ7Ct0DkQfJAp5aZK0od6sh/VR6Iog2+btG29xVDaZLxFCNk4WdaI Th9vakTovAw1Q6Nk6yJTvVYP4DZBfiqjl60Xc4YI4KJjjHA9IbhHImrPouHmMDMN +6kcyjNFK46bXvbKwrL5nJW3HbwA/qnGaMZoiDzn8jJw4vfE/TTrJRBi4ylahFQr F35JlPD729aJCFF51wlXsdHZ/T94ee5bgnLawN3NEbrBPmPXx0VIc8DJZ0Calxpv hzWmLwvKTEoVLCdaD99CFZWXh5np9r7yGzy+L30cO1T3fXvsc0w= =93U+ -----END PGP SIGNATURE----- --X+8siUETKMkW99st--