postgresql 10 pub/sub使用簡明教程

來源:互聯網
上載者:User

標籤:pub/sub   postgresql10   

1.初始化配置


1).pubdb

postgres=# create database pubdb;

postgres=# \c pubdb 

You are now connected to database "pubdb" as user "postgres".

pubdb=# create table t1(id bigserial primary key, name varchar(20));

CREATE TABLE

pubdb=# create publication pub_mdb1_t1 for table t1;

CREATE PUBLICATION

pubdb=# 


pubdb=# select * from pg_publication;

   pubname   | pubowner | puballtables | pubinsert | pubupdate | pubdelete 

-------------+----------+--------------+-----------+-----------+-----------

 pub_mdb1_t1 |       10 | f            | t         | t         | t

(1 row)


pubdb=# 

pubdb=# insert into t1(name) values (‘Peter‘),(‘Chris‘),(‘Jasmine‘),(‘Jeans‘),(‘Willam‘);

INSERT 0 5

pubdb=# select * from t1;

 id |  name   

----+---------

  1 | Peter

  2 | Chris

  3 | Jasmine

  4 | Jeans

  5 | Willam

(5 rows)


pubdb=# 

pubdb=# 

pubdb=# \q

[[email protected] ~]$


2).subdb

$ createdb subdb

恢複原發布資料

$ pg_restore -d subdb t1.dump


建立提交,不複製原存在資料,此時sub端啟動一個logical replication worker,

pub端建立一個pg_replication_slot


$ psql subdb

subdb=# CREATE SUBSCRIPTION sub_mdb1_t1

    CONNECTION ‘host=172.16.3.228 port=5432 user=postgres password=123456 dbname=pubdb‘

    PUBLICATION pub_mdb1_t1

    WITH (copy_data = false);

NOTICE:  created replication slot "sub_mdb1_t1" on publisher

CREATE SUBSCRIPTION

subdb=# 

subdb=# 

subdb=# 

subdb=# select * from t1;

 id |  name   

----+---------

  1 | Peter

  2 | Chris

  3 | Jasmine

  4 | Jeans

  5 | Willam

(5 rows)


查看提交資訊


subdb=# \dRs+

List of subscriptions

-[ RECORD 1 ]------+-----------------------------------------------------------------------

Name               | sub_mdb1_t1

Owner              | postgres

Enabled            | t

Publication        | {pub_mdb1_t1}

Synchronous commit | off

Conninfo           | host=172.16.3.228 port=5432 user=postgres password=123456 dbname=pubdb


subdb=# \dRs

List of subscriptions

-[ RECORD 1 ]--------------

Name        | sub_mdb1_t1

Owner       | postgres

Enabled     | t

Publication | {pub_mdb1_t1}

subdb=# 


3).pubdb查看發布複製資訊

pubdb=# 

pubdb=# select * from pg_stat_replication;

-[ RECORD 1 ]----+------------------------------

pid              | 16501

usesysid         | 10

usename          | postgres

application_name | sub_mdb1_t1

client_addr      | 172.16.3.230

client_hostname  | 

client_port      | 52682

backend_start    | 2017-10-10 14:59:18.469715+08

backend_xmin     | 

state            | streaming

sent_lsn         | 0/F036E90

write_lsn        | 0/F036E90

flush_lsn        | 0/F036E90

replay_lsn       | 0/F036E90

write_lag        | 

flush_lag        | 

replay_lag       | 

sync_priority    | 0

sync_state       | async


pubdb=# select * from pg_replication_slots ;

-[ RECORD 1 ]-------+------------

slot_name           | sub_mdb1_t1

plugin              | pgoutput

slot_type           | logical

datoid              | 26203

database            | pubdb

temporary           | f

active              | t

active_pid          | 16501

xmin                | 

catalog_xmin        | 604

restart_lsn         | 0/F036E58

confirmed_flush_lsn | 0/F036E90


2.插入增量發布記錄

1).pubdb插入增量

pubdb=# pubdb=# insert into t1(name) values(‘Zeng‘),(‘Feng‘),(‘Mia‘);

INSERT 0 3

pubdb=# \x

Expanded display is off.

pubdb=# select * from t1;

 id |  name   

----+---------

  1 | Peter

  2 | Chris

  3 | Jasmine

  4 | Jeans

  5 | Willam

  6 | Zeng

  7 | Feng

  8 | Mia

(8 rows)


pubdb=# 

pubdb=# select * from pg_stat_replication;

-[ RECORD 1 ]----+------------------------------

pid              | 16501

usesysid         | 10

usename          | postgres

application_name | sub_mdb1_t1

client_addr      | 172.16.3.230

client_hostname  | 

client_port      | 52682

backend_start    | 2017-10-10 14:59:18.469715+08

backend_xmin     | 

state            | streaming

sent_lsn         | 0/F0372B8

write_lsn        | 0/F0372B8

flush_lsn        | 0/F0372B8

replay_lsn       | 0/F0372B8

write_lag        | 

flush_lag        | 

replay_lag       | 

sync_priority    | 0

sync_state       | async


pubdb=# select * from pg_replication_slots ;

-[ RECORD 1 ]-------+------------

