diff --git a/check_pgactivity b/check_pgactivity index b3489e6..c87badc 100755 --- a/check_pgactivity +++ b/check_pgactivity @@ -2034,13 +2034,13 @@ sub check_archiver { ELSE 0 END, last_archived_wal, last_failed_wal, /* mod time of the next wal to archive - * executed only if superuser */ + * executed only if superuser and archiving is on */ (SELECT extract('epoch' from (current_timestamp - (pg_stat_file('pg_wal/'||pg_walfile_name( (to_hex((last_archived_off+1)/4294967296) ||'/'||to_hex((last_archived_off+1)%4294967296))::pg_lsn ), true)).modification )) - WHERE current_setting('is_superuser')::bool + WHERE (current_setting('is_superuser')::bool AND current_setting('archive_mode') <> 'off') ) AS oldest FROM ( SELECT last_archived_wal, last_archived_time, last_failed_wal, @@ -2102,12 +2102,15 @@ sub check_archiver { ELSE 0 END, last_archived_wal, last_failed_wal, /* mod time of the next wal to archive */ - extract('epoch' from (current_timestamp - + CASE WHEN current_setting('archive_mode')::bool IS TRUE + THEN extract('epoch' from (current_timestamp - (pg_stat_file('pg_wal/'||pg_walfile_name( (to_hex((last_archived_off+1)/4294967296) ||'/'||to_hex((last_archived_off+1)%4294967296))::pg_lsn ), true)).modification ) - ) AS oldest + ) + ELSE NULL + END AS oldest FROM ( SELECT last_archived_wal, last_archived_time, last_failed_wal, walsegsize, diff --git a/t/01-archiver.t b/t/01-archiver.t index 31b4a88..2f3ecf6 100644 --- a/t/01-archiver.t +++ b/t/01-archiver.t @@ -16,7 +16,7 @@ my $node = pgNode->get_new_node('prod'); my $pga_data = "$TestLib::tmp_check/pga.data"; my $wal; my @stdout; -my $num_tests = 25; +my $num_tests = 29; $node->init(has_archiving => 1); @@ -157,6 +157,44 @@ SKIP: { ); } +# Specific tests for PG9.4+, edge cases with pg_stat_archiver +# Covers issue #358 and #384 +SKIP: { + skip "checking with non superuser role is not supported before v10", 4 + if $node->version < '10'; + + # repair archiving + $node->append_conf('postgresql.conf', "archive_command = 'true'"); + $node->reload; + sleep(1); + # Turn archiving off, pg_stat_archiver remains + # see issue #358 and #384 + $node->stop; + $node->append_conf('postgresql.conf', "archive_mode = 'off'"); + $node->start; + $node->psql('template1', 'insert into t select generate_series(20001,30000) as i'); + $wal = $node->switch_wal; + + # Current behavior is to bail out an error "could not stat file" + + $node->command_checks_all( [ + './check_pgactivity', '--service' => 'archiver', + '--username' => $ENV{'USER'} || 'postgres', + '--status-file' => $pga_data, + '--format' => 'human' + ], + 0, + [ + # Should not return an error in this case + qr/^Service *: POSTGRES_ARCHIVER$/m, + qr/^Returns *: 0 \(OK\)$/m, + ], + [ qr/^$/ ], + 'do not bail out when archiver is off' + ); + +} + ### End of tests ### $node->stop( 'immediate' );