Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 5DF99632DF0 for ; Mon, 19 Apr 2010 15:18:22 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 67288-03-5 for ; Mon, 19 Apr 2010 18:18:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outmail148094.authsmtp.co.uk (outmail148094.authsmtp.co.uk [62.13.148.94]) by mail.postgresql.org (Postfix) with ESMTP id 540F96332F9 for ; Mon, 19 Apr 2010 15:18:05 -0300 (ADT) Received: from mail-c193.authsmtp.com (mail-c193.authsmtp.com [62.13.128.118]) by punt7.authsmtp.com (8.14.2/8.14.2) with ESMTP id o3JIHxwC058341; Mon, 19 Apr 2010 19:17:59 +0100 (BST) Received: from [192.168.0.2] (88-110-156-192.dynamic.dsl.as9105.com [88.110.156.192]) (authenticated bits=0) by mail.authsmtp.com (8.14.2/8.14.2) with ESMTP id o3JIHvj9010904; Mon, 19 Apr 2010 19:17:57 +0100 (BST) Subject: Re: Re: [COMMITTERS] pgsql: Tune GetSnapshotData() during Hot Standby by avoiding loop From: Simon Riggs To: Tom Lane Cc: Heikki Linnakangas , Robert Haas , pgsql-hackers@postgresql.org In-Reply-To: <14068.1271688932@sss.pgh.pa.us> References: <20100418180608.16F067541D1@cvs.postgresql.org> <4BCC07F1.6040405@enterprisedb.com> <1271664332.8305.17430.camel@ebony> <4BCC1644.70008@enterprisedb.com> <1271667922.8305.17650.camel@ebony> <13439.1271687068@sss.pgh.pa.us> <1271687489.8305.18879.camel@ebony> <4BCC6C37.9090703@enterprisedb.com> <1271688445.8305.18966.camel@ebony> <14068.1271688932@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="=-+6v72DCFilhtjCQAVbaW" Date: Mon, 19 Apr 2010 19:17:56 +0100 Message-Id: <1271701076.8305.19751.camel@ebony> Mime-Version: 1.0 X-Mailer: Evolution 2.26.1 X-Server-Quench: e1687d1e-4bdf-11df-97bb-002264978518 X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZOTQEd DAteCiN5VAwpPBRK HVkIKg5MJUcNSQVJ NksachtFaQxbbElD HGQLWlREUVR7WGR/ bg0fZQ1DY0tOQQRv VlZIS1BNEQ1vHhxP WR1kK2E0LQxGeX55 ZUIsX3VYXkw0IEd7 SkkAFDgbYTU0Pn0e URQOagpTIlFXfh9H aFZ7XXQFZGQPKBg1 TUcAHxkaHhhlExEd eAxfZWE5ZmMuPwIA aio5KH03ElYcQ3d7 EwEgJxYzEVoqNEwq KlsmRX4RLxZSS28W E1xXASlWb3kGW2IQ EQpcUUQPWDdMCS5C BRwkIxlHSjhVXTIw X-Authentic-SMTP: 61633235383639.1014:706/Kp X-AuthFastPath: 0 (Was 255) X-AuthVirus-Status: No virus detected - but ensure you scan with your own anti-virus system. X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.023 tagged_above=-10 required=5 tests=AWL=-0.123, BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201004/826 X-Sequence-Number: 160938 --=-+6v72DCFilhtjCQAVbaW Content-Type: text/plain Content-Transfer-Encoding: 7bit On Mon, 2010-04-19 at 10:55 -0400, Tom Lane wrote: > Simon Riggs writes: > > On Mon, 2010-04-19 at 17:44 +0300, Heikki Linnakangas wrote: > >>> Choices are > >>> 1. Check RecoveryInProgress() once outside of lock, plus wild rumour of > >>> Murphy > >>> 2. Check RecoveryInProgress() before and after holding lock > >>> 3. Check RecoveryInProgress() while holding lock > >> > >> 4. Check RecoveryInProgress() once outside of lock, and scan the > >> ProcArray anyway, just in case. That's what we did before this patch. > >> Document that takenDuringRecovery == true means that the snapshot was > >> most likely taken during recovery, but there is some race conditions > >> where takenDuringRecovery is true even though the snapshot was taken > >> just after recovery finished. AFAICS all of the other current uses of > >> takenDuringRecovery work fine with that. > > > Checking RecoveryInProgress() is much cheaper than scanning the whole > > ProcArray, so (4) is definitely worse than 1-3. > > If the lock we're talking about is an LWLock, #3 is okay. If it's a > spinlock, not so much. Just committed the following patch to implement option #3. We test RecoveryInProgress() after the LWLockAcquire rather than before. -- Simon Riggs www.2ndQuadrant.com --=-+6v72DCFilhtjCQAVbaW Content-Disposition: attachment; filename="switcheroo.patch" Content-Type: text/x-patch; name="switcheroo.patch"; charset="UTF-8" Content-Transfer-Encoding: 7bit *** a/src/backend/storage/ipc/procarray.c --- b/src/backend/storage/ipc/procarray.c *************** *** 1074,1081 **** GetSnapshotData(Snapshot snapshot) errmsg("out of memory"))); } - snapshot->takenDuringRecovery = RecoveryInProgress(); - /* * It is sufficient to get shared lock on ProcArrayLock, even if we are * going to set MyProc->xmin. --- 1074,1079 ---- *************** *** 1091,1098 **** GetSnapshotData(Snapshot snapshot) globalxmin = xmin = xmax; /* ! * If in recovery get any known assigned xids. */ if (!snapshot->takenDuringRecovery) { /* --- 1089,1103 ---- globalxmin = xmin = xmax; /* ! * If we're in recovery then snapshot data comes from a different place, ! * so decide which route we take before grab the lock. It is possible ! * for recovery to end before we finish taking snapshot, and for newly ! * assigned transaction ids to be added to the procarray. Xmax cannot ! * change while we hold ProcArrayLock, so those newly added transaction ! * ids would be filtered away, so we need not be concerned about them. */ + snapshot->takenDuringRecovery = RecoveryInProgress(); + if (!snapshot->takenDuringRecovery) { /* --=-+6v72DCFilhtjCQAVbaW--