slot_name           | sub_mdb1_t1

plugin              | pgoutput

slot_type           | logical

datoid              | 26203

database            | pubdb

temporary           | f

active              | t

active_pid          | 16501

xmin                | 

catalog_xmin        | 605

restart_lsn         | 0/F037280

confirmed_flush_lsn | 0/F0372B8


pubdb=# 

[[email protected] ~]$ ps -fU postgres

UID        PID  PPID  C STIME TTY          TIME CMD

postgres 15874 15873  0 12:44 pts/2    00:00:00 -bash

postgres 15950     1  0 12:50 ?        00:00:00 /opt/pgsql/10.0/bin/postmaster -D /pgdata10

postgres 15951 15950  0 12:50 ?        00:00:00 postgres: logger process   

postgres 15953 15950  0 12:50 ?        00:00:00 postgres: checkpointer process   

postgres 15954 15950  0 12:50 ?        00:00:00 postgres: writer process   

postgres 15955 15950  0 12:50 ?        00:00:00 postgres: wal writer process   

postgres 15956 15950  0 12:50 ?        00:00:00 postgres: autovacuum launcher process   

postgres 15957 15950  0 12:50 ?        00:00:00 postgres: archiver process   

postgres 15958 15950  0 12:50 ?        00:00:00 postgres: stats collector process   

postgres 15959 15950  0 12:50 ?        00:00:00 postgres: bgworker: logical replication launcher  

postgres 15961 15960  0 12:50 pts/1    00:00:00 -bash

postgres 16077 15874  0 13:08 pts/2    00:00:00 tail -f postgresql-Tue.log

postgres 16082 15950  0 13:10 ?        00:00:00 postgres: postgres pubdb 172.16.3.223(56608) idle

postgres 16083 15950  0 13:10 ?        00:00:00 postgres: postgres pubdb 172.16.3.223(56610) idle

postgres 16501 15950  0 14:59 ?        00:00:00 postgres: wal sender process postgres 172.16.3.230(52682) idle

postgres 16543 15961  0 15:09 pts/1    00:00:00 ps -fU postgres

[[email protected] ~]$ 


sequence增長為最新值8


CREATE SEQUENCE public.t1_id_seq

    INCREMENT 1

    START 8

    MINVALUE 1

    MAXVALUE 9223372036854775807

    CACHE 1;


2).查看增量提交記錄


subdb


subdb=# select * from t1;

 id |  name   

----+---------

  1 | Peter

  2 | Chris

  3 | Jasmine

  4 | Jeans

  5 | Willam

  6 | Zeng

  7 | Feng

  8 | Mia

(8 rows)


subdb=# 



[[email protected] log]$ ps -fU postgres

UID        PID  PPID  C STIME TTY          TIME CMD

postgres   935     1  0 10:28 ?        00:00:01 /opt/pgsql/10.0/bin/postmaster -D /pgdata10

postgres  1001   935  0 10:28 ?        00:00:00 postgres: logger process   

postgres  1005   935  0 10:28 ?        00:00:00 postgres: checkpointer process   

postgres  1006   935  0 10:28 ?        00:00:00 postgres: writer process   

postgres  1007   935  0 10:28 ?        00:00:01 postgres: wal writer process   

postgres  1008   935  0 10:28 ?        00:00:00 postgres: autovacuum launcher process   

postgres  1009   935  0 10:28 ?        00:00:00 postgres: archiver process   

postgres  1010   935  0 10:28 ?        00:00:00 postgres: stats collector process   

postgres  1011   935  0 10:28 ?        00:00:00 postgres: bgworker: logical replication launcher  

postgres  1084  1083  0 10:28 pts/0    00:00:00 -bash

postgres 15551 15550  0 11:48 pts/1    00:00:00 -bash

postgres 16206 16205  0 14:40 pts/2    00:00:00 -bash

postgres 16276  1084  0 14:46 pts/0    00:00:00 psql subdb

postgres 16277   935  0 14:46 ?        00:00:00 postgres: postgres subdb [local] idle

postgres 16324 16206  0 14:56 pts/2    00:00:00 psql subdb

postgres 16325   935  0 14:56 ?        00:00:00 postgres: postgres subdb [local] idle

postgres 16332   935  0 14:59 ?        00:00:00 postgres: bgworker: logical replication worker for subscription 24626  

postgres 16374 15551  0 15:11 pts/1    00:00:00 ps -fU postgres

[[email protected] log]$


sequence為原始值5,不增長


CREATE SEQUENCE public.t1_id_seq

    INCREMENT 1

    START 5

    MINVALUE 1

    MAXVALUE 9223372036854775807

    CACHE 1;


3.常見問題(特殊情景使用請參見官方文檔)


當遠端資料庫無法串連時,drop SUBSCRIPTION


# ALTER SUBSCRIPTION name DISABLE

# alter subscription sub_measurement set (slot_name=none);

# drop subscription sub_measurement;


本文出自 “yiyi” 部落格,請務必保留此出處http://heyiyi.blog.51cto.com/205455/1971194

postgresql 10 pub/sub使用簡明教程

相關文章

聯繫我們

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