pg_receivewal doesn't truncate compressed WAL files on restart

From: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: pg_receivewal doesn't truncate compressed WAL files on restart
Date: 2026-11-27 03:21:17
Message-ID: CAJTYsWXoBUOjpcdEP=O+oFXzFT1NfqjF1K33Sx-6sjrvosu9hw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While looking at BUG #19890 [1], I ran into another issue with
pg_receivewal. When it restarts and resumes into an existing compressed
.partial segment, it can leave old data at the end of the file. With
LZ4, every later start fails with ERROR_frameType_unknown until you
remove the segment. With gzip, pg_receivewal skips the segment at
startup with an incorrect uncompressed size warning.

dir_open_for_write() reopens the .partial file without O_TRUNC.
So the new compressed stream overwrites from offset 0, and if it ends up
shorter than what was already on disk (for example with --synchronous,
where live flushes compress worse than catching up after a restart), the
tail of the old file stays even after the segment is renamed.

IIUC, compressed segments are never padded and are always written from
the start, so we can just pass O_TRUNC for them. Attached 0001 does
that (I could repro it on all supported versions, so it seems worth
backpatching down to v15). 0002 is a TAP test that reproduces the
failure, not meant for commit.

Thoughts?

[1] https://postgr.es/m/19890-293babc93c520dc8@postgresql.org

Regards,
Ayush

Attachment Content-Type Size
v1-0001-pg_receivewal-Truncate-compressed-WAL-files-when-.patch application/octet-stream 2.5 KB
v1-0002-Add-a-test-for-resuming-into-a-compressed-partial.patch application/octet-stream 2.8 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-11-27 03:28:52 Re: Use option_parse_int() for pg_createsubscriber --recovery-timeout and pgbench --max-tries
Previous Message Chao Li 2026-11-27 02:42:01 Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument