-
Notifications
You must be signed in to change notification settings - Fork 2
/
replay_lay_mb.function
52 lines (42 loc) · 1.51 KB
/
replay_lay_mb.function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
CREATE OR REPLACE FUNCTION replay_lag_mb()
RETURNS numeric
LANGUAGE plpgsql
as $f$
DECLARE cloc text;
floc text[];
recv_numloc numeric;
rep_numloc numeric;
mb_lag numeric;
servver numeric;
BEGIN
-- get version number
SELECT setting
INTO servver
FROM pg_settings WHERE name = 'server_version_num';
-- if this is 9.2 or later, we can shortcut the calculations
-- and use location_diff
IF servver >= 90200 THEN
mb_lag = round( pg_xlog_location_diff(pg_last_xlog_receive_location(), pg_last_xlog_replay_location()) /
(1024^2)::numeric, 1 );
RETURN mb_lag;
END IF;
-- extract the two portions of the received log location
floc := regexp_matches(pg_last_xlog_receive_location(), $x$^([\w\d]+)/([\w\d]+)$$x$);
-- convert these to numerics and multiply the file position
-- by ff000000, then add.
EXECUTE $q$SELECT ( x'$q$ || floc[1] || $q$'::int8::numeric )
* ( x'ff000000'::int8::numeric )
+ x'$q$ || floc[2] || $q$'::int8::numeric $q$
INTO recv_numloc;
-- extract data from replay location
floc := regexp_matches(pg_last_xlog_replay_location(), $x$^([\w\d]+)/([\w\d]+)$$x$);
-- convert these to numerics and multiply the file position
-- by ff000000, then add.
EXECUTE $q$SELECT ( x'$q$ || floc[1] || $q$'::int8::numeric )
* ( x'ff000000'::int8::numeric )
+ x'$q$ || floc[2] || $q$'::int8::numeric $q$
INTO rep_numloc;
-- compute difference
mb_lag = round ( ( recv_numloc - rep_numloc ) / ( 1024^2 )::numeric, 1 );
RETURN mb_lag;
END;$f$;