<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>pg_rewind</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="app-pgresetwal.html" title="pg_resetwal" /><link rel="next" href="pgtestfsync.html" title="pg_test_fsync" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center"><span xmlns="http://www.w3.org/1999/xhtml" class="application">pg_rewind</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgresetwal.html" title="pg_resetwal">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-server.html" title="PostgreSQL Server Applications">Up</a></td><th width="60%" align="center">PostgreSQL Server Applications</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="pgtestfsync.html" title="pg_test_fsync">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="APP-PGREWIND"><div class="titlepage"></div><a id="id-1.9.5.8.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">pg_rewind</span></span></h2><p>pg_rewind — synchronize a <span class="productname">PostgreSQL</span> data directory with another data directory that was forked from it</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.5.8.4.1"><code class="command">pg_rewind</code> [<em class="replaceable"><code>option</code></em>...] { <code class="option">-D </code> | <code class="option">--target-pgdata</code> }<em class="replaceable"><code> directory</code></em> { <code class="option">--source-pgdata=<em class="replaceable"><code>directory</code></em></code> | <code class="option">--source-server=<em class="replaceable"><code>connstr</code></em></code> } </p></div></div><div class="refsect1" id="id-1.9.5.8.5"><h2>Description</h2><p> <span class="application">pg_rewind</span> is a tool for synchronizing a PostgreSQL cluster with another copy of the same cluster, after the clusters' timelines have diverged. A typical scenario is to bring an old master server back online after failover as a standby that follows the new master. </p><p> The result is equivalent to replacing the target data directory with the source one. Only changed blocks from relation files are copied; all other files are copied in full, including configuration files. The advantage of <span class="application">pg_rewind</span> over taking a new base backup, or tools like <span class="application">rsync</span>, is that <span class="application">pg_rewind</span> does not require reading through unchanged blocks in the cluster. This makes it a lot faster when the database is large and only a small fraction of blocks differ between the clusters. </p><p> <span class="application">pg_rewind</span> 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's <code class="filename">pg_wal</code> 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, the old WAL files might no longer be present. In that case, they can be manually copied from the WAL archive to the <code class="filename">pg_wal</code> directory, or fetched on startup by configuring <code class="filename">recovery.conf</code>. The use of <span class="application">pg_rewind</span> is not limited to failover, e.g. a standby server can be promoted, run some write transactions, and then rewinded to become a standby again. </p><p> When the target server is started for the first time after running <span class="application">pg_rewind</span>, it will go into recovery mode and replay all WAL generated in the source server after the point of divergence. If some of the WAL was no longer available in the source server when <span class="application">pg_rewind</span> was run, and therefore could not be copied by the <span class="application">pg_rewind</span> session, it must be made available when the target server is started. This can be done by creating a <code class="filename">recovery.conf</code> file in the target data directory with a suitable <code class="varname">restore_command</code>. </p><p> <span class="application">pg_rewind</span> requires that the target server either has the <a class="xref" href="runtime-config-wal.html#GUC-WAL-LOG-HINTS">wal_log_hints</a> option enabled in <code class="filename">postgresql.conf</code> or data checksums enabled when the cluster was initialized with <span class="application">initdb</span>. Neither of these are currently on by default. <a class="xref" href="runtime-config-wal.html#GUC-FULL-PAGE-WRITES">full_page_writes</a> must also be set to <code class="literal">on</code>, but is enabled by default. </p><div class="warning"><h3 class="title">Warning</h3><p> If <span class="application">pg_rewind</span> 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. </p><p> <span class="application">pg_rewind</span> 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 <span class="application">pg_rewind</span>. 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. </p></div></div><div class="refsect1" id="id-1.9.5.8.6"><h2>Options</h2><p> <span class="application">pg_rewind</span> accepts the following command-line arguments: </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-D <em class="replaceable"><code>directory</code></em></code><br /></span><span class="term"><code class="option">--target-pgdata=<em class="replaceable"><code>directory</code></em></code></span></dt><dd><p> This option specifies the target data directory that is synchronized with the source. The target server must be shut down cleanly before running <span class="application">pg_rewind</span> </p></dd><dt><span class="term"><code class="option">--source-pgdata=<em class="replaceable"><code>directory</code></em></code></span></dt><dd><p> 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. </p></dd><dt><span class="term"><code class="option">--source-server=<em class="replaceable"><code>connstr</code></em></code></span></dt><dd><p> Specifies a libpq connection string to connect to the source <span class="productname">PostgreSQL</span> 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 <span class="application">pg_rewind</span> on the source server (see Notes section for details) or a superuser role. This option requires the source server to be running and not in recovery mode. </p></dd><dt><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--dry-run</code></span></dt><dd><p> Do everything except actually modifying the target directory. </p></dd><dt><span class="term"><code class="option">-P</code><br /></span><span class="term"><code class="option">--progress</code></span></dt><dd><p> Enables progress reporting. Turning this on will deliver an approximate progress report while copying data from the source cluster. </p></dd><dt><span class="term"><code class="option">--debug</code></span></dt><dd><p> Print verbose debugging output that is mostly useful for developers debugging <span class="application">pg_rewind</span>. </p></dd><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>Display version information, then exit.</p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help</code></span></dt><dd><p>Show help, then exit.</p></dd></dl></div><p> </p></div><div class="refsect1" id="id-1.9.5.8.7"><h2>Environment</h2><p> When <code class="option">--source-server</code> option is used, <span class="application">pg_rewind</span> also uses the environment variables supported by <span class="application">libpq</span> (see <a class="xref" href="libpq-envars.html" title="34.14. Environment Variables">Section 34.14</a>). </p></div><div class="refsect1" id="id-1.9.5.8.8"><h2>Notes</h2><p> When executing <span class="application">pg_rewind</span> using an online cluster as source, a role having sufficient permissions to execute the functions used by <span class="application">pg_rewind</span> on the source cluster can be used instead of a superuser. Here is how to create such a role, named <code class="literal">rewind_user</code> here: </p><pre class="programlisting"> 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; </pre><p> </p><p> When executing <span class="application">pg_rewind</span> using an online cluster as source which has been recently promoted, it is necessary to execute a <code class="command">CHECKPOINT</code> after promotion so as its control file reflects up-to-date timeline information, which is used by <span class="application">pg_rewind</span> to check if the target cluster can be rewound using the designated source cluster. </p><div class="refsect2" id="id-1.9.5.8.8.4"><h3>How it works</h3><p> The basic idea is to copy all file system-level changes from the source cluster to the target cluster: </p><div class="procedure"><ol class="procedure" type="1"><li class="step"><p> Scan the WAL log of the target cluster, starting from the last checkpoint before the point where the source cluster's 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. </p></li><li class="step"><p> Copy all those changed blocks from the source cluster to the target cluster, either using direct file system access (<code class="option">--source-pgdata</code>) or SQL (<code class="option">--source-server</code>). </p></li><li class="step"><p> Copy all other files such as <code class="filename">pg_xact</code> and configuration files from the source cluster to the target cluster (everything except the relation files). Similarly to base backups, the contents of the directories <code class="filename">pg_dynshmem/</code>, <code class="filename">pg_notify/</code>, <code class="filename">pg_replslot/</code>, <code class="filename">pg_serial/</code>, <code class="filename">pg_snapshots/</code>, <code class="filename">pg_stat_tmp/</code>, and <code class="filename">pg_subtrans/</code> are omitted from the data copied from the source cluster. Any file or directory beginning with <code class="filename">pgsql_tmp</code> is omitted, as well as are <code class="filename">backup_label</code>, <code class="filename">tablespace_map</code>, <code class="filename">pg_internal.init</code>, <code class="filename">postmaster.opts</code> and <code class="filename">postmaster.pid</code>. </p></li><li class="step"><p> Apply the WAL from the source cluster, starting from the checkpoint created at failover. (Strictly speaking, <span class="application">pg_rewind</span> doesn't apply the WAL, it just creates a backup label file that makes <span class="productname">PostgreSQL</span> start by replaying all WAL from that checkpoint forward.) </p></li></ol></div></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgresetwal.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-server.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pgtestfsync.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="application">pg_resetwal</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">pg_test_fsync</span></td></tr></table></div></body></html>