简体   繁体   English

Bucardo自定义复制逻辑

[英]Bucardo custom replication logic

I have a question regarding the Bucardo capabilities in Postgre SQL. 我对Postgre SQL中的Bucardo功能有疑问。 Bucardo puts in sync tables between several databases. Bucardo在多个数据库之间放入同步表。 Imagine we have table Orders in DB1 and DB2. 假设我们在DB1和DB2中有表Orders。

create table orders(order_id integer primary key, item_id integer, quantity integer); 

And we're making changes to Orders table in DB1. 而且我们正在对DB1中的Orders表进行更改。

insert into orders(item_id,quantity) values(1,235);

Then Bucardo replicates all these changes to Orders table in DB2. 然后,Bucardo将所有这些更改复制到DB2中的Orders表。 But apart from this sync I want Bucardo to modify table Stock in DB2 但是除了这种同步之外,我还希望Bucardo修改DB2中的表Stock

create table stock(item_id integer primary key, name varchar(50), quantity integer);

Just to decrease the quantity field of stock table in DB2 by the value 235 (the value inserted to quantity field of orders table in DB1) for the record with item_id = 1. Is it possible to customize the Bucardo in this way? 只是将item_id = 1的记录的DB2中库存表的数量字段减少235(DB1中插入到订单表的数量字段中的值),是否可以通过这种方式自定义Bucardo? And what is the best way to implement this functionality? 实施此功能的最佳方法是什么?

This will be hard to achieve as bucardo doesn't replicate based on SQL statements. 由于bucardo不会基于SQL语句进行复制,因此这将很难实现。 You can however use stored procedures on the primary which is DB1 and also replicate that table. 但是,您可以在主数据库DB1上使用存储过程,也可以复制该表。

create or replace function stock_func() returns trigger as $$
DECLARE
    val int;
BEGIN
    val := NEW.quantity #do you addition or subtraction here
    #add your update statement below 
    execute 'UPDATE stock set .....;
    return NULL;
END;
$$ language plpgsql;

 create trigger update_stock before insert on address for each row execute procedure stock_func();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM