PostgreSQL 逻辑复制表结构变更测试

逻辑复制表结构变更测试

  • 发布者 INSERT 一条数据
postgres=# insert into lr2(id ,age) values(1,11);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
(1 row)
  • 订阅者查询 数据
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
(1 row)

postgres=#

结果:正常情况复制正常,数据复制正常

发布者一方新增一列字段

  • 新增 字段 name
postgres=# ALTER TABLE lr2 ADD name text;
ALTER TABLE
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
(1 row)

postgres=#
  • 查询订阅者
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
(1 row)

postgres=#

结 果:发布者变更表结构,订阅者表结构不会变更,即不会进行 DDL 语句复制。

  • 变更后 发布者 插入数据
postgres=# insert into lr2 (id,age,name) values (2,22,'隶属') ;
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
(2 rows)

postgres=#
  • 订阅者 查询
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
(1 row)

postgres=#

结 果:发布者变结构变更后,插入一条数据,订阅者并不会复制该数据,

  • 发布者新增字段后,更新数据

    id 为10 更新前:

postgres=# select * from lr2;
 id | age |         inserttime         |  addr
----+-----+----------------------------+--------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   |
  4 |  44 | 2018-05-30 13:27:48.824291 |
  8 |  88 | 2018-05-30 13:42:53.396291 |
  9 |  99 | 2018-05-30 13:46:42.669047 |
  3 |  33 | 2018-05-30 13:12:59.145076 |
 10 |  10 | 2018-05-30 14:13:33.318161 |
 11 |  11 | 2018-05-30 14:17:46.097985 |
 12 |  12 | 2018-05-30 14:20:22.122037 | 背北京
(9 rows)

更新后:

postgres=# update lr2 set age =13 where id =10;
UPDATE 1
postgres=# select * from lr2;
 id | age |         inserttime         |  addr
----+-----+----------------------------+--------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   |
  4 |  44 | 2018-05-30 13:27:48.824291 |
  8 |  88 | 2018-05-30 13:42:53.396291 |
  9 |  99 | 2018-05-30 13:46:42.669047 |
  3 |  33 | 2018-05-30 13:12:59.145076 |
 11 |  11 | 2018-05-30 14:17:46.097985 |
 12 |  12 | 2018-05-30 14:20:22.122037 | 背北京
 10 |  13 | 2018-05-30 14:13:33.318161 |
(9 rows)

postgres=#
  • 订阅者日志报错
2018-05-30 14:25:59.993 CST,,,727,,5b0e43f7.2d7,2,,2018-05-30 14:25:59 CST,4/5901,0,ERROR,55000,"logical replication target relation ""public.lr2"" is missing some replicated columns",,,,,,,,,""
  • 发布者更新后,订阅者查询
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  8 |  88 | 2018-05-30 13:42:53.396291
  9 |  99 | 2018-05-30 13:46:42.669047
  3 |  33 | 2018-05-30 13:12:59.145076
 10 |  10 | 2018-05-30 14:13:33.318161
 11 |  11 | 2018-05-30 14:17:46.097985
(8 rows)

postgres=#

结 果:发布者新增字段后,更新数据,订阅者不会同步复制该数据更新。

  • 发布者新增字段后,删除数据
postgres=# delete from lr2 where id =1;
DELETE 1
postgres=# select * from lr2;
 id | age |         inserttime         |  addr
----+-----+----------------------------+--------
  2 |  22 | 2018-05-30 13:04:03.1012   |
  4 |  44 | 2018-05-30 13:27:48.824291 |
  8 |  88 | 2018-05-30 13:42:53.396291 |
  9 |  99 | 2018-05-30 13:46:42.669047 |
  3 |  33 | 2018-05-30 13:12:59.145076 |
 11 |  11 | 2018-05-30 14:17:46.097985 |
 12 |  12 | 2018-05-30 14:20:22.122037 | 背北京
 10 |  13 | 2018-05-30 14:13:33.318161 |
(8 rows)

