postgres.git / summary / log / commit / refs
commit f1dc31245cb18e60236086207f2b8ff38d74f403
Author: Fujii Masao <fujii@postgresql.org>
Date: Fri Nov 20 11:44:04 2026 +0000
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 <cca5507@qq.com>
Reviewed-by: Surya Poondla <suryapoondla4@gmail.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Solai V <solai.cdac@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Xuneng Zhou <xunengzhou@gmail.com>
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 dffb1b12f72..9997a62164a 100644
--- a/doc/src/sgml/ref/pg_rewind.sgml
+++ b/doc/src/sgml/ref/pg_rewind.sgml
@@ -397,12 +397,13 @@ GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, b
</step>
<step>
<para>
- Create a <filename>backup_label</filename> file to begin WAL replay at
- the checkpoint created at failover and configure the
- <filename>pg_control</filename> file with a minimum consistency LSN
- defined as the result of <literal>pg_current_wal_insert_lsn()</literal>
- when rewinding from a live source or the last checkpoint LSN when
- rewinding from a stopped source.
+ Create a <filename>backup_label</filename> file to specify where WAL
+ replay must begin, and configure the <filename>pg_control</filename>
+ file with a minimum consistency LSN.
+ This LSN is obtained after copying the files. For a live primary,
+ it is the result of <function>pg_current_wal_flush_lsn()</function>;
+ for a live standby, it is the source's minimum recovery point;
+ for a stopped source, it is the last checkpoint LSN.
</para>
</step>
<step>
diff --git a/src/bin/pg_rewind/libpq_source.c b/src/bin/pg_rewind/libpq_source.c
index 011c9cce6eb..505c1c563c3 100644
--- a/src/bin/pg_rewind/libpq_source.c
+++ b/src/bin/pg_rewind/libpq_source.c
@@ -69,7 +69,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);
/*
@@ -92,7 +92,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/%X", &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 2e50485c395..061584eb9aa 100644
--- a/src/bin/pg_rewind/local_source.c
+++ b/src/bin/pg_rewind/local_source.c
@@ -48,7 +48,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 78ba54d30f3..9e2ec8a74fb 100644
--- a/src/bin/pg_rewind/pg_rewind.c
+++ b/src/bin/pg_rewind/pg_rewind.c
@@ -635,8 +635,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)
{
@@ -650,13 +650,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 = ControlFile_source_after.checkPointCopy.ThisTimeLineID;
}
}
diff --git a/src/bin/pg_rewind/rewind_source.h b/src/bin/pg_rewind/rewind_source.h
index 1310e86e75a..2d68f5df55e 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.
[parent: f753bbf57809]