Logical Replication Internals - How PostgreSQL Rocks ... · 5 –IS –PostgreSQL 10の新機能...

Preview:

Citation preview

Logical Replication

Internals

Noriyoshi Shinoda

December 8, 2017

PGConf.ASIA 2017

Download URL: https://goo.gl/MUYuE3

自己紹介篠田典良(しのだのりよし)

–所属

– 日本ヒューレット・パッカード株式会社 Pointnext事業統括

–現在の業務

– PostgreSQLをはじめOracle Database , Microsoft SQL Server, Vertica, Sybase ASE等

RDBMS全般に関するシステムの設計、チューニング、コンサルティング

– Oracle ACE

– Oracle Database関連書籍の執筆

– オープンソース製品に関する調査、検証

–関連する URL

– 「PostgreSQL 虎の巻」シリーズ

– http://h30507.www3.hp.com/t5/user/viewprofilepage/user-id/838802

– Oracle ACEってどんな人?

– http://www.oracle.com/technetwork/jp/database/articles/vivadeveloper/index-1838335-ja.html

2

Download URL: https://goo.gl/MUYuE3

Agenda

– Logical Replicationとは?

–試してみよう

–アーキテクチャ

–制約

– トラブルシューティング

–今後に期待

3

Master / Slaveマスター/スレーブ

PUB/SUB表記を統一

Logical Replicationとは?

4

Download URL: https://goo.gl/MUYuE3

Logical Replicationとは?Logical Replicationとは?

5

– IS

– PostgreSQL 10の新機能

– テーブル単位のレプリケーション

– テーブルの「データ」をレプリケーションする機能

– トランザクション単位のレプリケーション

– SQL文の結果が同一になることを保証

– Publish / Subscribeモデルを使用

– ≒ Slony-I

– IS NOT

– SQLの再実行

– 物理的なページ・フォーマットの一致

Download URL: https://goo.gl/MUYuE3

Logical Replicationとは?基礎となるテクノロジー

6

– Streaming Replication (9.0)

– Background Worker process (9.3)

– Logical Decoding (9.3)

– 「WALをデコードする」機能

– Replication Slot (9.4)

– Logical Replication Slot

– pgoutput plugin

– Replication Origin (9.5)

– pglogical Extension (2ndQuadrant)

Download URL: https://goo.gl/MUYuE3

Logical Replicationとは?利用シナリオ

7

–部分レプリケーション

– 拠点間でマスター・データだけ同期したい

– 分析用インスタンスで、テーブルにインデックスを追加したい

– 分析結果を別テーブルに保存したい

–バージョンアップ

– PostgreSQL 11への無停止バージョンアップ

Download URL: https://goo.gl/MUYuE3

Logical Replicationとは?Streaming Replicationとの比較

8

比較項目 Streaming Replication Logical Replication

スキーマ名 同一 同一

テーブル名 同一 同一

列名 同一 同一

列データ型 同一 同一(*)

列の定義順 同一 異なっていてもよい

列追加 不可 可能

追加インデックス作成 不可 可能

更新DML すべて伝播する 選択可能

DDL 伝播する 伝播しない

- (*) 暗黙の型変換ができれば異なっていても良い

Download URL: https://goo.gl/MUYuE3

Logical Replicationとは?Streaming Replicationとの比較

9

比較項目 Streaming Replication Logical Replication

レプリケーション範囲 クラスター全体 テーブル単位

System Identifier 同一 異なっていてもよい

スレーブの動作 Read Only Read Write

文字コード 同一 異なっていてもよい

バージョン 同一 異なっていてもよい

Replication Slot 利用しなくてもよい 必須

データベース名 同一 異なっていてもよい

テーブルの所有者 同一 異なっていてもよい

試してみよう

10

Download URL: https://goo.gl/MUYuE3

試してみようPUBLISHインスタンス上の操作

11

# TYPE DATABASE USER ADDRESS METHODhost pubdb repusr1 192.168.1.100/32 md5

- pg_hba.conf ファイルの修正

pubdb=# CREATE ROLE repusr1 PASSWORD 'Passw0rd' LOGIN REPLICATION ;CREATE ROLE

