数据库版本pg10升级到pg11.5
1.修改旧版本数据库的参数(需要重启数据库)
wal_level = logical
max_replication_slots = 8
max_wal_senders = 10
2.导出旧版本的表结构
pg_dump -d test -s >/data/test.sql
3.查询迁移的数据库中没有主键的表
--检查同步的数据库是否有主键(pg10及以下版本)
SELECT nspname || '.' ||relname,relhaspkey FROM pg_catalog.pg_namespace,pg_class WHERE nspname !~ '^pg_' AND nspname not in('information_schema','pglogical') and pg_class.relnamespace = pg_catalog.pg_namespace.oid and relkind = 'r' and relhaspkey=false;
--检查同步的数据库是否有主键(pg11及以上版本)
SELECT n.nspname AS "Schema",c.relname AS "Table Name"
FROM
pg_catalog.pg_class c,
pg_namespace n
where
c.relnamespace = n.oid
AND n.nspname NOT IN ('information_schema', 'pg_catalog')
AND c.relkind='r'
AND not exists (select 1 from pg_index i where c.oid=i.indrelid and i.indisprimary='t' ) ;
4.将没有主键的表增加REPLICA IDENTITY FULL
ALTER TABLE rdl_bmc_r03_parts_inhouse REPLICA IDENTITY FULL;
3.在新版本创建数据库,并导入表结构
postgres=# create database test;
psql -d test -f /data/test.sql
4.旧版本数据库为发布端,新版本数据库为订阅端
发布节点上创建逻辑复制用户,逻辑复制用户需要具备REPLICATION权限,并赋权
test=# CREATE USER logical_user REPLICATION LOGIN CONNECTION LIMIT 8 ENCRYPTED PASSWORD 'logical_user';
test=# GRANT USAGE ON SCHEMA public TO logical_user;
test=# GRANT SELECT ON all tables IN SCHEMA public TO logical_user;
5.发布节点创建发布
test=# CREATE PUBLICATION pub1 FOR ALL TABLES ;
CREATE PUBLICATION
FOR TABLE:指加入到发布的表列表,目前仅支持普通表的发布,临时表、外部表、视图、物化视图、分区表暂不支持发布,如果想将分区表添加到发布中,需逐个添加分区表分区到发布。
FOR ALL TABLES:将当前库中所有表添加到发布中,包括以后在这个库中新建的表。这种模式相当于在全库级别逻辑复制所有表。
6.发布节点上查询pg_publication视图
test=# SELECT * FROM pg_publication;
• pubname | pubowner | puballtables | pubinsert | pubupdate | pubdelete
------------+----------+--------------+-----------+-----------+-----------
• pub1 | 16391 | f | t | t | t
(1 row)
7.订阅端发布订阅(新版本数据库)
test=# CREATE SUBSCRIPTION sub1 CONNECTION 'host=10.122.111.171 port=5432
• dbname=upsell_prd user=logical_user' PUBLICATION pub1;
8.发布节点查看复制槽
test=# SELECT slot_name,plugin,slot_type,database,active,restart_lsn
test-# FROM pg_replication_slots where slot_name='sub1';
slot_name | plugin | slot_type | database | active | restart_lsn
-----------+----------+-----------+------------+--------+---------------
sub1 | pgoutput | logical | test | t | 2198/8CF3CE60
(1 row)
9.订阅端查看订阅信息
test=# select * from pg_stat_subscription;
subid | subname | pid | relid | received_lsn | last_msg_send_time | last_msg_receipt_time | latest_end_lsn | latest_end_time
-------+---------+-------+-------+---------------+-------------------------------+-------------------------------+----------------+-------------------------------
19658 | sub1 | 4404 | 16656 | | 2021-10-20 17:25:27.926446+08 | 2021-10-20 17:25:27.926446+08 | | 2021-10-20 17:25:27.926446+08
19658 | sub1 | 5573 | 19427 | | 2021-10-20 17:32:33.014151+08 | 2021-10-20 17:32:33.014151+08 | | 2021-10-20 17:32:33.014151+08
19658 | sub1 | 25118 | | 2198/8CF3CE98 | 2021-10-20 17:32:33.035482+08 | 2021-10-20 17:32:33.035666+08 | 2198/8CF3CE98 | 2021-10-20 17:32:33.035482+08
10.订阅端查看当前的数据位置
test=# select * from pg_replication_origin_status;
local_id | external_id | remote_lsn | local_lsn
----------+-------------+------------+-------------
1 | pg_19658 | 0/0 | 21/983EF8C8
(1 row)
11.数据迁移完成后,删除发布、订阅
test=# drop PUBLICATION pub1;
test=# drop SUBSCRIPTION sub1;
12.注意事项
1)利用逻辑复制建议pg版本要大于等于10
2)表需要有主键,没有主键需要增加REPLICA IDENTITY FULL
3)不支持 DDL 和数据库 Schema 的复制不支持
4)不支持 Sequence data(serial or identity columns)
5)不支持 TRUNCATE 操作
6)不支持 Large objects