Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TC8QU-0003vG-1M for pgsql-bugs@postgresql.org; Thu, 13 Sep 2012 12:21:46 +0000 Received: from mail-lpp01m010-f46.google.com ([209.85.215.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TC8QQ-0004l2-93 for pgsql-bugs@postgresql.org; Thu, 13 Sep 2012 12:21:45 +0000 Received: by lagy9 with SMTP id y9so1866370lag.19 for ; Thu, 13 Sep 2012 05:21:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:reply-to:user-agent:mime-version:to:cc :subject:references:in-reply-to:content-type; bh=MeStjBrZ92TBb7gNLyP3bNsqUvALsPX/tKOpa0RRQm0=; b=JZmCJ2aj1t7gty1FZpWwp6YXZw9gnzS2T5578uZyWVAR7HDaJMsRoIT4vgCekgbTc/ DcZciMRssY04TNkZ4t/lTRws3qEqHQ+chMNa/HbpQjWxmbr9m5NzWK5ejHZKf87/H1EN 0FAcA9NET5kTcT88EGq3kf6RjeGM6mv6+pMN+gUH0XGDQxfgFlvVTl23aP6j4F7rJiBx JGbTWMhKFTab+lGdz8+4EVWEvfuKXoGPByMNv+RllLuspHu+KpVnmVd6NYe5uG8v2RBn LjShTb90Yp4eYA2UYRa9hxPu0B/jxYorc6OkBgxf1sncY7id5d9750NZ8Eis4w73+mor uJ6w== Received: by 10.152.123.133 with SMTP id ma5mr1814023lab.8.1347538901081; Thu, 13 Sep 2012 05:21:41 -0700 (PDT) Received: from [192.168.1.102] (dsl-hkibrasgw2-fedadf00-197.dhcp.inet.fi. [80.223.218.197]) by mx.google.com with ESMTPS id ly17sm23199075lab.2.2012.09.13.05.21.39 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 13 Sep 2012 05:21:40 -0700 (PDT) Message-ID: <5051CFD2.60103@iki.fi> Date: Thu, 13 Sep 2012 15:21:38 +0300 From: Heikki Linnakangas Reply-To: hlinnaka@iki.fi User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.6esrpre) Gecko/20120817 Icedove/10.0.6 MIME-Version: 1.0 To: Fujii Masao CC: amit.kapila@huawei.com, pgsql-bugs@postgresql.org Subject: Re: BUG #7533: Client is not able to connect cascade standby incase basebackup is taken from hot standby References: In-Reply-To: Content-Type: multipart/mixed; boundary="------------090901060809020608020202" X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201209/114 X-Sequence-Number: 34109 This is a multi-part message in MIME format. --------------090901060809020608020202 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12.09.2012 22:03, Fujii Masao wrote: > On Wed, Sep 12, 2012 at 8:47 PM, wrote: >> The following bug has been logged on the website: >> >> Bug reference: 7533 >> Logged by: Amit Kapila >> Email address: amit.kapila@huawei.com >> PostgreSQL version: 9.2.0 >> Operating system: Suse >> Description: >> >> M host is primary, S host is standby and CS host is cascaded standby. >> >> 1.Set up postgresql-9.2beta2/RC1 on all hosts. >> 2.Execute command initdb on host M to create fresh database. >> 3.Modify the configure file postgresql.conf on host M like this: >> listen_addresses = 'M' >> port = 15210 >> wal_level = hot_standby >> max_wal_senders = 4 >> hot_standby = on >> 4.modify the configure file pg_hba.conf on host M like this: >> host replication repl M/24 md5 >> 5.Start the server on host M as primary. >> 6.Connect one client to primary server and create a user ‘repl’ >> Create user repl superuser password '123'; >> 7.Use the command pg_basebackup on the host S to retrieve database of >> primary host >> pg_basebackup -D /opt/t38917/data -F p -x fetch -c fast -l repl_backup -P >> -v -h M -p 15210 -U repl –W >> 8. Copy one recovery.conf.sample from share folder of package to database >> folder of the host S. Then rename this file to recovery.conf >> 9.Modify the file recovery.conf on host S as below: >> standby_mode = on >> primary_conninfo = 'host=M port=15210 user=repl password=123' >> 10. Modify the file postgresql.conf on host S as follow: >> listen_addresses = 'S' >> 11.Start the server on host S as standby server. >> 12.Use the command pg_basebackup on the host CS to retrieve database of >> standby host >> pg_basebackup -D /opt/t38917/data -F p -x fetch -c fast -l repl_backup -P >> -v -h M -p 15210 -U repl –W >> 13.Modify the file recovery.conf on host CS as below: >> standby_mode = on >> primary_conninfo = 'host=S port=15210 user=repl password=123' >> 14. Modify the file postgresql.conf on host S as follow: >> listen_addresses = 'CS' >> 15.Start the server on host CS as Cascaded standby server node. >> 16. Try to connect a client to host CS but it gives error as: >> FATAL: the database system is starting up > > This procedures didn't reproduce the problem in HEAD. But when I restarted > the master server between the step 11 and 12, I was able to reproduce the > problem. > >> Observations related to bug >> ------------------------------ >> In the above scenario it is observed that Start-up process has read all data >> (in our defect scenario minRecoveryPoint is 5016220) till the position >> 5016220 and then it goes and check for recovery consistency by following >> condition in function CheckRecoveryConsistency: >> if (!reachedConsistency&& >> XLByteLE(minRecoveryPoint, EndRecPtr)&& >> XLogRecPtrIsInvalid(ControlFile->backupStartPoint)) >> >> At this point first two conditions are true but last condition is not true >> because still redo has not been applied and hence backupStartPoint has not >> been reset. So it does not signal postmaster regarding consistent stage. >> After this it goes and applies the redo and then reset backupStartPoint and >> then it goes to read next set of record. Since all records have been already >> read, so it starts waiting for the new record from the Standby node. But >> since there is no new record from Standby node coming so it keeps waiting >> for that and it does not get chance to recheck the recovery consistent >> level. And hence client connection does not get allowed. > > If cascaded standby starts a recovery at a normal checkpoint record, > this problem will not happen. Because if wal_level is set to hot_standby, > XLOG_RUNNING_XACTS WAL record always follows after the normal > checkpont record. So while XLOG_RUNNING_XACTS record is being replayed, > ControlFile->backupStartPoint can be reset, and then cascaded standby > can pass through the consistency test. > > The problem happens when cascaded standby starts a recovery at a > shutdown checkpoint record. In this case, no WAL record might follow > the checkpoint one yet. So, after replaying the shutdown checkpoint > record, cascaded standby needs to wait for new WAL record to appear > before reaching the code block for resetting ControlFile->backupStartPoint. > The cascaded standby cannot reach a consistent state and a client cannot > connect to the cascaded standby until new WAL has arrived. > > Attached patch will fix the problem. In this patch, if recovery is > beginning at a shutdown checkpoint record, any ControlFile fields > (like backupStartPoint) required for checking that an end-of-backup is > reached are not set at first. IOW, cascaded standby thinks that the > database is consistent from the beginning. This is safe because > a shutdown checkpoint record means that there is no running database > activity at that point and the database is in consistent state. Hmm, I think the CheckRecoveryConsistency() call in the redo loop is misplaced. It's called after we got a record from ReadRecord, but *before* replaying it (rm_redo). Even if replaying record X makes the system consistent, we won't check and notice that until we have fetched record X+1. In this particular test case, record X is a shutdown checkpoint record, but it could as well be a running-xacts record, or the record that reaches minRecoveryPoint. Does the problem go away if you just move the CheckRecoveryConsistency() call *after* rm_redo (attached)? - Heikki --------------090901060809020608020202 Content-Type: text/x-diff; name="move-check-consistency.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="move-check-consistency.patch" diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index ff56c26..13764ee 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -6651,9 +6651,6 @@ StartupXLOG(void) /* Handle interrupt signals of startup process */ HandleStartupProcInterrupts(); - /* Allow read-only connections if we're consistent now */ - CheckRecoveryConsistency(); - /* * Have we reached our recovery target? */ @@ -6746,6 +6743,9 @@ StartupXLOG(void) LWLockRelease(ControlFileLock); } + /* Allow read-only connections if we're consistent now */ + CheckRecoveryConsistency(); + /* * Update shared recoveryLastRecPtr after this record has been * replayed. --------------090901060809020608020202--