Skip to content

Instantly share code, notes, and snippets.

@dpirotte
Last active November 10, 2015 05:16
Show Gist options
  • Save dpirotte/3ad9986a641fc19da9e7 to your computer and use it in GitHub Desktop.
Save dpirotte/3ad9986a641fc19da9e7 to your computer and use it in GitHub Desktop.
Find replication lag in SQL without user defined functions
with primary_server as (
select split_part(x.full,'/',1) as xlog,
split_part(x.full,'/',2) as offset
from (select '88DC/113BBDA0'::text as full) x
),
standby_server as (
select split_part(x.full,'/',1) as xlog,
split_part(x.full,'/',2) as offset
from (select '88DB/113BBDA0'::text as full) x
)
select
(
x'FF000000'::bigint
* (format('x%s',lpad(primary_server.xlog,8,'0')))::bit(32)::bigint
+ (format('x%s',lpad(primary_server.offset,8,'0')))::bit(32)::bigint
) - (
x'FF000000'::bigint
* (format('x%s',lpad(standby_server.xlog,8,'0')))::bit(32)::bigint
+ (format('x%s',lpad(standby_server.offset,8,'0')))::bit(32)::bigint
)
FROM primary_server, standby_server;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment