aboutsummaryrefslogtreecommitdiff
path: root/doc/src/sgml/ref/pg_rewind.sgml
blob: ef0cc280d14bc455614b7a3b0623a122ddfab8f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!--
doc/src/sgml/ref/pg_rewind.sgml
PostgreSQL documentation
-->

<refentry id="app-pgrewind">
 <indexterm zone="app-pgrewind">
  <primary>pg_rewind</primary>
 </indexterm>

 <refmeta>
  <refentrytitle><application>pg_rewind</application></refentrytitle>
  <manvolnum>1</manvolnum>
  <refmiscinfo>Application</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>pg_rewind</refname>
  <refpurpose>synchronize a <productname>PostgreSQL</productname> data directory with another data directory that was forked from the first one</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
  <cmdsynopsis>
   <command>pg_rewind</command>
   <arg rep="repeat"><replaceable>option</replaceable></arg>
   <group choice="plain">
    <group choice="req">
     <arg choice="plain"><option>-D </option></arg>
     <arg choice="plain"><option>--target-pgdata</option></arg>
    </group>
    <replaceable> directory</replaceable>
    <group choice="req">
     <arg choice="plain"><option>--source-pgdata=<replaceable>directory</replaceable></option></arg>
     <arg choice="plain"><option>--source-server=<replaceable>connstr</replaceable></option></arg>
    </group>
   </group>
  </cmdsynopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <application>pg_rewind</> 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.
  </para>

  <para>
   The result is equivalent to replacing the target data directory with the
   source one. All files are copied, including configuration files. The
   advantage of <application>pg_rewind</> over taking a new base backup, or
   tools like <application>rsync</>, is that <application>pg_rewind</> does
   not require reading through all unchanged files in the cluster. That makes
   it a lot faster when the database is large and only a small portion of it
   differs between the clusters.
  </para>

  <para>
   <application>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's <filename>pg_xlog</> directory
   reaching all the way back to the point of divergence. In the typical
   failover scenario where the target cluster was shut down soon after the
   divergence, that is not a problem, but if the target cluster had run for a
   long time after the divergence, the old WAL files might not be present
   anymore. In that case, they can be manually copied from the WAL archive to
   the <filename>pg_xlog</> directory. Fetching missing files from a WAL
   archive automatically is currently not supported.
  </para>

  <para>
   When the target server is started up for the first time after running
   <application>pg_rewind</>, 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
   <application>pg_rewind</> was run, and therefore could not be copied by
   <application>pg_rewind</> session, it needs to be made available when the
   target server is started up. That can be done by creating a
   <filename>recovery.conf</> file in the target data directory with a
   suitable <varname>restore_command</>.
  </para>

  <para>
   <application>pg_rewind</> requires that the target server either has
   the <xref linkend="guc-wal-log-hints"> option is enabled
   in <filename>postgresql.conf</> or that data checksums were enabled when
   the cluster was initialized with <application>initdb</>.  Neither of these
   are currently on by default.
   <xref linkend="guc-full-page-writes"> must also be enabled.  That is the
   default.
  </para>
 </refsect1>

 <refsect1>
  <title>Options</title>

   <para>
    <application>pg_rewind</application> accepts the following command-line
    arguments:

    <variablelist>
     <varlistentry>
      <term><option>-D <replaceable class="parameter">directory</replaceable></option></term>
      <term><option>--target-pgdata=<replaceable class="parameter">directory</replaceable></option></term>
      <listitem>
       <para>
        This option specifies the target data directory that is synchronized
        with the source. The target server must shut down cleanly before
        running <application>pg_rewind</application>
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><option>--source-pgdata=<replaceable class="parameter">directory</replaceable></option></term>
      <listitem>
       <para>
        Specifies path to the data directory of the source server, to
        synchronize the target with. When <option>--source-pgdata</> is
        used, the source server must be cleanly shut down.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><option>--source-server=<replaceable class="parameter">connstr</replaceable></option></term>
      <listitem>
       <para>
        Specifies a libpq connection string to connect to the source
        <productname>PostgreSQL</> server to synchronize the target with.
        The connection must be a normal (non-replication) connection 
        with superuser access. The server must be up and running, 
        and must not be in recovery mode.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><option>-n</option></term>
      <term><option>--dry-run</option></term>
      <listitem>
       <para>
        Do everything except actually modifying the target directory.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><option>-P</option></term>
      <term><option>--progress</option></term>
      <listitem>
       <para>
        Enables progress reporting. Turning this on will deliver an approximate
        progress report while copying data over from the source cluster.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><option>--debug</option></term>
      <listitem>
       <para>
        Print verbose debugging output that is mostly useful for developers
        debugging <application>pg_rewind</>.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><option>-V</option></term>
      <term><option>--version</option></term>
      <listitem><para>Display version information, then exit.</para></listitem>
     </varlistentry>

     <varlistentry>
      <term><option>-?</option></term>
      <term><option>--help</option></term>
      <listitem><para>Show help, then exit.</para></listitem>
     </varlistentry>

    </variablelist>
   </para>
 </refsect1>

 <refsect1>
  <title>Environment</title>

  <para>
   When <option>--source-server</> option is used,
   <application>pg_rewind</application> also uses the environment variables
   supported by <application>libpq</> (see <xref linkend="libpq-envars">).
  </para>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <refsect2>
   <title>How it works</title>

   <para>
    The basic idea is to copy everything from the new cluster to the old
    cluster, except for the blocks that we know to be the same.
   </para>

   <procedure>
    <step>
     <para>
      Scan the WAL log of the old cluster, starting from the last checkpoint
      before the point where the new cluster's timeline history forked off
      from the old cluster. For each WAL record, make a note of the data
      blocks that were touched. This yields a list of all the data blocks
      that were changed in the old cluster, after the new cluster forked off.
     </para>
    </step>
    <step>
     <para>
      Copy all those changed blocks from the new cluster to the old cluster.
     </para>
    </step>
    <step>
     <para>
      Copy all other files such as <filename>clog</filename> and configuration files from the new cluster
      to the old cluster, everything except the relation files.
     </para>
    </step>
    <step>
     <para>
      Apply the WAL from the new cluster, starting from the checkpoint
      created at failover. (Strictly speaking, <application>pg_rewind</>
      doesn't apply the WAL, it just creates a backup label file indicating
      that when <productname>PostgreSQL</> is started, it will start replay
      from that checkpoint and apply all the required WAL.)
     </para>
    </step>
   </procedure>
  </refsect2>
 </refsect1>

</refentry>