summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man1/pg_rewind.1
diff options
context:
space:
mode:
Diffstat (limited to 'doc/src/sgml/man1/pg_rewind.1')
-rw-r--r--doc/src/sgml/man1/pg_rewind.1357
1 files changed, 357 insertions, 0 deletions
diff --git a/doc/src/sgml/man1/pg_rewind.1 b/doc/src/sgml/man1/pg_rewind.1
new file mode 100644
index 0000000..48c2f7e
--- /dev/null
+++ b/doc/src/sgml/man1/pg_rewind.1
@@ -0,0 +1,357 @@
+'\" t
+.\" Title: pg_rewind
+.\" Author: The PostgreSQL Global Development Group
+.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
+.\" Date: 2023
+.\" Manual: PostgreSQL 15.5 Documentation
+.\" Source: PostgreSQL 15.5
+.\" Language: English
+.\"
+.TH "PG_REWIND" "1" "2023" "PostgreSQL 15.5" "PostgreSQL 15.5 Documentation"
+.\" -----------------------------------------------------------------
+.\" * Define some portability stuff
+.\" -----------------------------------------------------------------
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.\" http://bugs.debian.org/507673
+.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
+.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.ie \n(.g .ds Aq \(aq
+.el .ds Aq '
+.\" -----------------------------------------------------------------
+.\" * set default formatting
+.\" -----------------------------------------------------------------
+.\" disable hyphenation
+.nh
+.\" disable justification (adjust text to left margin only)
+.ad l
+.\" -----------------------------------------------------------------
+.\" * MAIN CONTENT STARTS HERE *
+.\" -----------------------------------------------------------------
+.SH "NAME"
+pg_rewind \- synchronize a PostgreSQL data directory with another data directory that was forked from it
+.SH "SYNOPSIS"
+.HP \w'\fBpg_rewind\fR\ 'u
+\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}
+.SH "DESCRIPTION"
+.PP
+pg_rewind
+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\&.
+.PP
+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
+rsync,
+pg_rewind
+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\&.
+.PP
+pg_rewind
+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
+pg_wal
+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
+pg_wal
+directory, or run
+pg_rewind
+with the
+\-c
+option to automatically retrieve them from the WAL archive\&. The use of
+pg_rewind
+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\&.
+.PP
+After running
+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
+pg_rewind
+was run, and therefore could not be copied by the
+pg_rewind
+session, it must be made available when the target server is started\&. This can be done by creating a
+recovery\&.signal
+file in the target data directory and by configuring a suitable
+restore_command
+in
+postgresql\&.conf\&.
+.PP
+pg_rewind
+requires that the target server either has the
+wal_log_hints
+option enabled in
+postgresql\&.conf
+or data checksums enabled when the cluster was initialized with
+initdb\&. Neither of these are currently on by default\&.
+full_page_writes
+must also be set to
+on, but is enabled by default\&.
+.if n \{\
+.sp
+.\}
+.RS 4
+.it 1 an-trap
+.nr an-no-space-flag 1
+.nr an-break-flag 1
+.br
+.ps +1
+\fBWarning\fR
+.ps -1
+.br
+.PP
+If
+pg_rewind
+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\&.
+.PP
+As
+pg_rewind
+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\&.
+.PP
+pg_rewind
+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
+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\&.
+.sp .5v
+.RE
+.SH "OPTIONS"
+.PP
+pg_rewind
+accepts the following command\-line arguments:
+.PP
+\fB\-D \fR\fB\fIdirectory\fR\fR
+.br
+\fB\-\-target\-pgdata=\fR\fB\fIdirectory\fR\fR
+.RS 4
+This option specifies the target data directory that is synchronized with the source\&. The target server must be shut down cleanly before running
+pg_rewind
+.RE
+.PP
+\fB\-\-source\-pgdata=\fR\fB\fIdirectory\fR\fR
+.RS 4
+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\&.
+.RE
+.PP
+\fB\-\-source\-server=\fR\fB\fIconnstr\fR\fR
+.RS 4
+Specifies a libpq connection string to connect to the source
+PostgreSQL
+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
+pg_rewind
+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\&.
+.RE
+.PP
+\fB\-R\fR
+.br
+\fB\-\-write\-recovery\-conf\fR
+.RS 4
+Create
+standby\&.signal
+and append connection settings to
+postgresql\&.auto\&.conf
+in the output directory\&.
+\-\-source\-server
+is mandatory with this option\&.
+.RE
+.PP
+\fB\-n\fR
+.br
+\fB\-\-dry\-run\fR
+.RS 4
+Do everything except actually modifying the target directory\&.
+.RE
+.PP
+\fB\-N\fR
+.br
+\fB\-\-no\-sync\fR
+.RS 4
+By default,
+\fBpg_rewind\fR
+will wait for all files to be written safely to disk\&. This option causes
+\fBpg_rewind\fR
+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\&.
+.RE
+.PP
+\fB\-P\fR
+.br
+\fB\-\-progress\fR
+.RS 4
+Enables progress reporting\&. Turning this on will deliver an approximate progress report while copying data from the source cluster\&.
+.RE
+.PP
+\fB\-c\fR
+.br
+\fB\-\-restore\-target\-wal\fR
+.RS 4
+Use
+\fIrestore_command\fR
+defined in the target cluster configuration to retrieve WAL files from the WAL archive if these files are no longer available in the
+pg_wal
+directory\&.
+.RE
+.PP
+\fB\-\-config\-file=\fR\fB\fIfilename\fR\fR
+.RS 4
+Use the specified main server configuration file for the target cluster\&. This affects
+pg_rewind
+when it uses internally the
+postgres
+command for the rewind operation on this cluster (when retrieving
+\fIrestore_command\fR
+with the option
+\fB\-c/\-\-restore\-target\-wal\fR
+and when forcing a completion of crash recovery)\&.
+.RE
+.PP
+\fB\-\-debug\fR
+.RS 4
+Print verbose debugging output that is mostly useful for developers debugging
+pg_rewind\&.
+.RE
+.PP
+\fB\-\-no\-ensure\-shutdown\fR
+.RS 4
+pg_rewind
+requires that the target server is cleanly shut down before rewinding\&. By default, if the target server is not shut down cleanly,
+pg_rewind
+starts the target server in single\-user mode to complete crash recovery first, and stops it\&. By passing this option,
+pg_rewind
+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\&.
+.RE
+.PP
+\fB\-V\fR
+.br
+\fB\-\-version\fR
+.RS 4
+Display version information, then exit\&.
+.RE
+.PP
+\fB\-?\fR
+.br
+\fB\-\-help\fR
+.RS 4
+Show help, then exit\&.
+.RE
+.SH "ENVIRONMENT"
+.PP
+When
+\fB\-\-source\-server\fR
+option is used,
+pg_rewind
+also uses the environment variables supported by
+libpq
+(see
+Section\ \&34.15)\&.
+.PP
+The environment variable
+\fBPG_COLOR\fR
+specifies whether to use color in diagnostic messages\&. Possible values are
+always,
+auto
+and
+never\&.
+.SH "NOTES"
+.PP
+When executing
+pg_rewind
+using an online cluster as source, a role having sufficient permissions to execute the functions used by
+pg_rewind
+on the source cluster can be used instead of a superuser\&. Here is how to create such a role, named
+rewind_user
+here:
+.sp
+.if n \{\
+.RS 4
+.\}
+.nf
+CREATE USER rewind_user LOGIN;
+GRANT EXECUTE ON function pg_catalog\&.pg_ls_dir(text, boolean, boolean) TO rewind_user;
+GRANT EXECUTE ON function pg_catalog\&.pg_stat_file(text, boolean) TO rewind_user;
+GRANT EXECUTE ON function pg_catalog\&.pg_read_binary_file(text) TO rewind_user;
+GRANT EXECUTE ON function pg_catalog\&.pg_read_binary_file(text, bigint, bigint, boolean) TO rewind_user;
+.fi
+.if n \{\
+.RE
+.\}
+.PP
+When executing
+pg_rewind
+using an online cluster as source which has been recently promoted, it is necessary to execute a
+\fBCHECKPOINT\fR
+after promotion such that its control file reflects up\-to\-date timeline information, which is used by
+pg_rewind
+to check if the target cluster can be rewound using the designated source cluster\&.
+.SS "How It Works"
+.PP
+The basic idea is to copy all file system\-level changes from the source cluster to the target cluster:
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 1.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 1." 4.2
+.\}
+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
+pg_rewind
+with the
+\fB\-c\fR
+option to search for the missing files in the WAL archive\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 2.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 2." 4.2
+.\}
+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\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 3.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 3." 4.2
+.\}
+Copy all other files, including new relation files, WAL segments,
+pg_xact, and configuration files from the source cluster to the target cluster\&. Similarly to base backups, the contents of the directories
+pg_dynshmem/,
+pg_notify/,
+pg_replslot/,
+pg_serial/,
+pg_snapshots/,
+pg_stat_tmp/, and
+pg_subtrans/
+are omitted from the data copied from the source cluster\&. The files
+backup_label,
+tablespace_map,
+pg_internal\&.init,
+postmaster\&.opts, and
+postmaster\&.pid, as well as any file or directory beginning with
+pgsql_tmp, are omitted\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 4.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 4." 4.2
+.\}
+Create a
+backup_label
+file to begin WAL replay at the checkpoint created at failover and configure the
+pg_control
+file with a minimum consistency LSN defined as the result of
+pg_current_wal_insert_lsn()
+when rewinding from a live source or the last checkpoint LSN when rewinding from a stopped source\&.
+.RE
+.sp
+.RS 4
+.ie n \{\
+\h'-04' 5.\h'+01'\c
+.\}
+.el \{\
+.sp -1
+.IP " 5." 4.2
+.\}
+When starting the target,
+PostgreSQL
+replays all the required WAL, resulting in a data directory in a consistent state\&.
+.RE