lib/distribute_reads.rb in distribute_reads-0.1.1 vs lib/distribute_reads.rb in distribute_reads-0.1.2
- old
+ new
@@ -12,10 +12,15 @@
self.default_to_primary = true
def self.lag
conn = ActiveRecord::Base.connection
if %w(PostgreSQL PostGIS).include?(conn.adapter_name)
- conn.execute("SELECT EXTRACT(EPOCH FROM NOW() - pg_last_xact_replay_timestamp()) AS lag").first["lag"].to_f
+ conn.execute(
+ "SELECT CASE
+ WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0
+ ELSE EXTRACT (EPOCH FROM NOW() - pg_last_xact_replay_timestamp())
+ END AS lag"
+ ).first["lag"].to_f
else
raise "Option not supported with this adapter"
end
end
end