[BUG] Take a long time to reach consistent after pg_rewind
[BUG] Take a long time to reach consistent after pg_rewind
От:
"cca5507" <cca5507@qq.com>
Дата:
Hi, Steps to reproduce (PG19): 1) start two nodes, node1 (primary), node2 (standby), both with the following configuration: ``` archive_mode = on archive_command = '/bin/true' archive_timeout = 10 checkpoint_timeout = '60min' wal_keep_size = 1024 logging_collector = on ``` 2) promote node2 3) stop node1 4) make sure the pg_current_wal_insert_lsn() of node2 is at the begin of a wal segment (end with 000028), if not, do a checkpoint and recheck. (archive_timeout will switch the wal) 5) execute pg_rewind with node1 6) start node1 7) now node1 can't reach consistent until node2 write some wal Logs of node1: ``` 2026-04-10 16:16:07.802 CST [45623] LOG: starting backup recovery with redo LSN 0/02000028, checkpoint LSN 0/02000088, on timeline ID 1 2026-04-10 16:16:07.802 CST [45623] LOG: entering standby mode 2026-04-10 16:16:07.803 CST [45623] LOG: redo starts at 0/02000028 2026-04-10 16:16:07.803 CST [45623] LOG: completed backup recovery with redo LSN 0/02000028 and end LSN 0/02000130 2026-04-10 16:16:07.806 CST [45624] LOG: started streaming WAL from primary at 0/04000000 on timeline 2 2026-04-10 16:19:13.083 CST [47039] FATAL: the database system is not yet accepting connections 2026-04-10 16:19:13.083 CST [47039] DETAIL: Consistent recovery state has not been yet reached. 2026-04-10 16:20:16.413 CST [45623] LOG: consistent recovery state reached at 0/04000048 2026-04-10 16:20:16.413 CST [45616] LOG: database system is ready to accept read-only connections ``` Root cause: The min recovery point of node1 is at 0/04000028, but node2 doesn't have any wal after that and may keep idle for a long time. Possible fix: The pg_rewind use pg_current_wal_insert_lsn() to set the min recovery point, which calls GetXLogInsertRecPtr() and returns the latest wal insert pointer. Maybe we should use GetXLogInsertEndRecPtr() which returns the latest wal record end pointer. Thoughts? -- Regards, ChangAo Chen
Re: [BUG] Take a long time to reach consistent after pg_rewind
> Possible fix: > > The pg_rewind use pg_current_wal_insert_lsn() to set the min recovery point, which calls > GetXLogInsertRecPtr() and returns the latest wal insert pointer. Maybe we should use > GetXLogInsertEndRecPtr() which returns the latest wal record end pointer. > > Thoughts? Another solution: If minRecoveryPoint is just after a xlog page header, we can move it to the begin of the page. It's safe because we just skip the xlog page header. Do I miss something? Attach a patch done like this. -- Regards, ChangAo Chen
Re: [BUG] Take a long time to reach consistent after pg_rewind
Hi Surya, Thanks for the review and sorry for the late reply. I get a new idea to fix this bug from: https://www.postgresql.org/message-id/05DEB465-3C99-417E-B7FA-275A28068D90%40yandex-team.ru Please see the v2 patch for details. -- Regards, ChangAo Chen
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,
> As I understand it, the root of the problem is that
> get_current_wal_insert_lsn() returns the next WAL insertion position,
> while minRecoveryPoint is expected to point just past the end of the
> last required record. At positions immediately following a WAL page or
> segment header, those locations can be logically equivalent but
> numerically different.
Yes.
> I think pg_rewind is probably using the insert LSN because it wants to
> choose a conservative position as far ahead as possible. It might be
> possible to use the flush LSN if the copying logic is carefully
> arranged, but I would prefer to keep using the insert LSN if we can.
The insert LSN is not crash safe, is this really make sense to use it? For
example, the primary has insert LSN 1000, flush LSN 500, the standby
sets minRecoveryPoint to 1000, and then the primary crash and restart.
The primary now only has LSN 500, but the standby cannot reach
consistent until LSN 1000. This doesn’t make sense to me.
> I also think it is unfortunate to require client-side tools to be
> aware of the exact "end of last record + 1" representation. If both
> representations are logically equivalent at those positions, I would
> rather have the recovery side accept and normalize them than require
> every tool to produce exactly the expected form.
Sounds reasonable.
> So I wonder whether we should instead normalize minRecoveryPoint when
> reading it from the control file, so that these equivalent
> representations are treated as the same position. That would also make
> the recovery side a bit more robust.
>
> For example, something like this:
>
> diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
> index 73b78a83fa7..660c681795d 100644
> --- a/src/backend/access/transam/xlogrecovery.c
> +++ b/src/backend/access/transam/xlogrecovery.c
> @@ -955,6 +955,20 @@ InitWalRecovery(ControlFileData *ControlFile, bool *wasShutdown_ptr,
> {
> minRecoveryPoint = ControlFile->minRecoveryPoint;
> minRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
> +
> + /*
> + * minRecoveryPoint in the control file is expected to point to the
> + * location immediately following the end of the target
> + * record. However, some tools may record a location immediately after
> + * a WAL segment or page header when the target position is at the
> + * beginning of a segment or page. Normalize such values here before
> + * further processing.
> + */
> + if (XLogSegmentOffset(minRecoveryPoint, wal_segment_size)
> + == SizeOfXLogLongPHD)
> + minRecoveryPoint -= SizeOfXLogLongPHD;
> + else if (minRecoveryPoint % XLOG_BLCKSZ == SizeOfXLogShortPHD)
> + minRecoveryPoint -= SizeOfXLogShortPHD;
> }
> else
> {
I had thought about this before. To update minRecoveryPoint in place, I think we
should make sure that it won't cause any side effects. That means we need to
check every places we use minRecoveryPoint. That's why the v1 patch introduces
GetEffectiveMinRecoveryPoint() rather than updates it in place.
--
Regards,
ChangAo Chen
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Дата:
Hello. At Thu, 11 Jun 2026 15:50:19 +0800, "cca5507" wrote in > > I think pg_rewind is probably using the insert LSN because it wants to > > choose a conservative position as far ahead as possible. It might be > > possible to use the flush LSN if the copying logic is carefully > > arranged, but I would prefer to keep using the insert LSN if we can. > > The insert LSN is not crash safe, is this really make sense to use it? For > example, the primary has insert LSN 1000, flush LSN 500, the standby > sets minRecoveryPoint to 1000, and then the primary crash and restart. > The primary now only has LSN 500, but the standby cannot reach > consistent until LSN 1000. This doesn’t make sense to me. My understanding had been that the state produced by pg_rewind only needed to be valid with respect to the source server at the time pg_rewind was run. If the expectation is that the rewound standby must also remain usable after the source server subsequently goes through crash recovery, then I agree that using the insert LSN becomes harder to justify. > I had thought about this before. To update minRecoveryPoint in place, I think we > should make sure that it won't cause any side effects. That means we need to > check every places we use minRecoveryPoint. That's why the v1 patch introduces > GetEffectiveMinRecoveryPoint() rather than updates it in place. The reason I suggested that approach in my earlier email was simply that, as far as I could tell, that was the only place that needed to interpret a minRecoveryPoint value. That said, since minRecoveryPoint is expected to point to the end of the last required record, I think normalizing the value earlier would also be reasonable. Anyway, if we decide to use the flush LSN instead, then none of this should be necessary. Regards, -- Kyotaro Horiguchi NTT Open Source Software Center
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi,
Thanks ChangAo for v2, and Kyotaro for working through the alternatives. I think v2 is on the right track.
For me the Flush LSN looks correct for the below reasons:
1. The standby can reach it. XLOG_SWITCH (driven by archive_timeout) calls XLogFlush(EndPos) with EndPos = segment boundary. xlogreader treats SWITCH specially and advances NextRecPtr to end-of-segment
(XLogDecodeNextRecord()), so lastReplayedEndRecPtr lands on the segment boundary and the consistency check fires. With v2, minRecoveryPoint = segment boundary, which is exactly reachable.
This restores the implicit producer-side invariant: pg_basebackup's XLOG_BACKUP_END EndRecPtr, UpdateMinRecoveryPoint() via GetCurrentReplayRecPtr(), and the standby-source path in pg_rewind all yield a value record-driven progress can match.
Caveat: XLogBackgroundFlush rounds WriteRqst.Write down to a page boundary, and the asyncXactLSN bump only re-targets when we've already flushed past it. On a busy primary the flush LSN can therefore sit mid-record, but that's fine because live insertions advance the standby past whatever endrec we pin. Not the scenario in this bug, just worth noting that no producer-side or recovery-side fix eliminates every theoretical liveness corner.
2. GetXLogInsertRecPtr() reads CurrBytePos from shared memory not durable. If the source crashes after pg_rewind, it recovers to its flush LSN, leaving the target's minRecoveryPoint pinned to a higher
insert LSN which is unreachable forever. flush LSN sits at the segment boundary, which is on disk and recovery cannot regress past it. So this approach is crash-safe
3. perform_rewind() does: copy files -> fetch source control file -> get_current_wal_flush_lsn.
Any WAL-logged page on source's disk has page-LSN <= source's flush-LSN at the time it was written (FlushBuffer enforces XLogFlush before eviction; hint-bit pages with checksums use XLogSaveBufferForHint + PageSetLSN). Flush LSN is monotonic, so the value dominates every copied page-LSN. FSM/VM pages are exempt by design (freespace.c uses MarkBufferDirtyHint without WAL) but aren't gated by minRecoveryPoint. Unlogged tables are out of scope. Torn pages are closed by the existing full_page_writes=on precondition.
Review comments
1. Commit message understates the fix. It only describes the page-header symptom. The crash-safety property is the stronger argument and the one that resolves the back-and-forth on which LSN to use. Suggest adding:
a. "Using the flush LSN is also crash-safe with respect to the source: the insert LSN lives only in shared memory and can be lost on a source crash, leaving the standby's minRecoveryPoint ahead of any LSN the source can subsequently reach."
2. Code comment should explain why flush LSN is sufficient. The current "We must replay to the last WAL flush location" doesn't say why. Suggest:
a. "Use the source's flush LSN as the target's minRecoveryPoint: every WAL-logged page we copied has page-LSN <= source's flush LSN at copy time (WAL-before-data), and flush LSN is monotonic. We avoid the insert LSN because it can sit one page-header past a record's end at segment boundaries (where no record will end), and it is not durable, a source crash can leave flush LSN behind an insert LSN we already pinned."
3. Worth a comment in rewind_source.h that the callback must only be invoked against a non-standby source, pg_current_wal_flush_lsn() errors out under recovery.
4. No regression test. We can add a regression test under src/bin/pg_rewind/t/.
Otherwise +1 on the v2 direction.
Regards,
Surya Poondla
Thanks ChangAo for v2, and Kyotaro for working through the alternatives. I think v2 is on the right track.
For me the Flush LSN looks correct for the below reasons:
1. The standby can reach it. XLOG_SWITCH (driven by archive_timeout) calls XLogFlush(EndPos) with EndPos = segment boundary. xlogreader treats SWITCH specially and advances NextRecPtr to end-of-segment
(XLogDecodeNextRecord()), so lastReplayedEndRecPtr lands on the segment boundary and the consistency check fires. With v2, minRecoveryPoint = segment boundary, which is exactly reachable.
This restores the implicit producer-side invariant: pg_basebackup's XLOG_BACKUP_END EndRecPtr, UpdateMinRecoveryPoint() via GetCurrentReplayRecPtr(), and the standby-source path in pg_rewind all yield a value record-driven progress can match.
Caveat: XLogBackgroundFlush rounds WriteRqst.Write down to a page boundary, and the asyncXactLSN bump only re-targets when we've already flushed past it. On a busy primary the flush LSN can therefore sit mid-record, but that's fine because live insertions advance the standby past whatever endrec we pin. Not the scenario in this bug, just worth noting that no producer-side or recovery-side fix eliminates every theoretical liveness corner.
2. GetXLogInsertRecPtr() reads CurrBytePos from shared memory not durable. If the source crashes after pg_rewind, it recovers to its flush LSN, leaving the target's minRecoveryPoint pinned to a higher
insert LSN which is unreachable forever. flush LSN sits at the segment boundary, which is on disk and recovery cannot regress past it. So this approach is crash-safe
3. perform_rewind() does: copy files -> fetch source control file -> get_current_wal_flush_lsn.
Any WAL-logged page on source's disk has page-LSN <= source's flush-LSN at the time it was written (FlushBuffer enforces XLogFlush before eviction; hint-bit pages with checksums use XLogSaveBufferForHint + PageSetLSN). Flush LSN is monotonic, so the value dominates every copied page-LSN. FSM/VM pages are exempt by design (freespace.c uses MarkBufferDirtyHint without WAL) but aren't gated by minRecoveryPoint. Unlogged tables are out of scope. Torn pages are closed by the existing full_page_writes=on precondition.
Review comments
1. Commit message understates the fix. It only describes the page-header symptom. The crash-safety property is the stronger argument and the one that resolves the back-and-forth on which LSN to use. Suggest adding:
a. "Using the flush LSN is also crash-safe with respect to the source: the insert LSN lives only in shared memory and can be lost on a source crash, leaving the standby's minRecoveryPoint ahead of any LSN the source can subsequently reach."
2. Code comment should explain why flush LSN is sufficient. The current "We must replay to the last WAL flush location" doesn't say why. Suggest:
a. "Use the source's flush LSN as the target's minRecoveryPoint: every WAL-logged page we copied has page-LSN <= source's flush LSN at copy time (WAL-before-data), and flush LSN is monotonic. We avoid the insert LSN because it can sit one page-header past a record's end at segment boundaries (where no record will end), and it is not durable, a source crash can leave flush LSN behind an insert LSN we already pinned."
3. Worth a comment in rewind_source.h that the callback must only be invoked against a non-standby source, pg_current_wal_flush_lsn() errors out under recovery.
4. No regression test. We can add a regression test under src/bin/pg_rewind/t/.
Otherwise +1 on the v2 direction.
Regards,
Surya Poondla
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi ChangAo,
Thanks for the v3, the commit message, in-line comment, and the rewind_source.h note all look good
On the test front: I don't think a hang-detection test can be made reliable. The bug requires the source's insert LSN to be exactly
segment_boundary + SizeOfXLogLongPHD with no further WAL activity, but bgwriter's periodic LogStandbySnapshot emits a RUNNING_XACTS which can advance the insert LSN
nondeterministically between pg_switch_wal() and the rewind. In my reproduction bgwriter ended the hang after ~9s; that's the kind of timing we don't want in CI.
The deterministic alternative is to parse pg_controldata on the target after pg_rewind and assert minRecoveryPoint does not land
at "boundary + SizeOfXLogLongPHD". That's a direct check on the patched behavior independent of source idleness or replay
timing. It doesn't exercise the integration property that the rewound node reaches consistency without further upstream WAL.
So I am not sure if this testcase is a complete one in our scenario.
Regards,
Surya Poondla
Thanks for the v3, the commit message, in-line comment, and the rewind_source.h note all look good
On the test front: I don't think a hang-detection test can be made reliable. The bug requires the source's insert LSN to be exactly
segment_boundary + SizeOfXLogLongPHD with no further WAL activity, but bgwriter's periodic LogStandbySnapshot emits a RUNNING_XACTS which can advance the insert LSN
nondeterministically between pg_switch_wal() and the rewind. In my reproduction bgwriter ended the hang after ~9s; that's the kind of timing we don't want in CI.
The deterministic alternative is to parse pg_controldata on the target after pg_rewind and assert minRecoveryPoint does not land
at "boundary + SizeOfXLogLongPHD". That's a direct check on the patched behavior independent of source idleness or replay
timing. It doesn't exercise the integration property that the rewound node reaches consistency without further upstream WAL.
So I am not sure if this testcase is a complete one in our scenario.
Regards,
Surya Poondla
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
solai v <solai.cdac@gmail.com>
Дата:
Hi all, On Wed, Jul 15, 2026 at 5:39 PM cca5507 wrote: > > Hi, > > Thanks for the comments! > > > 1. Commit message understates the fix. It only describes the page-header symptom. The crash-safety property is the stronger argument and the one that resolves the > back-and-forth on which LSN to use. Suggest adding: > > a. "Using the flush LSN is also crash-safe with respect to the source: the insert LSN lives only in shared memory and can be lost on a source crash, leaving the standby's minRecoveryPoint ahead of any LSN the source can subsequently reach." > > 2. Code comment should explain why flush LSN is sufficient. The current "We must replay to the last WAL flush location" doesn't say why. Suggest: > > a. "Use the source's flush LSN as the target's minRecoveryPoint: every WAL-logged page we copied has page-LSN <= source's flush LSN at copy time (WAL-before-data), and flush LSN is monotonic. We avoid the insert LSN because it can sit one page-header past a record's end at segment boundaries (where no record will end), and it is not durable, a source crash can leave flush LSN behind an insert LSN we already pinned." > > 3. Worth a comment in rewind_source.h that the callback must only be invoked against a non-standby source, pg_current_wal_flush_lsn() errors out under recovery. > > Fixed. > > > 4. No regression test. We can add a regression test under src/bin/pg_rewind/t/. > > Currently I don't have a good idea about the test, I will work on it later. Any help is welcome! > I reviewed and tested this patch on my PostgreSQL 20devel tree. The patch applied cleanly and built successfully without any issues. I first reproduced the reported issue on an unpatched tree using a two-node streaming replication setup. After promoting the standby, stopping the old primary, ensuring the WAL insert LSN reached the required boundary, and running pg_rewind, the rewound server entered standby mode and started streaming from the promoted primary. However, it remained in recovery but the client connections failed with: psql: error: connection to server on socket "/tmp/.s.PGSQL.55450" failed: FATAL: the database system is not yet accepting connections DETAIL: Consistent recovery state has not been yet reached. Before generating new WAL: LOG: redo starts at 0/07000028 LOG: started streaming WAL from primary at 0/0C000000 on timeline 2 FATAL: the database system is not yet accepting connections DETAIL: Consistent recovery state has not been yet reached. After generating WAL on the promoted primary, the server reached a consistent recovery state only after generating additional WAL on the promoted primary, which matches the behavior described in the discussion thread: LOG: completed backup recovery with redo LSN 0/07000028 and end LSN 0/0C000028 LOG: consistent recovery state reached at 0/0C000048 LOG: database system is ready to accept read-only connections I then applied the V3 patch and repeated the same test. After pg_rewind, the rewound standby completed backup recovery, reached a consistent recovery state, and became ready to accept read-only connections without requiring any additional WAL generation on the promoted primary: LOG: completed backup recovery with redo LSN 0/07000060 and end LSN 0/0E000060 LOG: consistent recovery state reached at 0/0E000060 LOG: invalid record length at 0/0E000060: expected at least 24, got 0 LOG: database system is ready to accept read-only connections LOG: started streaming WAL from primary at 0/0E000000 on timeline 2 I also verified that the rewound server remained in recovery and that the replicated data was accessible. Overall based on my testing, the patch resolves the reported issue, and did not observe any regressions during the tested scenario. The patch looks good to me. Regards, Solai
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi ChangAo,
The latest patch looks good.
The latest patch looks good.
Regards,
Surya Poondla
Surya Poondla
On Sun, Aug 23, 2026 at 2:43 AM cca5507 <cca5507@qq.com> wrote:
Hi,
Rebased.
--
Regards,
ChangAo Chen
Re: [BUG] Take a long time to reach consistent after pg_rewind
On Tue, Aug 25, 2026 at 2:48 AM surya poondla wrote: > The latest patch looks good. Attached is the updated version of the patch. It fixes the pg_rewind docs, which still described pg_current_wal_insert_lsn() as the minimum consistency LSN, to refer to pg_current_wal_flush_lsn() for a live primary. I also revised the commit message and made some cosmetic changes. Is it sufficient to apply this only to master, or we should backpatch it to all supported versions? I've also attached backpatch versions in case we decide to backpatch. Regards, -- Fujii Masao
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
surya poondla <suryapoondla4@gmail.com>
Дата:
Hi Fujii,
Thanks for v5.
I tested v5 on master, the patch looks good.
Regarding backpatching, yes it can go to all supported versions.
Regards,
Surya Poondla
Thanks for v5.
I tested v5 on master, the patch looks good.
Regarding backpatching, yes it can go to all supported versions.
Regards,
Surya Poondla
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 8:34 AM surya poondla wrote: > > Hi Fujii, > > Thanks for v5. > > I tested v5 on master, the patch looks good. > Regarding backpatching, yes it can go to all supported versions. > > Regards, > Surya Poondla +1 for backpatching and v5 LGTM. Do we need a regression test for this issue in master? It seems that 24723e7cf39, b1f14c96720 and the $subject all suffer from the extra header skipping of pg_current_wal_insert_lsn()/GetXLogInsertRecPtr(). Is it helpful to say more about this function's hidden intricacy(maybe as a separate patch)? -- Regards, Xuneng Zhou HighGo Software Co., Ltd.
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 5:19 PM Xuneng Zhou wrote: > +1 for backpatching and v5 LGTM. Thanks for the review! I've pushed the patch. > Do we need a regression test for this > issue in master? It seems that 24723e7cf39, b1f14c96720 and the > $subject all suffer from the extra header skipping of > pg_current_wal_insert_lsn()/GetXLogInsertRecPtr(). So you're suggesting adding tests to verify that some operations such as WAIT FOR and pg_rewind handle the extra header situation correctly? Regards, -- Fujii Masao
Re: [BUG] Take a long time to reach consistent after pg_rewind
От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Fri, Nov 20, 2026 at 8:05 PM Fujii Masao wrote: > > On Fri, Nov 20, 2026 at 5:19 PM Xuneng Zhou wrote: > > +1 for backpatching and v5 LGTM. > > Thanks for the review! I've pushed the patch. > > > Do we need a regression test for this > > issue in master? It seems that 24723e7cf39, b1f14c96720 and the > > $subject all suffer from the extra header skipping of > > pg_current_wal_insert_lsn()/GetXLogInsertRecPtr(). > > So you're suggesting adding tests to verify that some operations such as > WAIT FOR and pg_rewind handle the extra header situation correctly? WAIT has the test in 24723e7cf39. I'm not sure whether it's feasible/needed to add one for this issue. I'm thinking whether we need to document/comment more about how pg_current_wal_insert_lsn()/GetXLogInsertRecPtr() and its peer GetXLogInsertEndRecPtr() handle the extra header situation . The current abrevity of doc/comment seems to assume that users already know the differences between these two and when to use each. But that assumption seems unheld because we've seen several issues of misusing them. -- Regards, Xuneng Zhou HighGo Software Co., Ltd.