postgres=#
  • 订阅者查询
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  8 |  88 | 2018-05-30 13:42:53.396291
  9 |  99 | 2018-05-30 13:46:42.669047
  3 |  33 | 2018-05-30 13:12:59.145076
 10 |  10 | 2018-05-30 14:13:33.318161
 11 |  11 | 2018-05-30 14:17:46.097985
(8 rows)

postgres=#

结 果:发布者新增字段后,删除数据,订阅者不会同步删除该数据

  • 订阅者修改为与发布者 同样的表结构
postgres=#  ALTER TABLE lr2 ADD name text;
ALTER TABLE
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
(2 rows)

postgres=#

结 果:订阅者会把之前发布者插入的数据立刻同步复制过来 。

此时在发布者一方插入数据

postgres=# insert into lr2 (id,age,name) values (3,33,'李四');
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  3 |  33 | 2018-05-30 13:12:59.145076 | 李四
(3 rows)
postgres=#

查询订阅者

postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  3 |  33 | 2018-05-30 13:12:59.145076 | 李四
(3 rows)

postgres=#
  • 发布者删除表中一列字段
postgres=# alter table lr2 drop column name;
ALTER TABLE
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  3 |  33 | 2018-05-30 13:12:59.145076
(3 rows)

postgres=#
  • 订阅者查询

postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  3 |  33 | 2018-05-30 13:12:59.145076 | 李四
(3 rows)

postgres=#

结果:订阅者不会同步复制该删除的DDL语句,订阅者表结构不会变化

  • 此时发布者插入数据
postgres=# insert into lr2 (id,age) values (4,44);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  3 |  33 | 2018-05-30 13:12:59.145076
  4 |  44 | 2018-05-30 13:27:48.824291
(4 rows)

postgres=#
  • 查询订阅者
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  3 |  33 | 2018-05-30 13:12:59.145076 | 李四
  4 |  44 | 2018-05-30 13:27:48.824291 |
(4 rows)

postgres=#

结 果:订阅者会同步复制发布者删除字段后插入的数据。

  • 更新发布者值

此时id为3的值为33

postgres=# select * from lr2;
id | age | inserttime
----+-----+----------------------------
1 | 11 | 2018-05-30 11:30:27.474004
2 | 22 | 2018-05-30 13:04:03.1012
3 | 33 | 2018-05-30 13:12:59.145076
4 | 44 | 2018-05-30 13:27:48.824291
5 | 55 | 2018-05-30 13:29:40.270789
(5 rows)

更新后
“`
postgres=# update lr2 set age = 66 where id = 3;
UPDATE 1
postgres=# select * from lr2;
id | age | inserttime
—-+—–+—————————-
1 | 11 | 2018-05-30 11:30:27.474004
2 | 22 | 2018-05-30 13:04:03.1012
4 | 44 | 2018-05-30 13:27:48.824291
5 | 55 | 2018-05-30 13:29:40.270789
3 | 66 | 2018-05-30 13:12:59.145076
(5 rows)

postgres=#
“`

  • 查询订阅者
    “`
    postgres=# select * from lr2;
    id | age | inserttime | name
    —-+—–+—————————-+——
    1 | 11 | 2018-05-30 11:30:27.474004 |
    2 | 22 | 2018-05-30 13:04:03.1012 | 隶属
    4 | 44 | 2018-05-30 13:27:48.824291 |
    5 | 55 | 2018-05-30 13:29:40.270789 |
    3 | 66 | 2018-05-30 13:12:59.145076 | 李四
    (5 rows)

postgres=#
“`

结果:订阅者数据也会同步该变更。

  • 发布者删除数据
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  3 |  66 | 2018-05-30 13:12:59.145076
  5 |  77 | 2018-05-30 13:29:40.270789
(5 rows)

postgres=# delete from lr2 where id = 5;
DELETE 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  3 |  66 | 2018-05-30 13:12:59.145076
(4 rows)
  • 查询订阅者数据
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
(4 rows)

postgres=#

结 果: 订阅者也没有该条记录,删除数据同步支持

  • 在订阅者插入数据
