Fork Instance with XFS

Clone a PostgreSQL instance on the same machine with pg-fork.

Pigsty provides pg-fork, a utility script for quickly cloning a PostgreSQL instance on the same machine.

If your filesystem supports Copy-on-Write (XFS with reflink, Btrfs, ZFS), cloning is usually sub-second and requires almost no extra storage.


Quick Start

Run as postgres (dbsu):

pg-fork 1                         # /pg/data -> /pg/data1, port 15432
pg-fork 2 -d /pg/data1            # /pg/data1 -> /pg/data2, port 25432
pg-fork 3 -D /tmp/test -P 5555    # custom target directory and port

Start and verify the cloned instance:

pg_ctl -D /pg/data1 start
psql -p 15432

Syntax

pg-fork <FORK_ID> [options]

Required:

ParamDescription
<FORK_ID>Clone ID (1-9), used to derive default target data dir and port

Optional:

ParamDescriptionDefault
-d, --data <datadir>Source data directory/pg/data or $PG_DATA
-D, --dst <dst_dir>Target data directory/pg/data<FORK_ID>
-p, --port <port>Source instance port5432 or $PG_PORT
-P, --dst-port <port>Target instance port<FORK_ID>5432
-s, --skipSkip backup API and use cold copy mode-
-y, --yesSkip confirmation prompts-
-h, --helpPrint help-

How It Works

pg-fork supports two modes:

  • Hot backup mode (default): calls pg_backup_start() and pg_backup_stop(), then copies data with cp --reflink=auto.
  • Cold copy mode (-s): directly copies the data directory when source PostgreSQL is stopped.

In both cases, pg-fork updates the cloned instance config to avoid conflicts with the source instance.