Mind you, here is a simple suggestion: SELECT application_name, pg_xlog_location_diff(sent_location, flush_location) AS replay_delta, sync_priority, sync_state FROM pg_stat_replication ORDER BY replay_delta ASC, application_name;
This is much more simple, but I guess would work only with v9.2 and higher (?).