- REPLICATION属性/LOGIN属性を持つロール作成

- ターゲット・インスタンスから接続されるロール

pubdb=# GRANT CREATE ON DATABASE pubdb TO pubusr ;GRANT

- テーブル所有者にデータベースのCREATE権限を付与

- DATABASE=replication 項目不要

マニュアル上はREPLICATIONのみ

Download URL: https://goo.gl/MUYuE3

試してみようPUBLISHインスタンス上の操作

12

- パラメーターwal_levelの変更

pubdb=> SHOW wal_level ;wal_level-----------logical(1 row)

Download URL: https://goo.gl/MUYuE3

試してみようPUBLISHデータベース(pubdb)上の操作

13

–レプリケーション対象テーブルの作成(アプリケーション要件)

pubdb=> CREATE TABLE data1 (c1 INT PRIMARY KEY, c2 VARCHAR(5)) ;CREATE TABLE

pubdb=> CREATE PUBLICATION pub1 FOR TABLE data1 ;CREATE PUBLICATION

– PUBLICATIONオブジェクトの作成

–レプリケーション対象テーブルの参照を接続ユーザーに許可

pubdb=> GRANT SELECT ON data1 TO repusr1 ;GRANT

Download URL: https://goo.gl/MUYuE3

試してみようSUBSCRIBEデータベース(subdb)上の操作

14

–レプリケーション対象テーブルの作成

subdb=> CREATE TABLE data1 (c1 INT PRIMARY KEY, c2 CHAR(5)) ;CREATE TABLE

subdb=# CREATE SUBSCRIPTION sub1 CONNECTION 'host=srchost1 dbname=srcdb user=repusr1 password=Passw0rd' PUBLICATION pub1 ;

CREATE SUBSCRIPTION

– PUBLICATIONオブジェクトの作成(SUPERUSER)

Download URL: https://goo.gl/MUYuE3

試してみようレプリケーション確認

15

–確認(PUBLISHデータベース)

pubdb=# SELECT application_name, state, sync_state FROM pg_stat_replication ;

application_name | state | sync_state------------------+-----------+------------sub1 | streaming | async(1 row)

subdb=# SELECT subname, received_lsn FROM pg_stat_subscription ;subname | received_lsn---------+--------------sub1 | 0/1C8FF738

–確認(SUBSCRIBEデータベース)

Download URL: https://goo.gl/MUYuE3

試してみようログ

16

– PUBLICATION

– LOG: starting logical decoding for slot "subx"

– SUBSCRIPTION作成

– LOG: logical replication apply worker for subscription "sub2" has started

– LOG: logical replication table synchronization worker for subscription "sub2", table "data2" has started

– LOG: logical replication table synchronization worker for subscription "sub2", table "data2" has finished

– DETAIL: streaming transactions committing after 0/280718B0, reading WAL from 0/28071878

– LOG: logical decoding found consistent point at 0/28071878

– DETAIL: There are no running transactions.

アーキテクチャ

17

Download URL: https://goo.gl/MUYuE3

Subscribe InstancePublish Instance

アーキテクチャPUBLICATIONとSUBSCRIPTIONの関係

18

Publish Database Subscribe Database

PUBLICATION SUBSCRIPTION

Tables

logical replication

worker

TablesTables

Tables

WAL decode messagewal

sender

pgoutputplugin

Download URL: https://goo.gl/MUYuE3

アーキテクチャプロセス

19

– wal sender process {user} {client ip} {state}

– PUBLICATIONインスタンスで起動

– WALデコード・メッセージ送信プロセス

– SUBSCRIPTIONからの接続単位に起動

– bgworker: logical replication launcher

– PUBLICATION/SUBSCRIPTIONインスタンスで起動

– logical replication workerを起動

– bgworker: logical replication worker for subscription {oid}

– SUBSCRIPTIONインスタンスで起動

– wal sender processに接続

– WALデコード・メッセージを受信し、テーブルを更新

– SUBSCRIPTION単位に起動

Download URL: https://goo.gl/MUYuE3

アーキテクチャー関連パラメーター(PUBLICATION)

