1PG_REWIND(1) PostgreSQL 13.3 Documentation PG_REWIND(1)
2
3
4
6 pg_rewind - synchronize a PostgreSQL data directory with another data
7 directory that was forked from it
8
10 pg_rewind [option...] {-D | --target-pgdata} directory
11 {--source-pgdata=directory | --source-server=connstr}
12
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 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
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 not
103 in recovery mode.
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 synchronized data directory corrupt. Generally, this
121 option is 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 --debug
137 Print verbose debugging output that is mostly useful for developers
138 debugging pg_rewind.
139
140 --no-ensure-shutdown
141 pg_rewind requires that the target server is cleanly shut down
142 before rewinding. By default, if the target server is not shut down
143 cleanly, pg_rewind starts the target server in single-user mode to
144 complete crash recovery first, and stops it. By passing this
145 option, pg_rewind skips this and errors out immediately if the
146 server is not cleanly shut down. Users are expected to handle the
147 situation themselves in that case.
148
149 -V
150 --version
151 Display version information, then exit.
152
153 -?
154 --help
155 Show help, then exit.
156
158 When --source-server option is used, pg_rewind also uses the
159 environment variables supported by libpq (see Section 33.14).
160
161 The environment variable PG_COLOR specifies whether to use color in
162 diagnostic messages. Possible values are always, auto and never.
163
165 When executing pg_rewind using an online cluster as source, a role
166 having sufficient permissions to execute the functions used by
167 pg_rewind on the source cluster can be used instead of a superuser.
168 Here is how to create such a role, named rewind_user here:
169
170 CREATE USER rewind_user LOGIN;
171 GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text, boolean, boolean) TO rewind_user;
172 GRANT EXECUTE ON function pg_catalog.pg_stat_file(text, boolean) TO rewind_user;
173 GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text) TO rewind_user;
174 GRANT EXECUTE ON function pg_catalog.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
175
176 When executing pg_rewind using an online cluster as source which has
177 been recently promoted, it is necessary to execute a CHECKPOINT after
178 promotion such that its control file reflects up-to-date timeline
179 information, which is used by pg_rewind to check if the target cluster
180 can be rewound using the designated source cluster.
181
182 How It Works
183 The basic idea is to copy all file system-level changes from the source
184 cluster to the target cluster:
185
186 1. Scan the WAL log of the target cluster, starting from the last
187 checkpoint before the point where the source cluster's timeline
188 history forked off from the target cluster. For each WAL record,
189 record each data block that was touched. This yields a list of all
190 the data blocks that were changed in the target cluster, after the
191 source cluster forked off. If some of the WAL files are no longer
192 available, try re-running pg_rewind with the -c option to search
193 for the missing files in the WAL archive.
194
195 2. Copy all those changed blocks from the source cluster to the target
196 cluster, either using direct file system access (--source-pgdata)
197 or SQL (--source-server). Relation files are now in a state
198 equivalent to the moment of the last completed checkpoint prior to
199 the point at which the WAL timelines of the source and target
200 diverged plus the current state on the source of any blocks changed
201 on the target after that divergence.
202
203 3. Copy all other files, including new relation files, WAL segments,
204 pg_xact, and configuration files from the source cluster to the
205 target cluster. Similarly to base backups, the contents of the
206 directories pg_dynshmem/, pg_notify/, pg_replslot/, pg_serial/,
207 pg_snapshots/, pg_stat_tmp/, and pg_subtrans/ are omitted from the
208 data copied from the source cluster. The files backup_label,
209 tablespace_map, pg_internal.init, postmaster.opts, and
210 postmaster.pid, as well as any file or directory beginning with
211 pgsql_tmp, are omitted.
212
213 4. Create a backup_label file to begin WAL replay at the checkpoint
214 created at failover and configure the pg_control file with a
215 minimum consistency LSN defined as the result of
216 pg_current_wal_insert_lsn() when rewinding from a live source or
217 the last checkpoint LSN when rewinding from a stopped source.
218
219 5. When starting the target, PostgreSQL replays all the required WAL,
220 resulting in a data directory in a consistent state.
221
222
223
224PostgreSQL 13.3 2021 PG_REWIND(1)