pg_upgrade has a separate option for each of its file transfer modes
(--clone, --copy, --copy-file-range, --link, and --swap), which I find
somewhat silly given only one mode can be used at a time.  The options are
scattered around the documentation and the --help output, and every new
mode means yet another one (I'm guilty of adding the most recent).  ISTM we
ought to combine them.

The attached patch adds --transfer-mode, which accepts the mode as its
argument (e.g., --transfer-mode=link).  The documentation for each mode
moves to the new option, and the existing options are now listed as
deprecated spellings of it.  They continue to work as before, and I doubt
we'll ever be able to remove them, but IMHO this is still an improvement.

This has come up a couple of times before.  Michael suggested it while
--clone was being developed [0], and it was discussed again when --copy was
added [1] [2], but AFAICT nobody has really argued against it.  It's just
always been easier to add one more option.  The closest thing to an
objection I could find is Daniel's remark that multiple-choice parameters
aren't a common pattern in our utilities [3], but we've since added
--sync-method and --set-char-signedness, so I think that's less true than
it used to be.  There's also precedent for this sort of change.  After
pg_basebackup gained -X/--xlog-method, -x/--xlog was documented as
"equivalent of using -X with method fetch" until it was removed in v10.

In the same message, Daniel noted that we typically use a last-one-wins
strategy for options that are specified more than once, so I didn't add any
checks for conflicting options (e.g., --transfer-mode=clone overrides an
earlier --link).  I also haven't touched pg_combinebackup, which has four
of the same options, but I'm happy to give it the same treatment if folks
like this.

Thoughts?

[0] https://postgr.es/m/20180928051953.GH1500%40paquier.xyz
[1] https://postgr.es/m/50a97009-8ff9-ca4d-a0f6-6086a6775a5b%40enterprisedb.com
[2] 
https://postgr.es/m/20221202.095650.413092142832173887.horikyota.ntt%40gmail.com
[3] https://postgr.es/m/4BB4EA0D-E818-4ECC-8041-4F46F7E63B30%40yesql.se

-- 
nathan
>From 4948f6cb88e4d0378dba9fad2d97f10abd9eb593 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <[email protected]>
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
      <varlistentry>
       <term><option>-k</option></term>
       <term><option>--link</option></term>
-      <listitem><para>use hard links instead of copying files to the new
-      cluster</para></listitem>
+      <listitem>
+       <para>
+        Deprecated spelling of <option>--transfer-mode=link</option>.
+       </para>
+      </listitem>
      </varlistentry>
 
      <varlistentry>
@@ -221,19 +224,7 @@ PostgreSQL documentation
       <term><option>--clone</option></term>
       <listitem>
        <para>
-        Use efficient file cloning (also known as <quote>reflinks</quote> 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 <option>-k</option>/<option>--link</option> while
-        leaving the old cluster untouched.
-       </para>
-
-       <para>
-        File cloning is only supported on some operating systems and file
-        systems.  If it is selected but not supported, the
-        <application>pg_upgrade</application> 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 <option>--transfer-mode=clone</option>.
        </para>
       </listitem>
      </varlistentry>
@@ -242,9 +233,7 @@ PostgreSQL documentation
       <term><option>--copy</option></term>
       <listitem>
        <para>
-        Copy files to the new cluster.  This is the default.  (See also
-        <option>--link</option>, <option>--clone</option>,
-        <option>--copy-file-range</option>, and <option>--swap</option>.)
+        Deprecated spelling of <option>--transfer-mode=copy</option>.
        </para>
       </listitem>
      </varlistentry>
@@ -253,11 +242,8 @@ PostgreSQL documentation
       <term><option>--copy-file-range</option></term>
       <listitem>
        <para>
-        Use the <function>copy_file_range</function> system call for efficient
-        copying.  On some file systems this gives results similar to
-        <option>--clone</option>, 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
+        <option>--transfer-mode=copy-file-range</option>.
        </para>
       </listitem>
      </varlistentry>
@@ -328,24 +314,7 @@ PostgreSQL documentation
       <term><option>--swap</option></term>
       <listitem>
        <para>
-        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 <option>--link</option>,
-        <option>--clone</option>, <option>--copy</option>, and
-        <option>--copy-file-range</option>, especially on clusters with many
-        relations.
-       </para>
-       <para>
-        However, this mode creates many garbage files in the old cluster, which
-        can prolong the file synchronization step if
-        <option>--sync-method=syncfs</option> is used.  Therefore, it is
-        recommended to use <option>--sync-method=fsync</option> with
-        <option>--swap</option>.
-       </para>
-       <para>
-        Additionally, once the file transfer step begins, the old cluster will
-        be destructively modified and therefore will no longer be safe to
-        start.  See <xref linkend="pgupgrade-step-revert"/> for details.
+        Deprecated spelling of <option>--transfer-mode=swap</option>.
        </para>
       </listitem>
      </varlistentry>
@@ -373,6 +342,98 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      
<term><option>--transfer-mode=</option><replaceable>mode</replaceable></term>
+      <listitem>
+       <para>
+        Specifies how files are transferred from the old cluster to the new
+        cluster.  <replaceable>mode</replaceable> can be one of the following:
+
+        <variablelist>
+         <varlistentry>
+          <term><literal>clone</literal></term>
+          <listitem>
+           <para>
+            Use efficient file cloning (also known as <quote>reflinks</quote>
+            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 <literal>link</literal> while
+            leaving the old cluster untouched.
+           </para>
+
+           <para>
+            File cloning is only supported on some operating systems and file
+            systems.  If it is selected but not supported, the
+            <application>pg_upgrade</application> 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.
+           </para>
+          </listitem>
+         </varlistentry>
+
+         <varlistentry>
+          <term><literal>copy</literal></term>
+          <listitem>
+           <para>
+            Copy files to the new cluster.  This is the default.
+           </para>
+          </listitem>
+         </varlistentry>
+
+         <varlistentry>
+          <term><literal>copy-file-range</literal></term>
+          <listitem>
+           <para>
+            Use the <function>copy_file_range</function> system call for
+            efficient copying.  On some file systems this gives results
+            similar to <literal>clone</literal>, 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.
+           </para>
+          </listitem>
+         </varlistentry>
+
+         <varlistentry>
+          <term><literal>link</literal></term>
+          <listitem>
+           <para>
+            Use hard links instead of copying files to the new cluster.
+           </para>
+          </listitem>
+         </varlistentry>
+
+         <varlistentry>
+          <term><literal>swap</literal></term>
+          <listitem>
+           <para>
+            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 <literal>link</literal>,
+            <literal>clone</literal>, <literal>copy</literal>, and
+            <literal>copy-file-range</literal>, especially on clusters with
+            many relations.
+           </para>
+           <para>
+            However, this mode creates many garbage files in the old cluster,
+            which can prolong the file synchronization step if
+            <option>--sync-method=syncfs</option> is used.  Therefore, it is
+            recommended to use <option>--sync-method=fsync</option> with
+            <literal>swap</literal>.
+           </para>
+           <para>
+            Additionally, once the file transfer step begins, the old cluster
+            will be destructively modified and therefore will no longer be
+            safe to start.  See <xref linkend="pgupgrade-step-revert"/> for
+            details.
+           </para>
+          </listitem>
+         </varlistentry>
+        </variablelist>
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>-?</option></term>
       <term><option>--help</option></term>
@@ -598,8 +659,7 @@ pg_upgrade.exe
      running. <command>pg_upgrade --check</command> 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 <option>--link</option>, <option>--clone</option>,
-     <option>--copy-file-range</option>, or <option>--swap</option> with
+     should use the option <option>--transfer-mode</option> with
      <option>--check</option> to enable mode-specific checks.
      <command>pg_upgrade</command> requires write permission in the current 
directory.
     </para>
@@ -890,15 +950,15 @@ psql --username=postgres --file=script.sql postgres
 
       <listitem>
        <para>
-        If neither <option>--link</option> nor <option>--swap</option> 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.
        </para>
       </listitem>
 
       <listitem>
        <para>
-        If the <option>--link</option> 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:
 
         <itemizedlist>
          <listitem>
@@ -933,8 +993,8 @@ psql --username=postgres --file=script.sql postgres
 
       <listitem>
        <para>
-        If the <option>--swap</option> option was used, the old cluster might
-        be destructively modified:
+        If swap mode was used, the old cluster might be destructively
+        modified:
 
         <itemizedlist>
          <listitem>
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 <xref linkend="pgupgrade"/> module allows an installation to
     be migrated in-place from one major <productname>PostgreSQL</productname>
     version to another.  Upgrades can be performed in minutes,
-    particularly with <option>--link</option> mode.  It requires steps similar 
to
+    particularly with <option>--transfer-mode=link</option>.  It requires 
steps similar to
     <application>pg_dumpall</application> above, e.g.,  starting/stopping the 
server,
     running <application>initdb</application>.  The 
<application>pg_upgrade</application> <link
     linkend="pgupgrade">documentation</link> 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

Reply via email to