PostgreSQL 10 Logical Replication



發佈端 Publisher
login as: root
root@192.168.235.10's password:
Last login: Mon Jun 11 10:14:09 2018 from 192.168.235.1
[root@vsource ~]# su - postgres
Last login: Mon Jun 11 11:14:06 CST 2018 on pts/1
-bash-4.2$ psql
psql (10.4)
Type "help" for help.

postgres=# ALTER SYSTEM SET listen_addresses to '*';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET wal_level = 'logical';
ALTER SYSTEM
postgres=# SHOW max_wal_senders;
 max_wal_senders
-----------------
 10
(1 row)

postgres=# SHOW max_replication_slots;
 max_replication_slots
-----------------------
 10
(1 row)
postgres-# \q
-bash-4.2$ /usr/pgsql-10/bin/pg_ctl -D $PGDATA restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2018-06-11 15:04:57.851 CST [3618] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2018-06-11 15:04:57.851 CST [3618] LOG:  listening on IPv6 address "::", port 5432
2018-06-11 15:04:57.853 CST [3618] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-11 15:04:57.856 CST [3618] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-06-11 15:04:57.869 CST [3618] LOG:  redirecting log output to logging collector process
2018-06-11 15:04:57.869 CST [3618] HINT:  Future log output will appear in directory "log".
 done
server started
-bash-4.2$ psql
psql (10.4)
Type "help" for help.

