From b8bbe0e6acd4eacafc36784c7f0f3e7ff36cfaa2 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Sun, 6 Dec 2026 13:51:49 +0800 Subject: [PATCH v2] Fix stale blocks in incremental backups after relation truncation. When a relation is truncated and later extended with unwritten zero-filled blocks before an incremental backup, such as during COPY bulk extension, GetFileBackupMethod() sets truncation_block_length to the current file size rather than clamping it to the WAL summary's limit block. Because the comparison against relative_limit used '<' instead of '>', pg_combinebackup treats those unwritten blocks as unchanged and copies pre-truncation blocks from the earlier backup, bringing back deleted rows and leaving indexes inconsistent with the heap. When the current segment size was smaller than relative_limit, the reversed comparison also inflated truncation_block_length past the segment's actual block count. Fix this by clamping truncation_block_length down to relative_limit when the current segment size exceeds it. When the segment has grown past limit_block, also include its final block in the incremental backup before checking the 90% full-backup threshold, so reconstruction zero-fills unwritten blocks and preserves the segment length (otherwise a short segment would hide any subsequent segments). With truncation_block_length only lowered from the segment's current block count (which is already bounded by RELSEG_SIZE), replace the now-unreachable RELSEG_SIZE cap added by commit ecd275718be with an assertion. Note that incremental backups taken before this fix already record the unclamped truncation_block_length in their incremental files, so a new backup should be taken from an uncorrupted server after updating. Reported-by: Boyu Yang Author: Zhijie Hou Reviewed-by: Raja Sai Pranav Reviewed-by: zibuyu Discussion: https://postgr.es/m/CAPnDWbagY0Y91qZmJkuJX4+byVJ-dJysnx4QmW23wgR_AvECVg@mail.gmail.com Backpatch-through: 17 --- src/backend/backup/basebackup_incremental.c | 30 +++++---- .../pg_combinebackup/t/011_ib_truncation.pl | 63 +++++++++++++++++-- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c index bb52c94b133..21d0bf45586 100644 --- a/src/backend/backup/basebackup_incremental.c +++ b/src/backend/backup/basebackup_incremental.c @@ -691,6 +691,7 @@ GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path, BlockNumber limit_block; BlockNumber start_blkno; BlockNumber stop_blkno; + bool include_last_block; RelFileLocator rlocator; BlockRefTableEntry *brtentry; unsigned i; @@ -820,11 +821,22 @@ GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path, segno, size)); /* - * This will write *absolute* block numbers into the output array, but - * we'll transpose them below. + * If the segment has grown past limit_block, trailing zero-filled blocks + * may have been added without generating WAL. Include the segment's final + * block explicitly so reconstruction zero-fills unwritten blocks and + * preserves the segment length, since a short segment would hide any + * subsequent segments. Exclude that block from the summary lookup so it + * appears once. This writes *absolute* block numbers into the output + * array, which we'll transpose below. */ - nblocks = BlockRefTableEntryGetBlocks(brtentry, start_blkno, stop_blkno, + include_last_block = limit_block < stop_blkno; + nblocks = BlockRefTableEntryGetBlocks(brtentry, start_blkno, + include_last_block ? stop_blkno - 1 : stop_blkno, relative_block_numbers, RELSEG_SIZE); + + if (include_last_block) + relative_block_numbers[nblocks++] = stop_blkno - 1; + Assert(nblocks <= RELSEG_SIZE); /* @@ -880,18 +892,12 @@ GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path, * treat blocks from older backups as valid current contents even if * they were subsequently truncated away. */ - if (*truncation_block_length < relative_limit) + if (*truncation_block_length > relative_limit) *truncation_block_length = relative_limit; - - /* - * We also can't set a truncation_block_length in excess of the - * segment size, since the reconstructed file can't be larger than - * that. - */ - if (*truncation_block_length > RELSEG_SIZE) - *truncation_block_length = RELSEG_SIZE; } + Assert(*truncation_block_length <= RELSEG_SIZE); + /* Send it incrementally. */ return BACK_UP_FILE_INCREMENTALLY; } diff --git a/src/bin/pg_combinebackup/t/011_ib_truncation.pl b/src/bin/pg_combinebackup/t/011_ib_truncation.pl index 11749286467..19d438d42e8 100644 --- a/src/bin/pg_combinebackup/t/011_ib_truncation.pl +++ b/src/bin/pg_combinebackup/t/011_ib_truncation.pl @@ -1,8 +1,9 @@ # Copyright (c) 2025-2026, PostgreSQL Global Development Group # -# This test aims to validate two things: (1) that the calculated truncation -# block never exceeds the segment size and (2) that the correct limit block -# length is calculated for the VM fork. +# This test aims to validate three things: (1) that the calculated truncation +# block never exceeds the segment size, (2) that the correct limit block +# length is calculated for the VM fork, and (3) that truncating and regrowing +# a relation with unwritten zero-filled blocks does not restore stale blocks. use strict; use warnings FATAL => 'all'; @@ -13,7 +14,12 @@ use Test::More; # Initialize primary node my $primary = PostgreSQL::Test::Cluster->new('primary'); $primary->init(has_archiving => 1, allows_streaming => 1); -$primary->append_conf('postgresql.conf', 'summarize_wal = on'); +# Raise shared_buffers above the 1MB test default so COPY can extend +# relations by multiple blocks at once. +$primary->append_conf('postgresql.conf', <start; # Backup locations @@ -115,7 +121,54 @@ my $restored_count = is($restored_count, $rows_after_truncation, 'Restored backup has correct row count'); -$primary->stop; $restored->stop; +# Truncate t to four blocks and regrow it via two COPY commands with a low +# fillfactor so each COPY extends t by two blocks and writes only the first. +# With six-block segments, the first unwritten block is at the end of the +# first segment and a new row is in the second, verifying that reconstruction +# zero-fills unwritten blocks and preserves the first segment's length. +my $pad = + '0123456789ABCDEF0123456789ABCDEF' x int($block_size / (2 * 32)); +my $t_relpath = + $primary->safe_psql('postgres', "SELECT pg_relation_filepath('t');"); +(my $t_inc_relpath = $t_relpath) =~ s{([^/]+)$}{INCREMENTAL.$1}; + +$primary->safe_psql('postgres', < 4; +VACUUM (TRUNCATE) t; +COPY t FROM STDIN; +100 $pad +\\. +COPY t FROM STDIN; +101 $pad +\\. +EOF + +$t_blocks = $primary->safe_psql('postgres', + "SELECT pg_relation_size('t') / $block_size;"); +is($t_blocks, '8', 'COPY re-extended the relation to eight blocks'); + +$primary->backup('incr2', + backup_options => [ '--incremental', "$full_backup/backup_manifest" ]); +$primary->stop; + +ok(-f "$backup_path/incr2/$t_inc_relpath", + 'truncated and regrown relation was backed up incrementally'); + +my $node3 = PostgreSQL::Test::Cluster->new('node3'); +$node3->init_from_backup($primary, 'incr2', combine_with_prior => ['full']); +$node3->start(); + +my $node3_rows = + $node3->safe_psql('postgres', "SELECT array_agg(id ORDER BY id) FROM t;"); +is($node3_rows, '{1,2,3,4,100,101}', + 'restored backup does not retain rows from truncated blocks'); +my $node3_blocks = $node3->safe_psql('postgres', + "SELECT pg_relation_size('t') / $block_size;"); +is($node3_blocks, '8', 'restored backup preserves relation block length'); + +$node3->stop; + done_testing(); -- 2.54.0 (Apple Git-157)