This is a very simple example how to monitor PostgreSQL replication lags on replica with Telegraf

[[inputs.postgresql]]
  address = "host=localhost user=your_user_for_monitoring password=passsssssswww sslmode=disable dbname=postgres"

[[inputs.postgresql_extensible]]
  address = "host=localhost user=your_user_for_monitoring password=passsssssswww sslmode=disable dbname=postgres"
  [[inputs.postgresql_extensible.query]]
    sqlquery="select (extract(epoch from (now() - pg_last_xact_replay_timestamp()))*1000)::bigint  AS replication_delay, CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0 ELSE (EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))*1000)::INT END AS replication_lag_filtered, public.hex_to_int(replace(pg_last_xlog_receive_location()::text,'/','')) as position_received, public.hex_to_int(replace(pg_last_xlog_replay_location()::text,'/','')) as position_applied;"
    withdbname=false
    tagvalue="db"
    measurement="postgresql_replication"

Shows `replication_lag_raw` and `replication_lag_filtered` (when there are no new data for some time) + positions in WAL log – `position_received` and `position_applied`

PostgreSQL function “hex_to_int”:

<pre>create or replace function public.hex_to_int(hexval text) returns bigint
   immutable
   strict
   language plpgsql
as $$
DECLARE
    result  bigint;
BEGIN
    EXECUTE 'SELECT x''' || hexval || '''::bigint' INTO result;
    RETURN result;
END;

$$;