mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-27 08:39:28 +08:00
Change how first WAL segment on new timeline after promotion is created.
Two changes: 1. When copying a WAL segment from old timeline to create the first segment on the new timeline, only copy up to the point where the timeline switch happens, and zero-fill the rest. This avoids corner cases where we might think that the copied WAL from the previous timeline belong to the new timeline. 2. If the timeline switch happens at a segment boundary, don't copy the whole old segment to the new timeline. It's pointless, because it's 100% identical to the old segment.
This commit is contained in:
parent
38628db8d8
commit
ba94518aad
@ -2904,12 +2904,15 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
|
|||||||
* srcTLI, srclog, srcseg: identify segment to be copied (could be from
|
* srcTLI, srclog, srcseg: identify segment to be copied (could be from
|
||||||
* a different timeline)
|
* a different timeline)
|
||||||
*
|
*
|
||||||
|
* upto: how much of the source file to copy? (the rest is filled with zeros)
|
||||||
|
*
|
||||||
* Currently this is only used during recovery, and so there are no locking
|
* Currently this is only used during recovery, and so there are no locking
|
||||||
* considerations. But we should be just as tense as XLogFileInit to avoid
|
* considerations. But we should be just as tense as XLogFileInit to avoid
|
||||||
* emplacing a bogus file.
|
* emplacing a bogus file.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno)
|
XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno,
|
||||||
|
int upto)
|
||||||
{
|
{
|
||||||
char path[MAXPGPATH];
|
char path[MAXPGPATH];
|
||||||
char tmppath[MAXPGPATH];
|
char tmppath[MAXPGPATH];
|
||||||
@ -2948,16 +2951,31 @@ XLogFileCopy(XLogSegNo destsegno, TimeLineID srcTLI, XLogSegNo srcsegno)
|
|||||||
*/
|
*/
|
||||||
for (nbytes = 0; nbytes < XLogSegSize; nbytes += sizeof(buffer))
|
for (nbytes = 0; nbytes < XLogSegSize; nbytes += sizeof(buffer))
|
||||||
{
|
{
|
||||||
errno = 0;
|
int nread;
|
||||||
if ((int) read(srcfd, buffer, sizeof(buffer)) != (int) sizeof(buffer))
|
|
||||||
|
nread = upto - nbytes;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The part that is not read from the source file is filled with zeros.
|
||||||
|
*/
|
||||||
|
if (nread < sizeof(buffer))
|
||||||
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
|
||||||
|
if (nread > 0)
|
||||||
{
|
{
|
||||||
if (errno != 0)
|
if (nread > sizeof(buffer))
|
||||||
ereport(ERROR,
|
nread = sizeof(buffer);
|
||||||
(errcode_for_file_access(),
|
errno = 0;
|
||||||
errmsg("could not read file \"%s\": %m", path)));
|
if (read(srcfd, buffer, nread) != nread)
|
||||||
else
|
{
|
||||||
ereport(ERROR,
|
if (errno != 0)
|
||||||
(errmsg("not enough data in file \"%s\"", path)));
|
ereport(ERROR,
|
||||||
|
(errcode_for_file_access(),
|
||||||
|
errmsg("could not read file \"%s\": %m", path)));
|
||||||
|
else
|
||||||
|
ereport(ERROR,
|
||||||
|
(errmsg("not enough data in file \"%s\"", path)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if ((int) write(fd, buffer, sizeof(buffer)) != (int) sizeof(buffer))
|
if ((int) write(fd, buffer, sizeof(buffer)) != (int) sizeof(buffer))
|
||||||
@ -4960,10 +4978,15 @@ readRecoveryCommandFile(void)
|
|||||||
* Exit archive-recovery state
|
* Exit archive-recovery state
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
exitArchiveRecovery(TimeLineID endTLI, XLogSegNo endLogSegNo)
|
exitArchiveRecovery(TimeLineID endTLI, XLogRecPtr endOfLog)
|
||||||
{
|
{
|
||||||
char recoveryPath[MAXPGPATH];
|
char recoveryPath[MAXPGPATH];
|
||||||
char xlogfname[MAXFNAMELEN];
|
char xlogfname[MAXFNAMELEN];
|
||||||
|
XLogSegNo endLogSegNo;
|
||||||
|
XLogSegNo startLogSegNo;
|
||||||
|
|
||||||
|
/* we always switch to a new timeline after archive recovery */
|
||||||
|
Assert(endTLI != ThisTimeLineID);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We are no longer in archive recovery state.
|
* We are no longer in archive recovery state.
|
||||||
@ -4986,18 +5009,29 @@ exitArchiveRecovery(TimeLineID endTLI, XLogSegNo endLogSegNo)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If we are establishing a new timeline, we have to copy data from the
|
* Calculate the last segment on the old timeline, and the first segment
|
||||||
* last WAL segment of the old timeline to create a starting WAL segment
|
* on the new timeline. If the switch happens in the middle of a segment,
|
||||||
* for the new timeline.
|
* they are the same, but if the switch happens exactly at a segment
|
||||||
|
* boundary, startLogSegNo will be endLogSegNo + 1.
|
||||||
|
*/
|
||||||
|
XLByteToPrevSeg(endOfLog, endLogSegNo);
|
||||||
|
XLByteToSeg(endOfLog, startLogSegNo);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize the starting WAL segment for the new timeline. If the switch
|
||||||
|
* happens in the middle of a segment, copy data from the last WAL segment
|
||||||
|
* of the old timeline up to the switch point, to the starting WAL segment
|
||||||
|
* on the new timeline.
|
||||||
*
|
*
|
||||||
* Notify the archiver that the last WAL segment of the old timeline is
|
* Notify the archiver that the last WAL segment of the old timeline is
|
||||||
* ready to copy to archival storage if its .done file doesn't exist
|
* ready to copy to archival storage if its .done file doesn't exist
|
||||||
* (e.g., if it's the restored WAL file, it's expected to have .done file).
|
* (e.g., if it's the restored WAL file, it's expected to have .done file).
|
||||||
* Otherwise, it is not archived for a while.
|
* Otherwise, it is not archived for a while.
|
||||||
*/
|
*/
|
||||||
if (endTLI != ThisTimeLineID)
|
if (endLogSegNo == startLogSegNo)
|
||||||
{
|
{
|
||||||
XLogFileCopy(endLogSegNo, endTLI, endLogSegNo);
|
XLogFileCopy(startLogSegNo, endTLI, endLogSegNo,
|
||||||
|
endOfLog % XLOG_SEG_SIZE);
|
||||||
|
|
||||||
/* Create .ready file only when neither .ready nor .done files exist */
|
/* Create .ready file only when neither .ready nor .done files exist */
|
||||||
if (XLogArchivingActive())
|
if (XLogArchivingActive())
|
||||||
@ -5006,12 +5040,18 @@ exitArchiveRecovery(TimeLineID endTLI, XLogSegNo endLogSegNo)
|
|||||||
XLogArchiveCheckDone(xlogfname);
|
XLogArchiveCheckDone(xlogfname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bool use_existent = true;
|
||||||
|
|
||||||
|
XLogFileInit(startLogSegNo, &use_existent, true);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Let's just make real sure there are not .ready or .done flags posted
|
* Let's just make real sure there are not .ready or .done flags posted
|
||||||
* for the new segment.
|
* for the new segment.
|
||||||
*/
|
*/
|
||||||
XLogFileName(xlogfname, ThisTimeLineID, endLogSegNo);
|
XLogFileName(xlogfname, ThisTimeLineID, startLogSegNo);
|
||||||
XLogArchiveCleanup(xlogfname);
|
XLogArchiveCleanup(xlogfname);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -5599,7 +5639,7 @@ StartupXLOG(void)
|
|||||||
XLogRecPtr RecPtr,
|
XLogRecPtr RecPtr,
|
||||||
checkPointLoc,
|
checkPointLoc,
|
||||||
EndOfLog;
|
EndOfLog;
|
||||||
XLogSegNo endLogSegNo;
|
XLogSegNo startLogSegNo;
|
||||||
TimeLineID PrevTimeLineID;
|
TimeLineID PrevTimeLineID;
|
||||||
XLogRecord *record;
|
XLogRecord *record;
|
||||||
TransactionId oldestActiveXID;
|
TransactionId oldestActiveXID;
|
||||||
@ -6586,7 +6626,7 @@ StartupXLOG(void)
|
|||||||
*/
|
*/
|
||||||
record = ReadRecord(xlogreader, LastRec, PANIC, false);
|
record = ReadRecord(xlogreader, LastRec, PANIC, false);
|
||||||
EndOfLog = EndRecPtr;
|
EndOfLog = EndRecPtr;
|
||||||
XLByteToPrevSeg(EndOfLog, endLogSegNo);
|
XLByteToSeg(EndOfLog, startLogSegNo);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Complain if we did not roll forward far enough to render the backup
|
* Complain if we did not roll forward far enough to render the backup
|
||||||
@ -6687,14 +6727,14 @@ StartupXLOG(void)
|
|||||||
* we will use that below.)
|
* we will use that below.)
|
||||||
*/
|
*/
|
||||||
if (ArchiveRecoveryRequested)
|
if (ArchiveRecoveryRequested)
|
||||||
exitArchiveRecovery(xlogreader->readPageTLI, endLogSegNo);
|
exitArchiveRecovery(xlogreader->readPageTLI, EndOfLog);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prepare to write WAL starting at EndOfLog position, and init xlog
|
* Prepare to write WAL starting at EndOfLog position, and init xlog
|
||||||
* buffer cache using the block containing the last record from the
|
* buffer cache using the block containing the last record from the
|
||||||
* previous incarnation.
|
* previous incarnation.
|
||||||
*/
|
*/
|
||||||
openLogSegNo = endLogSegNo;
|
openLogSegNo = startLogSegNo;
|
||||||
openLogFile = XLogFileOpen(openLogSegNo);
|
openLogFile = XLogFileOpen(openLogSegNo);
|
||||||
openLogOff = 0;
|
openLogOff = 0;
|
||||||
Insert = &XLogCtl->Insert;
|
Insert = &XLogCtl->Insert;
|
||||||
|
Loading…
Reference in New Issue
Block a user