postgres=# insert into lr2 (id,age) values (7,77);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
  7 |  77 | 2018-05-30 13:37:12.454944 |
(5 rows)
  • 发布者插入与订阅者id相同的数据
postgres=# insert into lr2 (id,age) values (7,77);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
  7 |  77 | 2018-05-30 13:37:12.454944 |
(5 rows)
  • 发布者插入成功
postgres=# insert into lr2 (id ,age) values (7,88);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  3 |  66 | 2018-05-30 13:12:59.145076
  7 |  88 | 2018-05-30 13:37:47.736737
(5 rows)

postgres=#
  • 订阅者日志报错
2018-05-30 13:38:18.039 CST,,,27593,,5b0e38ca.6bc9,2,,2018-05-30 13:38:18 CST,4/937,42857584,ERROR,23505,"duplicate key value violates unique constraint ""lr2_pkey""","Key (id)=(7) already exists.",,,,,,,,""
  • 发布者插入后查询订阅者
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
  7 |  77 | 2018-05-30 13:37:12.454944 |
(5 rows)

postgres=#

结 果: 订阅者插入数据,发布者也插入id相同的数据时,订阅者不会同步复制发布者的数据

  • 发布者插入数据
postgres=# insert into lr2 (id ,age) values (8,88);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  3 |  66 | 2018-05-30 13:12:59.145076
  7 |  88 | 2018-05-30 13:37:47.736737
  8 |  88 | 2018-05-30 13:42:53.396291
(6 rows)

postgres=#
  • 订阅者查询
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
  7 |  77 | 2018-05-30 13:37:12.454944 |
(5 rows)

postgres=#

结 果: 此时逻辑复制关系中断,订阅者不会同步复制发布者插入的数据。

  • 发布者删除冲突的数据
postgres=# delete from lr2 where id = 7;
DELETE 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  3 |  66 | 2018-05-30 13:12:59.145076
  8 |  88 | 2018-05-30 13:42:53.396291
(5 rows)

postgres=#
  • 查询订阅者
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
  7 |  77 | 2018-05-30 13:37:12.454944 |
(5 rows)

postgres=#

结 果: 发布者删除与订阅者冲突的数据,订阅者也不会同步发布者插入的数据

  • 发布者插入
postgres=# insert into lr2 (id ,age) values (9,99);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  3 |  66 | 2018-05-30 13:12:59.145076
  8 |  88 | 2018-05-30 13:42:53.396291
  9 |  99 | 2018-05-30 13:46:42.669047
(6 rows)

postgres=#
  • 订阅者查询
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
  7 |  77 | 2018-05-30 13:37:12.454944 |
(5 rows)

postgres=#

结 果:冲突发生后,订阅者将不会同步复制发布者的数据,逻辑复制关系中断

  • 订阅者删除冲突的数据
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  3 |  66 | 2018-05-30 13:12:59.145076 | 李四
  7 |  77 | 2018-05-30 13:37:12.454944 |
(5 rows)

postgres=# delete from lr2 where id = 7;
DELETE 1
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  8 |  88 | 2018-05-30 13:42:53.396291 |
  9 |  99 | 2018-05-30 13:46:42.669047 |
  3 |  33 | 2018-05-30 13:12:59.145076 | 李四
(6 rows)

postgres=#

结 果:订阅者删除冲突数据后,会恢复逻辑复制关系,同步此时发布者的数据

  • 发布者插入数据
postgres=# insert into lr2 (id ,age) values (10,10);
INSERT 0 1
postgres=# select * from lr2;
 id | age |         inserttime
----+-----+----------------------------
  1 |  11 | 2018-05-30 11:30:27.474004
  2 |  22 | 2018-05-30 13:04:03.1012
  4 |  44 | 2018-05-30 13:27:48.824291
  8 |  88 | 2018-05-30 13:42:53.396291
  9 |  99 | 2018-05-30 13:46:42.669047
  3 |  33 | 2018-05-30 13:12:59.145076
 10 |  10 | 2018-05-30 14:13:33.318161
(7 rows)

postgres=#
  • 订阅者查询