20

パラメーター 必要な設定(デフォルト)

max_wal_senders SUBSCRIPTION + 1 数以上(8)

wal_level logical(replica)

max_replication_slots SUBSCRIPTION + 1 数以上(10)

synchronous_standby_names 任意('')

synchronous_commit 任意(on)????

wal_sender_timeout 任意(1min)

log_replication_commands 任意(off)

max_worker_processes = 0 にすると、logical replication launcherプロセスが起動しません。しかしソース・インスタンスには影響しません。

必要数とパラメーターの説明がごっちゃ?

Download URL: https://goo.gl/MUYuE3

アーキテクチャー関連パラメーター(SUBSCRIPTION)

21

パラメーター 必要な設定(デフォルト)

max_logical_replication_workers SUBSCRIPTION数+1(4)

max_worker_processes SUBSCRIPTION数+1(8)

max_sync_workers_per_subscriptionSUBSCRIPTIONで使用するワーカー・プロセス数(2)

wal_receiver_timeout PUBLICATIONの生存確認に使用 (1min)

wal_writer_delay???PUBLICATIONの生存確認時間のチェック間隔(200ms)

max_sync_workers_per_subscriptionは動作していない?

説明と必要数がごっちゃになっている

Download URL: https://goo.gl/MUYuE3

アーキテクチャーPUBLICATION作成時の動作

22

– pg_publicationカタログに情報格納

– 伝播するDML(INSERT / UPDATE / DELETE)

– 全テーブルを対象とするか(FOR ALL TABLES)

– pg_publication_relカタログに情報格納

– レプリケーション対象のテーブルOID

– pg_replication_tablesカタログはテーブル名を参照できる

– WITH句の指定

– 伝播するDML(INSERT / UPDATE / DELETE)

Download URL: https://goo.gl/MUYuE3

アーキテクチャSUBSCRIPTION作成時の動作

23

– pg_subscriptionカタログに情報格納

– 接続先インスタンスの情報

– Replication Slot名

– PUBLICATION名

– 同期/非同期レプリケーションの情報

– PUBLICATION側インスタンスに接続

– 接続ユーザーのREPLICATION権限のチェック

– Logical Replication Slot作成

– Replication Slotの名前はデフォルトはSUBSCRIPTION名

– PUBLICATIONが存在するかはチェックされない

– pg_subscription_relカタログにレプリケーション対象テーブルを登録

–初期データのロード

– 非同期に実行される

– SUBSCRIPTION側の既存データは削除されない

Download URL: https://goo.gl/MUYuE3

アーキテクチャSUBSCRIPTION作成時の動作

24

– WITHの説明

– PUBLICATIONへ接続する

– 初期データのコピーを行う

– Replication Slot名

– Replication Slotを作成するか

– 有効/無効

– 同期コミット設定

Download URL: https://goo.gl/MUYuE3

Target InstanceSource Instance

Backend process

wal sender process

wal sender process

アーキテクチャ初期データのロード

25

2. Create Replication Slot

3. Create Temporary Replication Slot

4. COPY TO STDOUT statement

logical replication worker

logical replication worker

Backend process

1.CREATE SUBSCRIPTION

Load Initial Data

Transaction Data

Download URL: https://goo.gl/MUYuE3

アーキテクチャ初期データのロード

26

LOG: logical replication table synchronization worker for subscription "sub1", table "data1" has startedLOG: logical replication table synchronization worker for subscription "sub1", table "data1" has finished

–初期同期ログ(SUBSCRIPTION instance)

Download URL: https://goo.gl/MUYuE3

アーキテクチャReplication Slot

27

– Logical Replication Slot

– SUBSCRIPTIONと1対1で構成

– 送信済WALの管理

– プラグインの提供

– SUBSCRIPTION作成時に以下のSQL文を実行

pg_create_logical_replication_slot ( name, 'pgoutput' )

– スロット名

–デフォルトではSUBSCRIPTION名

– CREATE SUBSCRIPTION WITH (slot_name=name) で変更可能

Download URL: https://goo.gl/MUYuE3

アーキテクチャpgoutputプラグイン

28

