Postgresql 遠程同步(非即時同步,小資料量)

來源:互聯網
上載者:User

標籤:

源端要開通目標的相關存取權限目標端:1.建立遠端資料表的視圖create view v_bill_tbl_version_update_control_info as SELECT * FROM dblink(‘hostaddr=10.10.10.8 port=4321 dbname=postgres user=postgres password=postgres‘, ‘SELECT id,appid,ratio,status,create_time,char_package_name,version from  tbl_version_update_control_info‘) AS t(id integer,appid  character(20),ratio integer,status character(1),create_time timestamp without time zone,char_package_name character varying(50),version character varying(8)); 

2.建立和遠端資料表一樣的判斷表以及實體表
CREATE TABLE tbl_version_update_control_info (    id integer NOT NULL,    appid character(20) NOT NULL,    ratio integer DEFAULT 0 NOT NULL,    status character(1) DEFAULT 0 NOT NULL,    create_time timestamp without time zone DEFAULT now(),    char_package_name character varying(50),    version character varying(8));

CREATE TABLE work_table_tbl_version_update_control_info (    id integer NOT NULL,    appid character(20) NOT NULL,    ratio integer DEFAULT 0 NOT NULL,    status character(1) DEFAULT 0 NOT NULL,    create_time timestamp without time zone DEFAULT now(),    char_package_name character varying(50),    version character varying(8));
3.建立同步函數CREATE OR REPLACE FUNCTION sync_tbl_version_update_control_info() RETURNS integer LANGUAGE plpgsqlAS $function$declarev_src_count int;   --存放來源資料統計資料v_dst_count int;  --存放目標端資料統計資料v_equal_count int;  --源端和目標端相同的資料v_run int8;      --統計運行改函數的進行數,如果大於1,說明存在,改函數在運行beginv_src_count := 0;v_dst_count := 0;v_equal_count := 0;select count(*) into v_run from pg_stat_activity where query ~ ‘sync_tbl_version_update_control_info‘;if v_run>1 then  raise notice ‘another process is running, this will exit soon.‘;  return 1;end if;if (pg_is_in_recovery()) then  raise notice ‘pg_is_in_recovery is true.‘;  return 1;end if;truncate table ONLY work_table_tbl_version_update_control_info;insert into work_table_tbl_version_update_control_info   (id,appid,ratio,status,create_time,char_package_name,version)   select id,appid,ratio,status,create_time,char_package_name,version from v_bill_tbl_version_update_control_info;select count(*) into v_src_count from work_table_tbl_version_update_control_info;select count(*) into v_dst_count from tbl_version_update_control_info;raise notice ‘v_src_count:%, v_dst_count:%‘,v_src_count,v_dst_count;if ( v_src_count = v_dst_count and v_src_count <> 0 ) then  select count(*) into v_equal_count from work_table_tbl_version_update_control_info t1,tbl_version_update_control_info t2    where t1.id=t2.id     and t1.appid = t2.appid    and t1.ratio = t2.ratio    and t1.status = t2.status    and t1.create_time = t2.create_time    and t1.char_package_name = t2.char_package_name    and t1.version = t2.version;  raise notice ‘v_src_count:%, v_dst_count:%, v_equal_count:%‘,v_src_count,v_dst_count,v_equal_count;  if ( v_equal_count <> v_src_count ) then    truncate table ONLY tbl_version_update_control_info;    insert into tbl_version_update_control_info     (id,appid,ratio,status,create_time,char_package_name,version)    select id,appid,ratio,status,create_time,char_package_name,version from work_table_tbl_version_update_control_info;  end if;elsif ( v_src_count <> v_dst_count and v_src_count <> 0 ) then  truncate table ONLY tbl_version_update_control_info;  insert into tbl_version_update_control_info   (id,appid,ratio,status,create_time,char_package_name,version)  select id,appid,ratio,status,create_time,char_package_name,version from work_table_tbl_version_update_control_info;elsif v_src_count = 0 then  raise notice ‘ERROR: src no data.‘;  return 1;end if;  return 0;end;$function$
4.執行函數進行同步並確認同步
select  sync_tbl_version_update_control_info();select count(*) from tbl_version_update_control_info;

5.系統定時任務添加:
15 2 * * * /home/postgres/sync_data.sh >>/tmp/sync.log 2>&1cat /home/postgres/sync_data.sh
echo -e "start sync tbl_version_update_control_info;"date +%F\ %Tpsql -h 127.0.0.1 hank hank -c "select * from sync_tbl_version_update_control_info()";date +%F\ %Techo -e "end sync tbl_version_update_control_info;"

Postgresql 遠程同步(非即時同步,小資料量)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.