diff --git a/app/models/pglogical_subscription.rb b/app/models/pglogical_subscription.rb index eae8dd90007..9fdd74234eb 100644 --- a/app/models/pglogical_subscription.rb +++ b/app/models/pglogical_subscription.rb @@ -108,10 +108,16 @@ def validate(new_connection_params = {}) end def backlog - connection.xlog_location_diff(remote_node_lsn, 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_node_lsn, remote_replication_lsn) + rescue PG::Error => e + _log.error(e.message) + nil + end end # translate the output from the pglogical stored proc to our object columns diff --git a/spec/models/pglogical_subscription_spec.rb b/spec/models/pglogical_subscription_spec.rb index d8a47b3da6b..b9e5ba59467 100644 --- a/spec/models/pglogical_subscription_spec.rb +++ b/spec/models/pglogical_subscription_spec.rb @@ -496,5 +496,12 @@ 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(:status).and_return("down") + + expect(remote_connection).not_to receive(:xlog_location) + expect(described_class.first.backlog).to be nil + end end end