From 56b850048fe2ec8be640745dafb8644a0e97df6f Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Sun, 6 Dec 2026 21:45:03 +0800 Subject: [PATCH v1] pg_combinebackup: Do not copy tablespace_map into output directory. When combining backups whose final input backup contains a tablespace_map file (such as an unpacked tar-format or server-side backup), pg_combinebackup copied tablespace_map verbatim into the output directory and recorded it in the generated backup_manifest. Because pg_combinebackup writes a plain-format output directory and already creates the pg_tblspc symbolic links (or in-place directories) according to any -T/--tablespace-mapping options, keeping the stale tablespace_map file caused recovery on the combined backup to unlink the pg_tblspc symbolic links and recreate them pointing to the source server's original tablespace paths (or fail to start if pg_tblspc/ was materialized as a non-empty in-place directory). On the same host, starting the combined cluster could then silently modify or corrupt the live source server's tablespaces. Fix this by skipping tablespace_map alongside backup_label and backup_manifest in process_directory_recursively(), matching basebackup and pg_rewind. Backpatch to 17, where pg_combinebackup was introduced. Author: Zhijie Hou Backpatch-through: 17 --- src/bin/pg_combinebackup/Makefile | 3 + src/bin/pg_combinebackup/meson.build | 1 + src/bin/pg_combinebackup/pg_combinebackup.c | 9 ++- .../pg_combinebackup/t/002_compare_backups.pl | 59 +++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/bin/pg_combinebackup/Makefile b/src/bin/pg_combinebackup/Makefile index 7a6c44d67c8..1e7a81340d5 100644 --- a/src/bin/pg_combinebackup/Makefile +++ b/src/bin/pg_combinebackup/Makefile @@ -18,6 +18,9 @@ subdir = src/bin/pg_combinebackup top_builddir = ../../.. include $(top_builddir)/src/Makefile.global +# make these available to TAP test scripts +export TAR + override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils diff --git a/src/bin/pg_combinebackup/meson.build b/src/bin/pg_combinebackup/meson.build index 64da66b338e..519cdc8dcd2 100644 --- a/src/bin/pg_combinebackup/meson.build +++ b/src/bin/pg_combinebackup/meson.build @@ -27,6 +27,7 @@ tests += { 'sd': meson.current_source_dir(), 'bd': meson.current_build_dir(), 'tap': { + 'env': {'TAR': tar.found() ? tar.full_path() : ''}, 'tests': [ 't/001_basic.pl', 't/002_compare_backups.pl', diff --git a/src/bin/pg_combinebackup/pg_combinebackup.c b/src/bin/pg_combinebackup/pg_combinebackup.c index 61ac17d6a11..e9110d6474e 100644 --- a/src/bin/pg_combinebackup/pg_combinebackup.c +++ b/src/bin/pg_combinebackup/pg_combinebackup.c @@ -1027,12 +1027,15 @@ process_directory_recursively(Oid tsoid, } /* - * Skip the backup_label and backup_manifest files; they require - * special handling and are handled elsewhere. + * Skip backup_label and backup_manifest, as they require special + * handling and are generated elsewhere. Also skip tablespace_map, + * since tablespace links are created directly in pg_tblspc and + * keeping it would cause recovery to overwrite them. */ if (relative_path == NULL && (strcmp(de->d_name, "backup_label") == 0 || - strcmp(de->d_name, "backup_manifest") == 0)) + strcmp(de->d_name, "backup_manifest") == 0 || + strcmp(de->d_name, "tablespace_map") == 0)) continue; /* diff --git a/src/bin/pg_combinebackup/t/002_compare_backups.pl b/src/bin/pg_combinebackup/t/002_compare_backups.pl index b509296a94a..64da62b1b63 100644 --- a/src/bin/pg_combinebackup/t/002_compare_backups.pl +++ b/src/bin/pg_combinebackup/t/002_compare_backups.pl @@ -3,6 +3,7 @@ use strict; use warnings FATAL => 'all'; use File::Compare qw(compare_text); +use File::Copy; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; @@ -116,6 +117,64 @@ my $archive_wait_query = $primary->poll_query_until('postgres', $archive_wait_query) or die "Timed out while waiting for WAL segment to be archived"; +# Verify that combining an unpacked tar-format incremental backup omits +# tablespace_map from the output directory and manifest. Run this before +# starting pitr2, since --link mode lets pitr2 modify files in backup1. +SKIP: +{ + my $tar = $ENV{TAR}; + skip "no tar program available", 4 + if (!defined $tar || $tar eq ''); + + my $backup2tar = $primary->backup_dir . '/backup2_tar'; + $primary->command_ok( + [ + 'pg_basebackup', + '--no-sync', + '--pgdata' => $backup2tar, + '--format' => 'tar', + '--wal-method' => 'none', + '--checkpoint' => 'fast', + '--incremental' => "$backup1path/backup_manifest", + ], + "tar-format incremental backup"); + + my $backup2untar = $primary->backup_dir . '/backup2_untar'; + my $tsbackup2tar = "$tempdir/tsbackup2tar"; + mkdir($backup2untar) || die "mkdir $backup2untar: $!"; + mkdir($tsbackup2tar) || die "mkdir $tsbackup2tar: $!"; + system_or_bail( + $tar, + 'xf' => "$backup2tar/base.tar", + '-C' => $backup2untar); + system_or_bail( + $tar, + 'xf' => "$backup2tar/$tsoid.tar", + '-C' => $tsbackup2tar); + copy("$backup2tar/backup_manifest", "$backup2untar/backup_manifest") + or die "could not copy $backup2tar/backup_manifest"; + dir_symlink($tsbackup2tar, "$backup2untar/pg_tblspc/$tsoid"); + + my $combined = $primary->backup_dir . '/combined_tar'; + my $tscombined = "$tempdir/tscombined"; + $primary->command_ok( + [ + 'pg_combinebackup', + '--no-sync', + $mode, + '--tablespace-mapping' => "${tsbackup2tar}=${tscombined}", + '--output' => $combined, + $backup1path, + $backup2untar, + ], + "combine unpacked tar incremental backup"); + ok(!-e "$combined/tablespace_map", + "tablespace_map is not copied to combined backup"); + $primary->command_ok( + [ 'pg_verifybackup', '--no-parse-wal', $combined ], + "verify combined backup from tar incremental"); +} + # Perform PITR from the full backup. Disable archive_mode so that the archive # doesn't find out about the new timeline; that way, the later PITR below will # choose the same timeline. -- 2.54.0 (Apple Git-157)