Skip to content

Commit 660f1fc

Browse files
committed
Prevent archive recovery from scanning non-existent WAL files.
Previously when there were multiple timelines listed in the history file of the recovery target timeline, archive recovery searched all of them, starting from the newest timeline to the oldest one, to find the segment to read. That is, archive recovery had to continuously fail scanning the segment until it reached the timeline that the segment belonged to. These scans for non-existent segment could be harmful on the recovery performance especially when archival area was located on the remote storage and each scan could take a long time. To address the issue, this commit changes archive recovery so that it skips scanning the timeline that the segment to read doesn't belong to. Per discussion, back-patch to all supported versions. Author: Kyotaro Horiguchi, tweaked a bit by Fujii Masao Reviewed-by: David Steele, Pavel Suderevsky, Grigory Smolkin Discussion: https://postgr.es/m/16159-f5a34a3a04dc67e0@postgresql.org Discussion: https://postgr.es/m/20200129.120222.1476610231001551715.horikyota.ntt@gmail.com
1 parent f863574 commit 660f1fc

File tree

1 file changed

+26
-1
lines changed
  • src/backend/access/transam

1 file changed

+26
-1
lines changed

src/backend/access/transam/xlog.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3692,11 +3692,36 @@ XLogFileReadAnyTLI(XLogSegNo segno, int emode, int source)
36923692

36933693
foreach(cell, tles)
36943694
{
3695-
TimeLineID tli = ((TimeLineHistoryEntry *) lfirst(cell))->tli;
3695+
TimeLineHistoryEntry *hent = (TimeLineHistoryEntry *) lfirst(cell);
3696+
TimeLineID tli = hent->tli;
36963697

36973698
if (tli < curFileTLI)
36983699
break; /* don't bother looking at too-old TLIs */
36993700

3701+
/*
3702+
* Skip scanning the timeline ID that the logfile segment to read
3703+
* doesn't belong to
3704+
*/
3705+
if (hent->begin != InvalidXLogRecPtr)
3706+
{
3707+
XLogSegNo beginseg = 0;
3708+
3709+
XLByteToSeg(hent->begin, beginseg);
3710+
3711+
/*
3712+
* The logfile segment that doesn't belong to the timeline is
3713+
* older or newer than the segment that the timeline started or
3714+
* ended at, respectively. It's sufficient to check only the
3715+
* starting segment of the timeline here. Since the timelines are
3716+
* scanned in descending order in this loop, any segments newer
3717+
* than the ending segment should belong to newer timeline and
3718+
* have already been read before. So it's not necessary to check
3719+
* the ending segment of the timeline here.
3720+
*/
3721+
if (segno < beginseg)
3722+
continue;
3723+
}
3724+
37003725
if (source == XLOG_FROM_ANY || source == XLOG_FROM_ARCHIVE)
37013726
{
37023727
fd = XLogFileRead(segno, emode, tli,

0 commit comments

Comments
 (0)