Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 835071337B4A for ; Tue, 26 Oct 2010 04:48:40 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 68103-05 for ; Tue, 26 Oct 2010 07:48:33 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og112.obsmtp.com (exprod7og112.obsmtp.com [64.18.2.177]) by mail.postgresql.org (Postfix) with SMTP id 141E6133798F for ; Tue, 26 Oct 2010 04:48:31 -0300 (ADT) Received: from source ([209.85.215.41]) by exprod7ob112.postini.com ([64.18.6.12]) with SMTP ID DSNKTMaHz+o2BEHPY6vI3ehdGL8UMe3kVBBB@postini.com; Tue, 26 Oct 2010 00:48:32 PDT Received: by ewy25 with SMTP id 25so2137952ewy.14 for ; Tue, 26 Oct 2010 00:48:30 -0700 (PDT) Received: by 10.213.11.17 with SMTP id r17mr6619908ebr.66.1288079310420; Tue, 26 Oct 2010 00:48:30 -0700 (PDT) Received: from [192.168.1.183] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi [88.195.122.240]) by mx.google.com with ESMTPS id x54sm8421212eeh.11.2010.10.26.00.48.29 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 26 Oct 2010 00:48:29 -0700 (PDT) Message-ID: <4CC687CB.6080306@enterprisedb.com> Date: Tue, 26 Oct 2010 10:48:27 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.1.12) Gecko/20100913 Icedove/3.0.7 MIME-Version: 1.0 To: Jeff Davis CC: pgsql-bugs@postgresql.org Subject: Re: Recovery bug References: <1287183483.30681.54.camel@jdavis-ux.asterdata.local> <1287355728.8516.383.camel@jdavis> <4CBD6458.4050400@enterprisedb.com> <1287507095.8516.547.camel@jdavis> <1287517214.20345.5.camel@jdavis-ux.asterdata.local> <4CC56DA5.3040908@enterprisedb.com> <1288022672.8516.801.camel@jdavis> In-Reply-To: <1288022672.8516.801.camel@jdavis> Content-Type: multipart/mixed; boundary="------------020201040201000005070306" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-4.2 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3 X-Spam-Level: X-Archive-Number: 201010/221 X-Sequence-Number: 28511 This is a multi-part message in MIME format. --------------020201040201000005070306 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 25.10.2010 19:04, Jeff Davis wrote: > On Mon, 2010-10-25 at 14:44 +0300, Heikki Linnakangas wrote: >> It seems we should use ReadRecord instead of the lower-level >> XLogPageRead function. One difference is that ReadRecord performs a >> bunch of sanity checks on the record, while XLogPageRead just reads the >> raw page. Extra sanity checking before removing backup_label seems like >> a good idea. Another difference is that in standby-mode, ReadRecord will >> retry until it succeeds. A standby server should keep retrying, even the >> very first record, until it succeeds, otherwise we have a change in >> behavior. > > The reason I didn't use ReadRecord is because it sets a global variable > to point to the next location in the log, so that subsequent calls can > just pass NULL for the location. True. XLogPageRead is new in 9.0, however. We'll have to use ReadRecord or invent something new for back-branches anyway. > It looks like the patch leaves the global variable pointing just after > the redo location rather than the checkpoint. I haven't tested your > patch yet, but it looks like some of the following code depends on > ReadRecord(NULL,...) fetching the record right after the checkpoint > record; so I think something else is required if you want to use > ReadRecord. Hmm, the next call to ReadRecord is this: > /* > * Find the first record that logically follows the checkpoint --- it > * might physically precede it, though. > */ > if (XLByteLT(checkPoint.redo, RecPtr)) > { > /* back up to find the record */ > record = ReadRecord(&(checkPoint.redo), PANIC, false); > } > else > { > /* just have to read next record after CheckPoint */ > record = ReadRecord(NULL, LOG, false); > } In the first case, the location is given explicitly. In the second case, the redo pointer equals the checkpoint record, so the current position is correct even with the patch. It makes me slightly nervous, though. It's correct today, but if someone adds code between the backup_label check and this that assumes that the current position is the checkpoint record, it'll fail. Then again, any new ReadRecord call in such added code would also break the assumption in the above block that the current position is the checkpoint record. In the case that the redo pointer is the same as the checkpoint record, we don't need to re-fetch the checkpoint record. I've added a test for that in the attached patch. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com --------------020201040201000005070306 Content-Type: text/x-diff; name="recovery-redo-3.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="recovery-redo-3.patch" diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 22fd578..6f1fedd 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -5839,14 +5839,29 @@ StartupXLOG(void) record = ReadCheckpointRecord(checkPointLoc, 0); if (record != NULL) { + memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint)); ereport(DEBUG1, (errmsg("checkpoint record is at %X/%X", checkPointLoc.xlogid, checkPointLoc.xrecoff))); InRecovery = true; /* force recovery even if SHUTDOWNED */ + + /* + * Make sure that REDO location exists. This may not be + * the case if there was a crash during an online backup, + * which left a backup_label around that references a WAL + * segment that's already been archived. + */ + if (XLByteLT(checkPoint.redo, checkPointLoc)) + { + if (!ReadRecord(&(checkPoint.redo), LOG, false)) + ereport(FATAL, + (errmsg("could not find redo location referenced by checkpoint record"), + errhint("If you are not restoring from a backup, try removing the file \"%s/backup_label\".", DataDir))); + } } else { - ereport(PANIC, + ereport(FATAL, (errmsg("could not locate required checkpoint record"), errhint("If you are not restoring from a backup, try removing the file \"%s/backup_label\".", DataDir))); } @@ -5892,10 +5907,10 @@ StartupXLOG(void) ereport(PANIC, (errmsg("could not locate a valid checkpoint record"))); } + memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint)); } LastRec = RecPtr = checkPointLoc; - memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint)); wasShutdown = (record->xl_info == XLOG_CHECKPOINT_SHUTDOWN); ereport(DEBUG1, --------------020201040201000005070306--