我已经使用可能对您有所帮助的视图完成了快速 POC。您必须将所有select
语句更改为 hit local_merge
,并且您的insert
andupdate
语句必须 hit local_changes
(尽管您可能已经这样做了)。
create table remote (
id serial primary key,
val varchar(20),
update_ts timestamp
);
create table local_changes (
id integer primary key,
val varchar(20),
update_ts timestamp
);
insert into remote (val, update_ts) values ('Hello', current_timestamp);
insert into remote (val, update_ts) values ('Hello, Europe', current_timestamp);
insert into local_changes values (2, 'Hello, World!', current_timestamp);
create or replace view local_merge as
select remote.id,
case
when remote.update_ts > local_changes.update_ts
or local_changes.update_ts is null
then remote.val
else local_changes.val
end as val,
case
when remote.update_ts > local_changes.update_ts
or local_changes.update_ts is null
then remote.update_ts
else local_changes.update_ts
end as update_ts
from remote left join local_changes
on remote.id = local_changes.id
;
-- example select
select * from local_merge where id = 2;