postgres=# select * from lr2;
 id | age |         inserttime         | name
----+-----+----------------------------+------
  1 |  11 | 2018-05-30 11:30:27.474004 |
  2 |  22 | 2018-05-30 13:04:03.1012   | 隶属
  4 |  44 | 2018-05-30 13:27:48.824291 |
  8 |  88 | 2018-05-30 13:42:53.396291 |
  9 |  99 | 2018-05-30 13:46:42.669047 |
  3 |  33 | 2018-05-30 13:12:59.145076 | 李四
 10 |  10 | 2018-05-30 14:13:33.318161 |
(7 rows)

postgres=#
  • 总 结:建立逻辑复制后,由于冲突或者表结构变更,导致逻辑复制关系挂起后,通过解决冲突和问题后,逻辑复制关系会恢复,并会同步此时发布者的数据

  • 订阅者查询

postgres=# select * from lr2 where addr is null;
 id | age |         inserttime         | addr
----+-----+----------------------------+------
  2 |  22 | 2018-05-30 13:04:03.1012   |
  4 |  44 | 2018-05-30 13:27:48.824291 |
  8 |  88 | 2018-05-30 13:42:53.396291 |
  9 |  99 | 2018-05-30 13:46:42.669047 |
  3 |  33 | 2018-05-30 13:12:59.145076 |
 11 |  11 | 2018-05-30 14:17:46.097985 |
 10 |  13 | 2018-05-30 14:13:33.318161 |
(7 rows)
  • 订阅者查询
postgres=# select * from lr2 where addr ='';
 id | age | inserttime | addr
----+-----+------------+------
(0 rows)

postgres=#
  • 结 果: 订阅者列多于发布者时,发布者插入的数据复制到订阅者时,多的字段内容为 null

逻辑复制冲突解决方法

发生冲突人为手动解决,删除或者修改冲突数据,忽略该冲突

  • 通过 node_name=订阅者名称 和一个 position 调用pg_replication_origin_advance()函数忽略冲突
pg_replication_origin_advance(node_name text, lsn pg_lsn)
  • 查看当前数据的位置
postgres=# select * from pg_replication_origin_status;
 local_id | external_id | remote_lsn | local_lsn
----------+-------------+------------+-----------
        1 | pg_24606    | 0/0        | 0/0
        2 | pg_24608    | 0/0        | 0/0
        3 | pg_24622    | 1/8D00B470 | 0/0
        4 | pg_24629    | 1/F80070D0 | 2/C01EED8
(4 rows)

逻辑复制约束

  • _不支持 DDL 和数据库 Schema 的复制不支持

    • 不支持 Sequence data(serial or identity columns)

    • 不支持 TRUNCATE 操作

    • 不支持 Large objects

    • 意 义

Replication is only possible from base tables to base tables. That is, the tables on the publication and on the subscription side must be normal tables, not views, materialized views, partition root tables, or foreign tables. In the case of partitions, you can therefore replicate a partition hierarchy one-toone, but you cannot currently replicate to a differently partitioned setup. Attempts to replicate tables other than base tables will result in an error

补充测试

  • 17、18 服务器创建逻辑复制表
lrdatabase=# create table lr1 (id serial,age int ,name varchar(10),sex char(1),birth varchar(10),addr text not null);
CREATE TABLE
lrdatabase=# \d+
                          List of relations
 Schema |    Name    |   Type   |  Owner   |    Size    | Description
--------+------------+----------+----------+------------+-------------
 public | lr1        | table    | postgres | 8192 bytes |
 public | lr1_id_seq | sequence | postgres | 8192 bytes |
(2 rows)
  • 17服务器创建发布者
lrdatabase=# grant all on lr1 to l_repl;
GRANT
lrdatabase=# CREATE PUBLICATION p_lr1 FOR TABLE lr1 WITH (publish = 'insert,update,delete');
CREATE PUBLICATION
lrdatabase=#
  • 18 创建逻辑复制表
lrdatabase=#  create table lr1 (id serial,age int ,name varchar(10),sex char(1),birth varchar(10),addr text not null);
CREATE TABLE
  • 18服务器创建订阅者
