1PG_REWIND(1)             PostgreSQL 11.3 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 a role having
88           sufficient permissions to execute the functions used by pg_rewind
89           on the source server (see Notes section for details) or a superuser
90           role. This option requires the source server to be running and not
91           in recovery mode.
92
93       -n
94       --dry-run
95           Do everything except actually modifying the target directory.
96
97       -P
98       --progress
99           Enables progress reporting. Turning this on will deliver an
100           approximate progress report while copying data from the source
101           cluster.
102
103       --debug
104           Print verbose debugging output that is mostly useful for developers
105           debugging pg_rewind.
106
107       -V
108       --version
109           Display version information, then exit.
110
111       -?
112       --help
113           Show help, then exit.
114

ENVIRONMENT

116       When --source-server option is used, pg_rewind also uses the
117       environment variables supported by libpq (see Section 34.14).
118

NOTES

120       When executing pg_rewind using an online cluster as source, a role
121       having sufficient permissions to execute the functions used by
122       pg_rewind on the source cluster can be used instead of a superuser.
123       Here is how to create such a role, named rewind_user here:
124
125           CREATE USER rewind_user LOGIN;
126           GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;
127           GRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;
128           GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;
129           GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
130
131       When executing pg_rewind using an online cluster as source which has
132       been recently promoted, it is necessary to execute a CHECKPOINT after
133       promotion so as its control file reflects up-to-date timeline
134       information, which is used by pg_rewind to check if the target cluster
135       can be rewound using the designated source cluster.
136
137   How it works
138       The basic idea is to copy all file system-level changes from the source
139       cluster to the target cluster:
140
141        1. Scan the WAL log of the target cluster, starting from the last
142           checkpoint before the point where the source cluster's timeline
143           history forked off from the target cluster. For each WAL record,
144           record each data block that was touched. This yields a list of all
145           the data blocks that were changed in the target cluster, after the
146           source cluster forked off.
147
148        2. Copy all those changed blocks from the source cluster to the target
149           cluster, either using direct file system access (--source-pgdata)
150           or SQL (--source-server).
151
152        3. Copy all other files such as pg_xact and configuration files from
153           the source cluster to the target cluster (everything except the
154           relation files). Similarly to base backups, the contents of the
155           directories pg_dynshmem/, pg_notify/, pg_replslot/, pg_serial/,
156           pg_snapshots/, pg_stat_tmp/, and pg_subtrans/ are omitted from the
157           data copied from the source cluster. Any file or directory
158           beginning with pgsql_tmp is omitted, as well as are backup_label,
159           tablespace_map, pg_internal.init, postmaster.opts and
160           postmaster.pid.
161
162        4. Apply the WAL from the source cluster, starting from the checkpoint
163           created at failover. (Strictly speaking, pg_rewind doesn't apply
164           the WAL, it just creates a backup label file that makes PostgreSQL
165           start by replaying all WAL from that checkpoint forward.)
166
167
168
169PostgreSQL 11.3                      2019                         PG_REWIND(1)
Impressum