Re: Incremental backup inconsistency after vacuum truncation + COPY regrowth

From: Zhijie Hou <houzhijie22(at)gmail(dot)com>
To: Boyu Yang <pgsql(at)mail-yby(dot)icu>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: Re: Incremental backup inconsistency after vacuum truncation + COPY regrowth
Date: 2026-12-02 16:30:11
Message-ID: 92fabbe7-49ed-47a0-a9ef-8e525976557e@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Wed, Jul29, 2026 at 04:32 PM Boyu Yang wrote:
>
> While studying how limit_block is handled during incremental backup, I
> found a scenario that leads to data inconsistency: a table is
> truncated by vacuum and then regrown by a bulk insert (COPY). After
> combining the incremental backup with the full backup, deleted rows
> reappear. Here is a POC:
...
>
> Analyze
> =======
> Here is a view for blocks layout where only "id" is shown here.
>
> (1) In full backup [1, 2, 3, 4, .... , 20, 21, 22, .... , 39 , 40]
>
> (2) After truncate [1] limit_block = 1
>
> (3) After bulk insert [1, 1000, 1001, 1002, ... , 1019 , NULL, NULL,
> NULL, ... , NULL] 33 blocks totally
>
> (4) In the incremental Backup, the INCREMENTAL.relfilenode
> blocks changed = [1000, 1001, .... , 1019]
> truncation_block_length = 33
>
> (5) combine backup [1, 1000, 1001, .... , 1018 ,1019, 22, 23 , ...39 ,
> 40]
>     |___   From incremental  ___|   |___ From full ____|
>

While investigating incremental backups after VACUUM truncation, I
also faced this issue.

> Fix
> ===
> My proposed fix is admittedly brute-force: if a truncation happened
> anywhere within a segment, we cannot tell how many of the blocks that
> were later added back are all-zero and never WAL-logged, so the safest
> option is to back up that entire segment fully.
>
> So: instead of comparing limit_block with segno * RELSEG_SIZE, we can
> compare it with (segno + 1) * RELSEG_SIZE

Rather than backing up the entire affected segment, I am thinking shall
change the following comparison to '>' so that *truncation_block_length
is clamped to relative_limit, matching the comment (the per-existing
codes appears to be inverted relative to the comment already):

/*
* We can't set a truncation_block_length in excess of the limit block
* number (relativized to the current segment). To do so would be to
* treat blocks from older backups as valid current contents even if
* they were subsequently truncated away.
*/
if (*truncation_block_length < relative_limit)
*truncation_block_length = relative_limit;

When clamping occurs, we also need to include the segment's final block
in the incremental file if it is not already present (and fall back to a
full file backup if that pushes the block count past the 90% threshold).
This ensures that reconstruction preserves the segment's actual length
rather than dropping trailing unwritten blocks, which is especially
important when a subsequent segment exists. Attached is a patch to show
this idea and a TAP test.

--
Best Regards,
Zhijie Hou

Attachment Content-Type Size
v1-0001-Fix-stale-blocks-in-incremental-backups-after-rel.patch text/plain 7.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-12-02 16:47:40 Re: Restore check_mut_excl_opts, usage in pg_restore and pg_dumpall
Previous Message Fujii Masao 2026-12-02 16:11:55 Re: Disable startup progress timeout during standby WAL replay