Postgresql中clog与xid对应关系计算方法(速查表)
知道xid计算clog文件名
CREATE or REPLACE PROCEDURE get_clog_name(xid bigint) as
$$
DECLARE
pageno bigint;
segno bigint;
BEGIN
-- 页面号:一个页面8K,一个字节8位能存4个事务的状态。
pageno := xid / (8192 * 4);
-- 段号:一个段(CLOG文件)256KB,能存32个8K页面。
segno := pageno / 32;
-- 段号的十六进制就是文件号。
RAISE NOTICE 'xid:[%], pageno:[%], segno:[%], filename:[%]', xid, pageno, segno, to_hex(segno);
END $$ language plpgsql;
执行结果
call postgres=# call get_clog_name(4210000000);
NOTICE: xid:[4210000000], pageno:[128479], segno:[4014], filename:[fae]
文件名为0FAE
知道clog文件名想计算xid范围
CREATE OR REPLACE PROCEDURE get_xid_range(clog_name TEXT) AS
$proc$
DECLARE
file_number BIGINT;
start_xid BIGINT;
end_xid BIGINT;
BEGIN
execute 'select x''' || clog_name || '''::int' into file_number;
raise notice '%',file_number;
start_xid := file_number * 1048576;
end_xid := (file_number + 1) * 1048576 - 1;
raise notice 'XID Range: % to %' , start_xid, end_xid;
END;
$proc$ LANGUAGE plpgsql;
使用
postgres=# call get_xid_range('0003');
NOTICE: 3
NOTICE: XID Range: 3145728 to 4194303
CALL
-- 验证范围对不对?
postgres=# call get_clog_name(3145727);
NOTICE: xid:[3145727], pageno:[95], segno:[2], filename:[2]
CALL
postgres=# call get_clog_name(3145728);
NOTICE: xid:[3145728], pageno:[96], segno:[3], filename:[3]
CALL
postgres=# call get_clog_name(4194303);
NOTICE: xid:[4194303], pageno:[127], segno:[3], filename:[3]
CALL
postgres=# call get_clog_name(4194304);
NOTICE: xid:[4194304], pageno:[128], segno:[4], filename:[4]
CALL
clog与xid对应关系速查表
uint32 xid的单位是0 - 4294967295,42亿左右。
filename | start | end |
---|---|---|
0 | 0 | 1048575 |
1 | 1048576 | 2097151 |
2 | 2097152 | 3145727 |
3 | 3145728 | 4194303 |
4 | 4194304 | 5242879 |
5 | 5242880 | 6291455 |
6 | 6291456 | 7340031 |
7 | 7340032 | 8388607 |
8 | 8388608 | 9437183 |
9 | 9437184 | 10485759 |
A | 10485760 | 11534335 |
B | 11534336 | 12582911 |
C | 12582912 | 13631487 |
D | 13631488 | 14680063 |
E | 14680064 | 15728639 |
F | 15728640 | 16777215 |
10 | 16777216 | 17825791 |
…
…
filename | start | end |
---|---|---|
F0 | 251658240 | 252706815 |
F1 | 252706816 | 253755391 |
F2 | 253755392 | 254803967 |
F3 | 254803968 | 255852543 |
F4 | 255852544 | 256901119 |
F5 | 256901120 | 257949695 |
F6 | 257949696 | 258998271 |
F7 | 258998272 | 260046847 |
F8 | 260046848 | 261095423 |
F9 | 261095424 | 262143999 |
FA | 262144000 | 263192575 |
FB | 263192576 | 264241151 |
FC | 264241152 | 265289727 |
FD | 265289728 | 266338303 |
FE | 266338304 | 267386879 |
FF | 267386880 | 268435455 |
100 | 268435456 | 269484031 |
101 | 269484032 | 270532607 |
102 | 270532608 | 271581183 |
103 | 271581184 | 272629759 |
104 | 272629760 | 273678335 |
105 | 273678336 | 274726911 |
106 | 274726912 | 275775487 |
107 | 275775488 | 276824063 |
108 | 276824064 | 277872639 |
109 | 277872640 | 278921215 |
10A | 278921216 | 279969791 |
…
…
filename | start | end |
---|---|---|
FF0 | 4278190080 | 4279238655 |
FF1 | 4279238656 | 4280287231 |
FF2 | 4280287232 | 4281335807 |
FF3 | 4281335808 | 4282384383 |
FF4 | 4282384384 | 4283432959 |
FF5 | 4283432960 | 4284481535 |
FF6 | 4284481536 | 4285530111 |
FF7 | 4285530112 | 4286578687 |
FF8 | 4286578688 | 4287627263 |
FF9 | 4287627264 | 4288675839 |
FFA | 4288675840 | 4289724415 |
FFB | 4289724416 | 4290772991 |
FFC | 4290772992 | 4291821567 |
FFD | 4291821568 | 4292870143 |
FFE | 4292870144 | 4293918719 |
FFF | 4293918720 | 4294967295 |
1000 | 4294967296 | 4296015871 |
1001 | 4296015872 | 4297064447 |
1002 | 4297064448 | 4298113023 |
1003 | 4298113024 | 4299161599 |
1004 | 4299161600 | 4300210175 |
1005 | 4300210176 | 4301258751 |
1006 | 4301258752 | 4302307327 |
1007 | 4302307328 | 4303355903 |
1008 | 4303355904 | 4304404479 |
1009 | 4304404480 | 4305453055 |
100A | 4305453056 | 4306501631 |
100B | 4306501632 | 4307550207 |
100C | 4307550208 | 4308598783 |
100D | 4308598784 | 4309647359 |
100E | 4309647360 | 4310695935 |
100F | 4310695936 | 4311744511 |
1010 | 4311744512 | 4312793087 |