lrdatabase=# CREATE SUBSCRIPTION lr1_sub CONNECTION 'host=10.10.56.17 port=5435 user=l_repl dbname=lrdatabase password=123456' PUBLICATION p_lr1;
NOTICE:  created replication slot "lr1_sub" on publisher
CREATE SUBSCRIPTION
lrdatabase=#

逻辑复制功能测试

  • 17 发布者一端插入数据
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),11,'lisi','1','2018-06-05','被经');
INSERT 0 1
lrdatabase=# select * from lr1;
 id | age | name | sex |   birth    | addr
----+-----+------+-----+------------+------
  1 |  11 | lisi | 1   | 2018-06-05 | 被经
(1 row)
  • 18服务器订阅者查询
lrdatabase=# select * from lr1;
 id | age | name | sex |   birth    | addr
----+-----+------+-----+------------+------
  1 |  11 | lisi | 1   | 2018-06-05 | 被经
(1 row)

lrdatabase=#

此时数据已经复制过来,逻辑复制功能完成

测试变更表结构字段数据类型

订阅者修改字段类型
  • 18服务器 订阅者修改 sex 字段 char(1) 类型修改为 char(2)
lrdatabase=# ALTER TABLE lr1 ALTER COLUMN sex TYPE char(2);
ALTER TABLE
lrdatabase=# \d+ lr1
                                                       Table "public.lr1"
 Column |         Type          | Collation | Nullable |             Default             | Storage  | Stats target | Description
--------+-----------------------+-----------+----------+---------------------------------+----------+--------------+-------------
 id     | integer               |           | not null | nextval('lr1_id_seq'::regclass) | plain    |              |
 age    | integer               |           |          |                                 | plain    |              |
 name   | character varying(10) |           |          |                                 | extended |              |
 sex    | character(2)          |           |          |                                 | extended |              |
 birth  | character varying(10) |           |          |                                 | extended |              |
 addr   | text                  |           | not null |                                 | extended |              |

lrdatabase=#
  • 17发布者插入数据
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),12,'lisi','1','2018-06-05','tt');
INSERT 0 1
lrdatabase=# select * from lr1;
 id | age | name | sex |   birth    | addr
----+-----+------+-----+------------+------
  1 |  11 | lisi | 1   | 2018-06-05 | 被经
  2 |  12 | lisi | 1   | 2018-06-05 | tt
(2 rows)

lrdatabase=#
  • 18订阅者查询数据
lrdatabase=# select * from lr1;
 id | age | name | sex |   birth    | addr
----+-----+------+-----+------------+------
  1 |  11 | lisi | 1   | 2018-06-05 | 被经
  2 |  12 | lisi | 1   | 2018-06-05 | tt
(2 rows)

lrdatabase=#

总 结: 订阅者修改字段类型char(1) 到char(2) ,如果新增数据类型为以前字段类型,则不会影响逻辑复制,

发布者修改字段类型
  • 在17发布者修改字段类型char(1) 到char(2)
lrdatabase=# ALTER TABLE lr1 ALTER COLUMN sex TYPE char(2);
ALTER TABLE
  • 17 插入数据为,sex字段为2字节的数据 测试
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),13,'lisi','22','2018-06-05','tt');
INSERT 0 1
lrdatabase=# select * from lr1;
 id | age | name | sex |   birth    | addr
----+-----+------+-----+------------+------
  1 |  11 | lisi | 1   | 2018-06-05 | 被经
  2 |  12 | lisi | 1   | 2018-06-05 | tt
  3 |  13 | lisi | 22  | 2018-06-05 | tt
(3 rows)
  • 18查询数据
lrdatabase=# select * from lr1;
 id | age | name | sex |   birth    | addr
----+-----+------+-----+------------+------
  1 |  11 | lisi | 1   | 2018-06-05 | 被经
  2 |  12 | lisi | 1   | 2018-06-05 | tt
(2 rows)

