|
6 | 6 | use warnings;
|
7 | 7 | use PostgreSQL::Test::Cluster;
|
8 | 8 | use PostgreSQL::Test::Utils;
|
9 |
| -use Test::More tests => 3; |
| 9 | +use Test::More tests => 7; |
10 | 10 | use File::Copy;
|
11 | 11 |
|
12 | 12 | # Initialize primary node, doing archives
|
|
28 | 28 | has_restoring => 1);
|
29 | 29 | $node_standby->append_conf('postgresql.conf',
|
30 | 30 | "wal_retrieve_retry_interval = '100ms'");
|
| 31 | + |
| 32 | +# Set archive_cleanup_command and recovery_end_command, checking their |
| 33 | +# execution by the backend with dummy commands. |
| 34 | +my $data_dir = $node_standby->data_dir; |
| 35 | +my $archive_cleanup_command_file = "archive_cleanup_command.done"; |
| 36 | +my $recovery_end_command_file = "recovery_end_command.done"; |
| 37 | +$node_standby->append_conf( |
| 38 | + 'postgresql.conf', qq( |
| 39 | +archive_cleanup_command = 'echo archive_cleanup_done > $archive_cleanup_command_file' |
| 40 | +recovery_end_command = 'echo recovery_ended_done > $recovery_end_command_file' |
| 41 | +)); |
31 | 42 | $node_standby->start;
|
32 | 43 |
|
33 | 44 | # Create some content on primary
|
|
36 | 47 | my $current_lsn =
|
37 | 48 | $node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();");
|
38 | 49 |
|
| 50 | +# Note the presence of this checkpoint for the archive_cleanup_command |
| 51 | +# check done below, before switching to a new segment. |
| 52 | +$node_primary->safe_psql('postgres', "CHECKPOINT"); |
| 53 | + |
39 | 54 | # Force archiving of WAL file to make it present on primary
|
40 | 55 | $node_primary->safe_psql('postgres', "SELECT pg_switch_wal()");
|
41 | 56 |
|
|
53 | 68 | $node_standby->safe_psql('postgres', "SELECT count(*) FROM tab_int");
|
54 | 69 | is($result, qq(1000), 'check content from archives');
|
55 | 70 |
|
| 71 | +# archive_cleanup_command is executed after generating a restart point, |
| 72 | +# with a checkpoint. |
| 73 | +$node_standby->safe_psql('postgres', q{CHECKPOINT}); |
| 74 | +ok( -f "$data_dir/$archive_cleanup_command_file", |
| 75 | + 'archive_cleanup_command executed on checkpoint'); |
| 76 | +ok( !-f "$data_dir/$recovery_end_command_file", |
| 77 | + 'recovery_end_command not executed yet'); |
| 78 | + |
56 | 79 | # Check the presence of temporary files specifically generated during
|
57 | 80 | # archive recovery. To ensure the presence of the temporary history
|
58 | 81 | # file, switch to a timeline large enough to allow a standby to recover
|
|
62 | 85 | # promoted.
|
63 | 86 | $node_standby->promote;
|
64 | 87 |
|
| 88 | +# recovery_end_command should have been triggered on promotion. |
| 89 | +ok( -f "$data_dir/$recovery_end_command_file", |
| 90 | + 'recovery_end_command executed after promotion'); |
| 91 | + |
65 | 92 | my $node_standby2 = PostgreSQL::Test::Cluster->new('standby2');
|
66 | 93 | $node_standby2->init_from_backup($node_primary, $backup_name,
|
67 | 94 | has_restoring => 1);
|
| 95 | + |
| 96 | +# Make execution of recovery_end_command fail. This should not affect |
| 97 | +# promotion, and its failure should be logged. |
| 98 | +$node_standby2->append_conf( |
| 99 | + 'postgresql.conf', qq( |
| 100 | +recovery_end_command = 'echo recovery_end_failed > missing_dir/xyz.file' |
| 101 | +)); |
| 102 | + |
68 | 103 | $node_standby2->start;
|
69 | 104 |
|
| 105 | +# Save the log location, to see the failure of recovery_end_command. |
| 106 | +my $log_location = -s $node_standby2->logfile; |
| 107 | + |
70 | 108 | # Now promote standby2, and check that temporary files specifically
|
71 | 109 | # generated during archive recovery are removed by the end of recovery.
|
72 | 110 | $node_standby2->promote;
|
|
75 | 113 | "RECOVERYHISTORY removed after promotion");
|
76 | 114 | ok( !-f "$node_standby2_data/pg_wal/RECOVERYXLOG",
|
77 | 115 | "RECOVERYXLOG removed after promotion");
|
| 116 | + |
| 117 | +# Check the logs of the standby to see that the commands have failed. |
| 118 | +my $log_contents = slurp_file($node_standby2->logfile, $log_location); |
| 119 | +like( |
| 120 | + $log_contents, |
| 121 | + qr/WARNING:.*recovery_end_command/s, |
| 122 | + "recovery_end_command failure detected in logs after promotion"); |
0 commit comments