26. 26
pg_hba.conf の設定
通常の Streaming Replication 用設定と同じで OK.
レプリケーション専用ユーザを使う場合には、別途レプリ
ケーション権限をもつユーザを作成しておく必要がある。
Logical Replication
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication postgres trust
この例では postgres ユーザで
接続してるけど、本当は
レプリケーション専用ユーザを
作る方がいいです。
58. 58
異なる構造間の Logical Replication
Logical Replication
列 A 列 B 列 C
AAA BBB CCC
複製元
列 C 列 B 列 A
CCC BBB AAA
複製先
列 A 列 B 列 C
AAA BBB CCC
複製元
列 A 列 B 列 C 列 X
AAA BBB CCC (null)
複製先
列 A 列 B 列 C
AAA BBB CCC
複製元
列 A 列 B
AAA BBB
複製先
62. 62
異バージョン間複製
PostgreSQL 10 と PostgreSQL 11 間での複製
Logical Replication
PostgreSQL 11 server (srv2)
PG11
DB クラスタ
PG10
DB クラスタ
全 DB 全 DB
PostgreSQL 10 server (srv1)
更新
操作
Logical
Replication
PG10
DB クラスタ
全 DB
PostgreSQL 10 server (srv2)
Physical
Replication
pg_upgrade
63. 63
異アーキテクチャ間複製
Linux 版と Windows 版との間の複製
Logical Replication
PostgreSQL 10 serve
PG10
DB クラスタ
PG10
DB クラスタ
全 DB 全 DB
PostgreSQL 10 server
更新
操作
Logical
Replication
74. 74
従来の定義方式
親テーブルの定義。
Declarative Partitioning
CREATE TABLE japan (
pref text,
city text,
data text
);
CREATE TABLE tokyo (
CHECK (pref IN (' 東京 '))
) INHERITS (japan);
CREATE TABLE kanagawa (
CHECK (pref IN (' 神奈川 '))
) INHERITS (japan);
CREATE TABLE shizuoka (
CHECK (pref IN (' 静岡 '))
) INHERITS (japan);
子テーブルの定義
To be continued
75. 75
従来の定義方式(つづき)
トリガ関数の定義
Declarative Partitioning
CREATE OR REPLACE FUNCTION pref_insert_trigger_func()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.pref = ' 東京 ') THEN
INSERT INTO tokyo VALUES (NEW.*);
ELSIF ( NEW.pref = ' 神奈川 ') THEN
INSERT INTO kanagawa VALUES (NEW.*);
ELSIF ( NEW.pref = ' 静岡 ') THEN
INSERT INTO shizuoka VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the pref_insert_trigger()
function!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
To be continued
78. 78
PostgreSQL 10 の定義方式
親テーブルの定義。
Declarative Partitioning
CREATE TABLE japan (
pref text,
city text,
data text
)
PARTITION BY LIST (pref);
CREATE TABLE tokyo PARTITION OF japan
FOR VALUES IN (' 東京 ');
CREATE TABLE kanagawa PARTITION OF japan
FOR VALUES IN (' 神奈川 ');
CREATE TABLE shizuoka PARTITION OF japan
FOR VALUES IN (' 静岡 ');
子テーブルの定義
これだけで完了! ( レンジパーティションもだいたい同じ)
80. 80
多段パーティションの定義例
親テーブルの定義。
Declarative Partitioning
CREATE TABLE japan (
pref text,
city text,
data text
)
PARTITION BY LIST (pref);
CREATE TABLE tokyo PARTITION OF japan
FOR VALUES IN (' 東京 ');
CREATE TABLE kanagawa PARTITION OF japan
FOR VALUES IN (' 神奈川 ')
PARTITION BY LIST (city);
CREATE TABLE shizuoka PARTITION OF japan
FOR VALUES IN (' 静岡 ');
子テーブルの定義
これだけで完了!
CREATE TABLE "kanagawa.yokohama"
PARTITION OF kanagawa
FOR VALUES IN (' 横浜 ');
CREATE TABLE "kanagawa.kawasaki"
PARTITION OF kanagawa
FOR VALUES IN (' 川崎 ');
CREATE TABLE "kanagawa.machida"
PARTITION OF kanagawa
FOR VALUES IN (' 町田 ');
81. 81
パーティション状態の確認
親テーブルの確認
Declarative Partitioning
CREATE TABLE japan (
pref tpartition=# d+ japan
Table "public.japan"
Column | Type | Collation | Nullable | Default | Storage | Stats target |
Description
--------+------+-----------+----------+---------+----------+--------------
+-------------
pref | text | | | | extended | |
city | text | | | | extended | |
data | text | | | | extended | |
Partition key: LIST (pref)
Partitions: kanagawa FOR VALUES IN (' 神奈川 '),
shizuoka FOR VALUES IN (' 静岡 '),
tokyo FOR VALUES IN (' 東京 ')
パーティション種別とキーを表示してくれる。
子テーブルとパーティションキー値を表示してくれる。
82. 82
パーティション状態の確認
子テーブルの確認
Declarative Partitioning
partition=# d+ kanagawa
Table "public.kanagawa"
Column | Type | Collation | Nullable | Default | Storage | Stats target |
Description
--------+------+-----------+----------+---------+----------+--------------
+-------------
pref | text | | | | extended | |
city | text | | | | extended | |
data | text | | | | extended | |
Partition of: japan FOR VALUES IN (' 神奈川 ')
所属する親テーブルと、パーティションキー値を表示して
くれる。
83. 83
パーティションキー重複のチェック
Declarative Partitioning
partition=# d+ japan
Table "public.japan"
Column | Type | Collation | Nullable | Default | Storage | Stats target |
Description
--------+------+-----------+----------+---------+----------+--------------
+-------------
pref | text | | | | extended | |
city | text | | | | extended | |
data | text | | | | extended | |
Partition key: LIST (pref)
Partitions: kanagawa FOR VALUES IN (' 神奈川 '),
shizuoka FOR VALUES IN (' 静岡 '),
tokyo FOR VALUES IN (' 東京 ')
partition=# CREATE TABLE kanagawa2 PARTITION OF japan FOR VALUES IN (' 神奈川 ');
ERROR: partition "kanagawa2" would overlap partition "kanagawa"
パーティションキーの重複を検知してエラーにする。
93. 93
パーティション間更新の制約
代替手段として、 DELETE→INSERT する必要がある。
Declarative Partitioning
To be continued
partition=# SELECT * FROM japan WHERE id = 6;
id | pref | city | data
----+--------+------+--------
6 | 神奈川 | 町田 | 竹の助
(1 row)
partition=# BEGIN;
BEGIN
partition=# DELETE FROM japan WHERE id = 6;
DELETE 1
partition=# INSERT INTO japan VALUES (6, ' 東京 ', ' 町田 ', ' 竹の助 ');
INSERT 0 1
partition=# COMMIT;
COMMIT
partition=# SELECT * FROM japan WHERE id = 6;
id | pref | city | data
----+------+------+--------
6 | 東京 | 町田 | 竹の助
(1 row)
94. 94
パーティション間更新の制約
RETURNING 句を使って 1 つのクエリにすることも可能
Declarative Partitioning
partition=# SELECT * FROM japan WHERE id = 6;
id | pref | city | data
----+--------+------+--------
6 | 神奈川 | 町田 | 竹の助
(1 row)
partition=# WITH tmp AS
(DELETE FROM japan WHERE id = 6 RETURNING id, ' 東京 ', city, data)
INSERT INTO japan SELECT * FROM tmp;
INSERT 0 1
partition=# SELECT * FROM japan WHERE id = 6;
id | pref | city | data
----+------+------+--------
6 | 東京 | 町田 | 竹の助
(1 row)
ぐぬぬ・・・
町田は東京に奪われました
95. 95
インデックス関連の制約
パーティションの親テーブルには Primary key が設定でき
ない!
Declarative Partitioning
partition=# CREATE TABLE japan (
id integer primary key,
pref text,
city text,
data text
)
PARTITION BY LIST (pref);
ERROR: primary key constraints are not supported on partitioned tables
LINE 2: id integer primary key,
^
To be continued
96. 96
インデックス関連の制約
子テーブルを持つテーブルにはインデックスが作成できない。
Declarative Partitioning
partition=# d japan
Table "public.japan"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
pref | text | | |
city | text | | |
data | text | | |
Partition key: LIST (pref)
Number of partitions: 3 (Use d+ to list them.)
partition=# CREATE INDEX japan_id ON japan USING btree (id);
ERROR: cannot create index on partitioned table "japan"
親テーブルにインデックス設定したら、子テーブルに自動的に
伝播してくれればいいのになあ、とも思う・・・。