diff --git a/src/bin/pg_rewind/parsexlog.c b/src/bin/pg_rewind/parsexlog.c index 6e87b00f8c2..b158e7fc700 100644 --- a/src/bin/pg_rewind/parsexlog.c +++ b/src/bin/pg_rewind/parsexlog.c @@ -48,6 +48,7 @@ typedef struct XLogPageReadPrivate { const char *restoreCommand; int tliIndex; + bool missing_ok; /* no error message for a missing segment */ } XLogPageReadPrivate; static int SimpleXLogPageRead(XLogReaderState *xlogreader, @@ -73,6 +74,7 @@ extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex, private.tliIndex = tliIndex; private.restoreCommand = restoreCommand; + private.missing_ok = false; xlogreader = XLogReaderAllocate(WalSegSz, datadir, XL_ROUTINE(.page_read = &SimpleXLogPageRead), &private); @@ -132,6 +134,7 @@ readOneRecord(const char *datadir, XLogRecPtr ptr, int tliIndex, private.tliIndex = tliIndex; private.restoreCommand = restoreCommand; + private.missing_ok = false; xlogreader = XLogReaderAllocate(WalSegSz, datadir, XL_ROUTINE(.page_read = &SimpleXLogPageRead), &private); @@ -161,6 +164,44 @@ readOneRecord(const char *datadir, XLogRecPtr ptr, int tliIndex, return endptr; } +/* + * Find the end of valid WAL, reading forward from 'startpoint', which must be + * the end of a record. Returns the end of the last valid record, or + * 'startpoint' if there is none after it. + * + * Only looks at WAL present in the data directory. + */ +XLogRecPtr +findEndOfWAL(const char *datadir, XLogRecPtr startpoint, int tliIndex) +{ + XLogReaderState *xlogreader; + char *errormsg; + XLogPageReadPrivate private; + XLogRecPtr endptr = startpoint; + + private.tliIndex = tliIndex; + private.restoreCommand = NULL; + private.missing_ok = true; + xlogreader = XLogReaderAllocate(WalSegSz, datadir, + XL_ROUTINE(.page_read = &SimpleXLogPageRead), + &private); + if (xlogreader == NULL) + pg_fatal("out of memory while allocating a WAL reading processor"); + + XLogBeginRead(xlogreader, startpoint); + while (XLogReadRecord(xlogreader, &errormsg) != NULL) + endptr = xlogreader->EndRecPtr; + + XLogReaderFree(xlogreader); + if (xlogreadfd != -1) + { + close(xlogreadfd); + xlogreadfd = -1; + } + + return endptr; +} + /* * Find the previous checkpoint preceding given WAL location. */ @@ -195,6 +236,7 @@ findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, int tliIndex, private.tliIndex = tliIndex; private.restoreCommand = restoreCommand; + private.missing_ok = false; xlogreader = XLogReaderAllocate(WalSegSz, datadir, XL_ROUTINE(.page_read = &SimpleXLogPageRead), &private); @@ -332,7 +374,8 @@ SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, */ if (private->restoreCommand == NULL) { - pg_log_error("could not open file \"%s\": %m", xlogfpath); + if (!private->missing_ok) + pg_log_error("could not open file \"%s\": %m", xlogfpath); return -1; } diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c index d1d726ee82d..8159ea63bd7 100644 --- a/src/bin/pg_rewind/pg_rewind.c +++ b/src/bin/pg_rewind/pg_rewind.c @@ -441,6 +441,19 @@ main(int argc, char **argv) target_wal_endrec = chkptendrec; } + /* + * minRecoveryPoint of a standby only tells how far the WAL was + * replayed when a page was last written out, or at best at + * shutdown. The standby can have replayed more, and it can have WAL + * it never replayed but will replay when started again. Either way + * that WAL must not be past the point of divergence, so look for the + * end of the WAL the target actually has. + */ + if (ControlFile_target.state == DB_SHUTDOWNED_IN_RECOVERY) + target_wal_endrec = findEndOfWAL(datadir_target, + target_wal_endrec, + targetNentries - 1); + /* * Check for the possibility that the target is in fact a direct * ancestor of the source. In that case, there is no divergent history diff --git a/src/bin/pg_rewind/pg_rewind.h b/src/bin/pg_rewind/pg_rewind.h index 9187c3bf72f..5b06c23952f 100644 --- a/src/bin/pg_rewind/pg_rewind.h +++ b/src/bin/pg_rewind/pg_rewind.h @@ -41,6 +41,8 @@ extern void findLastCheckpoint(const char *datadir, XLogRecPtr forkptr, XLogRecPtr *lastchkptredo, uint32 *lastchkptdatachecksums, const char *restoreCommand); +extern XLogRecPtr findEndOfWAL(const char *datadir, XLogRecPtr startpoint, + int tliIndex); extern XLogRecPtr readOneRecord(const char *datadir, XLogRecPtr ptr, int tliIndex, const char *restoreCommand);