-- A and B
CREATEEXTENSIONspock;-- A
SELECTspock.node_create('node_a','host=127.0.0.1 port=5541 dbname=lab user=postgres');-- B
SELECTspock.node_create('node_b','host=127.0.0.1 port=5542 dbname=lab user=postgres');
只在 A 填入初始行。在 B 上订阅 A,确认读到初始行,再让 A 订阅 B。反向订阅跳过初始复制。空来源列表让节点只发送本地产生的变更。
-- A
INSERTINTOpublic.accountsVALUES(1,'seed',100);-- B
SELECTspock.sub_create('from_a','host=127.0.0.1 port=5541 dbname=lab user=postgres',replication_sets:=ARRAY['default'],synchronize_structure:=false,synchronize_data:=true,forward_origins:='{}');SELECTspock.sub_wait_for_sync('from_a');SELECT*FROMpublic.accountsWHEREid=1;-- A
SELECTspock.sub_create('from_b','host=127.0.0.1 port=5542 dbname=lab user=postgres',replication_sets:=ARRAY['default'],synchronize_structure:=false,synchronize_data:=false,forward_origins:='{}');SELECTspock.sub_wait_for_sync('from_b');
验证两端都能写入
-- A
INSERTINTOpublic.accountsVALUES(2,'from_a',50);-- B
SELECT*FROMpublic.accountsWHEREid=2;-- B, after seeing (2, from_a, 50)
UPDATEpublic.accountsSETlabel='from_b',balance=balance+2WHEREid=2;-- A
SELECT*FROMpublic.accountsWHEREid=2;
每次读取都要等到目标行出现,再执行下一步。最终 A 应读到 (2,from_b,52)。分别查询 SELECT subscription_name, status FROM spock.sub_show_status(),两端状态都应为 replicating。本地提交可能先于对端看到新行。运行与监控