SELECT json_object_agg(application_name, row_to_json(T))
  FROM (
    SELECT
      application_name,
      EXTRACT(epoch FROM COALESCE(flush_lag,'0'::interval)) AS flush_lag,
      EXTRACT(epoch FROM COALESCE(replay_lag,'0'::interval)) AS replay_lag,
      EXTRACT(epoch FROM COALESCE(write_lag, '0'::interval)) AS write_lag
    FROM pg_stat_replication
  ) T