–テキスト・フォーマット

– 文字コード変換

– バイナリーからテキスト変換

– Protocol

– URL

–更新対象列を特定するデータと更新後のデータが送信

– Replica Identity

– pgoutputプラグイン

– Logical Replication用の標準プラグイン

– wal senderプロセスから利用

– WALからLogical Replicationメッセージを作成

Download URL: https://goo.gl/MUYuE3

アーキテクチャメッセージ

29

–テキスト・フォーマット

– テキスト変換時にエラーが発生する可能性

– Protocol

– URL

– Replica Identityの話

–更新対象列を特定するデータと更新後のデータが送信

– Replica Identity

Download URL: https://goo.gl/MUYuE3

アーキテクチャReplica Identity

30

比較項目 PUBLICATION SUBSCRIPTION

PRIMARY KEY O O

REPLICA IDENTITY FULL O

REPLICA IDENTITY +

UNIQUE + NOT NULLO O

–主キーまたはREPLICA IDENTITY設定が無いテーブルに対するUPDATE / DELETEはSQL実行エラー

postgres=> UPDATE data1 SET c2='update' WHERE c1=100 ;ERROR: cannot update table "data1" because it does not have replica identity and publishes updatesHINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.

Download URL: https://goo.gl/MUYuE3

アーキテクチャDML実行時の動作(非同期)

31

①COMMIT ③SIGUSR1 ④To Slavewal

senderbackend

②WAL

④COMMIT

pgoutput

Download URL: https://goo.gl/MUYuE3

アーキテクチャDML実行時の動作(同期)

32

①COMMIT ③SIGUSR1 ④To Slavewal

senderbackend

②WAL

④SIGUSR1⑤COMMIT

pgoutput

Download URL: https://goo.gl/MUYuE3

アーキテクチャstatement_timeout

33

– Sub error

LOG: terminating walsender process due to replication timeoutLOG: starting logical decoding for slot "sub1"DETAIL: streaming transactions committing after 0/5600ED48, reading WAL from 0/5600ED10LOG: logical decoding found consistent point at 0/5600ED102017-11-09 16:01:00.454 JST [7294] DETAIL: There are no running transactions.

ERROR: could not send data to WAL stream: server closed the connection unexpectedly

This probably means the server terminated abnormally

before or while processing the request.LOG: worker process: logical replication worker for subscription 16497 (PID 6911) exited with exit code 1LOG: logical replication apply worker for subscription "sub1" has

– SUBSCRIPTION側テーブルを LOCK TABLEでロックして検証

– wal_sender_timeout のエラーかも

– Pub error

Download URL: https://goo.gl/MUYuE3

アーキテクチャストレージ

34

–論理ログ

– wal senderプロセスがwal writerプロセスからSIGUSR1シグナルを受信すると作成

– WALファイルの読み込みを行いデータを作成

– トランザクション単位で作成?

${PGDATA}/pg_logical/snapshots/{LSN上位}-{LSN下位}.snap

${PGDATA}/pg_replslot/{SLOT_NAME}/xid-{XID}-lsn-{LSN上位}-{LSN下位}.snap

–スレーブ遅延時の論理ログ

– スレーブ・インスタンスが停止し、wal senderが再起動された場合に作成

– 転送が完了すると削除される

Download URL: https://goo.gl/MUYuE3

アーキテクチャ内部動作(Snapshot)

35

③Logical Log

④Replication Slot stat

WAL sender

②WAL

①WAL writer

Download URL: https://goo.gl/MUYuE3

アーキテクチャ内部動作

36

FROM wal sender

WAL

Logical Replication

Worker

Download URL: https://goo.gl/MUYuE3

設定方法同期レプリケーションの設定方法

37

–デフォルト設定では非同期になる

– Master synchronous_standby_names

– Slave CREATE SUBSCRIPTION CONNECTION ‘application_name = ap’

– WITH (SYNCHRONOUS_COMMIT = on)

– Application_name = subscription_name

Download URL: https://goo.gl/MUYuE3

アーキテクチャログ

38

– PUBLICATION DOWN

– ERROR: could not receive data from WAL stream: server closed the connection unexpectedly

