Do not fail WAL buffers read after on-disk xlog removal
Do not fail WAL buffers read after on-disk xlog removal
Hi, XLogSendPhysical checks for on-disk xlog removal even when the entire requested range was copied from WAL buffers. So this rejects data already available in the output buffer and terminates streaming unnecessarily. Attached checks for xlog files presence only in WAL buffers read failed. -- Best regards, Kirill Reshke
Re: Do not fail WAL buffers read after on-disk xlog removal
On Sat, 24 Oct 2026 at 22:19, Kirill Reshke wrote: > > Hi, > > XLogSendPhysical checks for on-disk xlog removal even when the entire requested > range was copied from WAL buffers. So this rejects data already available in the > output buffer and terminates streaming unnecessarily. > > Attached checks for xlog files presence only in WAL buffers read failed. > > -- > Best regards, > Kirill Reshke Also PFA TAP test demonstrating an issue. Not intended to commit thought. Just show-caseing a bug -- Best regards, Kirill Reshke
Re: Do not fail WAL buffers read after on-disk xlog removal
Hi Kirill,
The fix looks right to me. Your test fails 10 of 10 runs on master
and passes 10 of 10 with the fix.
A new walsender still fails, because its reader starts with
seg.ws_tli = 0, so the first read never uses the buffers. If I
terminate the paused walsender in your test, the receiver never
catches up, with or without the fix. Passing sendTimeLine fixes it
for me when wal_buffers is larger than a segment. Test and change
attached.
Also, the pg_stat_io check passes without the fix too, so I would drop
it. And since 16 never read from the buffers, I would keep this to
master.
Thanks,
The fix looks right to me. Your test fails 10 of 10 runs on master
and passes 10 of 10 with the fix.
A new walsender still fails, because its reader starts with
seg.ws_tli = 0, so the first read never uses the buffers. If I
terminate the paused walsender in your test, the receiver never
catches up, with or without the fix. Passing sendTimeLine fixes it
for me when wal_buffers is larger than a segment. Test and change
attached.
Also, the pg_stat_io check passes without the fix too, so I would drop
it. And since 16 never read from the buffers, I would keep this to
master.
Thanks,
Shihao
Re: Do not fail WAL buffers read after on-disk xlog removal
On Sat, Oct 24, 2026 at 10:19:42PM +0500, Kirill Reshke wrote:
> XLogSendPhysical checks for on-disk xlog removal even when the entire requested
> range was copied from WAL buffers. So this rejects data already available in the
> output buffer and terminates streaming unnecessarily.
>
> Attached checks for xlog files presence only in WAL buffers read failed.
- /* See logical_read_xlog_page(). */
- XLByteToSeg(startptr, segno, xlogreader->segcxt.ws_segsize);
- CheckXLogRemoved(segno, xlogreader->seg.ws_tli);
+ /*
+ * Check that the segment read from disk was not recycled while we were
+ * reading it. See logical_read_xlog_page(). WALReadFromBuffers() validates
+ * its own reads, which need not have corresponding files in pg_wal.
+ */
+ if (nbytes > 0)
+ {
+ XLByteToSeg(startptr, segno, xlogreader->segcxt.ws_segsize);
+ CheckXLogRemoved(segno, xlogreader->seg.ws_tli);
+ }
I have a hard time believing that this should be treated as a bug.
The current check is just not optimal as it is not required in the
case where we have no remaining data to read from the on-disk WAL
files, having read everything from the WAL buffers. I'd leave the
stable branches out of that, even if it means that a WAL sender can be
interrupted more frequently if a segment has been removed concurrently
of a physical read.
It seems to me that it would make the code more readable to move the
XLByteToSeg() and CheckXLogRemoved() calls to be in the same "if"
block as the WALRead(). No need for a comment if you move that, and
as far as I can see we don't need the XLByteToSeg() at all in the case
where we have read all the WAL from the buffers.
--
Michael