总 结:发布者表结构变更后,插入数据(1、字段为之前数据类型,2、字段为变更后的数据类型),逻辑复制挂起,均不能进行数据同步,当解决冲突后,逻辑复制恢复,同步数据。

不同字段数据类型变更测试

  • 17发布者变更表字段由 int--> varchar(2)
lrdatabase=# ALTER TABLE lr1 ALTER COLUMN age TYPE varchar(2);
ALTER TABLE
  • 17服务器插入变更后数据类型的数据 age '15'
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),'15','lis22','00','2018-06-05','tts');
INSERT 0 1
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 | 11  | lisi  | 1   | 2018-06-05 | 被经
  2 | 12  | lisi  | 1   | 2018-06-05 | tt
  3 | 13  | lisi  | 22  | 2018-06-05 | tt
  4 | 14  | lisi  | 0   | 2018-06-05 | tt
  5 | 15  | lis22 | 00  | 2018-06-05 | tts
(5 rows)
  • 18服务器插叙数据

lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 |  11 | lisi  | 1   | 2018-06-05 | 被经
  2 |  12 | lisi  | 1   | 2018-06-05 | tt
  3 |  13 | lisi  | 22  | 2018-06-05 | tt
  4 |  14 | lisi  | 0   | 2018-06-05 | tt
  5 |  15 | lis22 | 00  | 2018-06-05 | tts
(5 rows)

总结:发现数据可以同步,则数据类型 int –》varchar(2) ,并不会影响逻辑复制

  • 17 发布者 varchar 转换为 date 类型测试
lrdatabase=# ALTER TABLE lr1 ALTER COLUMN birth TYPE date USING birth::date;
ALTER TABLE
  • 17 插入varchar类型和date数据类型的数据
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),'15','lis22','00','2018-06-05','tts');
INSERT 0 1
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),'15','lis22','00',now(),'tts');
INSERT 0 1
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 | 11  | lisi  | 1   | 2018-06-05 | 被经
  2 | 12  | lisi  | 1   | 2018-06-05 | tt
  3 | 13  | lisi  | 22  | 2018-06-05 | tt
  4 | 14  | lisi  | 0   | 2018-06-05 | tt
  5 | 15  | lis22 | 00  | 2018-06-05 | tts
  6 | 15  | lis22 | 00  | 2018-06-05 | tts
  7 | 15  | lis22 | 00  | 2018-06-05 | tts
(7 rows)
lrdatabase=#
  • 18 查询数据
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 |  11 | lisi  | 1   | 2018-06-05 | 被经
  2 |  12 | lisi  | 1   | 2018-06-05 | tt
  3 |  13 | lisi  | 22  | 2018-06-05 | tt
  4 |  14 | lisi  | 0   | 2018-06-05 | tt
  5 |  15 | lis22 | 00  | 2018-06-05 | tts
  6 |  15 | lis22 | 00  | 2018-06-05 | tts
  7 |  15 | lis22 | 00  | 2018-06-05 | tts
(7 rows)
lrdatabase=#

总结:数据类型由varchar(2)—》date,并不会影响逻辑复制,数据可以正常同步

发布者新增索引测试

  • 17发布者新增索引
lrdatabase=# CREATE INDEX name_index ON lr1(name) ;
CREATE INDEX
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),'17','lis33','00',now(),'tty');
INSERT 0 1
lrdatabase=#
  • 18订阅者查询数据
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 |  11 | lisi  | 1   | 2018-06-05 | 被经
  2 |  12 | lisi  | 1   | 2018-06-05 | tt
  3 |  13 | lisi  | 22  | 2018-06-05 | tt
  4 |  14 | lisi  | 0   | 2018-06-05 | tt
  5 |  15 | lis22 | 00  | 2018-06-05 | tts
  6 |  15 | lis22 | 00  | 2018-06-05 | tts
  7 |  15 | lis22 | 00  | 2018-06-05 | tts
  8 |  17 | lis33 | 00  | 2018-06-05 | tty
(8 rows)

lrdatabase=#

总结:发布者新增索引字段,不会影响逻辑复制,数据可以正常同步

  • 17发布者更新索引,插入数据
