Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W0BK0-0007Un-P1 for pgsql-bugs@arkaria.postgresql.org; Mon, 06 Jan 2014 14:38:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W0BK0-0000GH-7e for pgsql-bugs@arkaria.postgresql.org; Mon, 06 Jan 2014 14:38:28 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W0BJy-0000EV-Fb for pgsql-bugs@postgresql.org; Mon, 06 Jan 2014 14:38:26 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W0BJu-0001sW-8q for pgsql-bugs@postgresql.org; Mon, 06 Jan 2014 14:38:25 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 961A2285E5; Mon, 6 Jan 2014 06:38:21 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 8DDB118444; Mon, 6 Jan 2014 06:38:21 -0800 (PST) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 4C737C0046; Mon, 6 Jan 2014 06:38:54 -0800 (PST) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA; Mon, 6 Jan 2014 06:38:51 -0800 (PST) Message-ID: <52CABFD8.8090101@vmware.com> Date: Mon, 06 Jan 2014 16:38:16 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131005 Icedove/17.0.9 MIME-Version: 1.0 To: Tomonari Katsumata CC: Tomonari Katsumata , pgsql-bugs@postgresql.org Subject: Re: BUG #8686: Standby could not restart. References: <52B46D0D.2070505@vmware.com> In-Reply-To: Content-Type: multipart/mixed; boundary="------------080802070104040108000208" X-Pg-Spam-Score: -7.2 (-------) 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 This is a multi-part message in MIME format. --------------080802070104040108000208 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 12/23/2013 08:15 AM, Tomonari Katsumata wrote: >> /* >>> * Initialize shared replayEndRecPtr, >>> lastReplayedEndRecPtr, and >>> * recoveryLastXTime. >>> * >>> * This is slightly confusing if we're starting from an >>> online >>> * checkpoint; we've just read and replayed the >>> checkpoint record, but >>> * we're going to start replay from its redo pointer, >>> which precedes >>> * the location of the checkpoint record itself. So even >>> though the >>> * last record we've replayed is indeed ReadRecPtr, we >>> haven't >>> * replayed all the preceding records yet. That's OK for >>> the current >>> * use of these variables. >>> */ >>> SpinLockAcquire(&xlogctl->info_lck); >>> xlogctl->replayEndRecPtr = ReadRecPtr; >>> xlogctl->lastReplayedEndRecPtr = EndRecPtr; >>> xlogctl->recoveryLastXTime = 0; >>> xlogctl->currentChunkStartTime = 0; >>> xlogctl->recoveryPause = false; >>> SpinLockRelease(&xlogctl->info_lck); >>> >> >> I think we need to fix that confusion. Your patch will do it by not >> setting EndRecPtr yet; that fixes the bug, but leaves those variables in a >> slightly strange state; I'm not sure what EndRecPtr points to in that case >> (0 ?), but ReadRecPtr would be set I guess. >> > Yes, the values were set like below. > ReadRecPtr:1/8E7F0B0 > EndRecPtr:0/0 > > > >> >> Perhaps we should reset replayEndRecPtr and lastReplayedEndRecPtr to the >> REDO point here, instead of ReadRecPtr/EndRecPtr. > > I made another patch. > I added a ReadRecord to make sure the REDO location is present or not. > The similar process are done when we use backup_label. > > Because the ReadRecord returns a record already read, > I set ReadRecPtr of the record to EndRecPtr. > And also I set record->xl_prev to ReadRecPtr. > As you said, it also worked fine. > > I'm not sure we should do same thing when crash recovery occurs, but now I > added the process when archive recovery is needed. > > Please see attached patch. Hmm. That would still initialize lastReplayedEndRecPtr to the checkpoint record, when you do crash recovery (or begin archive recovery from a filesystem snapshot, where you perform crash recovery before starting to read the archive). I'm not very comfortable with that, even though I don't see an immediate problem with it. I also noticed that CheckRecoveryConsistency() compares backupEndPoint with EndRecPtr, but minRecoveryPoint with lastReplayedEndRecPtr. That's correct as the code stands, but it's an accident waiting to happen: if you called CheckRecoveryConsistency() after reading a record with ReadRecord(), but before fully replaying it, it might conclude that it's reached the backup end location one record too early. And it's inconsistent, anyway. I propose the attached patch. I haven't tested it, but I think it's a slightly more robust fix. - Heikki --------------080802070104040108000208 Content-Type: text/x-diff; name="fix-lastReplayedEndRecPtr-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-lastReplayedEndRecPtr-1.patch" diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 43a0416..5661a17 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -6684,21 +6684,13 @@ StartupXLOG(void) } /* - * Initialize shared replayEndRecPtr, lastReplayedEndRecPtr, and - * recoveryLastXTime. - * - * This is slightly confusing if we're starting from an online - * checkpoint; we've just read and replayed the checkpoint record, but - * we're going to start replay from its redo pointer, which precedes - * the location of the checkpoint record itself. So even though the - * last record we've replayed is indeed ReadRecPtr, we haven't - * replayed all the preceding records yet. That's OK for the current - * use of these variables. + * Initialize shared variables for tracking progress of WAL replay. */ SpinLockAcquire(&xlogctl->info_lck); - xlogctl->replayEndRecPtr = ReadRecPtr; + /* pretend we had just replayed the record before the REDO location */ + xlogctl->replayEndRecPtr = checkpoint.redo; xlogctl->replayEndTLI = ThisTimeLineID; - xlogctl->lastReplayedEndRecPtr = EndRecPtr; + xlogctl->lastReplayedEndRecPtr = checkpoint.redo; xlogctl->lastReplayedTLI = ThisTimeLineID; xlogctl->recoveryLastXTime = 0; xlogctl->currentChunkStartTime = 0; @@ -7388,6 +7380,8 @@ StartupXLOG(void) static void CheckRecoveryConsistency(void) { + XLogRecPtr lastReplayedEndRecPtr; + /* * During crash recovery, we don't reach a consistent state until we've * replayed all the WAL. @@ -7396,10 +7390,16 @@ CheckRecoveryConsistency(void) return; /* + * assume that we are called in the startup process, and hence don't need + * a lock to read lastReplayedEndRecPtr + */ + lastReplayedEndRecPtr = XLogCtl->lastReplayedEndRecPtr; + + /* * Have we reached the point where our base backup was completed? */ if (!XLogRecPtrIsInvalid(ControlFile->backupEndPoint) && - ControlFile->backupEndPoint <= EndRecPtr) + ControlFile->backupEndPoint <= lastReplayedEndRecPtr) { /* * We have reached the end of base backup, as indicated by pg_control. @@ -7412,8 +7412,8 @@ CheckRecoveryConsistency(void) LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); - if (ControlFile->minRecoveryPoint < EndRecPtr) - ControlFile->minRecoveryPoint = EndRecPtr; + if (ControlFile->minRecoveryPoint < lastReplayedEndRecPtr) + ControlFile->minRecoveryPoint = lastReplayedEndRecPtr; ControlFile->backupStartPoint = InvalidXLogRecPtr; ControlFile->backupEndPoint = InvalidXLogRecPtr; @@ -7431,7 +7431,7 @@ CheckRecoveryConsistency(void) * consistent yet. */ if (!reachedConsistency && !ControlFile->backupEndRequired && - minRecoveryPoint <= XLogCtl->lastReplayedEndRecPtr && + minRecoveryPoint <= lastReplayedEndRecPtr && XLogRecPtrIsInvalid(ControlFile->backupStartPoint)) { /* @@ -7443,8 +7443,8 @@ CheckRecoveryConsistency(void) reachedConsistency = true; ereport(LOG, (errmsg("consistent recovery state reached at %X/%X", - (uint32) (XLogCtl->lastReplayedEndRecPtr >> 32), - (uint32) XLogCtl->lastReplayedEndRecPtr))); + (uint32) (lastReplayedEndRecPtr >> 32), + (uint32) lastReplayedEndRecPtr))); } /* --------------080802070104040108000208 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 --------------080802070104040108000208--