From 4948f6cb88e4d0378dba9fad2d97f10abd9eb593 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Thu, 3 Dec 2026 09:54:26 -0600 Subject: [PATCH v1 1/1] pg_upgrade: Add --transfer-mode. pg_upgrade has a separate option for each of its file transfer modes (--clone, --copy, --copy-file-range, --link, and --swap), even though only one mode can be used at a time. 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, and it seems unlikely that we'll ever be able to remove them. --- doc/src/sgml/ref/pgupgrade.sgml | 158 ++++++++++++++------- doc/src/sgml/runtime.sgml | 2 +- src/bin/pg_upgrade/option.c | 29 +++- src/bin/pg_upgrade/t/006_transfer_modes.pl | 12 +- 4 files changed, 140 insertions(+), 61 deletions(-) diff --git a/doc/src/sgml/ref/pgupgrade.sgml b/doc/src/sgml/ref/pgupgrade.sgml index e4e8c02e6d6..d492420bc3b 100644 --- a/doc/src/sgml/ref/pgupgrade.sgml +++ b/doc/src/sgml/ref/pgupgrade.sgml @@ -133,8 +133,11 @@ PostgreSQL documentation - use hard links instead of copying files to the new - cluster + + + Deprecated spelling of . + + @@ -221,19 +224,7 @@ PostgreSQL documentation - Use efficient file cloning (also known as reflinks on - some systems) instead of copying files to the new cluster. This can - result in near-instantaneous copying of the data files, giving the - speed advantages of / while - leaving the old cluster untouched. - - - - File cloning is only supported on some operating systems and file - systems. If it is selected but not supported, the - pg_upgrade 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 . @@ -242,9 +233,7 @@ PostgreSQL documentation - Copy files to the new cluster. This is the default. (See also - , , - , and .) + Deprecated spelling of . @@ -253,11 +242,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. + Deprecated spelling of + . @@ -328,24 +314,7 @@ PostgreSQL documentation - Move the data directories from the old cluster to the new cluster. - Then, replace the catalog files with those generated for the new - cluster. This mode can outperform , - , , and - , especially on clusters with many - relations. - - - However, this mode creates many garbage files in the old cluster, which - can prolong the file synchronization step if - is used. Therefore, it is - recommended to use with - . - - - Additionally, once the file transfer step begins, the old cluster will - be destructively modified and therefore will no longer be safe to - start. See for details. + Deprecated spelling of . @@ -373,6 +342,98 @@ PostgreSQL documentation + + mode + + + Specifies how files are transferred from the old cluster to the new + cluster. 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 cluster. + This can result in near-instantaneous copying of the data files, + giving the speed advantages of link while + leaving the old cluster untouched. + + + + File cloning is only supported on some operating systems and file + systems. If it is selected but not supported, the + pg_upgrade 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 + + + Copy files to the new cluster. 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. + + + + + + link + + + Use hard links instead of copying files to the new cluster. + + + + + + swap + + + Move the data directories from the old cluster to the new cluster. + Then, replace the catalog files with those generated for the new + cluster. This mode can outperform link, + clone, copy, and + copy-file-range, especially on clusters with + many relations. + + + However, this mode creates many garbage files in the old cluster, + which can prolong the file synchronization step if + is used. Therefore, it is + recommended to use with + swap. + + + Additionally, once the file transfer step begins, the old cluster + will be destructively modified and therefore will no longer be + safe to start. See for + details. + + + + + + + + @@ -598,8 +659,7 @@ pg_upgrade.exe running. pg_upgrade --check will also outline any manual adjustments you will need to make after the upgrade. If you are going to be using link, clone, copy-file-range, or swap mode, you - should use the option , , - , or with + should use the option with to enable mode-specific checks. pg_upgrade requires write permission in the current directory. @@ -890,15 +950,15 @@ psql --username=postgres --file=script.sql postgres - If neither nor was - used, the old cluster was unmodified; it can be restarted. + If neither link mode nor swap mode was used, the old cluster was + unmodified; it can be restarted. - If the option was used, the data - files might be shared between the old and new cluster: + If link mode was used, the data files might be shared between the + old and new cluster: @@ -933,8 +993,8 @@ psql --username=postgres --file=script.sql postgres - If the option was used, the old cluster might - be destructively modified: + If swap mode was used, the old cluster might be destructively + modified: diff --git a/doc/src/sgml/runtime.sgml b/doc/src/sgml/runtime.sgml index daa87bfaa64..e38b1a7edd7 100644 --- a/doc/src/sgml/runtime.sgml +++ b/doc/src/sgml/runtime.sgml @@ -1928,7 +1928,7 @@ pg_dumpall -p 5432 | psql -d postgres -p 5433 The module allows an installation to be migrated in-place from one major PostgreSQL version to another. Upgrades can be performed in minutes, - particularly with mode. It requires steps similar to + particularly with . It requires steps similar to pg_dumpall above, e.g., starting/stopping the server, running initdb. The pg_upgrade documentation outlines the necessary steps. diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c index 179eaf0d5e6..7a87bef7667 100644 --- a/src/bin/pg_upgrade/option.c +++ b/src/bin/pg_upgrade/option.c @@ -63,6 +63,7 @@ parseCommandLine(int argc, char *argv[]) {"no-statistics", no_argument, NULL, 5}, {"set-char-signedness", required_argument, NULL, 6}, {"swap", no_argument, NULL, 7}, + {"transfer-mode", required_argument, NULL, 8}, {NULL, 0, NULL, 0} }; @@ -234,6 +235,21 @@ parseCommandLine(int argc, char *argv[]) user_opts.transfer_mode = TRANSFER_MODE_SWAP; break; + case 8: + if (strcmp(optarg, "clone") == 0) + user_opts.transfer_mode = TRANSFER_MODE_CLONE; + else if (strcmp(optarg, "copy") == 0) + user_opts.transfer_mode = TRANSFER_MODE_COPY; + else if (strcmp(optarg, "copy-file-range") == 0) + user_opts.transfer_mode = TRANSFER_MODE_COPY_FILE_RANGE; + else if (strcmp(optarg, "link") == 0) + user_opts.transfer_mode = TRANSFER_MODE_LINK; + else if (strcmp(optarg, "swap") == 0) + user_opts.transfer_mode = TRANSFER_MODE_SWAP; + else + pg_fatal("invalid argument for option %s", "--transfer-mode"); + break; + default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), os_info.progname); @@ -314,7 +330,7 @@ usage(void) printf(_(" -d, --old-datadir=DATADIR old cluster data directory\n")); printf(_(" -D, --new-datadir=DATADIR new cluster data directory\n")); printf(_(" -j, --jobs=NUM number of simultaneous processes or threads to use\n")); - printf(_(" -k, --link link instead of copying files to new cluster\n")); + printf(_(" -k, --link (same as --transfer-mode=link, deprecated)\n")); printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n")); printf(_(" -o, --old-options=OPTIONS old cluster options to pass to the server\n")); printf(_(" -O, --new-options=OPTIONS new cluster options to pass to the server\n")); @@ -325,14 +341,17 @@ usage(void) printf(_(" -U, --username=NAME cluster superuser (default \"%s\")\n"), os_info.user); printf(_(" -v, --verbose enable verbose internal logging\n")); printf(_(" -V, --version display version information, then exit\n")); - printf(_(" --clone clone instead of copying files to new cluster\n")); - printf(_(" --copy copy files to new cluster (default)\n")); - printf(_(" --copy-file-range copy files to new cluster with copy_file_range\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(_(" --no-statistics do not import statistics from old cluster\n")); printf(_(" --set-char-signedness=OPTION set new cluster char signedness to \"signed\" or\n" " \"unsigned\"\n")); - printf(_(" --swap move data directories to new cluster\n")); + printf(_(" --swap (same as --transfer-mode=swap, deprecated)\n")); printf(_(" --sync-method=METHOD set method for syncing files to disk\n")); + printf(_(" --transfer-mode=MODE set file transfer mode to \"clone\", \"copy\",\n" + " \"copy-file-range\", \"link\", or \"swap\"\n")); printf(_(" -?, --help show this help, then exit\n")); printf(_("\n" "Before running pg_upgrade you must:\n" diff --git a/src/bin/pg_upgrade/t/006_transfer_modes.pl b/src/bin/pg_upgrade/t/006_transfer_modes.pl index 360d05c4b68..8e7dbb90ec8 100644 --- a/src/bin/pg_upgrade/t/006_transfer_modes.pl +++ b/src/bin/pg_upgrade/t/006_transfer_modes.pl @@ -119,7 +119,7 @@ sub test_mode '--socketdir' => $new->host, '--old-port' => $old->port, '--new-port' => $new->port, - $mode + '--transfer-mode' => $mode ], qr/.* not supported on this platform|could not .* between old and new data directories: .*/, qr/^$/, @@ -196,10 +196,10 @@ sub test_mode # builds. chdir ${PostgreSQL::Test::Utils::tmp_check}; -test_mode('--clone'); -test_mode('--copy'); -test_mode('--copy-file-range'); -test_mode('--link'); -test_mode('--swap'); +test_mode('clone'); +test_mode('copy'); +test_mode('copy-file-range'); +test_mode('link'); +test_mode('swap'); done_testing(); -- 2.55.0