Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit abd0abf

Browse files
committed
Fix pg_rewind bugs when rewinding a standby server.
If the target is a standby server, its WAL doesn't end at the last checkpoint record, but at minRecoveryPoint. We must scan all the WAL from the last common checkpoint all the way up to minRecoveryPoint for modified pages, and also consider that portion when determining whether the server needs rewinding. Backpatch to all supported versions. Author: Ian Barwick and me Discussion: https://www.postgresql.org/message-id/CABvVfJU-LDWvoz4-Yow3Ay5LZYTuPD7eSjjE4kGyNZpXC6FrVQ%40mail.gmail.com
1 parent eec90ff commit abd0abf

File tree

3 files changed

+201
-24
lines changed

3 files changed

+201
-24
lines changed

src/bin/pg_rewind/parsexlog.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ static int SimpleXLogPageRead(XLogReaderState *xlogreader,
5454
* Read WAL from the datadir/pg_wal, starting from 'startpoint' on timeline
5555
* index 'tliIndex' in target timeline history, until 'endpoint'. Make note of
5656
* the data blocks touched by the WAL records, and return them in a page map.
57+
*
58+
* 'endpoint' is the end of the last record to read. The record starting at
59+
* 'endpoint' is the first one that is not read.
5760
*/
5861
void
5962
extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex,
@@ -92,7 +95,13 @@ extractPageMap(const char *datadir, XLogRecPtr startpoint, int tliIndex,
9295

9396
extractPageInfo(xlogreader);
9497

95-
} while (xlogreader->ReadRecPtr != endpoint);
98+
} while (xlogreader->EndRecPtr < endpoint);
99+
100+
/*
101+
* If 'endpoint' didn't point exactly at a record boundary, the caller
102+
* messed up.
103+
*/
104+
Assert(xlogreader->EndRecPtr == endpoint);
96105

97106
XLogReaderFree(xlogreader);
98107
if (xlogreadfd != -1)

src/bin/pg_rewind/pg_rewind.c

+35-23
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ main(int argc, char **argv)
123123
XLogRecPtr chkptrec;
124124
TimeLineID chkpttli;
125125
XLogRecPtr chkptredo;
126+
XLogRecPtr target_wal_endrec;
126127
size_t size;
127128
char *buffer;
128129
bool no_ensure_shutdown = false;
@@ -315,43 +316,56 @@ main(int argc, char **argv)
315316
{
316317
pg_log_info("source and target cluster are on the same timeline");
317318
rewind_needed = false;
319+
target_wal_endrec = 0;
318320
}
319321
else
320322
{
323+
XLogRecPtr chkptendrec;
324+
321325
findCommonAncestorTimeline(&divergerec, &lastcommontliIndex);
322326
pg_log_info("servers diverged at WAL location %X/%X on timeline %u",
323327
(uint32) (divergerec >> 32), (uint32) divergerec,
324328
targetHistory[lastcommontliIndex].tli);
325329

330+
/*
331+
* Determine the end-of-WAL on the target.
332+
*
333+
* The WAL ends at the last shutdown checkpoint, or at
334+
* minRecoveryPoint if it was a standby. (If we supported rewinding a
335+
* server that was not shut down cleanly, we would need to replay
336+
* until we reach the first invalid record, like crash recovery does.)
337+
*/
338+
339+
/* read the checkpoint record on the target to see where it ends. */
340+
chkptendrec = readOneRecord(datadir_target,
341+
ControlFile_target.checkPoint,
342+
targetNentries - 1,
343+
restore_command);
344+
345+
if (ControlFile_target.minRecoveryPoint > chkptendrec)
346+
{
347+
target_wal_endrec = ControlFile_target.minRecoveryPoint;
348+
}
349+
else
350+
{
351+
target_wal_endrec = chkptendrec;
352+
}
353+
326354
/*
327355
* Check for the possibility that the target is in fact a direct
328356
* ancestor of the source. In that case, there is no divergent history
329357
* in the target that needs rewinding.
330358
*/
331-
if (ControlFile_target.checkPoint >= divergerec)
359+
if (target_wal_endrec > divergerec)
332360
{
333361
rewind_needed = true;
334362
}
335363
else
336364
{
337-
XLogRecPtr chkptendrec;
338-
339-
/* Read the checkpoint record on the target to see where it ends. */
340-
chkptendrec = readOneRecord(datadir_target,
341-
ControlFile_target.checkPoint,
342-
targetNentries - 1,
343-
restore_command);
344-
345-
/*
346-
* If the histories diverged exactly at the end of the shutdown
347-
* checkpoint record on the target, there are no WAL records in
348-
* the target that don't belong in the source's history, and no
349-
* rewind is needed.
350-
*/
351-
if (chkptendrec == divergerec)
352-
rewind_needed = false;
353-
else
354-
rewind_needed = true;
365+
/* the last common checkpoint record must be part of target WAL */
366+
Assert(target_wal_endrec == divergerec);
367+
368+
rewind_needed = false;
355369
}
356370
}
357371