– This probably means the server terminated abnormally

– before or while processing the request.

– LOG: worker process: logical replication worker for subscription 16458 (PID 10356) exited with exit code 1

– LOG: logical replication apply worker for subscription "subt1" has started

– WARNING: terminating connection because of crash of another server process

– DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.

– HINT: In a moment you should be able to reconnect to the database and repeat your command.

– ERROR: could not receive data from WAL stream: server closed the connection unexpectedly

– This probably means the server terminated abnormally

– before or while processing the request.

– LOG: worker process: logical replication worker for subscription 16392 (PID 10354) exited with exit code 1

– ERROR: could not connect to the publisher: FATAL: the database system is in recovery mode

– LOG: worker process: logical replication worker for subscription 16458 (PID 10369) exited with exit code 1

– LOG: logical replication apply worker for subscription "sub1" has started

制約

39

Download URL: https://goo.gl/MUYuE3

制約レプリケーションできないSQL

40

– TRUNCATE

– ALTER TABLE

Download URL: https://goo.gl/MUYuE3

制約レプリケーションできないオブジェクト

41

– PUBLICATIONに追加できるのはテーブル(pg_class.relkind='r')のみ

–レプリケーション対象外

– MATERIALIZED VIEW

– INDEX

– SEQUENCE

– FOREIGN TABLE

– UNLOGGED TABLE

– INHERIT TABLE = OK (ONLY句)

– Partition parent table

– Large Object

– User Defined Type?

– Array

– Json

Download URL: https://goo.gl/MUYuE3

制約SRIAL / GENERATED AS IDENTITY

42

– SERIAL列は内部的にSEQUENCEを使用

–シーケンス値が転送されるが、シーケンスの操作は行われない

Download URL: https://goo.gl/MUYuE3

制約トリガー

43

–一部だけ実行される

– ROW TRIGGERのみ

– STATEMENT TRIGGERは初期データ移行時のみ実行

– SUB側

– ALTER TABLE ENABLE ALWAYS | REPLICA TRIGGER文が必要

– Bug?

– BEFORE ROW DELETEトリガーが発行されない

– https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=360fd1a7b2fe779cc9e696b813b12f6a8e83b558

– 10.1でFIX

– The logical replication apply process currently only fires row triggers, not statement triggers. The initial table synchronization, however, is implemented like a COPY command and thus fires both row and statement triggers for <command>INSERT</command>.

統計情報は?

Download URL: https://goo.gl/MUYuE3

制約パラメーターlog_statement

44

– SQLを再実行しているわけではないので、log_statement = ‘all’にしてもレプリケーションの更新ログは出ない。

Download URL: https://goo.gl/MUYuE3

制約双方向レプリケーション

45

–同一テーブルを使った双方向レプリケーション不可

– 設定はできるがWALが循環してエラーになる

– テーブルが別々であればデータベース間の相互レプリケーションは可能

–ログ

– pg_resetlowalすると?

– pg_replication_origin_advanceでは解消できなかった

– ALTER SUB REFRESHでは?

Download URL: https://goo.gl/MUYuE3

制約インスタンス内レプリケーション

46

subdb=# CREATE SUBSCRIPTION sub1 CONNECTION 'dbname=pubdb' WITH (CONNECT=off) ;

–インスタンス内レプリケーションには注意が必要

– 単純に環境を作ろうとすると、CREATE SUBSCRIPTION文がハング

– SUBSCRIPTIONとReplication Slotを別々に作成する必要がある

pubdb=# SELECT

pg_create_logical_replication_slot ('sub1', 'pgoutput') ;

– SUBSCRIPTIONの作成

– Logical Replication Slotの作成

Download URL: https://goo.gl/MUYuE3

制約Streaming Replicationとの組み合わせ

47

– Streaming Replication環境との混在可能

–スタンバイ・インスタンスからのLogical Replication不可

– Replication SlotはStreaming Replication対象外

– Logical Replication Slotはスタンバイ・インスタンスでは作成不可

– Logical Decodingはスタンバイ・インスタンスでは実行不可

–スイッチ・オーバーを検知できない

