Skip to content

Instantly share code, notes, and snippets.

@rpfilomeno
Created November 10, 2014 23:34
Show Gist options
  • Save rpfilomeno/848f252d4cc04ee9c2ce to your computer and use it in GitHub Desktop.
Save rpfilomeno/848f252d4cc04ee9c2ce to your computer and use it in GitHub Desktop.
Postgres Tricks
On Master:
SELECT
client_addr,
sent_offset - (
replay_offset - (sent_xlog - replay_xlog) * 255 * 16 ^ 6 ) AS byte_lag
FROM (
SELECT
client_addr,
('x' || lpad(split_part(sent_location, '/', 1), 8, '0'))::bit(32)::bigint AS sent_xlog,
('x' || lpad(split_part(replay_location, '/', 1), 8, '0'))::bit(32)::bigint AS replay_xlog,
('x' || lpad(split_part(sent_location, '/', 2), 8, '0'))::bit(32)::bigint AS sent_offset,
('x' || lpad(split_part(replay_location, '/', 2), 8, '0'))::bit(32)::bigint AS replay_offset
FROM pg_stat_replication
) AS s;
On Slave:
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 log_delay;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment