Created
November 3, 2013 22:15
-
-
Save ruckus/7295501 to your computer and use it in GitHub Desktop.
Monitor PG slave lag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- SELECT * FROM streaming_slave_check() | |
CREATE OR REPLACE FUNCTION streaming_slave_check() RETURNS TABLE (client_hostname text, client_addr inet, byte_lag float) | |
LANGUAGE SQL SECURITY DEFINER | |
AS $$ | |
SELECT | |
client_hostname, | |
client_addr, | |
sent_offset - (replay_offset - (sent_xlog - replay_xlog) * 255 * 16 ^ 6 ) AS byte_lag | |
FROM ( | |
SELECT | |
client_hostname, | |
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; | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment