Skip to content

Commit caa0783

Browse files
committed
Improve handling and coverage of --no-ensure-shutdown in pg_rewind
This includes a couple of changes around the new behavior of pg_rewind which enforces recovery to happen once on a cluster not shut down cleanly: - Some comments and documentation improvements. - Shutdown the cluster to rewind with immediate mode in all the tests, this allows to check after the forced recovery behavior which is wanted as new default. - Use -F for the forced recovery step, so as postgres does not use fsync. This was useless as a final sync is done once the tool is done. Author: Michael Paquier Reviewed-by: Alexey Kondratov Discussion: https://postgr.es/m/20191004083721.GA1829@paquier.xyz
1 parent 732457b commit caa0783

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

doc/src/sgml/ref/pg_rewind.sgml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,14 @@ PostgreSQL documentation
169169
<term><option>--no-ensure-shutdown</option></term>
170170
<listitem>
171171
<para>
172-
<application>pg_rewind</application> verifies that the target server
173-
is cleanly shutdown before rewinding; by default, if it isn't, it
174-
starts the server in single-user mode to complete crash recovery.
172+
<application>pg_rewind</application> requires that the target server
173+
is cleanly shut down before rewinding. By default, if the target server
174+
is not shut down cleanly, <application>pg_rewind</application> starts
175+
the target server in single-user mode to complete crash recovery first,
176+
and stops it.
175177
By passing this option, <application>pg_rewind</application> skips
176178
this and errors out immediately if the server is not cleanly shut
177-
down. Users are expected to handle the situation themselves in that
179+
down. Users are expected to handle the situation themselves in that
178180
case.
179181
</para>
180182
</listitem>

src/bin/pg_rewind/pg_rewind.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,12 @@ main(int argc, char **argv)
270270
pg_free(buffer);
271271

272272
/*
273-
* If the target instance was not cleanly shut down, run a single-user
274-
* postgres session really quickly and reload the control file to get the
275-
* new state. Note if no_ensure_shutdown is specified, pg_rewind won't do
276-
* that automatically. That means users need to do themselves in advance,
277-
* else pg_rewind will soon quit, see sanityChecks().
273+
* If the target instance was not cleanly shut down, start and stop the
274+
* target cluster once in single-user mode to enforce recovery to finish,
275+
* ensuring that the cluster can be used by pg_rewind. Note that if
276+
* no_ensure_shutdown is specified, pg_rewind ignores this step, and users
277+
* need to make sure by themselves that the target cluster is in a clean
278+
* state.
278279
*/
279280
if (!no_ensure_shutdown &&
280281
ControlFile_target.state != DB_SHUTDOWNED &&
@@ -847,8 +848,12 @@ ensureCleanShutdown(const char *argv0)
847848
if (dry_run)
848849
return;
849850

850-
/* finally run postgres in single-user mode */
851-
snprintf(cmd, MAXCMDLEN, "\"%s\" --single -D \"%s\" template1 < \"%s\"",
851+
/*
852+
* Finally run postgres in single-user mode. There is no need to use
853+
* fsync here. This makes the recovery faster, and the target data folder
854+
* is synced at the end anyway.
855+
*/
856+
snprintf(cmd, MAXCMDLEN, "\"%s\" --single -F -D \"%s\" template1 < \"%s\"",
852857
exec_path, datadir_target, DEVNULL);
853858

854859
if (system(cmd) != 0)

src/bin/pg_rewind/t/005_same_timeline.pl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#
2+
# Test that running pg_rewind with the source and target clusters
3+
# on the same timeline runs successfully.
4+
#
15
use strict;
26
use warnings;
37
use TestLib;

src/bin/pg_rewind/t/RewindTest.pm

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,10 @@ sub run_pg_rewind
227227
# Append the rewind-specific role to the connection string.
228228
$standby_connstr = "$standby_connstr user=rewind_user";
229229

230-
# Stop the master and be ready to perform the rewind
231-
$node_master->stop;
230+
# Stop the master and be ready to perform the rewind. The cluster
231+
# needs recovery to finish once, and pg_rewind makes sure that it
232+
# happens automatically.
233+
$node_master->stop('immediate');
232234

233235
# At this point, the rewind processing is ready to run.
234236
# We now have a very simple scenario with a few diverged WAL record.
@@ -260,19 +262,21 @@ sub run_pg_rewind
260262
}
261263
elsif ($test_mode eq "remote")
262264
{
263-
264-
# Do rewind using a remote connection as source
265+
# Do rewind using a remote connection as source, generating
266+
# recovery configuration automatically.
265267
command_ok(
266268
[
267269
'pg_rewind', "--debug",
268270
"--source-server", $standby_connstr,
269-
"--target-pgdata=$master_pgdata", "-R",
270-
"--no-sync"
271+
"--target-pgdata=$master_pgdata", "--no-sync",
272+
"--write-recovery-conf"
271273
],
272274
'pg_rewind remote');
273275

274-
# Check that standby.signal has been created.
275-
ok(-e "$master_pgdata/standby.signal");
276+
# Check that standby.signal is here as recovery configuration
277+
# was requested.
278+
ok( -e "$master_pgdata/standby.signal",
279+
'standby.signal created after pg_rewind');
276280

277281
# Now, when pg_rewind apparently succeeded with minimal permissions,
278282
# add REPLICATION privilege. So we could test that new standby

0 commit comments

Comments
 (0)