1PG_REWIND(1)             PostgreSQL 10.7 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 master server back online after
17       failover as a standby that follows the new master.
18
19       The result is equivalent to replacing the target data directory with
20       the source one. Only changed blocks from relation files are copied; all
21       other files are copied in full, including configuration files. The
22       advantage of pg_rewind over taking a new base backup, or tools like
23       rsync, is that pg_rewind does not require reading through unchanged
24       blocks in the cluster. This makes it a lot faster when the database is
25       large and only a small fraction of blocks differ between the clusters.
26
27       pg_rewind examines the timeline histories of the source and target
28       clusters to determine the point where they diverged, and expects to
29       find WAL in the target cluster's pg_wal directory reaching all the way
30       back to the point of divergence. The point of divergence can be found
31       either on the target timeline, the source timeline, or their common
32       ancestor. In the typical failover scenario where the target cluster was
33       shut down soon after the divergence, this is not a problem, but if the
34       target cluster ran for a long time after the divergence, the old WAL
35       files might no longer be present. In that case, they can be manually
36       copied from the WAL archive to the pg_wal directory, or fetched on
37       startup by configuring recovery.conf. The use of pg_rewind is not
38       limited to failover, e.g. a standby server can be promoted, run some
39       write transactions, and then rewinded to become a standby again.
40
41       When the target server is started for the first time after running
42       pg_rewind, it will go into recovery mode and replay all WAL generated
43       in the source server after the point of divergence. If some of the WAL
44       was no longer available in the source server when pg_rewind was run,
45       and therefore could not be copied by the pg_rewind session, it must be
46       made available when the target server is started. This can be done by
47       creating a recovery.conf file in the target data directory with a
48       suitable restore_command.
49
50       pg_rewind requires that the target server either has the wal_log_hints
51       option enabled in postgresql.conf or data checksums enabled when the
52       cluster was initialized with initdb. Neither of these are currently on
53       by default.  full_page_writes must also be set to on, but is enabled by
54       default.
55
56           Warning
57           If pg_rewind fails while processing, then the data folder of the
58           target is likely not in a state that can be recovered. In such a
59           case, taking a new fresh backup is recommended.
60
61           pg_rewind will fail immediately if it finds files it cannot write
62           directly to. This can happen for example when the source and the
63           target server use the same file mapping for read-only SSL keys and
64           certificates. If such files are present on the target server it is
65           recommended to remove them before running pg_rewind. After doing
66           the rewind, some of those files may have been copied from the
67           source, in which case it may be necessary to remove the data copied
68           and restore back the set of links used before the rewind.
69

OPTIONS

71       pg_rewind accepts the following command-line arguments:
72
73       -D directory
74       --target-pgdata=directory
75           This option specifies the target data directory that is
76           synchronized with the source. The target server must be shut down
77           cleanly before running pg_rewind
78
79       --source-pgdata=directory
80           Specifies the file system path to the data directory of the source
81           server to synchronize the target with. This option requires the
82           source server to be cleanly shut down.
83
84       --source-server=connstr
85           Specifies a libpq connection string to connect to the source
86           PostgreSQL server to synchronize the target with. The connection
87           must be a normal (non-replication) connection with superuser
88           access. This option requires the source server to be running and
89           not in recovery mode.
90
91       -n
92       --dry-run
93           Do everything except actually modifying the target directory.
94
95       -P
96       --progress
97           Enables progress reporting. Turning this on will deliver an
98           approximate progress report while copying data from the source
99           cluster.
100
101       --debug
102           Print verbose debugging output that is mostly useful for developers
103           debugging pg_rewind.
104
105       -V
106       --version
107           Display version information, then exit.
108
109       -?
110       --help
111           Show help, then exit.
112

ENVIRONMENT

114       When --source-server option is used, pg_rewind also uses the
115       environment variables supported by libpq (see Section 33.14).
116

NOTES

118   How it works
119       The basic idea is to copy all file system-level changes from the source
120       cluster to the target cluster:
121
122        1. Scan the WAL log of the target cluster, starting from the last
123           checkpoint before the point where the source cluster's timeline
124           history forked off from the target cluster. For each WAL record,
125           record each data block that was touched. This yields a list of all
126           the data blocks that were changed in the target cluster, after the
127           source cluster forked off.
128
129        2. Copy all those changed blocks from the source cluster to the target
130           cluster, either using direct file system access (--source-pgdata)
131           or SQL (--source-server).
132
133        3. Copy all other files such as pg_xact and configuration files from
134           the source cluster to the target cluster (everything except the
135           relation files).
136
137        4. Apply the WAL from the source cluster, starting from the checkpoint
138           created at failover. (Strictly speaking, pg_rewind doesn't apply
139           the WAL, it just creates a backup label file that makes PostgreSQL
140           start by replaying all WAL from that checkpoint forward.)
141
142
143
144PostgreSQL 10.7                      2019                         PG_REWIND(1)
Impressum