– PUBLICATION側でpg_create_logical_replication_slot実行

– ALTER SUBSCRIPTION CONNECTIONで接続先変更

Download URL: https://goo.gl/MUYuE3

制約Streaming Replicationとの組み合わせ

48

–スレーブ・インスタンス上で

– CREATE PUBLICATION文はエラー(ERROR: cannot execute CREATE PUBLICATION in a read-only transaction)

– CREATE SUBSCRIPTION文はエラー(同上)

– マスターで作成したPUBLICATION/SUBSCRIPTIONは伝播する

–スレーブ・インスタンスをLogi Repのマスターに

– CREATE SUBSCRIPTION文はエラー(ERROR: could not create replication slot “sub1”: ERROR: logical decoding cannot be used while in recovery)

–マスター上に作成されたレプリケーション・スロット

– ストリーミング・レプリケーションのスレーブには伝播しない

Download URL: https://goo.gl/MUYuE3

制約***

49

–カスケード構成OK

–文字コードの変換はOK

– ただし文字集合には要注意

–異なるOSでもOK

– LOB

– INHERIT / PARTITION

– master列追加/削除後もslave列追加/削除すればレプリケーションは継続?

トラブルシューティング

50

Download URL: https://goo.gl/MUYuE3

トラブルシューティングリソース不足のログ

51

– max_replication_slots不足(PUBLICATION)

ERROR: could not create replication slot "sub1": ERROR: all replication slots are in use

– max_wal_senders不足(PUBLICATION)

ERROR: could not create replication slot "sub1": ERROR: all replication slots are in use

– max_logical_replication_workers不足(SUBSCRIPTION)

WARNING: out of logical replication worker slotsHINT: You might need to increase max_logical_replication_workers.

– max_worker_processes不足(SUBSCRIPTION)

ログ無し

Download URL: https://goo.gl/MUYuE3

トラブルシューティングその他

52

–初期データコピー時の権限不足(PUBLICATION)

ERROR: could not start initial contents copy for table "public.data1": ERROR: permission denied for relation data1

– DROP SUBSCRIPTION文の実行(正常)

FATAL: terminating logical replication worker due to administrator commandLOG: worker process: logical replication worker for subscription 16408 (PID 77332) exited with exit code 1

Download URL: https://goo.gl/MUYuE3

トラブルシューティング衝突パターンと動作

53

衝突パターン レプリケーション動作 ログ出力

主キー違反/一意キー違反 停止 あり

CHECK制約違反 停止 あり

WAL変換エラー 停止 あり

更新データが存在しない 継続 なし

削除データが存在しない 継続 なし

テーブルが存在しない 停止 あり

一部の列が存在しない 停止 あり

データ型の変換エラー 停止 あり

テーブル・ロック 待機 なし

更新対象レコード・ロック 待機 なし

– SUBSCRIPTION側で発生する衝突と動作

Download URL: https://goo.gl/MUYuE3

トラブルシューティングエラー発生時の動作とログ

54

–エラー発生時の動作

– 制約違反を検知すると、logical replication workerプロセス停止

– 5秒後に再起動し、ログ適用を再開

– 制約違反が解消するまで上記を繰り返し

– SUBSCRIPTION側のログ

ERROR: duplicate key value violates unique constraint "pk_data1"DETAIL: Key (c1)=(500) already exists.LOG: worker process: logical replication worker for subscription 16414 (PID 9644) exited with exit code 1

LOG: starting logical decoding for slot "sub1"DETAIL: streaming transactions committing after 0/164FED0, reading WAL from 0/164FE98LOG: logical decoding found consistent point at 0/164FE98There are no running transactions.

– PUBLICATION側のログ(レプリケーションの再開)

Download URL: https://goo.gl/MUYuE3

トラブルシューティングテキスト変換時のメモリー不足エラー

55

– SUBSCRIPTIONログ

ERROR: invalid memory alloc request size 258291203CONTEXT: slot "sub1", output plugin "pgoutput", in the change callback, associated LSN 0/2B2543E8LOG: could not send data to client: Broken pipe FATAL: connection to client lost

