Skip to content

Instantly share code, notes, and snippets.

@slardiere
Last active December 27, 2015 14:19
Show Gist options
  • Save slardiere/7339785 to your computer and use it in GitHub Desktop.
Save slardiere/7339785 to your computer and use it in GitHub Desktop.
Translate PostgreSQL Lag in bytes
-- Calculate From : http://munin-monitoring.org/browser/munin/plugins/node.d/postgres_streaming_.in
-- with help : http://www.postgresql.org/docs/9.3/static/monitoring-stats.html#PG-STAT-REPLICATION-VIEW
-- http://eulerto.blogspot.fr/2011/11/understanding-wal-nomenclature.html
create or replace function CalculateNumericalOffset(text)
returns bigint
language sql
as $$
select ('x'||lpad( 'ff000000', 16, '0'))::bit(64)::bigint
* ('x'||lpad( split_part( $1 ,'/',1), 16, '0'))::bit(64)::bigint
+ ('x'||lpad( split_part( $1 ,'/',2), 16, '0'))::bit(64)::bigint ;
$$
;
create or replace function pg_streaming_lag( text, text )
returns bigint
language sql
as $$
select CalculateNumericalOffset( $1 ) - CalculateNumericalOffset( $2 ) ;
$$
;
select pid, backend_start, sync_state,
pg_size_pretty(pg_streaming_lag(sent_location,write_location)) as write_lag,
pg_size_pretty(pg_streaming_lag(sent_location,flush_location)) as flush_lag,
pg_size_pretty(pg_streaming_lag(sent_location,replay_location) ) as replay_lag
from pg_stat_replication ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment