From 343394703ef5ec2886ada3428c86f51862f649e0 Mon Sep 17 00:00:00 2001 From: ChangAo Chen Date: Tue, 16 Jun 2026 11:22:13 +0800 Subject: [PATCH v5] pg_rewind: Fix recovery delays at WAL page boundaries After pg_rewind, a server restarted as a standby could remain unable to accept read-only connections until the source generated more WAL, even after replaying all available records. The cause was pg_rewind's use of pg_current_wal_insert_lsn() to set the target's minimum recovery point. At a WAL page boundary, this function could return a position just past the next page's header. Without another record to replay, recovery could not reach that position. Fix this by using the result of pg_current_wal_flush_lsn(), obtained after copying the files, as the target's minimum recovery point instead of pg_current_wal_insert_lsn(). The write-ahead rule ensures that this position covers all WAL needed to make the copied data consistent. In particular, after a WAL segment switch, it avoids requiring recovery to advance past the next segment's header. Backpatch to all supported versions. Author: ChangAo Chen Reviewed-by: Surya Poondla Reviewed-by: Kyotaro Horiguchi Reviewed-by: Solai V Discussion: https://www.postgresql.org/message-id/tencent_A37630454CE614AFE640C56E33B4A0F0AE05%40qq.com Backpatch-through: 15 --- doc/src/sgml/ref/pg_rewind.sgml | 13 +++++++------ src/bin/pg_rewind/libpq_source.c | 12 ++++++------ src/bin/pg_rewind/local_source.c | 2 +- src/bin/pg_rewind/pg_rewind.c | 16 +++++++++++----- src/bin/pg_rewind/rewind_source.h | 5 +++-- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/doc/src/sgml/ref/pg_rewind.sgml b/doc/src/sgml/ref/pg_rewind.sgml index b95e3868e9d..05b9217b9d7 100644 --- a/doc/src/sgml/ref/pg_rewind.sgml +++ b/doc/src/sgml/ref/pg_rewind.sgml @@ -435,12 +435,13 @@ GRANT EXECUTE ON FUNCTION pg_catalog.pg_read_binary_file(text, bigint, bigint, b - Create a backup_label file to begin WAL replay at - the checkpoint created at failover and configure the - pg_control file with a minimum consistency LSN - defined as the result of pg_current_wal_insert_lsn() - when rewinding from a live source or the last checkpoint LSN when - rewinding from a stopped source. + Create a backup_label file to specify where WAL + replay must begin, and configure the pg_control + file with a minimum consistency LSN. + This LSN is obtained after copying the files. For a live primary, + it is the result of pg_current_wal_flush_lsn(); + for a live standby, it is the source's minimum recovery point; + for a stopped source, it is the last checkpoint LSN. diff --git a/src/bin/pg_rewind/libpq_source.c b/src/bin/pg_rewind/libpq_source.c index 6955bc575ea..97435427803 100644 --- a/src/bin/pg_rewind/libpq_source.c +++ b/src/bin/pg_rewind/libpq_source.c @@ -68,7 +68,7 @@ static void libpq_queue_fetch_range(rewind_source *source, const char *path, static void libpq_finish_fetch(rewind_source *source); static char *libpq_fetch_file(rewind_source *source, const char *path, size_t *filesize); -static XLogRecPtr libpq_get_current_wal_insert_lsn(rewind_source *source); +static XLogRecPtr libpq_get_current_wal_flush_lsn(rewind_source *source); static void libpq_destroy(rewind_source *source); /* @@ -91,7 +91,7 @@ init_libpq_source(PGconn *conn) src->common.queue_fetch_file = libpq_queue_fetch_file; src->common.queue_fetch_range = libpq_queue_fetch_range; src->common.finish_fetch = libpq_finish_fetch; - src->common.get_current_wal_insert_lsn = libpq_get_current_wal_insert_lsn; + src->common.get_current_wal_flush_lsn = libpq_get_current_wal_flush_lsn; src->common.destroy = libpq_destroy; src->conn = conn; @@ -202,10 +202,10 @@ run_simple_command(PGconn *conn, const char *sql) } /* - * Call the pg_current_wal_insert_lsn() function in the remote system. + * Call the pg_current_wal_flush_lsn() function in the remote system. */ static XLogRecPtr -libpq_get_current_wal_insert_lsn(rewind_source *source) +libpq_get_current_wal_flush_lsn(rewind_source *source) { PGconn *conn = ((libpq_source *) source)->conn; XLogRecPtr result; @@ -213,10 +213,10 @@ libpq_get_current_wal_insert_lsn(rewind_source *source) uint32 lo; char *val; - val = run_simple_query(conn, "SELECT pg_current_wal_insert_lsn()"); + val = run_simple_query(conn, "SELECT pg_current_wal_flush_lsn()"); if (sscanf(val, "%X/%08X", &hi, &lo) != 2) - pg_fatal("unrecognized result \"%s\" for current WAL insert location", val); + pg_fatal("unrecognized result \"%s\" for current WAL flush location", val); result = ((uint64) hi) << 32 | lo; diff --git a/src/bin/pg_rewind/local_source.c b/src/bin/pg_rewind/local_source.c index 4841cf01fb7..029f13a7396 100644 --- a/src/bin/pg_rewind/local_source.c +++ b/src/bin/pg_rewind/local_source.c @@ -46,7 +46,7 @@ init_local_source(const char *datadir) src->common.queue_fetch_file = local_queue_fetch_file; src->common.queue_fetch_range = local_queue_fetch_range; src->common.finish_fetch = local_finish_fetch; - src->common.get_current_wal_insert_lsn = NULL; + src->common.get_current_wal_flush_lsn = NULL; src->common.destroy = local_destroy; src->datadir = datadir; diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c index 2e86fd158d0..6f6a75fc3aa 100644 --- a/src/bin/pg_rewind/pg_rewind.c +++ b/src/bin/pg_rewind/pg_rewind.c @@ -698,8 +698,8 @@ perform_rewind(filemap_t *filemap, rewind_source *source, { /* * The source is a live server. Like in an online backup, it's - * important that we recover all the WAL that was generated while we - * were copying files. + * important that we replay all the WAL needed to make the copied + * files consistent. */ if (ControlFile_source_after.state == DB_IN_ARCHIVE_RECOVERY) { @@ -713,13 +713,19 @@ perform_rewind(filemap_t *filemap, rewind_source *source, else { /* - * Source is a production, non-standby, server. We must replay to - * the last WAL insert location. + * Source is a live primary server. Use its WAL flush location + * after copying the files as the minimum recovery point. The + * write-ahead rule ensures that all WAL needed for the copied + * data has been flushed by then. + * + * Don't use the WAL insert location: at a page boundary, it can + * point past the next page's header, requiring additional WAL to + * be generated before recovery can reach consistency. */ if (ControlFile_source_after.state != DB_IN_PRODUCTION) pg_fatal("source system was in unexpected state at end of rewind"); - endrec = source->get_current_wal_insert_lsn(source); + endrec = source->get_current_wal_flush_lsn(source); endtli = Max(ControlFile_source_after.checkPointCopy.ThisTimeLineID, ControlFile_source_after.minRecoveryPointTLI); } diff --git a/src/bin/pg_rewind/rewind_source.h b/src/bin/pg_rewind/rewind_source.h index f2e9b84a732..24ab6b4d242 100644 --- a/src/bin/pg_rewind/rewind_source.h +++ b/src/bin/pg_rewind/rewind_source.h @@ -66,9 +66,10 @@ typedef struct rewind_source void (*finish_fetch) (struct rewind_source *); /* - * Get the current WAL insert position in the source system. + * Get the current WAL flush position in the source system. The source + * must be a live primary server. */ - XLogRecPtr (*get_current_wal_insert_lsn) (struct rewind_source *); + XLogRecPtr (*get_current_wal_flush_lsn) (struct rewind_source *); /* * Free this rewind_source object. -- 2.56.0