WaitForWALToBecomeAvailable:
if (!InArchiveRecovery)
currentSource = XLOG_FROM_PG_WAL;
else if (currentSource == 0)
currentSource = XLOG_FROM_ARCHIVE;
for (;;){
int oldSource = currentSource;
if (lastSourceFailed){
switch (currentSource){
case XLOG_FROM_ARCHIVE:
case XLOG_FROM_PG_WAL://如果是备机,并且执行了promote或者存在TriggerFile文件则shutdown receive进程
if (StandbyMode && CheckForStandbyTrigger()){
ShutdownWalRcv();
return false;
}
if (!StandbyMode)
return false;
/*
构建流复制关系的位点:
1、如果上次正常关闭,不需要恢复,则从checkpoint点开始
2、否则,从恢复的结束位置开始
*/
if (PrimaryConnInfo){
if (fetching_ckpt){
ptr = RedoStartLSN;
tli = ControlFile->checkPointCopy.ThisTimeLineID;
}else{
ptr = RecPtr;
tli = tliOfPointInHistory(tliRecPtr, expectedTLEs);
}
curFileTLI = tli;
RequestXLogStreaming(tli, ptr, PrimaryConnInfo, PrimarySlotName);
receivedUpto = 0;
}
currentSource = XLOG_FROM_STREAM;
break;
...
}
}
...
}
/*
1、流复制拉取日志的起始位置是位点所在文件的文件开头
2、也就是说,会以重新拉取整个xlog的文件并覆盖pg_xlog目录的相应文件
3、所以,当以主shutdown,再次以备启动时,需要注意,会从新主拉取相应文件并
4、覆盖本地文件,但是本地的pg_control文件并没有更新,所以读取的checkpoint位置会指向一个错误的位置
5、这种情况构建流复制需要谨慎注意。
*/
RequestXLogStreaming:
if (recptr % XLogSegSize != 0)
recptr -= recptr % XLogSegSize;
walrcv->receiveStart = recptr;
walrcv->receiveStartTLI = tli;
SendPostmasterSignal(PMSIGNAL_START_WALRECEIVER);//发起信号唤醒receiver进程