ERROR: could not receive data from WAL stream: ERROR: invalid memory alloc request size 258291203CONTEXT: slot "sub1", output plugin "pgoutput", in the change callback, associated LSN 0/2B2543E8

– bytea型データの転送時に発生

– パラメーターbytea_outputに応じてテキスト変換

– デフォルトでは「レコード・サイズ x 2 + 1」バイトのメモリーを確保

– PUBLICATIONログ

Download URL: https://goo.gl/MUYuE3

トラブルシューティング衝突解消方法

56

–自動的にエラーは解消しない

–解消方法は以下の2つ(SUBSCRIPTION側で行う)

– 衝突が発生したレコードを削除する(制約違反の解消)

– 衝突が発生したWALをスキップする(制約違反の解消/メモリー不足の解消)

Download URL: https://goo.gl/MUYuE3

トラブルシューティング衝突解消方法

57

–衝突が発生したトランザクション・ログをスキップする

– SUBSCRIPTIONでWALの適用を開始するLSNを指定する

postgres=# SELECT pg_current_wal_lsn() ; pg_current_wal_lsn--------------------0/7200B4F0

(1 row)

– PUBLICATION側で現在のLSNを確認

Download URL: https://goo.gl/MUYuE3

衝突の検知と対策衝突解消方法

58

– SUBSCRIPTION側で適用開始LSNを指定

postgres=# SELECT pg_replication_origin_advance ('pg_16425', '0/7200B4F0') ;

pg_replication_origin_advance-------------------------------

(1 row)

postgres=# SELECT * FROM pg_replication_origin_status;local_id | external_id | remote_lsn | local_lsn----------+-------------+------------+-----------

1 | pg_16425 | 0/7200B068 | 0/DA0078E8 (1 row)

– SUBSCRIPTION側でexternal_idを確認

Subscriptionとexternal_idの関係を入れる

Download URL: https://goo.gl/MUYuE3

衝突の検知と対策衝突解消方法

59

postgres=# SELECT pg_replication_origin_advance('pg_16399', '0/82708760') ;ERROR: replication origin with OID 1 is already active for PID 5566

–エラーになることがある

今後に期待

60

Download URL: https://goo.gl/MUYuE3

今後に期待将来のバージョンに対する期待

61

– TRUNCATE文対応

–衝突発生時の優先順位付け

– pg_stat_subscriptionビューのconnection列のマスキング

– CREATE SUBSCRIPTION文と接続情報の分離

– 例えばCREATE SERVER文との組み合わせ

– WAL BufferからWAL情報を取得できないか?

– 現状では書き込まれたWALをwal senderが再度読んでいる

– Replication SlotとPluginの分離

– デコードは本来はスレーブ側の仕事では?

–スタンバイ・インスタンスでデコード

– https://commitfest.postgresql.org/15/788/

Thank younoriyoshi.shinoda@hpe.com

62

仕様

63

このスライド以降は今のところ使わない予定

Download URL: https://goo.gl/MUYuE3

要調査衝突解消方法

64

– wal_level = replica にした場合は?

– Wal formatの違い

– Slaveに PKが無い場合は?

– UPDATE / DELETEは伝播しない

– UNIQUE + NOT NULL があってもSlaveにREPLICA IDENTITY USING INDEXが無いとINSERTもエラー?

– MasterにPkが無い場合は?

–異なるOS

– Wal sender -> receive のプロトコル

Download URL: https://goo.gl/MUYuE3

レプリケーション構成PUBLICATION

65

–レプリケーション対象テーブルを決定する

–マスター側データベースに作成(CREATE PUBLICATION文)

–特徴

– データベースに対してCREATE権限が必要(一部操作はSUPERUSER属性)

– レプリケーション対象テーブルの所有者のみ作成可能

– データベース内に複数作成可能

– テーブルの指定は、個別に行うかデータベース内の全テーブルを選択

– レプリケーション対象DMLを選択(INSERT, DELETE, UPDATE)

Download URL: https://goo.gl/MUYuE3

レプリケーション構成SUBSCRIPTION

66

–接続先インスタンスとPUBLICATIONを決定する

