]> begriffs open source - ai-pg/blob - full-docs/src/sgml/man1/pg_rewind.1
WIP: toc builder
[ai-pg] / full-docs / src / sgml / man1 / pg_rewind.1
1 '\" t
2 .\"     Title: pg_rewind
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "PG_REWIND" "1" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 pg_rewind \- synchronize a PostgreSQL data directory with another data directory that was forked from it
32 .SH "SYNOPSIS"
33 .HP \w'\fBpg_rewind\fR\ 'u
34 \fBpg_rewind\fR [\fIoption\fR...] {\fB\-D\fR | \fB\-\-target\-pgdata\fR}\fI directory\fR {\fB\-\-source\-pgdata=\fR\fB\fIdirectory\fR\fR | \fB\-\-source\-server=\fR\fB\fIconnstr\fR\fR}
35 .SH "DESCRIPTION"
36 .PP
37 pg_rewind
38 is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters\*(Aq timelines have diverged\&. A typical scenario is to bring an old primary server back online after failover as a standby that follows the new primary\&.
39 .PP
40 After a successful rewind, the state of the target data directory is analogous to a base backup of the source data directory\&. Unlike taking a new base backup or using a tool like
41 rsync,
42 pg_rewind
43 does not require comparing or copying unchanged relation blocks in the cluster\&. Only changed blocks from existing relation files are copied; all other files, including new relation files, configuration files, and WAL segments, are copied in full\&. As such the rewind operation is significantly faster than other approaches when the database is large and only a small fraction of blocks differ between the clusters\&.
44 .PP
45 pg_rewind
46 examines the timeline histories of the source and target clusters to determine the point where they diverged, and expects to find WAL in the target cluster\*(Aqs
47 pg_wal
48 directory reaching all the way back to the point of divergence\&. The point of divergence can be found either on the target timeline, the source timeline, or their common ancestor\&. In the typical failover scenario where the target cluster was shut down soon after the divergence, this is not a problem, but if the target cluster ran for a long time after the divergence, its old WAL files might no longer be present\&. In this case, you can manually copy them from the WAL archive to the
49 pg_wal
50 directory, or run
51 pg_rewind
52 with the
53 \-c
54 option to automatically retrieve them from the WAL archive\&. The use of
55 pg_rewind
56 is not limited to failover, e\&.g\&., a standby server can be promoted, run some write transactions, and then rewound to become a standby again\&.
57 .PP
58 After running
59 pg_rewind, WAL replay needs to complete for the data directory to be in a consistent state\&. When the target server is started again it will enter archive recovery and replay all WAL generated in the source server from the last checkpoint before the point of divergence\&. If some of the WAL was no longer available in the source server when
60 pg_rewind
61 was run, and therefore could not be copied by the
62 pg_rewind
63 session, it must be made available when the target server is started\&. This can be done by creating a
64 recovery\&.signal
65 file in the target data directory and by configuring a suitable
66 restore_command
67 in
68 postgresql\&.conf\&.
69 .PP
70 pg_rewind
71 requires that the target server either has the
72 wal_log_hints
73 option enabled in
74 postgresql\&.conf
75 or data checksums enabled when the cluster was initialized with
76 initdb\&. Neither of these are currently on by default\&.
77 full_page_writes
78 must also be set to
79 on, but is enabled by default\&.
80 .if n \{\
81 .sp
82 .\}
83 .RS 4
84 .it 1 an-trap
85 .nr an-no-space-flag 1
86 .nr an-break-flag 1
87 .br
88 .ps +1
89 \fBWarning: Failures While Rewinding\fR
90 .ps -1
91 .br
92 .PP
93 If
94 pg_rewind
95 fails while processing, then the data folder of the target is likely not in a state that can be recovered\&. In such a case, taking a new fresh backup is recommended\&.
96 .PP
97 As
98 pg_rewind
99 copies configuration files entirely from the source, it may be required to correct the configuration used for recovery before restarting the target server, especially if the target is reintroduced as a standby of the source\&. If you restart the server after the rewind operation has finished but without configuring recovery, the target may again diverge from the primary\&.
100 .PP
101 pg_rewind
102 will fail immediately if it finds files it cannot write directly to\&. This can happen for example when the source and the target server use the same file mapping for read\-only SSL keys and certificates\&. If such files are present on the target server it is recommended to remove them before running
103 pg_rewind\&. After doing the rewind, some of those files may have been copied from the source, in which case it may be necessary to remove the data copied and restore back the set of links used before the rewind\&.
104 .sp .5v
105 .RE
106 .SH "OPTIONS"
107 .PP
108 pg_rewind
109 accepts the following command\-line arguments:
110 .PP
111 \fB\-D \fR\fB\fIdirectory\fR\fR
112 .br
113 \fB\-\-target\-pgdata=\fR\fB\fIdirectory\fR\fR
114 .RS 4
115 This option specifies the target data directory that is synchronized with the source\&. The target server must be shut down cleanly before running
116 pg_rewind
117 .RE
118 .PP
119 \fB\-\-source\-pgdata=\fR\fB\fIdirectory\fR\fR
120 .RS 4
121 Specifies the file system path to the data directory of the source server to synchronize the target with\&. This option requires the source server to be cleanly shut down\&.
122 .RE
123 .PP
124 \fB\-\-source\-server=\fR\fB\fIconnstr\fR\fR
125 .RS 4
126 Specifies a libpq connection string to connect to the source
127 PostgreSQL
128 server to synchronize the target with\&. The connection must be a normal (non\-replication) connection with a role having sufficient permissions to execute the functions used by
129 pg_rewind
130 on the source server (see Notes section for details) or a superuser role\&. This option requires the source server to be running and accepting connections\&.
131 .RE
132 .PP
133 \fB\-R\fR
134 .br
135 \fB\-\-write\-recovery\-conf\fR
136 .RS 4
137 Create
138 standby\&.signal
139 and append connection settings to
140 postgresql\&.auto\&.conf
141 in the output directory\&. The dbname will be recorded only if the dbname was specified explicitly in the connection string or
142 environment variable\&.
143 \-\-source\-server
144 is mandatory with this option\&.
145 .RE
146 .PP
147 \fB\-n\fR
148 .br
149 \fB\-\-dry\-run\fR
150 .RS 4
151 Do everything except actually modifying the target directory\&.
152 .RE
153 .PP
154 \fB\-N\fR
155 .br
156 \fB\-\-no\-sync\fR
157 .RS 4
158 By default,
159 \fBpg_rewind\fR
160 will wait for all files to be written safely to disk\&. This option causes
161 \fBpg_rewind\fR
162 to return without waiting, which is faster, but means that a subsequent operating system crash can leave the data directory corrupt\&. Generally, this option is useful for testing but should not be used on a production installation\&.
163 .RE
164 .PP
165 \fB\-P\fR
166 .br
167 \fB\-\-progress\fR
168 .RS 4
169 Enables progress reporting\&. Turning this on will deliver an approximate progress report while copying data from the source cluster\&.
170 .RE
171 .PP
172 \fB\-c\fR
173 .br
174 \fB\-\-restore\-target\-wal\fR
175 .RS 4
176 Use
177 \fIrestore_command\fR
178 defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the
179 pg_wal
180 directory\&.
181 .RE
182 .PP
183 \fB\-\-config\-file=\fR\fB\fIfilename\fR\fR
184 .RS 4
185 Use the specified main server configuration file for the target cluster\&. This affects
186 pg_rewind
187 when it uses internally the
188 postgres
189 command for the rewind operation on this cluster (when retrieving
190 \fIrestore_command\fR
191 with the option
192 \fB\-c/\-\-restore\-target\-wal\fR
193 and when forcing a completion of crash recovery)\&.
194 .RE
195 .PP
196 \fB\-\-debug\fR
197 .RS 4
198 Print verbose debugging output that is mostly useful for developers debugging
199 pg_rewind\&.
200 .RE
201 .PP
202 \fB\-\-no\-ensure\-shutdown\fR
203 .RS 4
204 pg_rewind
205 requires that the target server is cleanly shut down before rewinding\&. By default, if the target server is not shut down cleanly,
206 pg_rewind
207 starts the target server in single\-user mode to complete crash recovery first, and stops it\&. By passing this option,
208 pg_rewind
209 skips this and errors out immediately if the server is not cleanly shut down\&. Users are expected to handle the situation themselves in that case\&.
210 .RE
211 .PP
212 \fB\-\-sync\-method=\fR\fB\fImethod\fR\fR
213 .RS 4
214 When set to
215 fsync, which is the default,
216 \fBpg_rewind\fR
217 will recursively open and synchronize all files in the data directory\&. The search for files will follow symbolic links for the WAL directory and each configured tablespace\&.
218 .sp
219 On Linux,
220 syncfs
221 may be used instead to ask the operating system to synchronize the whole file systems that contain the data directory, the WAL files, and each tablespace\&. See
222 recovery_init_sync_method
223 for information about the caveats to be aware of when using
224 syncfs\&.
225 .sp
226 This option has no effect when
227 \fB\-\-no\-sync\fR
228 is used\&.
229 .RE
230 .PP
231 \fB\-V\fR
232 .br
233 \fB\-\-version\fR
234 .RS 4
235 Display version information, then exit\&.
236 .RE
237 .PP
238 \fB\-?\fR
239 .br
240 \fB\-\-help\fR
241 .RS 4
242 Show help, then exit\&.
243 .RE
244 .SH "ENVIRONMENT"
245 .PP
246 When
247 \fB\-\-source\-server\fR
248 option is used,
249 pg_rewind
250 also uses the environment variables supported by
251 libpq
252 (see
253 Section\ \&32.15)\&.
254 .PP
255 The environment variable
256 \fBPG_COLOR\fR
257 specifies whether to use color in diagnostic messages\&. Possible values are
258 always,
259 auto
260 and
261 never\&.
262 .SH "NOTES"
263 .PP
264 When executing
265 pg_rewind
266 using an online cluster as source, a role having sufficient permissions to execute the functions used by
267 pg_rewind
268 on the source cluster can be used instead of a superuser\&. Here is how to create such a role, named
269 rewind_user
270 here:
271 .sp
272 .if n \{\
273 .RS 4
274 .\}
275 .nf
276 CREATE USER rewind_user LOGIN;
277 GRANT EXECUTE ON function pg_catalog\&.pg_ls_dir(text, boolean, boolean) TO rewind_user;
278 GRANT EXECUTE ON function pg_catalog\&.pg_stat_file(text, boolean) TO rewind_user;
279 GRANT EXECUTE ON function pg_catalog\&.pg_read_binary_file(text) TO rewind_user;
280 GRANT EXECUTE ON function pg_catalog\&.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
281 .fi
282 .if n \{\
283 .RE
284 .\}
285 .sp
286 .SS "How It Works"
287 .PP
288 The basic idea is to copy all file system\-level changes from the source cluster to the target cluster:
289 .sp
290 .RS 4
291 .ie n \{\
292 \h'-04' 1.\h'+01'\c
293 .\}
294 .el \{\
295 .sp -1
296 .IP "  1." 4.2
297 .\}
298 Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster\*(Aqs timeline history forked off from the target cluster\&. For each WAL record, record each data block that was touched\&. This yields a list of all the data blocks that were changed in the target cluster, after the source cluster forked off\&. If some of the WAL files are no longer available, try re\-running
299 pg_rewind
300 with the
301 \fB\-c\fR
302 option to search for the missing files in the WAL archive\&.
303 .RE
304 .sp
305 .RS 4
306 .ie n \{\
307 \h'-04' 2.\h'+01'\c
308 .\}
309 .el \{\
310 .sp -1
311 .IP "  2." 4.2
312 .\}
313 Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (\fB\-\-source\-pgdata\fR) or SQL (\fB\-\-source\-server\fR)\&. Relation files are now in a state equivalent to the moment of the last completed checkpoint prior to the point at which the WAL timelines of the source and target diverged plus the current state on the source of any blocks changed on the target after that divergence\&.
314 .RE
315 .sp
316 .RS 4
317 .ie n \{\
318 \h'-04' 3.\h'+01'\c
319 .\}
320 .el \{\
321 .sp -1
322 .IP "  3." 4.2
323 .\}
324 Copy all other files, including new relation files, WAL segments,
325 pg_xact, and configuration files from the source cluster to the target cluster\&. Similarly to base backups, the contents of the directories
326 pg_dynshmem/,
327 pg_notify/,
328 pg_replslot/,
329 pg_serial/,
330 pg_snapshots/,
331 pg_stat_tmp/, and
332 pg_subtrans/
333 are omitted from the data copied from the source cluster\&. The files
334 backup_label,
335 tablespace_map,
336 pg_internal\&.init,
337 postmaster\&.opts,
338 postmaster\&.pid
339 and
340 \&.DS_Store
341 as well as any file or directory beginning with
342 pgsql_tmp, are omitted\&.
343 .RE
344 .sp
345 .RS 4
346 .ie n \{\
347 \h'-04' 4.\h'+01'\c
348 .\}
349 .el \{\
350 .sp -1
351 .IP "  4." 4.2
352 .\}
353 Create a
354 backup_label
355 file to begin WAL replay at the checkpoint created at failover and configure the
356 pg_control
357 file with a minimum consistency LSN defined as the result of
358 pg_current_wal_insert_lsn()
359 when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source\&.
360 .RE
361 .sp
362 .RS 4
363 .ie n \{\
364 \h'-04' 5.\h'+01'\c
365 .\}
366 .el \{\
367 .sp -1
368 .IP "  5." 4.2
369 .\}
370 When starting the target,
371 PostgreSQL
372 replays all the required WAL, resulting in a data directory in a consistent state\&.
373 .RE