Skip to content

Commit 7ca3547

Browse files
committed
Remove temporary WAL and history files at the end of archive recovery
cbc55da has reworked the order of some actions at the end of archive recovery. Unfortunately this overlooked the fact that the startup process needs to remove RECOVERYXLOG (for temporary WAL segment newly recovered from archives) and RECOVERYHISTORY (for temporary history file) at this step, leaving the files around even after recovery ended. Backpatch to 9.5, like the previous commit. Author: Sawada Masahiko Reviewed-by: Fujii Masao, Michael Paquier Discussion: https://postgr.es/m/CAD21AoBO_eDQub6zojFnWtnmutRBWvYf7=cW4Hsqj+U_R26w3Q@mail.gmail.com Backpatch-through: 9.5
1 parent e3d23f6 commit 7ca3547

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/backend/access/transam/xlog.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5524,7 +5524,6 @@ readRecoveryCommandFile(void)
55245524
static void
55255525
exitArchiveRecovery(TimeLineID endTLI, XLogRecPtr endOfLog)
55265526
{
5527-
char recoveryPath[MAXPGPATH];
55285527
char xlogfname[MAXFNAMELEN];
55295528
XLogSegNo endLogSegNo;
55305529
XLogSegNo startLogSegNo;
@@ -5604,17 +5603,6 @@ exitArchiveRecovery(TimeLineID endTLI, XLogRecPtr endOfLog)
56045603
XLogFileName(xlogfname, ThisTimeLineID, startLogSegNo);
56055604
XLogArchiveCleanup(xlogfname);
56065605

5607-
/*
5608-
* Since there might be a partial WAL segment named RECOVERYXLOG, get rid
5609-
* of it.
5610-
*/
5611-
snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYXLOG");
5612-
unlink(recoveryPath); /* ignore any error */
5613-
5614-
/* Get rid of any remaining recovered timeline-history file, too */
5615-
snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYHISTORY");
5616-
unlink(recoveryPath); /* ignore any error */
5617-
56185606
/*
56195607
* Rename the config file out of the way, so that we don't accidentally
56205608
* re-enter archive recovery mode in a subsequent crash.
@@ -7489,6 +7477,7 @@ StartupXLOG(void)
74897477
if (ArchiveRecoveryRequested)
74907478
{
74917479
char reason[200];
7480+
char recoveryPath[MAXPGPATH];
74927481

74937482
Assert(InArchiveRecovery);
74947483

@@ -7545,6 +7534,17 @@ StartupXLOG(void)
75457534
*/
75467535
writeTimeLineHistory(ThisTimeLineID, recoveryTargetTLI,
75477536
EndRecPtr, reason);
7537+
7538+
/*
7539+
* Since there might be a partial WAL segment named RECOVERYXLOG, get
7540+
* rid of it.
7541+
*/
7542+
snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYXLOG");
7543+
unlink(recoveryPath); /* ignore any error */
7544+
7545+
/* Get rid of any remaining recovered timeline-history file, too */
7546+
snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYHISTORY");
7547+
unlink(recoveryPath); /* ignore any error */
75487548
}
75497549

75507550
/* Save the selected TimeLineID in shared memory, too */

src/test/recovery/t/002_archiving.pl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use warnings;
44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 1;
6+
use Test::More tests => 3;
77
use File::Copy;
88

99
# Initialize master node, doing archives
@@ -49,3 +49,26 @@
4949
my $result =
5050
$node_standby->safe_psql('postgres', "SELECT count(*) FROM tab_int");
5151
is($result, qq(1000), 'check content from archives');
52+
53+
# Check the presence of temporary files specifically generated during
54+
# archive recovery. To ensure the presence of the temporary history
55+
# file, switch to a timeline large enough to allow a standby to recover
56+
# a history file from an archive. As this requires at least two timeline
57+
# switches, promote the existing standby first. Then create a second
58+
# standby based on the promoted one. Finally, the second standby is
59+
# promoted.
60+
$node_standby->promote;
61+
62+
my $node_standby2 = get_new_node('standby2');
63+
$node_standby2->init_from_backup($node_master, $backup_name,
64+
has_restoring => 1);
65+
$node_standby2->start;
66+
67+
# Now promote standby2, and check that temporary files specifically
68+
# generated during archive recovery are removed by the end of recovery.
69+
$node_standby2->promote;
70+
my $node_standby2_data = $node_standby2->data_dir;
71+
ok( !-f "$node_standby2_data/pg_wal/RECOVERYHISTORY",
72+
"RECOVERYHISTORY removed after promotion");
73+
ok( !-f "$node_standby2_data/pg_wal/RECOVERYXLOG",
74+
"RECOVERYXLOG removed after promotion");

0 commit comments

Comments
 (0)