@@ -384,14 +398,12 @@ main(int argc, char **argv)
384398
/*
385399
* Read the target WAL from last checkpoint before the point of fork, to
386400
* extract all the pages that were modified on the target cluster after
387-
* the fork. We can stop reading after reaching the final shutdown record.
388-
* XXX: If we supported rewinding a server that was not shut down cleanly,
389-
* we would need to replay until the end of WAL here.
401+
* the fork.
390402
*/
391403
if (showprogress)
392404
pg_log_info("reading WAL in target");
393405
extractPageMap(datadir_target, chkptrec, lastcommontliIndex,
394-
ControlFile_target.checkPoint, restore_command);
406+
target_wal_endrec, restore_command);
395407
filemap_finalize();
396408

397409
if (showprogress)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#
2+
# Test situation where a target data directory contains
3+
# WAL records beyond both the last checkpoint and the divergence
4+
# point:
5+
#
6+
# Target WAL (TLI 2):
7+
#
8+
# backup ... Checkpoint A ... INSERT 'rewind this'
9+
# (TLI 1 -> 2)
10+
#
11+
# ^ last common ^ minRecoveryPoint
12+
# checkpoint
13+
#
14+
# Source WAL (TLI 3):
15+
#
16+
# backup ... Checkpoint A ... Checkpoint B ... INSERT 'keep this'
17+
# (TLI 1 -> 2) (TLI 2 -> 3)
18+
#
19+
#
20+
# The last common checkpoint is Checkpoint A. But there is WAL on TLI 2
21+
# after the last common checkpoint that needs to be rewound. We used to
22+
# have a bug where minRecoveryPoint was ignored, and pg_rewind concluded
23+
# that the target doesn't need rewinding in this scenario, because the
24+
# last checkpoint on the target TLI was an ancestor of the source TLI.
25+
#
26+
#
27+
# This test does not make use of RewindTest as it requires three
28+
# nodes.
29+
30+
use strict;
31+
use warnings;
32+
use PostgresNode;
33+
use TestLib;
34+
use Test::More tests => 3;
35+
36+
use File::Copy;
37+
38+
my $tmp_folder = TestLib::tempdir;
39+
40+
my $node_1 = get_new_node('node_1');
41+
$node_1->init(allows_streaming => 1);
42+
$node_1->append_conf('postgresql.conf', qq(
43+
wal_keep_size='100 MB'
44+
));
45+
46+
$node_1->start;
47+
48+
# Create a couple of test tables
49+
$node_1->safe_psql('postgres', 'CREATE TABLE public.foo (t TEXT)');
50+
$node_1->safe_psql('postgres', 'CREATE TABLE public.bar (t TEXT)');
51+
$node_1->safe_psql('postgres', "INSERT INTO public.bar VALUES ('in both')");
52+
53+
54+
# Take backup
55+
my $backup_name = 'my_backup';
56+
$node_1->backup($backup_name);
57+
58+
# Create streaming standby from backup
59+
my $node_2 = get_new_node('node_2');
60+
$node_2->init_from_backup($node_1, $backup_name,
61+
has_streaming => 1);
62+
$node_2->start;
63+
64+
# Create streaming standby from backup
65+
my $node_3 = get_new_node('node_3');
66+
$node_3->init_from_backup($node_1, $backup_name,
67+
has_streaming => 1);
68+
$node_3->start;
69+
70+
# Stop node_1
71+
72+
$node_1->stop('fast');
73+
74+
# Promote node_3
75+
$node_3->promote;
76+
77+
# node_1 rejoins node_3
78+
79+
my $node_3_connstr = $node_3->connstr;
80+
81+
$node_1->append_conf('postgresql.conf', qq(
82+
primary_conninfo='$node_3_connstr'
83+
));
84+
$node_1->set_standby_mode();
85+
$node_1->start();
86+
87+
# node_2 follows node_3
88+
89+
$node_2->append_conf('postgresql.conf', qq(
90+
primary_conninfo='$node_3_connstr'
91+
));
92+
$node_2->restart();
93+
94+
# Promote node_1
95+
96+
$node_1->promote;
97+
98+
# We now have a split-brain with two primaries. Insert a row on both to
99+
# demonstratively create a split brain. After the rewind, we should only
100+
# see the insert on 1, as the insert on node 3 is rewound away.
101+
$node_1->safe_psql('postgres', "INSERT INTO public.foo (t) VALUES ('keep this')");
102+
103+
# Insert more rows in node 1, to bump up the XID counter. Otherwise, if
104+
# rewind doesn't correctly rewind the changes made on the other node,
105+
# we might fail to notice if the inserts are invisible because the XIDs
106+
# are not marked as committed.
107+
$node_1->safe_psql('postgres', "INSERT INTO public.foo (t) VALUES ('and this')");
108+
$node_1->safe_psql('postgres', "INSERT INTO public.foo (t) VALUES ('and this too')");
109+
110+
# Also insert a row in 'bar' on node 3. It is unmodified in node 1, so it won't get
111+
# overwritten by replaying the WAL from node 1.
112+
$node_3->safe_psql('postgres', "INSERT INTO public.bar (t) VALUES ('rewind this')");
113+
114+
# Wait for node 2 to catch up
115+
$node_2->poll_query_until('postgres',
116+
q|SELECT COUNT(*) > 1 FROM public.bar|, 't');
117+
118+
# At this point node_2 will shut down without a shutdown checkpoint,
119+
# but with WAL entries beyond the preceding shutdown checkpoint.
120+
$node_2->stop('fast');
121+
$node_3->stop('fast');
122+
123+
my $node_2_pgdata = $node_2->data_dir;
124+
my $node_1_connstr = $node_1->connstr;
125+
126+
# Keep a temporary postgresql.conf or it would be overwritten during the rewind.
127+
copy(
128+
"$node_2_pgdata/postgresql.conf",
129+
"$tmp_folder/node_2-postgresql.conf.tmp");
130+
131+
command_ok(
132+
[
133+
'pg_rewind',
134+
"--source-server=$node_1_connstr",
135+
"--target-pgdata=$node_2_pgdata"
136+
],
137+
'pg_rewind detects rewind needed');
138+
139+
# Now move back postgresql.conf with old settings
140+
move(
141+
"$tmp_folder/node_2-postgresql.conf.tmp",
142+
"$node_2_pgdata/postgresql.conf");
143+
144+
$node_2->start;
145+
146+
# Check contents of the test tables after rewind. The rows inserted in node 3
147+
# before rewind should've been overwritten with the data from node 1.
148+
my $result;
149+
$result = $node_2->safe_psql('postgres', 'checkpoint');
150+
$result = $node_2->safe_psql('postgres', 'SELECT * FROM public.foo');
151+
is($result, qq(keep this
152+
and this
153+
and this too), 'table foo after rewind');
154+
155+
$result = $node_2->safe_psql('postgres', 'SELECT * FROM public.bar');
156+
is($result, qq(in both), 'table bar after rewind');

0 commit comments

Comments
 (0)