PostgreSQL資料庫Streaming Replication流複製主備延遲測試

來源:互聯網
上載者:User

標籤:

PostgreSQL資料庫流複製主庫和備庫之間的延遲時間是多少,無論對HA還是負載平衡來說都應該做個評估。比如單純的HA架構,當主庫發生故障時,我們允許多少時間內的資料丟失。不廢話,直接進入本次實驗測試。

測試環境:

主庫:記憶體:32G,CPU:8核,IP:192.168.122.101

備庫:記憶體:32G,CPU:8核,IP:192.168.122.102

資料庫配置:預設

測試準備:

在兩台伺服器上安裝好PostgreSQL資料庫,安裝過程不清楚的可以參考文章《PostgreSQL資料庫編譯安裝》,網址:http://www.sijitao.net/1535.html 。

搭建資料庫之間的非同步流複製環境,配置過程參考文章《PostgreSQL Streaming Replication流複製環境搭建》,網址:http://www.sijitao.net/1764.html 。

重要:測試之前一定要同步下主庫和備庫兩台伺服器的時間,不然會出現延遲時間不準備的情況。

測試步驟:

建立測試資料庫和測試表,這裡我用的德哥的測試模型,類比使用者登陸操作。

1、建立測試表
create table user_info(userid int,engname text,cnname text,occupation text,birthday date,signname text,email text,qq numeric,crt_time timestamp without time zone,mod_time timestamp without time zone);create table user_session(userid int,logintime timestamp(0) without time zone,login_count bigint default 0,logouttime timestamp(0) without time zone,online_interval interval default interval ‘0‘);create table user_login_rec(userid int,login_time timestamp without time zone,ip inet);create table user_logout_rec(userid int,logout_time timestamp without time zone,ip inet);
2、初始化測試資料
insert into user_info (userid,engname,cnname,occupation,birthday,signname,email,qq,crt_time,mod_time)select generate_series(1,2000000),‘zhangnq‘,‘章郎蟲‘,‘DBA‘,‘1970-01-01‘,E‘我就是章郎蟲。‘,‘[email protected]‘,248687950,clock_timestamp(),NULL;insert into user_session (userid) select generate_series(1,2000000);alter table user_info add constraint pk_user_info primary key (userid);alter table user_session add constraint pk_user_session primary key (userid);
3、建立業務函數
-- 類比使用者登入的函數create or replace function f_user_login (i_userid int,OUT o_userid int,OUT o_engname text,OUT o_cnname text,OUT o_occupation text,OUT o_birthday date,OUT o_signname text,OUT o_email text,OUT o_qq numeric)as $BODY$declarebeginselect userid,engname,cnname,occupation,birthday,signname,email,qqinto o_userid,o_engname,o_cnname,o_occupation,o_birthday,o_signname,o_email,o_qqfrom user_info where userid=i_userid;insert into user_login_rec (userid,login_time,ip) values (i_userid,now(),inet_client_addr());update user_session set logintime=now(),login_count=login_count+1 where userid=i_userid;return;end;$BODY$language plpgsql;-- 類比使用者退出的函數create or replace function f_user_logout(i_userid int,OUT o_result int)as $BODY$declarebegininsert into user_logout_rec (userid,logout_time,ip) values (i_userid,now(),inet_client_addr());update user_session set logouttime=now(),online_interval=online_interval+(now()-logintime) where userid=i_userid;o_result := 0;return;exception when others theno_result := 1;return;end;$BODY$language plpgsql;
4、建立測試指令碼
\setrandom userid 1 2000000SELECT f_user_login(:userid);
5、建立流複製時間延遲測試指令碼

在備資料庫中建立時間延遲測試的指令碼,這裡一起監測了備庫的負載,網路流量和同步延遲時間,這裡我測試了100次。

#!/bin/bashexport PATH=/opt/PostgreSQL/93/bin:$PATHexport PGDATA=/data/pgsqlexport PGHOME=/opt/PostgreSQL/93export PGPORT=5432i=0sql="SELECT        CASE                WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0                ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())        ENDAS replication_lag;"while [ $i -lt 100 ]do        echo -e "`/usr/bin/top -b -n 1 |sed -n ‘1p‘ |awk ‘{print +$(NF-2)}‘` | \c";echo -e `psql -t -A -c "$sql" -d zhangnq`" | \c";/usr/bin/ifstat -i eth0 -n 1 1 | awk ‘NR>2 {print $1 " KB/s"}‘        let i=$i+1done
6、開始測試

在主庫使用pgbench對資料庫施壓。

pgbench -M prepared -n -r -f ./test.sql -h 127.0.0.1 -p 5432 -U postgres -c 64 -j 32 -T 300 zhangnq

同時在備庫上運行流複寫延遲測試指令碼,記錄測試後的數值。

修改pgbench的串連數和線程數後測試多次,得到類似如下的結果。

[email protected]:~$ ./pglag_time.sh 0.24 | 28.444522 | 3833.48 KB/s0.24 | 28.442567 | 4260.23 KB/s0.24 | 28.442438 | 4676.84 KB/s0.3 | 0 | 5151.29 KB/s0.3 | 28.442349 | 5439.33 KB/s............

 

測試結果

同步延遲最大時間基本都是在8秒左右,串連並發數增大時延遲次數增加。

頻寬使用使用量和串連並發數成正比關係。

系統負載在資料庫連接並發數增加時沒怎麼變化,系統資源使用率不高。

接下來就可以最佳化或者可以把延遲資料添加進nagios監控了。

原文連結:http://www.sijitao.net/1860.html

參考網址:

http://blog.163.com/[email protected]/blog/static/163877040201221382150858/

https://vibhorkumar.wordpress.com/2014/05/21/monitoring-approach-for-streaming-replication-with-hot-standby-in-postgresql-9-3/

PostgreSQL資料庫Streaming Replication流複製主備延遲測試

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.