Hello!
Isn't this the same change that Adam Lee proposed in [1] earlier this
year for recovery_target_action = shutdown? His patch advanced
minRecoveryPoint to the replay position at every restartpoint and then
Heikki suggested calling UpdateMinRecoveryPoint(), which is what your
patch does.
In that thread Michael and Horiguchi-san argued against it, but this
could be another example in favor of it.
>From a different perspective, isn't this something that could be
possibly fixed in pg_rewind?
For a standby target it could read the WAL forward from
minRecoveryPoint until the first invalid record, and use that as the
end of the target's WAL. That doesn't change what minRecoveryPoint
means, and it also works for a standby that was stopped by a server
without the fix. See the attached quick PoC.
However, looking at your other patch [2]: it uses the minRecoveryPoint
of a stopped standby as the consistency point for the copied files.
With hot_standby = off on the source a replayed commit reaches pg_xact
without moving minRecoveryPoint, so doesn't that patch need this
server side change? If yes, that's an argument for keeping it, even if
pg_rewind looks at the target's WAL.
[1]: https://www.postgresql.org/message-id/aczc9cxkr7SEHXV5%40MAC-CVW1VHW5R6
[2]:
https://www.postgresql.org/message-id/51de3e6e-01de-4740-ad11-1bf69ff0d68d%40planetscale.com
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);