云数据库 RDS-使用pgl_ddl_deploy插件:基本使用

时间:2024-10-25 14:46:10

基本使用

插件涉及到发布订阅和依赖于pglogical,需要添加和配置参数。

wal_level = 'logical'
shared_preload_libraries = 'pglogical'

shared_preload_libraries参数的修改可以参考修改shared_preload_libraries参数

-- 发布端配置
SELECT control_extension ('create', 'pglogical');
SELECT control_extension ('create', 'pgl_ddl_deploy');
CREATE TABLE foo (id INT PRIMARY KEY);
-- 创建发布
CREATE PUBLICATION testpub FOR TABLE foo;
-- 配置复制集
INSERT INTO pgl_ddl_deploy.set_configs (set_name, include_schema_regex, driver) VALUES ('testpub', '.*', 'native'::pgl_ddl_deploy.driver);
-- 部署这个发布
SELECT pgl_ddl_deploy.deploy('testpub');
-- 添加用户权限
SELECT pgl_ddl_deploy.add_role(oid) FROM pg_roles WHERE rolname='root';
-- 订阅端配置
SELECT control_extension ('create', 'pglogical');
SELECT control_extension ('create', 'pgl_ddl_deploy');
CREATE TABLE foo (id INT PRIMARY KEY);
-- 创建订阅
CREATE SUBSCRIPTION testsub CONNECTION conninfo PUBLICATION testpub;
ALTER SUBSCRIPTION testsub REFRESH PUBLICATION;

配置完之后在发布端执行DDL语句:

ALTET TABLE foo ADD COLUMN bla INT;
CREATE TABLE bra (id INT PRIMARY KEY);

可以在订阅端验证:

\d foo
                Table "public.foo"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           | not null |
 bla    | integer |           |          |
Indexes:
    "foo_pkey" PRIMARY KEY, btree (id)
\dt
          List of relations
 Schema |  Name   | Type  |  Owner
--------+---------+-------+----------
 public | bar     | table | root
 public | foo     | table | root
(2 rows)
support.huaweicloud.com/usermanual-rds/rds_09_0061.html