lrdatabase=# ALTER INDEX name_index RENAME TO name_index_update;
ALTER INDEX
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),'18','lis44','11',now(),'ssy');
INSERT 0 1
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 | 11  | lisi  | 1   | 2018-06-05 | 被经
  2 | 12  | lisi  | 1   | 2018-06-05 | tt
  3 | 13  | lisi  | 22  | 2018-06-05 | tt
  4 | 14  | lisi  | 0   | 2018-06-05 | tt
  5 | 15  | lis22 | 00  | 2018-06-05 | tts
  6 | 15  | lis22 | 00  | 2018-06-05 | tts
  7 | 15  | lis22 | 00  | 2018-06-05 | tts
  8 | 17  | lis33 | 00  | 2018-06-05 | tty
  9 | 18  | lis44 | 11  | 2018-06-05 | ssy
(9 rows)
lrdatabase=#
  • 18 订阅者查询数据
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 |  11 | lisi  | 1   | 2018-06-05 | 被经
  2 |  12 | lisi  | 1   | 2018-06-05 | tt
  3 |  13 | lisi  | 22  | 2018-06-05 | tt
  4 |  14 | lisi  | 0   | 2018-06-05 | tt
  5 |  15 | lis22 | 00  | 2018-06-05 | tts
  6 |  15 | lis22 | 00  | 2018-06-05 | tts
  7 |  15 | lis22 | 00  | 2018-06-05 | tts
  8 |  17 | lis33 | 00  | 2018-06-05 | tty
  9 |  18 | lis44 | 11  | 2018-06-05 | ssy
(9 rows)
lrdatabase=#

总结:发布者变更数据索引,不影响逻辑复制功能,数据正常同步。

  • 17发布者删除索引,新增数据
lrdatabase=# DROP INDEX name_index_update;
DROP INDEX
lrdatabase=# insert into lr1 values (nextval('lr1_id_seq'),'19','lis55','00',now(),'yyt');
INSERT 0 1
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 | 11  | lisi  | 1   | 2018-06-05 | 被经
  2 | 12  | lisi  | 1   | 2018-06-05 | tt
  3 | 13  | lisi  | 22  | 2018-06-05 | tt
  4 | 14  | lisi  | 0   | 2018-06-05 | tt
  5 | 15  | lis22 | 00  | 2018-06-05 | tts
  6 | 15  | lis22 | 00  | 2018-06-05 | tts
  7 | 15  | lis22 | 00  | 2018-06-05 | tts
  8 | 17  | lis33 | 00  | 2018-06-05 | tty
  9 | 18  | lis44 | 11  | 2018-06-05 | ssy
 10 | 19  | lis55 | 00  | 2018-06-05 | yyt
(10 rows)

lrdatabase=#
  • 18 订阅者查询数据
lrdatabase=# select * from lr1;
 id | age | name  | sex |   birth    | addr
----+-----+-------+-----+------------+------
  1 |  11 | lisi  | 1   | 2018-06-05 | 被经
  2 |  12 | lisi  | 1   | 2018-06-05 | tt
  3 |  13 | lisi  | 22  | 2018-06-05 | tt
  4 |  14 | lisi  | 0   | 2018-06-05 | tt
  5 |  15 | lis22 | 00  | 2018-06-05 | tts
  6 |  15 | lis22 | 00  | 2018-06-05 | tts
  7 |  15 | lis22 | 00  | 2018-06-05 | tts
  8 |  17 | lis33 | 00  | 2018-06-05 | tty
  9 |  18 | lis44 | 11  | 2018-06-05 | ssy
 10 |  19 | lis55 | 00  | 2018-06-05 | yyt
(10 rows)
lrdatabase=#

总 结:发布者删除索引,不影响逻辑复制,数据可以正常同步,逻辑复制只会对数据进行复制同步,不会对索引进行同步

结 论:通过上述测试得出,逻辑复制仅仅支持数据复制,并不会对 DDL、索引等进行复制。

猜你喜欢

转载自blog.csdn.net/yaoqiancuo3276/article/details/80846002