内部構造から学ぶPostgreSQL 設計・運用計画の鉄則 ch10 (高可用化と負荷分散) (5/6)

OSS-DBPostgreSQL勉強メモ

出典: 


論理レプリケーション

  • 公式
  • 対象とするテーブルや操作を指定できる

    • cf. ストリーミングレプリケーション(物理レプリケーション)はデータベースクラスタ全体

論理レプリケーションの仕組み

  • WALの転送により実現
  • pub/subモデル

ロジカルデコーディングとバックグラウンドワーカ

パブリケーションとサブスクリプション

  • 公式/Publication
  • 公式/Subscription
  • サブスクライバも、他のサーバから見てパブリッシャになりうる
  • のでプライマリ/スタンバイ という分類じゃない

論理レプリケーションの制限事項

  • 対象操作はDMLのみ

    • ver11でTRUNCATE (DDL)も追加
  • ver10

Publications can choose to limit the changes they produce
to any combination of INSERT, UPDATE, and DELETE,
similar to how triggers are fired by particular event types.
By default, all operation types are replicated.

  • ver11

Publications can choose to limit the changes they produce
to any combination of INSERT, UPDATE, DELETE, and
TRUNCATE, similar to how triggers are fired by particular
event types. By default, all operation types are replicated.

  • 対象オブジェクトは通常のテーブルのみ
Publications may currently
only contain tables. Objects must be added explicitly,
except when a publication is created for ALL TABLES.
  • 対象としないもの

    • シーケンス
    • LOB
    • ビュー
    • マテビュー
    • パーティションの親テーブル
    • 外部テーブル
  • REPLICA IDENTITY

    • UPDATE/DELETEのレプリケーションのために設定が必要

      • 操作対象の行を特定するための情報がWALに書き込まれる
    • 行を特定できること

      • DEFAULT

        • 主キー
      • USING INDEX <index_name>

        • 候補キーたりうるインデックス
        • uniqueかつnot nullであること
      • FULL

        • 行全体
    • ALTER TABLEで設定可能

可能なレプリケーション構成

  • ロジカルレプリケーションと同じ構成が可能

    • 1:N
    • カスケード
  • 双方向、N:1も可能

設定手順の整理

  • docker環境作った

[https://github.com/wand2016/postgresqllogicalreplication_practice:embed:cite]

  • testdb
  • testtbl
CREATE TABLE testtbl(i int primary key, j int)

パブリッシャの設定

postgres=# SELECT name,setting,context FROM pg_settings WHERE name IN ('wal_level','max_replication_slots','max_wal_senders');
         name          | setting |  context   
-----------------------+---------+------------
 max_replication_slots | 10      | postmaster
 max_wal_senders       | 10      | postmaster
 wal_level             | replica | postmaster
(3 rows)
  • ぜんぶ設定反映にrestart必要
ALTER SYSTEM SET wal_level = 'logical';
ALTER SYSTEM SET max_replication_slots = 2;
ALTER SYSTEM SET max_wal_senders = 2;

サブスクライバの設定

postgres=# SELECT name,setting,context FROM pg_settings WHERE name IN ('max_replication_slots','max_sync_workers_per_subscription','max_logical_replication_workers','max_worker_processes');
               name                | setting |  context   
-----------------------------------+---------+------------
 max_logical_replication_workers   | 4       | postmaster
 max_replication_slots             | 2       | postmaster
 max_sync_workers_per_subscription | 2       | sighup
 max_worker_processes              | 8       | postmaster
(4 rows)
ALTER SYSTEM SET max_replication_slots = 1;
ALTER SYSTEM SET max_sync_workers_per_subscription = 1;
ALTER SYSTEM SET max_logical_replication_workers = 2;
ALTER SYSTEM SET max_worker_processes = 3;