postgres.git / summary / log / commit / refs
commit 459595e3270d26cc0bc9b78602db1db4b82002af
Author: Fujii Masao <fujii@postgresql.org>
Date: Thu Nov 19 07:10:49 2026 +0000
Fix pg_combinebackup handling of intermediate truncations
Restoring a backup with pg_combinebackup could bring back deleted
rows and leave tables inconsistent with their indexes. This could
happen when a relation was truncated before an intermediate
incremental backup and later extended with zero-filled pages that
were not recorded in WAL, as can occur during bulk relation extension
by COPY.
The problem was that pg_combinebackup used only the latest
incremental file's truncation_block_length to determine which blocks
could be copied from earlier backups. It ignored smaller truncation
limits in intermediate backups, allowing stale blocks from before a
truncation to be copied into pages that should have been zero-filled.
Fix this by tracking the truncation limit while walking backward
through the backup chain, lowering it after processing each incremental
file's own blocks. This prevents older backups from supplying blocks
at or beyond the limit while preserving blocks supplied by newer backups.
Backpatch to 17, where incremental backup was introduced.
Reported-by: Ke <kehan5800@gmail.com>
Author: Ke <kehan5800@gmail.com>
Author: Rajesh Kumar Thandapani <rajeshkumarth@google.com>
Reviewed-by: Raja Sai Pranav <rajasaipranav0@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/19776-c9e815f92ff29628@postgresql.org
Discussion: https://postgr.es/m/CAJoZ81s07r3akkKp-nJ8sgQNoCM0nu24g3HZ0FxTSY1OqsgurQ@mail.gmail.com
Backpatch-through: 17
src/bin/pg_combinebackup/reconstruct.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/src/bin/pg_combinebackup/reconstruct.c b/src/bin/pg_combinebackup/reconstruct.c
index f0fa36ec98b..c6f1af249a5 100644
--- a/src/bin/pg_combinebackup/reconstruct.c
+++ b/src/bin/pg_combinebackup/reconstruct.c
@@ -106,6 +106,7 @@ reconstruct_from_incremental_file(char *input_filename,
off_t *offsetmap;
unsigned block_length;
unsigned i;
+ unsigned truncation_block_length;
unsigned sidx = n_prior_backups;
bool full_copy_possible = true;
int copy_source_index = -1;
@@ -129,6 +130,7 @@ reconstruct_from_incremental_file(char *input_filename,
latest_source = make_incremental_rfile(input_filename);
source[n_prior_backups] = latest_source;
block_length = find_reconstructed_block_length(latest_source);
+ truncation_block_length = latest_source->truncation_block_length;
/*
* For each block in the output file, we need to know from which file we
@@ -225,7 +227,7 @@ reconstruct_from_incremental_file(char *input_filename,
* later backup is one that we can't validate.
*/
blocklength = sb.st_size / BLCKSZ;
- for (b = 0; b < latest_source->truncation_block_length; ++b)
+ for (b = 0; b < truncation_block_length; ++b)
{
if (sourcemap[b] == NULL && b < blocklength)
{
@@ -265,7 +267,7 @@ reconstruct_from_incremental_file(char *input_filename,
{
BlockNumber b = s->relative_block_numbers[i];
- if (b < latest_source->truncation_block_length &&
+ if (b < truncation_block_length &&
sourcemap[b] == NULL)
{
sourcemap[b] = s;
@@ -279,6 +281,19 @@ reconstruct_from_incremental_file(char *input_filename,
full_copy_possible = false;
}
}
+
+ /*
+ * Blocks at or beyond this incremental file's truncation_block_length
+ * were either not present or truncated away before this backup, so
+ * their contents in any earlier backup are stale and must not be
+ * used. This also prevents copying an earlier full file whole even if
+ * its size happens to match latest_source->truncation_block_length.
+ */
+ if (s->truncation_block_length < truncation_block_length)
+ {
+ truncation_block_length = s->truncation_block_length;
+ full_copy_possible = false;
+ }
}
/*
[parent: 1901aa07eda1]