0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PostgreSQLのLogical Decodingを試す

Posted at

有効化手順

postgresql.confを書き換えて再起動します。

wal_level = logical			# minimal, replica, or logical
max_replication_slots = 10	# max number of replication slots

AzureのFlexible Serverでも設定可能です。

image.png

image.png

Logicalレプリケーション用のスロットを作ります。

SELECT * FROM pg_create_logical_replication_slot('rep_slot', 'test_decoding');

使い方

  • pg_logical_slot_get_changes 変更情報を取り出す。取ると消える
  • pg_logical_slot_peek_changes 変更情報を取り出さずにチェックする。消えない

引数は同じ

  1. 読み出し先の論理レプリケーション・スロットの名前
  2. 読み出しを終えるポイント(トランザクションログのIDであるLSN、もしくは行数で指定)
  3. 出力プラグインのオプションをkeyとvalueで指定

動作確認

2行INSERTした場合

INSERT INTO test (id, name) VALUES (7, '7');
INSERT INTO test (id, name) VALUES (8, '8');

image.png

複数行の一括更新の場合

テーブルに6件のレコードが入っている状態でUpdateを行う。

UPDATE test SET name = '1234'

行ごとのデータになった。

SELECT * FROM pg_logical_slot_get_changes('rep_slot', NULL, NULL);

image.png

カラム追加

ALTER TABLE test ADD COLUMN num bigint;

image.png

カラム追加。デフォルト値あり

ALTER TABLE test ADD COLUMN num_default_zero bigint DEFAULT 0 NOT NULL;

image.png

削除した場合

DELETE FROM test

image.png

便利なような、イレギュラー対応しずらいような…

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?