Implemented CRDTs#
現在、6つのCRDTデータ型が実装されています。
Grow専用のカウンターと合計
ポジティブ/ネガティブカウンタと合計
デルタカウンターとサム
カウンタと合計は、カウンタタイプが整数ベースbigint
であるのに対し、sumタイプが10進数ベースnumeric
であることを除き、ほとんど同じ動作をします。
次のクエリーを使用して、現在実装されているCRDTデータ型をリストできます。
SELECT n.nspname, t.typname
FROM bdr.crdt_handlers c
JOIN (pg_type t JOIN pg_namespace n ON t.typnamespace = n.oid)
ON t.oid = c.crdt_type_id;
Grow専用カウンター crdt_gcounter#
非負の値のインクリメントのみをサポート
value + intおよびcounter + bigint演算子。#演算子を使用するか、bigintにキャストすることにより、カウンターの現在値を取得できます。counter = valueのような単純な割り当てとは互換性がありません。これは、新しい値がアプリケーションのどこかで計算される場合の一般的なパターンです。!演算子counter = !counterを使用してカウンターを単純にリセットできます。crdt_gcounter_to_textを使用して内部状態を検査できます。
CREATE TABLE crdt_test (
id INT PRIMARY KEY,
cnt bdr.crdt_gcounter NOT NULL DEFAULT 0
);
INSERT INTO crdt_test VALUES (1, 0); -- initialized to 0
INSERT INTO crdt_test VALUES (2, 129824); -- initialized to 129824
INSERT INTO crdt_test VALUES (3, -4531); -- error: negative value
- - enable CLCD on the table
ALTER TABLE crdt_test REPLICA IDENTITY FULL;
SELECT bdr.alter_table_conflict_detection(crdt_test, column_modify_timestamp, cts);
- - increment counters
UPDATE crdt_test SET cnt = cnt + 1 WHERE id = 1;
UPDATE crdt_test SET cnt = cnt + 120 WHERE id = 2;
- - error: minus operator not defined
UPDATE crdt_test SET cnt = cnt - 1 WHERE id = 1;
- - error: increment has to be non-negative
UPDATE crdt_test SET cnt = cnt + (-1) WHERE id = 1;
- - reset counter
UPDATE crdt_test SET cnt = !cnt WHERE id = 1;
- - get current counter value
SELECT id, cnt::bigint, cnt FROM crdt_test;
- - show internal structure of counters
SELECT id, bdr.crdt_gcounter_to_text(cnt) FROM crdt_test;
Grow-only sum crdt_gsum#
負でない値のインクリメントのみをサポート
sum + numeric。#演算子を使用するか、numericにキャストすることにより、合計の現在値を取得できます。sum = valueのような単純な割り当てとは互換性がありません。これは、新しい値がアプリケーションのどこかで計算される場合の一般的なパターンです。!演算子sum = !sumを使用して合計を単純にリセットできます。crdt_gsum_to_textを使用して内部状態を検査できます。
CREATE TABLE crdt_test (
id INT PRIMARY KEY,
gsum bdr.crdt_gsum NOT NULL DEFAULT 0.0
);
INSERT INTO crdt_test VALUES (1, 0.0); -- initialized to 0
INSERT INTO crdt_test VALUES (2, 1298.24); -- initialized to 1298.24
INSERT INTO crdt_test VALUES (3, -45.31); -- error: negative value
- - enable CLCD on the table
ALTER TABLE crdt_test REPLICA IDENTITY FULL;
SELECT bdr.alter_table_conflict_detection(crdt_test, column_modify_timestamp, cts);
- - increment sum
UPDATE crdt_test SET gsum = gsum + 11.5 WHERE id = 1;
UPDATE crdt_test SET gsum = gsum + 120.33 WHERE id = 2;
- - error: minus operator not defined
UPDATE crdt_test SET gsum = gsum - 15.2 WHERE id = 1;
- - error: increment has to be non-negative
UPDATE crdt_test SET gsum = gsum + (-1.56) WHERE id = 1;
- - reset sum
UPDATE crdt_test SET gsum = !gsum WHERE id = 1;
- - get current sum value
SELECT id, gsum::numeric, gsum FROM crdt_test;
- - show internal structure of sums
SELECT id, bdr.crdt_gsum_to_text(gsum) FROM crdt_test;
ポジティブネガカウンター crdt_pncounter#
正と負の値の両方のインクリメントをサポート
counter + intおよびcounter + bigint演算子を介して。#演算子を使用するか、bigintにキャストすることにより、カウンターの現在値を取得できます。counter = valueのような単純な割り当てとは互換性がありません。これは、新しい値がアプリケーションのどこかで計算される場合の一般的なパターンです。!演算子counter = !counterを使用してカウンターを単純にリセットできます。crdt_pncounter_to_textを使用して内部状態を検査できます。
CREATE TABLE crdt_test (
id INT PRIMARY KEY,
cnt bdr.crdt_pncounter NOT NULL DEFAULT 0
);
INSERT INTO crdt_test VALUES (1, 0); -- initialized to 0
INSERT INTO crdt_test VALUES (2, 129824); -- initialized to 129824
INSERT INTO crdt_test VALUES (3, -4531); -- initialized to -4531
- - enable CLCD on the table
ALTER TABLE crdt_test REPLICA IDENTITY FULL;
SELECT bdr.alter_table_conflict_detection(crdt_test, column_modify_timestamp, cts);
- - increment counters
UPDATE crdt_test SET cnt = cnt + 1 WHERE id = 1;
UPDATE crdt_test SET cnt = cnt + 120 WHERE id = 2;
UPDATE crdt_test SET cnt = cnt + (-244) WHERE id = 3;
- - decrement counters
UPDATE crdt_test SET cnt = cnt - 73 WHERE id = 1;
UPDATE crdt_test SET cnt = cnt - 19283 WHERE id = 2;
UPDATE crdt_test SET cnt = cnt - (-12) WHERE id = 3;
- - get current counter value
SELECT id, cnt::bigint, cnt FROM crdt_test;
- - show internal structure of counters
SELECT id, bdr.crdt_pncounter_to_text(cnt) FROM crdt_test;
- - reset counter
UPDATE crdt_test SET cnt = !cnt WHERE id = 1;
- - get current counter value after the reset
SELECT id, cnt::bigint, cnt FROM crdt_test;
正負の合計crdt_pnsum#
sum + numericを介して正と負の値の両方のインクリメントをサポートします。then
#演算子を使用するか、numericにキャストすることにより、合計の現在値を取得できます。sum = valueのような単純な割り当てとは互換性がありません。これは、新しい値がアプリケーションのどこかで計算される場合の一般的なパターンです。!演算子sum = !sumを使用して合計を単純にリセットできます。crdt_pnsum_to_textを使用して内部状態を検査できます。
CREATE TABLE crdt_test (
id INT PRIMARY KEY,
pnsum bdr.crdt_pnsum NOT NULL DEFAULT 0
);
INSERT INTO crdt_test VALUES (1, 0); -- initialized to 0
INSERT INTO crdt_test VALUES (2, 1298.24); -- initialized to 1298.24
INSERT INTO crdt_test VALUES (3, -45.31); -- initialized to -45.31
- - enable CLCD on the table
ALTER TABLE crdt_test REPLICA IDENTITY FULL;
SELECT bdr.alter_table_conflict_detection(crdt_test, column_modify_timestamp, cts);
- - increment sums
UPDATE crdt_test SET pnsum = pnsum + 1.44 WHERE id = 1;
UPDATE crdt_test SET pnsum = pnsum + 12.20 WHERE id = 2;
UPDATE crdt_test SET pnsum = pnsum + (-24.34) WHERE id = 3;
- - decrement sums
UPDATE crdt_test SET pnsum = pnsum - 7.3 WHERE id = 1;
UPDATE crdt_test SET pnsum = pnsum - 192.83 WHERE id = 2;
UPDATE crdt_test SET pnsum = pnsum - (-12.22) WHERE id = 3;
- - get current sum value
SELECT id, pnsum::numeric, pnsum FROM crdt_test;
- - show internal structure of sum
SELECT id, bdr.crdt_pnsum_to_text(pnsum) FROM crdt_test;
- - reset sum
UPDATE crdt_test SET pnsum = !pnsum WHERE id = 1;
- - get current sum value after the reset
SELECT id, pnsum::numeric, pnsum FROM crdt_test;
デルタカウンター crdt_delta_counter#
bigintドメインとして定義されているため、bigint列とまったく同様に動作します。正と負の値の両方のインクリメントをサポートします。
counter = valueのような単純な割り当てと互換性があります。これは、新しい値がアプリケーションのどこかで計算される場合に一般的です。値を確実にリセットする簡単な方法はありません。
CREATE TABLE crdt_test (
id INT PRIMARY KEY,
cnt bdr.crdt_delta_counter NOT NULL DEFAULT 0
);
INSERT INTO crdt_test VALUES (1, 0); -- initialized to 0
INSERT INTO crdt_test VALUES (2, 129824); -- initialized to 129824
INSERT INTO crdt_test VALUES (3, -4531); -- initialized to -4531
- - enable CLCD on the table
ALTER TABLE crdt_test REPLICA IDENTITY FULL;
SELECT bdr.alter_table_conflict_detection(crdt_test, column_modify_timestamp, cts);
- - increment counters
UPDATE crdt_test SET cnt = cnt + 1 WHERE id = 1;
UPDATE crdt_test SET cnt = cnt + 120 WHERE id = 2;
UPDATE crdt_test SET cnt = cnt + (-244) WHERE id = 3;
- - decrement counters
UPDATE crdt_test SET cnt = cnt - 73 WHERE id = 1;
UPDATE crdt_test SET cnt = cnt - 19283 WHERE id = 2;
UPDATE crdt_test SET cnt = cnt - (-12) WHERE id = 3;
- - get current counter value
SELECT id, cnt FROM crdt_test;
デルタサム crdt_delta_sum#
numericドメインとして定義されるため、numeric列とまったく同様に動作します。正と負の値の両方のインクリメントをサポートします。
sum = valueのような単純な割り当てと互換性があります。これは、新しい値がアプリケーションのどこかで計算される場合に一般的です。値を確実にリセットする簡単な方法はありません。
CREATE TABLE crdt_test (
id INT PRIMARY KEY,
dsum bdr.crdt_delta_sum NOT NULL DEFAULT 0
);
INSERT INTO crdt_test VALUES (1, 0); -- initialized to 0
INSERT INTO crdt_test VALUES (2, 129.824); -- initialized to 129824
INSERT INTO crdt_test VALUES (3, -4.531); -- initialized to -4531
- - enable CLCD on the table
ALTER TABLE crdt_test REPLICA IDENTITY FULL;
SELECT bdr.alter_table_conflict_detection(crdt_test, column_modify_timestamp, cts);
- - increment counters
UPDATE crdt_test SET dsum = dsum + 1.32 WHERE id = 1;
UPDATE crdt_test SET dsum = dsum + 12.01 WHERE id = 2;
UPDATE crdt_test SET dsum = dsum + (-2.4) WHERE id = 3;
- - decrement counters
UPDATE crdt_test SET dsum = dsum - 7.33 WHERE id = 1;
UPDATE crdt_test SET dsum = dsum - 19.83 WHERE id = 2;
UPDATE crdt_test SET dsum = dsum - (-1.2) WHERE id = 3;
- - get current counter value
SELECT id, cnt FROM crdt_test;