From 58a56ede6c2b629ccb277aae5453fc6b3ddb3669 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Fri, 4 Dec 2026 09:48:34 -0600 Subject: [PATCH v2 2/2] pg_combinebackup: Add --transfer-mode. pg_combinebackup has a separate option for each of its file transfer modes (--clone, --copy, --copy-file-range, and --link). As was recently done for pg_upgrade, this commit adds a new --transfer-mode option that accepts the mode as its argument (e.g., --transfer-mode=link). The existing options are now documented as deprecated spellings of the new one, but they continue to work as before. Discussion: https://postgr.es/m/axGSXghPd_TNT5qZ%40nathan --- doc/src/sgml/ref/pg_combinebackup.sgml | 156 +++++++++++++------- src/bin/pg_combinebackup/pg_combinebackup.c | 26 +++- src/bin/pg_combinebackup/t/010_hardlink.pl | 2 +- 3 files changed, 126 insertions(+), 58 deletions(-) diff --git a/doc/src/sgml/ref/pg_combinebackup.sgml b/doc/src/sgml/ref/pg_combinebackup.sgml index 675b802463a..fef2ee12445 100644 --- a/doc/src/sgml/ref/pg_combinebackup.sgml +++ b/doc/src/sgml/ref/pg_combinebackup.sgml @@ -86,26 +86,7 @@ PostgreSQL documentation - Use hard links instead of copying files to the synthetic backup. - Reconstruction of the synthetic backup might be faster (no file copying) - and use less disk space, but care must be taken when using the output - directory, because any modifications to that directory (for example, - starting the server) can also affect the input directories. Likewise, - changes to the input directories (for example, starting the server on - the full backup) could affect the output directory. Thus, this option - is best used when the input directories are only copies that will be - removed after pg_combinebackup has completed. - - - - Requires that the input backups and the output directory are in the - same file system. - - - - If a backup manifest is not available or does not contain checksum of - the right type, hard links will still be created, but the file will be - also read block-by-block for the checksum calculation. + Deprecated spelling of . @@ -170,23 +151,7 @@ PostgreSQL documentation - Use efficient file cloning (also known as reflinks on - some systems) instead of copying files to the new data directory, - which can result in near-instantaneous copying of the data files. - - - - If a backup manifest is not available or does not contain checksum of - the right type, file cloning will be used to copy the file, but the - file will be also read block-by-block for the checksum calculation. - - - - File cloning is only supported on some operating systems and file - systems. If it is selected but not supported, the - pg_combinebackup run will error. At present, - it is supported on Linux (kernel 4.5 or later) with Btrfs and XFS (on - file systems created with reflink support), and on macOS with APFS. + Deprecated spelling of . @@ -195,9 +160,7 @@ PostgreSQL documentation - Perform regular file copy. This is the default. (See also - , , and - /.) + Deprecated spelling of . @@ -206,18 +169,8 @@ PostgreSQL documentation - Use the copy_file_range system call for efficient - copying. On some file systems this gives results similar to - , sharing physical disk blocks, while on others - it may still copy blocks, but do so via an optimized path. At present, - it is supported on Linux and FreeBSD. - - - - If a backup manifest is not available or does not contain checksum of - the right type, copy_file_range will be used to - copy the file, but the file will be also read block-by-block for the - checksum calculation. + Deprecated spelling of + . @@ -273,6 +226,105 @@ PostgreSQL documentation + + + + + Specifies how files are transferred from the input directories to the + output directory. mode can be one of the + following: + + + + clone + + + Use efficient file cloning (also known as reflinks + on some systems) instead of copying files to the new data + directory, which can result in near-instantaneous copying of the + data files. + + + + If a backup manifest is not available or does not contain checksum + of the right type, file cloning will be used to copy the file, but + the file will be also read block-by-block for the checksum + calculation. + + + + File cloning is only supported on some operating systems and file + systems. If it is selected but not supported, the + pg_combinebackup run will error. At + present, it is supported on Linux (kernel 4.5 or later) with Btrfs + and XFS (on file systems created with reflink support), and on + macOS with APFS. + + + + + + copy + + + Perform regular file copy. This is the default. + + + + + + copy-file-range + + + Use the copy_file_range system call for + efficient copying. On some file systems this gives results similar + to clone, sharing physical disk blocks, while on + others it may still copy blocks, but do so via an optimized path. + At present, it is supported on Linux and FreeBSD. + + + + If a backup manifest is not available or does not contain checksum + of the right type, copy_file_range will be + used to copy the file, but the file will be also read + block-by-block for the checksum calculation. + + + + + + link + + + Use hard links instead of copying files to the synthetic backup. + Reconstruction of the synthetic backup might be faster (no file + copying) and use less disk space, but care must be taken when using + the output directory, because any modifications to that directory + (for example, starting the server) can also affect the input + directories. Likewise, changes to the input directories (for + example, starting the server on the full backup) could affect the + output directory. Thus, this mode is best used when the input + directories are only copies that will be removed after + pg_combinebackup has completed. + + + + Requires that the input backups and the output directory are in the + same file system. + + + + If a backup manifest is not available or does not contain checksum + of the right type, hard links will still be created, but the file + will be also read block-by-block for the checksum calculation. + + + + + + + + diff --git a/src/bin/pg_combinebackup/pg_combinebackup.c b/src/bin/pg_combinebackup/pg_combinebackup.c index 61ac17d6a11..6fa39bc5834 100644 --- a/src/bin/pg_combinebackup/pg_combinebackup.c +++ b/src/bin/pg_combinebackup/pg_combinebackup.c @@ -143,6 +143,7 @@ main(int argc, char *argv[]) {"clone", no_argument, NULL, 4}, {"copy", no_argument, NULL, 5}, {"copy-file-range", no_argument, NULL, 6}, + {"transfer-mode", required_argument, NULL, 7}, {NULL, 0, NULL, 0} }; @@ -221,6 +222,18 @@ main(int argc, char *argv[]) case 6: opt.copy_method = COPY_METHOD_COPY_FILE_RANGE; break; + case 7: + if (strcmp(optarg, "clone") == 0) + opt.copy_method = COPY_METHOD_CLONE; + else if (strcmp(optarg, "copy") == 0) + opt.copy_method = COPY_METHOD_COPY; + else if (strcmp(optarg, "copy-file-range") == 0) + opt.copy_method = COPY_METHOD_COPY_FILE_RANGE; + else if (strcmp(optarg, "link") == 0) + opt.copy_method = COPY_METHOD_LINK; + else + pg_fatal("invalid argument for option %s", "--transfer-mode"); + break; default: /* getopt_long already emitted a complaint */ pg_log_error_hint("Try \"%s --help\" for more information.", progname); @@ -443,7 +456,7 @@ main(int argc, char *argv[]) /* Warn about the possibility of compromising the backups, when link mode */ if (opt.copy_method == COPY_METHOD_LINK) - pg_log_warning("--link mode was used; any modifications to the output " + pg_log_warning("link mode was used; any modifications to the output " "directory might destructively modify input directories"); /* It's a success, so don't remove the output directories. */ @@ -788,19 +801,22 @@ help(const char *progname) printf(_(" %s [OPTION]... DIRECTORY...\n"), progname); printf(_("\nOptions:\n")); printf(_(" -d, --debug generate lots of debugging output\n")); - printf(_(" -k, --link link files instead of copying\n")); + printf(_(" -k, --link (same as --transfer-mode=link, deprecated)\n")); printf(_(" -n, --dry-run do not actually do anything\n")); printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n")); printf(_(" -o, --output=DIRECTORY output directory\n")); printf(_(" -T, --tablespace-mapping=OLDDIR=NEWDIR\n" " relocate tablespace in OLDDIR to NEWDIR\n")); - printf(_(" --clone clone (reflink) files instead of copying\n")); - printf(_(" --copy copy files (default)\n")); - printf(_(" --copy-file-range copy using copy_file_range() system call\n")); + printf(_(" --clone (same as --transfer-mode=clone, deprecated)\n")); + printf(_(" --copy (same as --transfer-mode=copy, deprecated)\n")); + printf(_(" --copy-file-range (same as --transfer-mode=copy-file-range,\n" + " deprecated)\n")); printf(_(" --manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE\n" " use algorithm for manifest checksums\n")); printf(_(" --no-manifest suppress generation of backup manifest\n")); printf(_(" --sync-method=METHOD set method for syncing files to disk\n")); + printf(_(" --transfer-mode=MODE set file transfer mode to \"clone\",\n" + " \"copy\" (default), \"copy-file-range\", or \"link\"\n")); printf(_(" -V, --version output version information, then exit\n")); printf(_(" -?, --help show this help, then exit\n")); diff --git a/src/bin/pg_combinebackup/t/010_hardlink.pl b/src/bin/pg_combinebackup/t/010_hardlink.pl index 5fbbe6a923b..e4509abd0dc 100644 --- a/src/bin/pg_combinebackup/t/010_hardlink.pl +++ b/src/bin/pg_combinebackup/t/010_hardlink.pl @@ -84,7 +84,7 @@ my $restore = PostgreSQL::Test::Cluster->new('restore'); $restore->init_from_backup( $primary, 'backup2', combine_with_prior => ['backup1'], - combine_mode => '--link'); + combine_mode => '--transfer-mode=link'); # Ensure files have the expected count of hard links. We expect all data files # from test_1 to contain 2 hard links, because they were not touched between the -- 2.55.0