1PG_REWIND(1)             PostgreSQL 15.4 Documentation            PG_REWIND(1)
2
3
4

NAME

6       pg_rewind - synchronize a PostgreSQL data directory with another data
7       directory that was forked from it
8

SYNOPSIS

10       pg_rewind [option...] {-D | --target-pgdata} directory
11                 {--source-pgdata=directory | --source-server=connstr}
12

DESCRIPTION

14       pg_rewind is a tool for synchronizing a PostgreSQL cluster with another
15       copy of the same cluster, after the clusters' timelines have diverged.
16       A typical scenario is to bring an old primary server back online after
17       failover as a standby that follows the new primary.
18
19       After a successful rewind, the state of the target data directory is
20       analogous to a base backup of the source data directory. Unlike taking
21       a new base backup or using a tool like rsync, pg_rewind does not
22       require comparing or copying unchanged relation blocks in the cluster.
23       Only changed blocks from existing relation files are copied; all other
24       files, including new relation files, configuration files, and WAL
25       segments, are copied in full. As such the rewind operation is
26       significantly faster than other approaches when the database is large
27       and only a small fraction of blocks differ between the clusters.
28
29       pg_rewind examines the timeline histories of the source and target
30       clusters to determine the point where they diverged, and expects to
31       find WAL in the target cluster's pg_wal directory reaching all the way
32       back to the point of divergence. The point of divergence can be found
33       either on the target timeline, the source timeline, or their common
34       ancestor. In the typical failover scenario where the target cluster was
35       shut down soon after the divergence, this is not a problem, but if the
36       target cluster ran for a long time after the divergence, its old WAL
37       files might no longer be present. In this case, you can manually copy
38       them from the WAL archive to the pg_wal directory, or run pg_rewind
39       with the -c option to automatically retrieve them from the WAL archive.
40       The use of pg_rewind is not limited to failover, e.g., a standby server
41       can be promoted, run some write transactions, and then rewound to
42       become a standby again.
43
44       After running pg_rewind, WAL replay needs to complete for the data
45       directory to be in a consistent state. When the target server is
46       started again it will enter archive recovery and replay all WAL
47       generated in the source server from the last checkpoint before the
48       point of divergence. If some of the WAL was no longer available in the
49       source server when pg_rewind was run, and therefore could not be copied
50       by the pg_rewind session, it must be made available when the target
51       server is started. This can be done by creating a recovery.signal file
52       in the target data directory and by configuring a suitable
53       restore_command in postgresql.conf.
54
55       pg_rewind requires that the target server either has the wal_log_hints
56       option enabled in postgresql.conf or data checksums enabled when the
57       cluster was initialized with initdb. Neither of these are currently on
58       by default.  full_page_writes must also be set to on, but is enabled by
59       default.
60
61           Warning
62           If pg_rewind fails while processing, then the data folder of the
63           target is likely not in a state that can be recovered. In such a
64           case, taking a new fresh backup is recommended.
65
66           As pg_rewind copies configuration files entirely from the source,
67           it may be required to correct the configuration used for recovery
68           before restarting the target server, especially if the target is
69           reintroduced as a standby of the source. If you restart the server
70           after the rewind operation has finished but without configuring
71           recovery, the target may again diverge from the primary.
72
73           pg_rewind will fail immediately if it finds files it cannot write
74           directly to. This can happen for example when the source and the
75           target server use the same file mapping for read-only SSL keys and
76           certificates. If such files are present on the target server it is
77           recommended to remove them before running pg_rewind. After doing
78           the rewind, some of those files may have been copied from the
79           source, in which case it may be necessary to remove the data copied
80           and restore back the set of links used before the rewind.
81

OPTIONS