postgres=# CREATE TABLE test(
postgres(# id int primary key,
postgres(# txt text
postgres(# );
CREATE TABLE

Insert 10筆資料
postgres=# INSERT INTO test SELECT generate_series(1,10), random()::text;
INSERT 0 10
postgres=# CREATE PUBLICATION testpub FOR TABLE test
postgres-# WITH (publish = 'insert, update, delete');
CREATE PUBLICATION
postgres=# select * from pg_publication_tables ;
 pubname | schemaname | tablename
---------+------------+-----------
 testpub | public     | test
(1 row)

postgres=# select * from pg_publication;
 pubname | pubowner | puballtables | pubinsert | pubupdate | pubdelete
---------+----------+--------------+-----------+-----------+-----------
 testpub |       10 | f            | t         | t         | t
(1 row)

postgres=# select * from pg_stat_replication;
 pid | usesysid | usename | application_name | client_addr | client_hostname | c
ent_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_l
 | replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state
-----+----------+---------+------------------+-------------+-----------------+--
---------+---------------+--------------+-------+----------+-----------+--------
-+------------+-----------+-----------+------------+---------------+------------
(0 rows)

查看目前table資料
postgres=# select * from test;
 id |        txt
----+-------------------
  1 | 0.521709179040045
  2 | 0.865239345468581
  3 | 0.683285959996283
  4 | 0.528353065252304
  5 | 0.934886322356761
  6 | 0.918178070802242
  7 | 0.357960605528206
  8 | 0.336081576067954
  9 | 0.348159779794514
 10 | 0.722193683031946
(10 rows)

再insert 10筆資料
postgres=# INSERT INTO test SELECT generate_series(11,20), random()::text;
INSERT 0 10
再查看table資料及到Subsciber查看table資料是否抄寫正常
postgres=# select * from test;
 id |        txt
----+-------------------
  1 | 0.521709179040045
  2 | 0.865239345468581
  3 | 0.683285959996283
  4 | 0.528353065252304
  5 | 0.934886322356761
  6 | 0.918178070802242
  7 | 0.357960605528206
  8 | 0.336081576067954
  9 | 0.348159779794514
 10 | 0.722193683031946
 11 | 0.233744856435806
 12 | 0.310788231901824
 13 | 0.854852778837085
 14 | 0.352138324640691
 15 | 0.329302508849651
 16 | 0.192081692628562
 17 | 0.719286013394594
 18 | 0.566931300330907
 19 | 0.843170332722366
 20 | 0.263836818281561
(20 rows)

再insert 1筆資料
postgres=# INSERT INTO test VALUES (21,'hello');
INSERT 0 1
再查看table資料及到Subsciber查看table資料是否抄寫正常
postgres=# select * from test;
 id |        txt
----+-------------------
  1 | 0.521709179040045
  2 | 0.865239345468581
  3 | 0.683285959996283
  4 | 0.528353065252304
  5 | 0.934886322356761
  6 | 0.918178070802242
  7 | 0.357960605528206
  8 | 0.336081576067954
  9 | 0.348159779794514
 10 | 0.722193683031946
 11 | 0.233744856435806
 12 | 0.310788231901824
 13 | 0.854852778837085
 14 | 0.352138324640691
 15 | 0.329302508849651
 16 | 0.192081692628562
 17 | 0.719286013394594
 18 | 0.566931300330907
 19 | 0.843170332722366
 20 | 0.263836818281561
 21 | hello
(21 rows)

postgres=#


訂閱端 Subscriber
login as: root
root@192.168.235.11's password:
Last login: Mon Jun 11 13:44:42 2018 from 192.168.235.1
[root@vsource ~]# su - postgres
Last login: Mon Jun 11 11:14:06 CST 2018 on pts/1
-bash-4.2$ psql
psql (10.4)
Type "help" for help.

postgres=# ALTER SYSTEM SET listen_addresses to '*';
ALTER SYSTEM
postgres=# ALTER SYSTEM SET wal_level = 'logical';
ALTER SYSTEM
postgres=# SHOW max_wal_senders;
 max_wal_senders
-----------------
 10
(1 row)

postgres=# SHOW max_replication_slots;
 max_replication_slots
-----------------------
 10
(1 row)

postgres=#\q
-bash-4.2$ /usr/pgsql-10/bin/pg_ctl -D $PGDATA restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2018-06-11 15:37:53.710 CST [6101] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2018-06-11 15:37:53.710 CST [6101] LOG:  listening on IPv6 address "::", port 5432
2018-06-11 15:37:53.712 CST [6101] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-11 15:37:53.714 CST [6101] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-06-11 15:37:53.726 CST [6101] LOG:  redirecting log output to logging collector process
2018-06-11 15:37:53.726 CST [6101] HINT:  Future log output will appear in directory "log".
 done
server started
-bash-4.2$ psql
psql (10.4)
Type "help" for help.

postgres=# CREATE TABLE test(
postgres(# id int primary key,
postgres(# txt text
postgres(# );
CREATE TABLE

postgres=# CREATE SUBSCRIPTION testsub
CONNECTION 'host=192.168.235.10 dbname=postgres'
PUBLICATION testpub
WITH (copy_data='true',create_slot='true',
 enabled='true',synchronous_commit='off');
NOTICE:  created replication slot "testsub" on publisher
CREATE SUBSCRIPTION

確認資料抄寫正常
postgres=# select * from test;
 id |        txt
----+-------------------
  1 | 0.521709179040045
  2 | 0.865239345468581
  3 | 0.683285959996283
  4 | 0.528353065252304
  5 | 0.934886322356761
  6 | 0.918178070802242
  7 | 0.357960605528206
  8 | 0.336081576067954
  9 | 0.348159779794514
 10 | 0.722193683031946
(10 rows)

postgres=# select * from test;
 id |        txt
----+-------------------
  1 | 0.521709179040045
  2 | 0.865239345468581
  3 | 0.683285959996283
  4 | 0.528353065252304
  5 | 0.934886322356761
  6 | 0.918178070802242
  7 | 0.357960605528206
  8 | 0.336081576067954
  9 | 0.348159779794514
 10 | 0.722193683031946
 11 | 0.233744856435806
 12 | 0.310788231901824
 13 | 0.854852778837085
 14 | 0.352138324640691
 15 | 0.329302508849651
 16 | 0.192081692628562
 17 | 0.719286013394594
 18 | 0.566931300330907
 19 | 0.843170332722366
 20 | 0.263836818281561
(20 rows)

postgres=# select * from test;
 id |        txt
----+-------------------
  1 | 0.521709179040045
  2 | 0.865239345468581
  3 | 0.683285959996283
  4 | 0.528353065252304
  5 | 0.934886322356761
  6 | 0.918178070802242
  7 | 0.357960605528206
  8 | 0.336081576067954
  9 | 0.348159779794514
 10 | 0.722193683031946
 11 | 0.233744856435806
 12 | 0.310788231901824
 13 | 0.854852778837085
 14 | 0.352138324640691
 15 | 0.329302508849651
 16 | 0.192081692628562
 17 | 0.719286013394594
 18 | 0.566931300330907
 19 | 0.843170332722366
 20 | 0.263836818281561
 21 | hello
(21 rows)

postgres=#

留言

這個網誌中的熱門文章

MSSQL 瘦身 : 壓縮資料庫

[SAP] 什麼是SAP? R/3 and S/4 是什麼意思? 差別在哪? (勿轉臉書)

InTrust 自動幫您蒐集 AD 帳號的登入/登出紀錄,長時間保存並保護