def get_replication_delays()

in postgresql_metrics/postgres_queries.py [0:0]


def get_replication_delays(conn):
    sql = ("SELECT client_addr, "
           "pg_xlog_location_diff(pg_current_xlog_location(), replay_location) AS bytes_diff "
           "FROM public.pg_stat_repl")
    if is_in_recovery(conn):
        # pg_current_xlog_location cannot be called in a replica
        # use pg_last_xlog_receive_location for monitoring cascade replication
        sql = sql.replace("pg_current_xlog_location", "pg_last_xlog_receive_location")
    if conn.server_version >= 100000: # PostgreSQL 10 and higher
        sql = sql.replace('_xlog', '_wal')
        sql = sql.replace('_location', '_lsn')
    all_delays = []
    results = query(conn, sql)
    for result_row in results:
        client_addr = result_row[0]
        bytes_diff = int(result_row[1])
        all_delays.append((client_addr, bytes_diff))
    return all_delays