Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A0066633753 for ; Mon, 19 Apr 2010 06:06:17 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 61130-01-3 for ; Mon, 19 Apr 2010 09:06:06 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outmail148092.authsmtp.com (outmail148092.authsmtp.com [62.13.148.92]) by mail.postgresql.org (Postfix) with ESMTP id 4E34B633216 for ; Mon, 19 Apr 2010 06:05:32 -0300 (ADT) Received: from mail-c194.authsmtp.com (mail-c194.authsmtp.com [62.13.128.121]) by punt5.authsmtp.com (8.14.2/8.14.2/Kp) with ESMTP id o3J95QEh076811; Mon, 19 Apr 2010 10:05:26 +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 o3J95MqY060211; Mon, 19 Apr 2010 10:05:23 +0100 (BST) Subject: Re: [COMMITTERS] pgsql: Tune GetSnapshotData() during Hot Standby by avoiding loop From: Simon Riggs To: Heikki Linnakangas Cc: pgsql-hackers@postgresql.org In-Reply-To: <4BCC1644.70008@enterprisedb.com> References: <20100418180608.16F067541D1@cvs.postgresql.org> <4BCC07F1.6040405@enterprisedb.com> <1271664332.8305.17430.camel@ebony> <4BCC1644.70008@enterprisedb.com> Content-Type: text/plain Date: Mon, 19 Apr 2010 10:05:22 +0100 Message-Id: <1271667922.8305.17650.camel@ebony> Mime-Version: 1.0 X-Mailer: Evolution 2.26.1 Content-Transfer-Encoding: 7bit X-Server-Quench: afee3882-4b92-11df-80b9-0022640b883e X-AuthReport-Spam: If SPAM / abuse - report it at: http://www.authsmtp.com/abuse X-AuthRoute: OCdxZQATClZOTQEd DAteCiN5VAwpPBRK HVkIKg5MJUcNSQVJ NksadBtFaQxbbElD HGQLWlREUV17WWZ/ aQkfZQ1DY0tOQQRv VlZIS1BNEQ1vHhxP WR1kWxwPNmxGfn5y ZEcsWHFTWgorIxMo QUZcF3ACN25idWEe BBZFfwMGeR5Kfh1H bFR/BnsFYjBJBC9q VzwYGBUUHAljBR5u d0kVIkkLRRRDIiM6 SlgtECkUG0QdXCU0 MzAvNldcXQ4dL18z N1RpeVUEeyQOAwtS FVAvSCVIb1AfQCsn DAVVFUoZFyU1 X-Authentic-SMTP: 61633235383639.1015:706/Kp X-AuthFastPath: 255 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201004/797 X-Sequence-Number: 160909 On Mon, 2010-04-19 at 11:37 +0300, Heikki Linnakangas wrote: > Simon Riggs wrote: > > On Mon, 2010-04-19 at 10:36 +0300, Heikki Linnakangas wrote: > >> Simon Riggs wrote: > >>> Log Message: > >>> ----------- > >>> Tune GetSnapshotData() during Hot Standby by avoiding loop > >>> through normal backends. Makes code clearer also, since we > >>> avoid various Assert()s. Performance of snapshots taken > >>> during recovery no longer depends upon number of read-only > >>> backends. > >> I think there's a little race condition there. > >> snapshot->takenDuringRecovery is set before acquiring ProcArrayLock, so > >> it's possible that by the time we acquire the lock, we're no longer in > >> recovery. So this can happen: > >> > >> 1. Backend A starts to take a snapshot, while we're still in recovery. > >> takenDuringRecovery is assigned true. > >> 2. Recovery ends, and a normal transaction X begins in backend B. > >> 3. A skips scanning ProcArray because takenDuringRecovery is true. > >> > >> The snapshot doesn't include X, so any changes done in that transaction > >> will not be visible to the snapshot while the transaction is still > >> running, but will be after it commits. > >> > >> Of course, it's highly improbable for 2. to happen, but it's there. > > > > The order of events is as you say, though I don't see the problem. The > > new xids will be beyond xmax and would be filtered out even if we did > > scan the procs, so they will be treated as running, which they are. Xmax > > will not have advanced since that relies on completed transactions, not > > started ones. > > Good point. However, it is theoretically possible that yet another > transaction starts and commits in that same window, bumping xmax. If the snapshotting backend (#1) froze at the exact point between one CPU instruction and the next, during which time recovery ends, two new sessions connect, two new write transactions start (#2 and #3), they begin to write data and so assign xids, then write WAL, then the #3 writes commit record into WAL, flushes disk (maybe), updates clog and then is granted procarraylock in exclusive mode before snapshotting backend attempts to do so, while #2 keeps running. Yes, it does seem theoretically possible, at that one exact point in time, never to be repeated. It doesn't seem to be something we should place highly on the list of events we need protection from, does it? -- Simon Riggs www.2ndQuadrant.com