Hello
I looked at this patch together with the other open pg_rewind patches,
and I think the clamp makes things worse in one case. While testing
that I also found a problem with 90bcc7c2db1 itself.
1. the clamp
+ * These segments belong to the target, which may be on
a higher
+ * TLI than the source. Clamp to the source's latest
TLI so that
+ * keepwal entries refer to segments the source can
hold.
+ */
+ current_tli = Min(xlogreader->seg.ws_tli, source_tli);
If the target is on a higher timeline than the source, it went through
a timeline with the source's number itself, so it can have its own
segment with exactly that name. If the source doesn't have that
segment anymore, the keepwal entry now protects the target's file. I
took your test and let the source recycle the segment after the
promotion:
$node_standby->safe_psql('postgres',
"ALTER SYSTEM SET wal_keep_size = 0; SELECT pg_reload_conf()");
for (1 .. 4)
{
$node_standby->safe_psql('postgres',
"CREATE TABLE IF NOT EXISTS r4 (i int); INSERT INTO r4
VALUES (1); SELECT pg_switch_wal()");
$node_standby->safe_psql('postgres', "CHECKPOINT");
}
With that the servers diverged at 0/03000110. After the rewind pg_wal
has 000000020000000000000003, which is the target's own timeline 2,
and recovery replays it as the source's timeline 2:
LOG: redo starts at 0/03000028
CONTEXT: WAL redo at 0/03002110 for XLOG/CHECKPOINT_SHUTDOWN: redo
0/03002110; tli 1; prev tli 1; ...
CONTEXT: WAL redo at 0/030022B8 for XLOG/CHECKPOINT_SHUTDOWN: redo
0/030022B8; tli 2; prev tli 2; ...
LOG: invalid record length at 0/03002340: expected at least 24, got 0
These are the old records of the target after the fork.
2. 90bcc7c2db1
The same thing can happen without the patch/without extra timelines.
The segment that contains the point of divergence is kept on the
target, but it still has the target's WAL after that point. Usually
recovery doesn't read it, because the source's segment of the new
timeline is copied and that one is preferred. But if the source
already recycled it, which is the scenario of this thread, recovery
falls back to the kept segment and reads on past the fork.
The attached script shows it on master: promote the standby, insert 10
more rows on the old primary and stop it, let the new primary recycle
the segment, pg_rewind, start as standby. The fork is at 0/03000110:
LOG: redo starts at 0/03000028
LOG: invalid record length at 0/03000538: expected at least 24, got 0
FATAL: could not receive data from WAL stream: ERROR: requested WAL
segment 000000020000000000000003 has already been removed
After stopping it again the 10 rows are back in the table file, and
the latest checkpoint in pg_control is the old primary's shutdown
checkpoint (0/030004B0 on timeline 1). Running pg_rewind again says
"source and target cluster are on the same timeline" and "no rewind
required".
It seems that Heikki's "Don't read past current TLI during archive
recovery" from [1] is about this, but that never got committed, and
that tries to handle this from the server side.
As a different direction, maybe in pg_rewind the kept segment could be
zeroed after the point of divergence? A quick PoC attached for that.
[1]:
https://www.postgresql.org/message-id/fe7af87d-b9bf-481a-902c-089c49aa911d%40iki.fi
diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c
index d1d726ee82d..05f1f4c6ea4 100644
--- a/src/bin/pg_rewind/pg_rewind.c
+++ b/src/bin/pg_rewind/pg_rewind.c
@@ -46,6 +46,8 @@ static void createBackupLabel(XLogRecPtr startpoint,
TimeLineID starttli,
static void digestControlFile(ControlFileData *ControlFile,
const char *content,
size_t size);
static void getRestoreCommand(const char *argv0);
+static void clear_wal_after_divergence(file_entry_t *entry,
+
XLogRecPtr divergerec);
static void sanityChecks(void);
static TimeLineHistoryEntry *getTimelineHistory(TimeLineID tli, bool is_source,
int *nentries);
@@ -650,7 +652,7 @@ perform_rewind(filemap_t *filemap, rewind_source *source,
switch (entry->action)
{
case FILE_ACTION_NONE:
- /* nothing else to do */
+ clear_wal_after_divergence(entry, divergerec);
break;
case FILE_ACTION_COPY:
@@ -819,6 +821,48 @@ perform_rewind(filemap_t *filemap, rewind_source *source,
update_controlfile(datadir_target, &ControlFile_new, do_sync);
}
+/*
+ * A WAL segment that exists only in the target is kept if it has WAL between
+ * the last common checkpoint and the point of divergence. If the point of
+ * divergence is in it, the rest of the segment is WAL of the target's old
+ * timeline. Recovery must not find that: when the source's segment for the
+ * new timeline is not available, it would read on in this one and replay the
+ * WAL we are rewinding. Zero it out.
+ */
+static void
+clear_wal_after_divergence(file_entry_t *entry, XLogRecPtr divergerec)
+{
+ static const PGAlignedXLogBlock zeros = {0};
+ const char *fname;
+ TimeLineID tli;
+ XLogSegNo segno;
+ XLogSegNo divergesegno;
+ off_t off;
+
+ if (entry->source_exists || !entry->target_exists)
+ return;
+ if (strncmp(entry->path, XLOGDIR "/", strlen(XLOGDIR "/")) != 0)
+ return;
+ fname = entry->path + strlen(XLOGDIR "/");
+ if (!IsXLogFileName(fname))
+ return;
+
+ XLogFromFileName(fname, &tli, &segno, WalSegSz);
+ XLByteToSeg(divergerec, divergesegno, WalSegSz);
+ if (segno != divergesegno)
+ return;
+
+ pg_log_debug("clearing WAL after %X/%08X in \"%s\"",
+ LSN_FORMAT_ARGS(divergerec), entry->path);
+
+ open_target_file(entry->path, false);
+ for (off = XLogSegmentOffset(divergerec, WalSegSz);
+ off < entry->target_size;
+ off += XLOG_BLCKSZ)
+ write_target_range(unconstify(char *, zeros.data), off,
+ Min(XLOG_BLCKSZ,
entry->target_size - off));
+}
+
static void
sanityChecks(void)
{