diff --git a/app/models/pglogical_subscription.rb b/app/models/pglogical_subscription.rb index 525285d6b6b..1cb6f5e4f4a 100644 --- a/app/models/pglogical_subscription.rb +++ b/app/models/pglogical_subscription.rb @@ -100,10 +100,16 @@ def validate(new_connection_params = {}) end def backlog - connection.xlog_location_diff(remote_region_lsn, subscription_attributes["remote_replication_lsn"]) - rescue PG::Error => e - _log.error(e.message) - nil + if status != "replicating" + _log.error("Is `#{dbname}` running on host `#{host}` and accepting TCP/IP connections on port #{port} ?") + return nil + end + begin + connection.xlog_location_diff(remote_region_lsn, subscription_attributes["remote_replication_lsn"]) + rescue PG::Error => e + _log.error(e.message) + nil + end end def sync_tables diff --git a/spec/models/pglogical_subscription_spec.rb b/spec/models/pglogical_subscription_spec.rb index b7c15c9b417..d8323eaa353 100644 --- a/spec/models/pglogical_subscription_spec.rb +++ b/spec/models/pglogical_subscription_spec.rb @@ -468,6 +468,13 @@ expect(described_class.first.backlog).to be nil end + + it 'does not attempt to calculate backlog and returns nil unless subscription status is "replicating"' do + allow(described_class).to receive(:subscription_status).and_return("down") + + expect(remote_connection).not_to receive(:xlog_location) + expect(described_class.first.backlog).to be nil + end end private