AntDB基于PGXC开发,保留了原PGXC的重分布逻辑,本文记录了常见的重分布流程。
集群环境
coordinator节点:cd1、cd2
datanode节点:dn1、dn2、dn3
案例1
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、复制表重分布成复制表。2、节点减少。 |
alter table t to node(dn1, dn3); |
R |
× |
× |
dn1、dn2、dn3 |
R |
× |
× |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : TRUNCATE public.t
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t to node(dn1, dn3);
cd1 -> dn2 : PREPARE TRANSACTION 'T1906'
cd1 -> cd2 : PREPARE TRANSACTION 'T1906'
cd1 -> dn2 : COMMIT PREPARED 'T1906'
cd1 -> cd2 : COMMIT PREPARED 'T1906'
案例2
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、复制表重分布成复制表。2、节点变化。 |
alter table t to node(dn1, dn2); |
R |
× |
× |
dn1、dn3 |
R |
× |
× |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : TRUNCATE public.t
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : COPY public.t FROM STDIN
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t to node(dn1, dn2);
cd1 -> dn1 : PREPARE TRANSACTION 'T1907'
cd1 -> dn3 : PREPARE TRANSACTION 'T1907'
cd1 -> dn2 : PREPARE TRANSACTION 'T1907'
cd1 -> cd2 : PREPARE TRANSACTION 'T1907'
cd1 -> dn1 : COMMIT PREPARED 'T1907'
cd1 -> dn3 : COMMIT PREPARED 'T1907'
cd1 -> dn2 : COMMIT PREPARED 'T1907'
cd1 -> cd2 : COMMIT PREPARED 'T1907'
案例3
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、复制表重分布成复制表。2、节点数增加。 |
alter table t to node(dn1, dn2, dn3); |
R |
× |
× |
dn1、dn2 |
R |
× |
× |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : COPY public.t FROM STDIN
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t to node(dn1, dn2, dn3)
cd1 -> dn1 : PREPARE TRANSACTION 'T1908'
cd1 -> dn3 : PREPARE TRANSACTION 'T1908'
cd1 -> cd2 : PREPARE TRANSACTION 'T1908'
cd1 -> dn1 : COMMIT PREPARED 'T1908'
cd1 -> dn3 : COMMIT PREPARED 'T1908'
cd1 -> cd2 : COMMIT PREPARED 'T1908'
案例4
概述
|
重分布前 |
重分布后 |
|
|
|
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、复制表重分布成哈希表。 |
alter table t distribute by hash(id); |
R |
× |
× |
dn1、dn2、dn3 |
H |
hash |
id |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : DELETE FROM public.t WHERE abs(hashint4(id)) % 3 != 0
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : DELETE FROM public.t WHERE id IS NULL OR abs(hashint4(id)) % 3 != 1
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : DELETE FROM public.t WHERE id IS NULL OR abs(hashint4(id)) % 3 != 2
cd1 -> dn1 : alter table t distribute by hash(id);
cd1 -> dn2 : alter table t distribute by hash(id);
cd1 -> dn3 : alter table t distribute by hash(id);
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t distribute by hash(id);
cd1 -> dn1 : PREPARE TRANSACTION 'T1909'
cd1 -> dn2 : PREPARE TRANSACTION 'T1909'
cd1 -> dn3 : PREPARE TRANSACTION 'T1909'
cd1 -> cd2 : PREPARE TRANSACTION 'T1909'
cd1 -> dn1 : COMMIT PREPARED 'T1909'
cd1 -> dn2 : COMMIT PREPARED 'T1909'
cd1 -> dn3 : COMMIT PREPARED 'T1909'
cd1 -> cd2 : COMMIT PREPARED 'T1909'
案例5
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、哈希表重分布成哈希表。2、节点减少。 |
alter table t to node(dn1, dn3); |
H |
hash |
id |
dn1、dn2、dn3 |
H |
hash |
id |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn2 : COPY public.t TO STDOUT
cd1 -> dn3 : COPY public.t TO STDOUT
cd1 -> dn1 : TRUNCATE public.t
cd1 -> dn2 : TRUNCATE public.t
cd1 -> dn3 : TRUNCATE public.t
cd1 -> dn1 : COPY public.t FROM STDIN
cd1 -> dn3 : COPY public.t FROM STDIN
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t to node(dn1, dn3);
cd1 -> dn1 : PREPARE TRANSACTION 'T1910'
cd1 -> dn2 : PREPARE TRANSACTION 'T1910'
cd1 -> dn3 : PREPARE TRANSACTION 'T1910'
cd1 -> cd2 : PREPARE TRANSACTION 'T1910'
cd1 -> dn1 : COMMIT PREPARED 'T1910'
cd1 -> dn2 : COMMIT PREPARED 'T1910'
cd1 -> dn3 : COMMIT PREPARED 'T1910'
cd1 -> cd2 : COMMIT PREPARED 'T1910'
案例6
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、哈希表重分布成哈希表。2、节点变化。 |
alter table t to node(dn1, dn2); |
H |
hash |
id |
dn1、dn3 |
H |
hash |
id |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn3 : COPY public.t TO STDOUT
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : TRUNCATE public.t
cd1 -> dn2 : TRUNCATE public.t
cd1 -> dn3 : TRUNCATE public.t
cd1 -> dn1 : COPY public.t FROM STDIN
cd1 -> dn2 : COPY public.t FROM STDIN
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t to node(dn1, dn2);
cd1 -> dn1 : PREPARE TRANSACTION 'T1911'
cd1 -> dn3 : PREPARE TRANSACTION 'T1911'
cd1 -> dn2 : PREPARE TRANSACTION 'T1911'
cd1 -> cd2 : PREPARE TRANSACTION 'T1911'
cd1 -> dn1 : COMMIT PREPARED 'T1911'
cd1 -> dn3 : COMMIT PREPARED 'T1911'
cd1 -> dn2 : COMMIT PREPARED 'T1911'
cd1 -> cd2 : COMMIT PREPARED 'T1911'
案例7
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、哈希表重分布成哈希表。2、节点增加。 |
alter table t to node(dn1, dn2, dn3); |
H |
hash |
id |
dn1、dn2 |
H |
hash |
id |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn2 : COPY public.t TO STDOUT
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : TRUNCATE public.t
cd1 -> dn2 : TRUNCATE public.t
cd1 -> dn3 : TRUNCATE public.t
cd1 -> dn1 : COPY public.t FROM STDIN
cd1 -> dn2 : COPY public.t FROM STDIN
cd1 -> dn3 : COPY public.t FROM STDIN
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t to node(dn1, dn2, dn3);
cd1 -> dn1 : PREPARE TRANSACTION 'T1912'
cd1 -> dn2 : PREPARE TRANSACTION 'T1912'
cd1 -> dn3 : PREPARE TRANSACTION 'T1912'
cd1 -> cd2 : PREPARE TRANSACTION 'T1912'
cd1 -> dn1 : COMMIT PREPARED 'T1912'
cd1 -> dn2 : COMMIT PREPARED 'T1912'
cd1 -> dn3 : COMMIT PREPARED 'T1912'
cd1 -> cd2 : COMMIT PREPARED 'T1912'
案例8
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、哈希表重分布成哈希表。2、分片键变化。 |
alter table t distribute by hash(value); |
H |
hash |
id |
dn1、dn2、dn3 |
H |
hash |
value |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn2 : COPY public.t TO STDOUT
cd1 -> dn3 : COPY public.t TO STDOUT
cd1 -> dn1 : TRUNCATE public.t
cd1 -> dn2 : TRUNCATE public.t
cd1 -> dn3 : TRUNCATE public.t
cd1 -> dn1 : COPY public.t FROM STDIN
cd1 -> dn2 : COPY public.t FROM STDIN
cd1 -> dn3 : COPY public.t FROM STDIN
cd1 -> dn1 : alter table t distribute by hash(value);
cd1 -> dn2 : alter table t distribute by hash(value);
cd1 -> dn3 : alter table t distribute by hash(value);
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t distribute by hash(value);
cd1 -> dn1 : PREPARE TRANSACTION 'T1913'
cd1 -> dn2 : PREPARE TRANSACTION 'T1913'
cd1 -> dn3 : PREPARE TRANSACTION 'T1913'
cd1 -> cd2 : PREPARE TRANSACTION 'T1913'
cd1 -> dn1 : COMMIT PREPARED 'T1913'
cd1 -> dn2 : COMMIT PREPARED 'T1913'
cd1 -> dn3 : COMMIT PREPARED 'T1913'
cd1 -> cd2 : COMMIT PREPARED 'T1913'
案例9
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、哈希表重分布成自定义分片表。2、分片函数变化。 |
alter table t distribute by dist(id, value); |
H |
hash |
id |
dn1、dn2、dn3 |
U |
dist |
idvalue |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn2 : COPY public.t TO STDOUT
cd1 -> dn3 : COPY public.t TO STDOUT
cd1 -> dn1 : TRUNCATE public.t
cd1 -> dn2 : TRUNCATE public.t
cd1 -> dn3 : TRUNCATE public.t
cd1 -> dn1 : COPY public.t FROM STDIN
cd1 -> dn2 : COPY public.t FROM STDIN
cd1 -> dn3 : COPY public.t FROM STDIN
cd1 -> dn1 : alter table t distribute by dist(id, value);
cd1 -> dn2 : alter table t distribute by dist(id, value);
cd1 -> dn3 : alter table t distribute by dist(id, value);
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t distribute by dist(id, value);
cd1 -> dn1 : PREPARE TRANSACTION 'T1915'
cd1 -> dn2 : PREPARE TRANSACTION 'T1915'
cd1 -> dn3 : PREPARE TRANSACTION 'T1915'
cd1 -> cd2 : PREPARE TRANSACTION 'T1915'
cd1 -> dn1 : COMMIT PREPARED 'T1915'
cd1 -> dn2 : COMMIT PREPARED 'T1915'
cd1 -> dn3 : COMMIT PREPARED 'T1915'
cd1 -> cd2 : COMMIT PREPARED 'T1915'
案例10
概述
|
重分布前 |
|
|
|
重分布后 |
|
|
|
重分布CASE |
重分布SQL |
分片方式 |
分片函数 |
分片字段 |
分片节点 |
分片方式 |
分片函数 |
分片字段 |
1、自定义分片表重分布成复制表。 |
alter table t distribute by replication; |
U |
dist |
idvalue |
dn1、dn2、dn3 |
R |
× |
× |
重分布流程
cd1 -> cd2 : SELECT pg_catalog.pg_try_advisory_xact_lock_shared(65535, 65535)
cd1 -> dn1 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn3 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> dn1 : COPY public.t TO STDOUT
cd1 -> dn2 : COPY public.t TO STDOUT
cd1 -> dn3 : COPY public.t TO STDOUT
cd1 -> dn1 : TRUNCATE public.t
cd1 -> dn2 : TRUNCATE public.t
cd1 -> dn3 : TRUNCATE public.t
cd1 -> dn1 : COPY public.t FROM STDIN
cd1 -> dn2 : COPY public.t FROM STDIN
cd1 -> dn3 : COPY public.t FROM STDIN
cd1 -> dn1 : alter table t distribute by replication;
cd1 -> dn2 : alter table t distribute by replication;
cd1 -> dn3 : alter table t distribute by replication;
cd1 -> cd2 : START TRANSACTION ISOLATION LEVEL read committed READ WRITE
cd1 -> cd2 : alter table t distribute by replication;
cd1 -> dn1 : PREPARE TRANSACTION 'T1916'
cd1 -> dn2 : PREPARE TRANSACTION 'T1916'
cd1 -> dn3 : PREPARE TRANSACTION 'T1916'
cd1 -> cd2 : PREPARE TRANSACTION 'T1916'
cd1 -> dn1 : COMMIT PREPARED 'T1916'
cd1 -> dn2 : COMMIT PREPARED 'T1916'
cd1 -> dn3 : COMMIT PREPARED 'T1916'
cd1 -> cd2 : COMMIT PREPARED 'T1916'