PG_REWIND(1)
PG_REWIND(1) โ PostgreSQL 18.6 Documentation ๐ซ
okay bestie, welcome to pg_rewind โ the tool that synchronizes a PostgreSQL data directory with another data directory that was forked from it ๐ซถ
โจ NAME โจ
pg_rewind โ synchronize a PostgreSQL data directory with another data directory that was forked from it. literally that's the whole job description ๐
๐ธ SYNOPSIS ๐ธ
pg_rewind [option...] {-D | --target-pgdata} directory
{--source-pgdata=directory | --source-server=connstr}
๐ DESCRIPTION ๐
okay so, pg_rewind is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, and it does this after the clusters' timelines have diverged. messy breakup energy, but make it databases ๐ญ The typical scenario is bringing an old primary server back online after a failover, so it can serve as a standby that follows the new primary. main character arc fr fr ๐
After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory. But here's the slay: unlike taking a new base backup or using a tool like rsync, pg_rewind does not require comparing or copying unchanged relation blocks in the cluster. Only changed blocks from existing relation files are copied. All other files โ including new relation files, configuration files, and WAL segments โ are copied in full. So when the database is huge and only a small fraction of blocks differ between the clusters, the rewind operation is significantly faster than other approaches. not me being efficient ๐
pg_rewind examines the timeline histories of the source and target clusters to determine the point where they diverged, and it expects to find WAL in the target cluster's pg_wal directory reaching all the way back to the point of divergence. That point of divergence can be found either on the target timeline, the source timeline, or their common ancestor. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem. But if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present ๐ฌ In that case you can manually copy them from the WAL archive to the pg_wal directory, or run pg_rewind with the -c option to automatically retrieve them from the WAL archive. And btw, the use of pg_rewind is not limited to failover โ e.g., a standby server can be promoted, run some write transactions, and then rewound to become a standby again. glow up, glow down, it's a whole journey ๐
After running pg_rewind, WAL replay needs to complete for the data directory to be in a consistent state. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence. If some of the WAL was no longer available in the source server when pg_rewind was run, and therefore could not be copied by the pg_rewind session, it must be made available when the target server is started. You do this by creating a recovery.signal file in the target data directory and by configuring a suitable restore_command in postgresql.conf. got it? got it. ๐
pg_rewind requires that the target server either has the wal_log_hints option enabled in postgresql.conf or data checksums enabled when the cluster was initialized with initdb (the default). full_page_writes must also be set to on, but is enabled by default. so like, defaults are carrying you, don't stress ๐ง
โ ๏ธ Warning: Failures While Rewinding โ ๏ธ
If pg_rewind fails while processing, then the data folder of the target is likely not in a state that can be recovered. In such a case, taking a new fresh backup is recommended. ๐
As pg_rewind copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary. the drama never ends ๐
pg_rewindwill fail immediately if it finds files it cannot write directly to. This can happen for example when the source and the target server use the same file mapping for read-only SSL keys and certificates. If such files are present on the target server it is recommended to remove them before runningpg_rewind. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind. ๐
๐ OPTIONS ๐
pg_rewind accepts the following command-line arguments:
-Ddirectory,--target-pgdata=directory โ This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before runningpg_rewind. no sneaky running servers, bestie ๐--source-pgdata=directory โ Specifies the file system path to the data directory of the source server to synchronize the target with. This option requires the source server to be cleanly shut down. yes, also cleanly. we have standards โจ--source-server=connstr โ Specifies alibpqconnection string to connect to the source PostgreSQL server to synchronize the target with. The connection must be a normal (non-replication) connection with a role having sufficient permissions to execute the functions used bypg_rewindon the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and accepting connections. so like, alive and answering, iconic behavior ๐-R,--write-recovery-confโ Createstandby.signaland append connection settings topostgresql.auto.confin the output directory. Thedbnamewill be recorded only if thedbnamewas specified explicitly in the connection string or environment variable.--source-serveris mandatory with this option. mandatory, bestie, no skipping ๐ซถ-n,--dry-runโ Do everything except actually modifying the target directory. window shopping but for file systems ๐ฆ-N,--no-syncโ By default,pg_rewindwill wait for all files to be written safely to disk. This option causespg_rewindto return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt. Generally, this option is useful for testing but should not be used on a production installation. faster โ safer, i'm just saying ๐ญ-P,--progressโ Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster. we love a progress bar in this house ๐-c,--restore-target-walโ Userestore_commanddefined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in thepg_waldirectory.--config-file=filename โ Use the specified main server configuration file for the target cluster. This affectspg_rewindwhen it uses internally thepostgrescommand for the rewind operation on this cluster (when retrievingrestore_commandwith the option-c/--restore-target-waland when forcing a completion of crash recovery).--debugโ Print verbose debugging output that is mostly useful for developers debuggingpg_rewind. devs only, iykyk ๐ฅ--no-ensure-shutdownโpg_rewindrequires that the target server is cleanly shut down before rewinding. By default, if the target server is not shut down cleanly,pg_rewindstarts the target server in single-user mode to complete crash recovery first, and stops it. By passing this option,pg_rewindskips this and errors out immediately if the server is not cleanly shut down. Users are expected to handle the situation themselves in that case. you're on your own, bestie, you asked for this ๐--sync-method=method โ When set tofsync, which is the default,pg_rewindwill recursively open and synchronize all files in the data directory. The search for files will follow symbolic links for the WAL directory and each configured tablespace.
On Linux, syncfs may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace. See recovery_init_sync_method for information about the caveats to be aware of when using syncfs.
This option has no effect when --no-sync is used. noted ๐
-V,--versionโ Display version information, then exit. bye ๐-?,--helpโ Show help, then exit. also bye, but helpful ๐โจ
๐ ENVIRONMENT ๐
When --source-server option is used, pg_rewind also uses the environment variables supported by libpq (see Section 32.15).
The environment variable PG_COLOR specifies whether to use color in diagnostic messages. Possible values are always, auto and never. aesthetic matters, always ๐
โจ NOTES โจ
okay so here's the tea ๐ซ โ if you're running pg_rewind against an online cluster as your source, you do not actually need a superuser. A role with enough permissions to run the functions pg_rewind uses on the source cluster works just fine. Slay.
Here's how to make one, and in this example it's called rewind_user:
CREATE USER rewind_user LOGIN;
GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;
GRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;
GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;
GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
copy that, paste it, bestie โ four grants, no more, no less. ๐
๐ฆ How It Works ๐ฆ
the whole vibe here is simple: copy every file system-level change from the source cluster over to the target cluster. That's it. That's the plot.
- Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's timeline history forked off from the target cluster. For each WAL record, record each data block that was touched. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off. If some of the WAL files are no longer available, try re-running
pg_rewindwith the-coption to search for the missing files in the WAL archive. - Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (
--source-pgdata) or SQL (--source-server). Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence. - Copy all other files, including new relation files, WAL segments,
pg_xact, and configuration files from the source cluster to the target cluster. Similarly to base backups, the contents of the directoriespg_dynshmem/,pg_notify/,pg_replslot/,pg_serial/,pg_snapshots/,pg_stat_tmp/, andpg_subtrans/are omitted from the data copied from the source cluster. The filesbackup_label,tablespace_map,pg_internal.init,postmaster.opts,postmaster.pidand.DS_Storeas well as any file or directory beginning withpgsql_tmp, are omitted. - Create a
backup_labelfile to begin WAL replay at the checkpoint created at failover and configure thepg_controlfile with a minimum consistency LSN defined as the result ofpg_current_wal_insert_lsn()when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source. - When starting the target, PostgreSQL replays all the required WAL, resulting in a data directory in a consistent state.
heads up: step 3 is lowkey the picky one โ that list of directories and files is exactly the stuff that gets left behind, so don't come for me if you were expecting pg_stat_tmp/ to make the trip. It does not. ๐
and that's the whole thing, bestie โ you now know pg_rewind better than half the internet does. go rewind something. ๐
this page is PostgreSQL 18.6, dated 2026, and it's PG_REWIND(1) โ mwah, iconic, no notes. ๐๐
๐ฆ where this page comes from
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- opensuse-leap 16.0: postgresql18 18.6-160000.1.1 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms