1PG_REWIND(1)             PostgreSQL 12.6 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. The use of
37       pg_rewind is not limited to failover, e.g., a standby server can be
38       promoted, run some write transactions, and then rewound to become a
39       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.signal file in the target data directory and
48       configuring suitable restore_command in postgresql.conf.
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       -N
98       --no-sync
99           By default, pg_rewind will wait for all files to be written safely
100           to disk. This option causes pg_rewind to return without waiting,
101           which is faster, but means that a subsequent operating system crash
102           can leave the synchronized data directory corrupt. Generally, this
103           option is useful for testing but should not be used when creating a
104           production installation.
105
106       -P
107       --progress
108           Enables progress reporting. Turning this on will deliver an
109           approximate progress report while copying data from the source
110           cluster.
111
112       --debug
113           Print verbose debugging output that is mostly useful for developers
114           debugging pg_rewind.
115
116       -V
117       --version
118           Display version information, then exit.
119
120       -?
121       --help
122           Show help, then exit.
123

ENVIRONMENT

125       When --source-server option is used, pg_rewind also uses the
126       environment variables supported by libpq (see Section 33.14).
127
128       The environment variable PG_COLOR specifies whether to use color in
129       diagnostic messages. Possible values are always, auto and never.
130

NOTES

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