Sub topic 4
Copy, clone or link: choosing a transfer mode
This chapter assumes you can run pg_ctl, know where a cluster's data directory lives, and have read the previous chapter on what pg_upgrade does internally. It covers the choice you make before running the upgrade: how the old cluster's data files get into the new cluster.
What the transfer mode decides
pg_upgrade does not rewrite your table and index files. The on-disk format of user data stays compatible within a release series, so the tool only needs to make the old relation files appear in the new data directory. It recreates the catalogs by dumping and restoring the schema. The transfer mode is the method used for the file step.
There are three modes:
| Mode | Option | How files arrive | Extra disk space | Old cluster usable afterwards |
|---|---|---|---|---|
| Copy | `--copy` (default) | Every file is read and written again | About the size of the old cluster | Yes |
| Clone | `--clone` | The filesystem makes copy-on-write (reflink) copies | Almost none at first | Yes |
| Link | `--link` or `-k` | Hard links to the old files | Almost none | No, not once the new cluster has started |
The choice trades three things: how long the downtime lasts, how much disk space you need, and how easily you can go back.
Copy mode
Copy mode is the default, so you get it when you pass no transfer option. pg_upgrade reads each relation file from the old data directory and writes it into the new one.
- Time: proportional to data volume. A 2 TB cluster takes as long as copying 2 TB on your storage, which can be hours.
- Space: you need free space for a complete second copy of the data, including files in tablespaces. Check with
df -hbefore the window starts. A full disk partway through leaves a half-populated new cluster, which you delete and recreate. - Safety: this is the safest mode. The old cluster is never modified, so if anything goes wrong you stop, discard the new data directory, and start the old server again.
PostgreSQL 17 added --copy-file-range. It is still a copy, but it asks the kernel to do the copying with copy_file_range(). On some Linux and FreeBSD filesystems this is faster than user-space reads and writes. Test it on your own storage before you rely on it.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--jobs=4Copy mode is the right choice when you have the disk space and the downtime budget, and when you want the simplest rollback.
Clone mode
Clone mode asks the filesystem to create each new file as a reflink. A reflink is a copy-on-write copy that shares the same disk blocks as the original until one side changes a block. Creating one takes metadata work only, so the run takes roughly the time of link mode.
Requirements:
- The filesystem must support reflinks. Common choices are XFS (created with
reflink=1, the default in recentmkfs.xfs), Btrfs, OpenZFS 2.2 or later with block cloning enabled, and APFS on macOS. - The old and new data directories must be on the same filesystem. A reflink cannot cross filesystems.
- If the filesystem cannot clone, the upgrade fails with an error. It does not fall back to copying.
You can check the first condition on XFS like this:
xfs_info /data | grep -o 'reflink=[01]'Because the files are independent copies that share blocks, a write in the new cluster allocates fresh blocks for that file and leaves the old cluster's data untouched. The old cluster stays fully usable. Clone mode therefore gives you most of link mode's speed together with copy mode's rollback.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--clone \
--jobs=4Two details to remember. First, "almost no extra space" holds only at the start. As the new cluster runs and rewrites pages, the two copies diverge and consume real space, so do not delete your free-space margin. Second, performance of a cloned file depends on the filesystem. Heavily cloned, fragmented files can behave differently from freshly written ones, so watch I/O after the upgrade.
Link mode
Link mode creates a hard link from each file in the new data directory to the existing file in the old one. A hard link is a second directory entry pointing at the same inode, so there is only one copy of the data on disk, and both clusters see it.
- Time: nearly independent of data size. It depends on how many files there are, because each file needs a link call. A multi-terabyte cluster can finish the file step in seconds.
- Space: almost none beyond the new catalogs.
- Constraint: both data directories must be on the same filesystem, since hard links cannot cross mount points.
The command from this tutorial's running example:
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--link \
--jobs=4Why link mode removes the safety net
Because the files are shared, the moment the new server starts and writes to a table, it also changes the old cluster's file. The old catalogs would no longer match the data, so the old cluster is corrupt for practical purposes.
To stop you from starting it by accident, a successful link-mode run renames the old cluster's global/pg_control to pg_control.old. The old server refuses to start until you rename the file back. That rename is only safe to undo if the new cluster has never been started.
The old cluster's data is safe until the new server starts. If pg_upgrade fails during the file or schema steps, the old cluster is unchanged apart from the pg_control rename in some cases. You can fix the problem, rename pg_control.old back if present, and retry.
Parallelism with --jobs
--jobs=N applies in all modes. It runs the schema dump and restore for several databases at once, and it transfers files in parallel. Its effect depends on the shape of your data:
- Many databases or many relations: a clear speedup.
- One database with a few huge tables: little gain from the file step, because each file is handled by one worker.
- Link and clone: the file step is already short, so
--jobsmostly speeds up the schema work. This matters if you have hundreds of thousands of objects.
A reasonable starting value is the number of CPU cores, bounded by what your storage can serve in the copy case.
Checking that the mode will work
You choose the mode before you start the upgrade, and --check lets you test it without downtime. Pass the same transfer option you plan to use:
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--link \
--checkWith --link or --clone, the check also verifies that the tool can create links or clones between the two directories. That catches a wrong mount layout or an unsupported filesystem. You can also compare device numbers yourself:
stat -c '%d %n' /data/pg16 /data/pg17Both lines must show the same device number for link and clone to work. If the new data directory is a separate mount, pg_upgrade cannot link into it.
Tablespaces need a separate look. In copy mode, the tablespace files are copied too, so each tablespace location needs room. In link mode, the old and new tablespace directories must be on the same filesystem as each other. Check every tablespace path, not only the main data directory.
How to decide
Use these criteria in order:
- Downtime budget. If the copy time fits the maintenance window, prefer copy. Its rollback is the simplest.
- Disk space. If you cannot hold two full copies, copy is out.
- Filesystem. If reflinks are available, clone gives link-like speed and keeps the old cluster. Choose it over link whenever you can.
- Rollback needs. Choose link only when speed or space forces it, and only with a backup or snapshot you have tested.
Common mistakes
- Assuming
--linkcopies nothing and is therefore harmless. It is harmless only until the new server starts. - Using
--cloneon a filesystem without reflinks. The upgrade fails. Run--check --cloneahead of time. - Putting the new data directory on a different mount. This breaks link and clone. Plan the layout when you initialize the new cluster.
- Forgetting the old cluster in link mode. Don't restart the old service from an init script or a monitoring restart rule after the upgrade. Disable the old unit so it cannot try to start.
- Sizing disk for copy mode by the database sizes alone. Include tablespaces and leave room for WAL and the first days of new writes.
Exercise
A 4 TB cluster sits on XFS with reflinks enabled. Both data directories are on the same volume, free space is 500 GB, and the downtime window is 20 minutes. The team wants to be able to start the old cluster if the application fails after the upgrade. Which mode should you choose, and why not the other two?
Show answer
Choose --clone. Copy needs about 4 TB of free space and far more than 20 minutes, so it fails on both counts. Link would fit the window and space but makes the old cluster unusable once the new server starts, which breaks the rollback requirement. Clone is fast, uses almost no extra space at first, and leaves the old cluster intact. Confirm with --check --clone first.
Exercise
After a successful --link upgrade, someone renames pgcontrol.old back to pgcontrol and starts the old cluster, having already run the new cluster for an hour. What is the state of the data, and what is the recovery path?
Show answer
The two clusters share the same data files, and the new cluster has been modifying them for an hour. The old cluster's catalogs no longer match the files, so its data cannot be trusted. Do not use it. Recover from the backup or filesystem snapshot taken before the upgrade, and redo the upgrade.