–スレーブ側データベースに作成(CREATE SUBSCRIPTION文)

–特徴

– データベース内に複数作成可能

– SUPERUSER属性が必要

– マスター・インスタンス側のLogical Replication Slotとセット

– 初期データの移行も行うことができる

– 初期データの移行時は、SUBSCRIPTION側の既存データは削除されない

– 自動的にテーブルを作る機能は無い

Download URL: https://goo.gl/MUYuE3

DatabaseDatabase

レプリケーション構成PUBLICATIONとSUBSCRIPTIONの関係

67

PUBLICATION SUBSCRIPTION

TABLE#1

TABLE#2

TABLE#1

TABLE#2

– PUBLICATIONには複数のテーブルを登録可能

Download URL: https://goo.gl/MUYuE3

Database

DatabaseDatabase

レプリケーション構成PUBLICATIONとSUBSCRIPTIONの関係

68

PUBLICATION#1

TABLE#1

PUBLICATION#2

–テーブルは複数のPUBLICATIONに登録可能

SUBSCRIPTION#1

SUBSCRIPTION#2

TABLE#1

TABLE#1

Download URL: https://goo.gl/MUYuE3

レプリケーション構成PUBLICATIONとSUBSCRIPTIONの関係

69

– PUBRICATIONは複数のSUBSCRIPTIONにログ送付可能

Database Database

SUBSCRIPTION#1

TABLE#1 TABLE#1

Database

SUBSCRIPTION#2

TABLE#1

PUBLICATION#1

Download URL: https://goo.gl/MUYuE3

DatabaseDatabase

レプリケーション構成PUBLICATIONとSUBSCRIPTIONの関係

70

SUBSCRIPTION

– SUBSCRIPTIONは複数のPUBLICATIONからレプリケーション可能

TABLE#1

TABLE#2

TABLE#1

TABLE#2PUBLICATION#2

PUBLICATION#1

Download URL: https://goo.gl/MUYuE3

レプリケーション構成PUBLICATIONとSUBSCRIPTIONの関係

71

PUBLICATION

SUBSCRIPTION

PUBLICATION

–カスケード構成も可能

TABLE#1

TABLE#1

SUBSCRIPTION

TABLE#1

Download URL: https://goo.gl/MUYuE3

設定方法SUBSCRIPTIONオブジェクト

72

CREATE SUBSCRIPTION nameCONNECTION 'conn_info' PUBLICATION publication [, … ][ WITH ( parameter [ = value ] , … ]

–構文

パラメーター 説明 デフォルト

copy_data 初期データ移行を行うか on

create_slot レプリケーション・スロットを作成するか on

enabled SUBSCRIPTIONは有効 on

slot_name レプリケーション・スロット名SUBSCRIPTION

synchronous_commit 同期コミット設定 off

connect PUBLICATIONへ接続 on

Download URL: https://goo.gl/MUYuE3

設定方法オブジェクト確認方法

73

postgres=# SELECT * FROM pg_subscription ;postgres=# SELECT * FROM pg_subscription_rel ;

postgres=# SELECT * FROM pg_publication ;postgres=# SELECT * FROM pg_publication_rel ;postgres=# SELECT * FROM pg_publication_tables ;

– SUBSCRIPTION側

– PUBLICATION側

Download URL: https://goo.gl/MUYuE3

設定方法レプリケーション確認方法

74

postgres=# SELECT pid, application_name, state, sync_state FROM pg_stat_replication;-[ RECORD 1 ]----+----------pid | 5731application_name | sub1state | streamingsync_state | async

– PUBLICATION側

postgres=# SELECT * FROM pg_stat_subscription

Download URL: https://goo.gl/MUYuE3

設定方法レプリケーション確認方法

75

postgres=# SELECT * FROM pg_subscription;-[ RECORD 1 ]---+---------------------------------------------------------------------------------subdbid | 16385subname | sub1subowner | 10subenabled | tsubconninfo | port=5433 dbname=demodb host=192.168.1.101 user=replication password=replicationsubslotname | sub1subsynccommit | offsubpublications | {pub1}

– SUBSCRIPTION側

Recommended