Debian Codegraph @

src/test/recovery/t/020_archive_status.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 #
5 # Tests related to WAL archiving and recovery.
6 #
7 use strict;
8 use warnings FATAL => 'all';
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12
13 my $primary = PostgreSQL::Test::Cluster->new('primary');
14 $primary->init(
15 has_archiving => 1,
16 allows_streaming => 1);
17 $primary->append_conf('postgresql.conf', 'autovacuum = off');
18 $primary->start;
19 my $primary_data = $primary->data_dir;
20
21 # Temporarily use an archive_command value to make the archiver fail,
22 # knowing that archiving is enabled. Note that we cannot use a command
23 # that does not exist as in this case the archiver process would just exit
24 # without reporting the failure to pg_stat_archiver. This also cannot
25 # use a plain "false" as that's unportable on Windows. So, instead, as
26 # a portable solution, use an archive command based on a command known to
27 # work but will fail: copy with an incorrect original path.
28 my $incorrect_command =
29 $PostgreSQL::Test::Utils::windows_os
30 ? qq{copy "%p_does_not_exist" "%f_does_not_exist"}
31 : qq{cp "%p_does_not_exist" "%f_does_not_exist"};
32 $primary->safe_psql(
33 'postgres', qq{
34 ALTER SYSTEM SET archive_command TO '$incorrect_command';
35 SELECT pg_reload_conf();
36 });
37
38 # Save the WAL segment currently in use and switch to a new segment.
39 # This will be used to track the activity of the archiver.
40 my $segment_name_1 = $primary->safe_psql('postgres',
41 q{SELECT pg_walfile_name(pg_current_wal_lsn())});
42 my $segment_path_1 = "pg_wal/archive_status/$segment_name_1";
43 my $segment_path_1_ready = "$segment_path_1.ready";
44 my $segment_path_1_done = "$segment_path_1.done";
45 $primary->safe_psql(
46 'postgres', q{
47 CREATE TABLE mine AS SELECT generate_series(1,10) AS x;
48 SELECT pg_switch_wal();
49 CHECKPOINT;
50 });
51
52 # Wait for an archive failure.
53 $primary->poll_query_until('postgres',
54 q{SELECT failed_count > 0 FROM pg_stat_archiver}, 't')
55 or die "Timed out while waiting for archiving to fail";
56 ok( -f "$primary_data/$segment_path_1_ready",
57 ".ready file exists for WAL segment $segment_name_1 waiting to be archived"
58 );
59 ok( !-f "$primary_data/$segment_path_1_done",
60 ".done file does not exist for WAL segment $segment_name_1 waiting to be archived"
61 );
62
63 is( $primary->safe_psql(
64 'postgres', q{
65 SELECT archived_count, last_failed_wal
66 FROM pg_stat_archiver
67 }),
68 "0|$segment_name_1",
69 "pg_stat_archiver failed to archive $segment_name_1");
70
71 # Crash the cluster for the next test in charge of checking that non-archived
72 # WAL segments are not removed.
73 $primary->stop('immediate');
74
75 # Recovery tests for the archiving with a standby partially check
76 # the recovery behavior when restoring a backup taken using a
77 # snapshot with no pg_backup_start/stop. In this situation,
78 # the recovered standby should enter first crash recovery then
79 # switch to regular archive recovery. Note that the base backup
80 # is taken here so as archive_command will fail. This is necessary
81 # for the assumptions of the tests done with the standbys below.
82 $primary->backup_fs_cold('backup');
83
84 $primary->start;
85 ok( -f "$primary_data/$segment_path_1_ready",
86 ".ready file for WAL segment $segment_name_1 still exists after crash recovery on primary"
87 );
88
89 # Allow WAL archiving again and wait for a success.
90 $primary->safe_psql(
91 'postgres', q{
92 ALTER SYSTEM RESET archive_command;
93 SELECT pg_reload_conf();
94 });
95
96 $primary->poll_query_until('postgres',
97 q{SELECT archived_count FROM pg_stat_archiver}, '1')
98 or die "Timed out while waiting for archiving to finish";
99
100 ok(!-f "$primary_data/$segment_path_1_ready",
101 ".ready file for archived WAL segment $segment_name_1 removed");
102
103 ok(-f "$primary_data/$segment_path_1_done",
104 ".done file for archived WAL segment $segment_name_1 exists");
105
106 is( $primary->safe_psql(
107 'postgres', q{ SELECT last_archived_wal FROM pg_stat_archiver }),
108 $segment_name_1,
109 "archive success reported in pg_stat_archiver for WAL segment $segment_name_1"
110 );
111
112 # Create some WAL activity and a new checkpoint so as the next standby can
113 # create a restartpoint. As this standby starts in crash recovery because
114 # of the cold backup taken previously, it needs a clean restartpoint to deal
115 # with existing status files.
116 my $segment_name_2 = $primary->safe_psql('postgres',
117 q{SELECT pg_walfile_name(pg_current_wal_lsn())});
118 my $segment_path_2 = "pg_wal/archive_status/$segment_name_2";
119 my $segment_path_2_ready = "$segment_path_2.ready";
120 my $segment_path_2_done = "$segment_path_2.done";
121 $primary->safe_psql(
122 'postgres', q{
123 INSERT INTO mine SELECT generate_series(10,20) AS x;
124 CHECKPOINT;
125 });
126
127 # Switch to a new segment and use the returned LSN to make sure that
128 # standbys have caught up to this point.
129 my $primary_lsn = $primary->safe_psql(
130 'postgres', q{
131 SELECT pg_switch_wal();
132 });
133
134 $primary->poll_query_until('postgres',
135 q{ SELECT last_archived_wal FROM pg_stat_archiver },
136 $segment_name_2)
137 or die "Timed out while waiting for archiving to finish";
138
139 # Test standby with archive_mode = on.
140 my $standby1 = PostgreSQL::Test::Cluster->new('standby');
141 $standby1->init_from_backup($primary, 'backup', has_restoring => 1);
142 $standby1->append_conf('postgresql.conf', "archive_mode = on");
143 my $standby1_data = $standby1->data_dir;
144 $standby1->start;
145
146 # Wait for the replay of the segment switch done previously, ensuring
147 # that all segments needed are restored from the archives.
148 $standby1->poll_query_until('postgres',
149 qq{ SELECT pg_wal_lsn_diff(pg_last_wal_replay_lsn(), '$primary_lsn') >= 0 }
150 ) or die "Timed out while waiting for xlog replay on standby1";
151
152 $standby1->safe_psql('postgres', q{CHECKPOINT});
153
154 # Recovery with archive_mode=on does not keep .ready signal files inherited
155 # from backup. Note that this WAL segment existed in the backup.
156 ok( !-f "$standby1_data/$segment_path_1_ready",
157 ".ready file for WAL segment $segment_name_1 present in backup got removed with archive_mode=on on standby"
158 );
159
160 # Recovery with archive_mode=on should not create .ready files.
161 # Note that this segment did not exist in the backup.
162 ok( !-f "$standby1_data/$segment_path_2_ready",
163 ".ready file for WAL segment $segment_name_2 not created on standby when archive_mode=on on standby"
164 );
165
166 # Recovery with archive_mode = on creates .done files.
167 ok( -f "$standby1_data/$segment_path_2_done",
168 ".done file for WAL segment $segment_name_2 created when archive_mode=on on standby"
169 );
170
171 # Test recovery with archive_mode = always, which should always keep
172 # .ready files if archiving is enabled, though here we want the archive
173 # command to fail to persist the .ready files. Note that this node
174 # has inherited the archive command of the previous cold backup that
175 # will cause archiving failures.
176 my $standby2 = PostgreSQL::Test::Cluster->new('standby2');
177 $standby2->init_from_backup($primary, 'backup', has_restoring => 1);
178 $standby2->append_conf('postgresql.conf', 'archive_mode = always');
179 my $standby2_data = $standby2->data_dir;
180 $standby2->start;
181
182 # Wait for the replay of the segment switch done previously, ensuring
183 # that all segments needed are restored from the archives.
184 $standby2->poll_query_until('postgres',
185 qq{ SELECT pg_wal_lsn_diff(pg_last_wal_replay_lsn(), '$primary_lsn') >= 0 }
186 ) or die "Timed out while waiting for xlog replay on standby2";
187
188 $standby2->safe_psql('postgres', q{CHECKPOINT});
189
190 ok( -f "$standby2_data/$segment_path_1_ready",
191 ".ready file for WAL segment $segment_name_1 existing in backup is kept with archive_mode=always on standby"
192 );
193
194 ok( -f "$standby2_data/$segment_path_2_ready",
195 ".ready file for WAL segment $segment_name_2 created with archive_mode=always on standby"
196 );
197
198 # Reset statistics of the archiver for the next checks.
199 $standby2->safe_psql('postgres', q{SELECT pg_stat_reset_shared('archiver')});
200
201 # Now crash the cluster to check that recovery step does not
202 # remove non-archived WAL segments on a standby where archiving
203 # is enabled.
204 $standby2->stop('immediate');
205 $standby2->start;
206
207 ok( -f "$standby2_data/$segment_path_1_ready",
208 "WAL segment still ready to archive after crash recovery on standby with archive_mode=always"
209 );
210
211 # Allow WAL archiving again, and wait for the segments to be archived.
212 $standby2->safe_psql(
213 'postgres', q{
214 ALTER SYSTEM RESET archive_command;
215 SELECT pg_reload_conf();
216 });
217 $standby2->poll_query_until('postgres',
218 q{SELECT last_archived_wal FROM pg_stat_archiver},
219 $segment_name_2)
220 or die "Timed out while waiting for archiving to finish";
221
222 is( $standby2->safe_psql(
223 'postgres', q{SELECT archived_count FROM pg_stat_archiver}),
224 '2',
225 "correct number of WAL segments archived from standby");
226
227 ok( !-f "$standby2_data/$segment_path_1_ready"
228 && !-f "$standby2_data/$segment_path_2_ready",
229 ".ready files removed after archive success with archive_mode=always on standby"
230 );
231
232 ok( -f "$standby2_data/$segment_path_1_done"
233 && -f "$standby2_data/$segment_path_2_done",
234 ".done files created after archive success with archive_mode=always on standby"
235 );
236
237 # Check that the archiver process calls the shell archive module's shutdown
238 # callback.
239 $standby2->append_conf('postgresql.conf', "log_min_messages = debug1");
240 $standby2->reload;
241
242 # Run a query to make sure that the reload has taken effect.
243 $standby2->safe_psql('postgres', q{SELECT 1});
244 my $log_location = -s $standby2->logfile;
245
246 $standby2->stop;
247 my $logfile = slurp_file($standby2->logfile, $log_location);
248 ok( $logfile =~ qr/archiver process shutting down/,
249 'check shutdown callback of shell archive module');
250
251 # Test that we can enter and leave backup mode without crashes
252 my ($stderr, $cmdret);
253 $cmdret = $primary->psql(
254 'postgres',
255 "SELECT pg_backup_start('onebackup'); "
256 . "SELECT pg_backup_stop();"
257 . "SELECT pg_backup_start(repeat('x', 1026))",
258 stderr => \$stderr);
259 is($cmdret, 3, "psql fails correctly");
260 like($stderr, qr/backup label too long/, "pg_backup_start fails gracefully");
261 $primary->safe_psql('postgres',
262 "SELECT pg_backup_start('onebackup'); SELECT pg_backup_stop();");
263 $primary->safe_psql('postgres', "SELECT pg_backup_start('twobackup')");
264
265 done_testing();