{Aurora}Aurora での PostgreSQL 論理レプリケーションの使用

rds.logical_replication パラメータを有効にすると、DB クラスターのパフォーマンスに影響します。


パブリッシャー:
Aurora PostgreSQL バージョン 2.2.0 (PostgreSQL 10.6 と互換) 以降

サブスクライバー:
Aurora PostgreSQL データベースバージョン 2.2.0 (PostgreSQL 10.6 と互換) 以降。
Amazon RDS for PostgreSQL データベース (PostgreSQL DB エンジンバージョン 10.4 以降を搭載)。


-- 1. パブリッシャーDB作成 (Aurora PostgreSQL 12.6)

aws rds create-db-cluster-parameter-group \
--db-parameter-group-family aurora-postgresql12 \
--db-cluster-parameter-group-name cpg01 \
--description cpg01

aws rds modify-db-cluster-parameter-group \
--db-cluster-parameter-group-name cpg01 \
--parameters ParameterName=rds.logical_replication,ParameterValue=1,ApplyMethod=pending-reboot

 

aws rds create-db-cluster \
--db-cluster-identifier cluster01 \
--engine aurora-postgresql \
--engine-version 12.6 \
--master-username postgres \
--master-user-password 'password' \
--db-cluster-parameter-group-name cpg01

aws rds create-db-instance \
--db-instance-identifier cluster01-instance01 \
--db-cluster-identifier cluster01 \
--db-instance-class db.t3.medium \
--engine aurora-postgresql \
--no-auto-minor-version-upgrade

-- 2. サブスクライバーDB作成 (RDS PostgreSQL 10.4)

aws rds create-db-instance \
--db-instance-identifier postgres01 \
--allocated-storage 20 \
--db-instance-class db.t3.micro \
--engine postgres \
--master-username postgres \
--master-user-password 'password' \
--no-multi-az \
--engine-version 10.4 \
--storage-type gp2 \
--no-publicly-accessible \
--no-enable-performance-insights

-- 3. 論理レプリケーション動作確認

-- パブリッシャーで実行

create table tab1 (col1 int primary key);
insert into tab1 values (generate_series(1,10000));
create publication pub01 for table tab1;

select count(*) from tab1;

-- サブスクライバーで実行
create table tab1(col1 int primary key);

select count(*) from tab1;

create subscription sub01 connection 'host=cluster01-instance01.xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com port=5432 dbname=test user=postgres password=password' publication pub01;

select count(*) from tab1;

 

select * from pg_stat_replication;
select * from pg_stat_subscription;
select * from pg_replication_slots;
select * from pg_publication;
select * from pg_publication_tables;

 

-- 4. クリーンアップ

-- RDSインスタンス削除
aws rds delete-db-instance \
--db-instance-identifier postgres01 \
--skip-final-snapshot


-- Auroraクラスタ削除

aws rds delete-db-instance \
--db-instance-identifier cluster01-instance01 \
--skip-final-snapshot

aws rds delete-db-cluster \
--db-cluster-identifier cluster01 \
--skip-final-snapshot

-- クラスタパラメータグループ削除
aws rds delete-db-cluster-parameter-group --db-cluster-parameter-group-name cpg01