Sub topic 4
Copy, Clone or Link: Choosing a Transfer Mode
This chapter assumes that you know PostgreSQL clusters, data directories, pg_ctl, Linux file permissions, and basic backup methods. It focuses on how pg_upgrade moves the old cluster's data files into the new cluster.
Why the transfer mode matters
A major upgrade creates a new data directory for the new PostgreSQL version. pg_upgrade must make the old cluster's table and index files available there. The transfer mode controls whether it copies those files, shares them temporarily, or creates copy-on-write clones.
The choice mainly affects three things:
- Upgrade duration.
- Extra disk space required.
- Whether the old cluster remains safe to start after the upgrade begins.
| Mode | Speed | Extra space | Old cluster after upgrade starts |
|---|---|---|---|
| Copy | Slowest | About one full copy | Safe to preserve |
| Clone | Usually fast | Depends on filesystem changes | Safe if cloning is supported |
| Link | Fastest | Very little | Must not be started |
Copy mode: the safest default
Copy mode creates independent copies of the required data files. It is the default behavior, so you can also select it explicitly with --copy.
/opt/pg17/bin/pg_upgrade \
--old-bindir=/opt/pg16/bin \
--new-bindir=/opt/pg17/bin \
--old-datadir=/data/pg16 \
--new-datadir=/data/pg17 \
--copy \
--jobs=4Copy mode needs enough free space for the old and new clusters at the same time. It can take a long time for a large database because every file must be read and written.
Its main advantage is isolation. The new cluster receives its own files. You can keep the old cluster stopped and use it as a rollback source if validation finds a problem.
Copy mode is a good choice when disk space is available and rollback simplicity matters more than the shortest maintenance window.
Link mode: fast, but destructive to the old copy
Link mode creates hard links instead of copying files. A hard link is another directory entry for the same file data. Creating one is almost immediate and uses very little additional space.
/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=4The old and new clusters initially refer to the same underlying files. Therefore, after the new cluster starts, you must not start or modify the old cluster. Changes made through either cluster could affect shared files and corrupt the other cluster.
Link mode is appropriate when:
- The maintenance window must be short.
- Both data directories are on the same filesystem.
- You have a verified backup or another recoverable copy.
- You accept that rollback means restoring the old cluster from that backup.
A filesystem cannot create a hard link across separate filesystems. This limitation commonly causes link-mode failures.
Clone mode: fast with copy-on-write
Clone mode asks the filesystem to create cloned files. A clone initially shares storage blocks with the source, but later writes create separate blocks. This behavior is called copy-on-write.
/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=4Clone mode can provide link-like speed while keeping the old files logically independent. However, it depends on filesystem support, kernel support, mount options, and sufficient free space for later copy-on-write changes. Test it on the exact storage system used in production. Do not assume that a filesystem mounted on one host supports the same operation on another host.
If clone mode is unsupported, use copy mode or link mode rather than treating clone as a guaranteed fallback.
Choosing deliberately
Use this decision path:
Run the selected mode during a rehearsal with production-sized data if possible. Measure both the upgrade time and the available free space. The fastest transfer is useful only if the storage system and rollback plan can support it.
Exercise
You have enough disk space for a second full cluster, but your filesystem does not support cloning. You need a straightforward rollback path. Which mode should you choose, and why?
Show answer
Choose copy mode. It creates independent files, works without filesystem cloning support, and lets you preserve the stopped old cluster for rollback.