83       pg_rewind accepts the following command-line arguments:
84
85       -D directory
86       --target-pgdata=directory
87           This option specifies the target data directory that is
88           synchronized with the source. The target server must be shut down
89           cleanly before running pg_rewind
90
91       --source-pgdata=directory
92           Specifies the file system path to the data directory of the source
93           server to synchronize the target with. This option requires the
94           source server to be cleanly shut down.
95
96       --source-server=connstr
97           Specifies a libpq connection string to connect to the source
98           PostgreSQL server to synchronize the target with. The connection
99           must be a normal (non-replication) connection with a role having
100           sufficient permissions to execute the functions used by pg_rewind
101           on the source server (see Notes section for details) or a superuser
102           role. This option requires the source server to be running and
103           accepting connections.
104
105       -R
106       --write-recovery-conf
107           Create standby.signal and append connection settings to
108           postgresql.auto.conf in the output directory.  --source-server is
109           mandatory with this option.
110
111       -n
112       --dry-run
113           Do everything except actually modifying the target directory.
114
115       -N
116       --no-sync
117           By default, pg_rewind will wait for all files to be written safely
118           to disk. This option causes pg_rewind to return without waiting,
119           which is faster, but means that a subsequent operating system crash
120           can leave the data directory corrupt. Generally, this option is
121           useful for testing but should not be used on a production
122           installation.
123
124       -P
125       --progress
126           Enables progress reporting. Turning this on will deliver an
127           approximate progress report while copying data from the source
128           cluster.
129
130       -c
131       --restore-target-wal
132           Use restore_command defined in the target cluster configuration to
133           retrieve WAL files from the WAL archive if these files are no
134           longer available in the pg_wal directory.
135
136       --config-file=filename
137           Use the specified main server configuration file for the target
138           cluster. This affects pg_rewind when it uses internally the
139           postgres command for the rewind operation on this cluster (when
140           retrieving restore_command with the option -c/--restore-target-wal
141           and when forcing a completion of crash recovery).
142
143       --debug
144           Print verbose debugging output that is mostly useful for developers
145           debugging pg_rewind.
146
147       --no-ensure-shutdown
148           pg_rewind requires that the target server is cleanly shut down
149           before rewinding. By default, if the target server is not shut down
150           cleanly, pg_rewind starts the target server in single-user mode to
151           complete crash recovery first, and stops it. By passing this
152           option, pg_rewind skips this and errors out immediately if the
153           server is not cleanly shut down. Users are expected to handle the
154           situation themselves in that case.
155
156       -V
157       --version
158           Display version information, then exit.
159
160       -?
161       --help
162           Show help, then exit.
163

ENVIRONMENT

165       When --source-server option is used, pg_rewind also uses the
166       environment variables supported by libpq (see Section 34.15).
167
168       The environment variable PG_COLOR specifies whether to use color in
169       diagnostic messages. Possible values are always, auto and never.
170

NOTES

172       When executing pg_rewind using an online cluster as source, a role
173       having sufficient permissions to execute the functions used by
174       pg_rewind on the source cluster can be used instead of a superuser.
175       Here is how to create such a role, named rewind_user here:
176
177           CREATE USER rewind_user LOGIN;
178           GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;
179           GRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;
180           GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;
181           GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
182
183       When executing pg_rewind using an online cluster as source which has
184       been recently promoted, it is necessary to execute a CHECKPOINT after
185       promotion such that its control file reflects up-to-date timeline
186       information, which is used by pg_rewind to check if the target cluster
187       can be rewound using the designated source cluster.
188
189   How It Works
190       The basic idea is to copy all file system-level changes from the source
191       cluster to the target cluster:
192
193        1. Scan the WAL log of the target cluster, starting from the last
194           checkpoint before the point where the source cluster's timeline
195           history forked off from the target cluster. For each WAL record,
196           record each data block that was touched. This yields a list of all
197           the data blocks that were changed in the target cluster, after the
198           source cluster forked off. If some of the WAL files are no longer
199           available, try re-running pg_rewind with the -c option to search
200           for the missing files in the WAL archive.
201
202        2. Copy all those changed blocks from the source cluster to the target
203           cluster, either using direct file system access (--source-pgdata)
204           or SQL (--source-server). Relation files are now in a state
205           equivalent to the moment of the last completed checkpoint prior to
206           the point at which the WAL timelines of the source and target
207           diverged plus the current state on the source of any blocks changed
208           on the target after that divergence.
209
210        3. Copy all other files, including new relation files, WAL segments,
211           pg_xact, and configuration files from the source cluster to the
212           target cluster. Similarly to base backups, the contents of the
213           directories pg_dynshmem/, pg_notify/, pg_replslot/, pg_serial/,
214           pg_snapshots/, pg_stat_tmp/, and pg_subtrans/ are omitted from the
215           data copied from the source cluster. The files backup_label,
216           tablespace_map, pg_internal.init, postmaster.opts, and
217           postmaster.pid, as well as any file or directory beginning with
218           pgsql_tmp, are omitted.
219
220        4. Create a backup_label file to begin WAL replay at the checkpoint
221           created at failover and configure the pg_control file with a
222           minimum consistency LSN defined as the result of
223           pg_current_wal_insert_lsn() when rewinding from a live source or
224           the last checkpoint LSN when rewinding from a stopped source.
225
226        5. When starting the target, PostgreSQL replays all the required WAL,
227           resulting in a data directory in a consistent state.
228
229
230
231PostgreSQL 15.4                      2023                         PG_REWIND(1)
Impressum