【发布时间】:2025-12-27 11:50:06
【问题描述】:
我有一个使用本地数据库和远程数据库进行同步的应用程序。本地数据库使用 SQLite,远程数据库我使用 postgres。我需要将数据从一个数据库移动到另一个数据库,并避免重复信息。
大概是我现在做的:
BEGIN; //remote database (start transaction)
SELECT * FROM local.queued TOP 1; //local database (select first queued element)
INSERT INTO remote.queued VALUES ( element ) //remote database (insert first queued element on remote database)
BEGIN; //local database (start transaction)
DELETE * FROM local.queued LIMIT 1; //local database (delete first queued element on local database)
END; //local database (finalize transaction local database)
END; //remote database (finalize transaction remote database)
这在大多数情况下都比较有效,但顺便说一下,在对程序进行硬重置后,我注意到数据记录被复制了。我相信这与交易完成有关。因为我使用了两种不同的技术,所以不可能使用 WAL 归档创建单个原子提交。
关于如何改进这个概念以避免重复条目的任何想法。
【问题讨论】:
-
你遇到过saga模式吗?
-
PostgreSQL 有 XA 驱动程序,但据我所知 SQLite 没有。两阶段提交在这里可能不是一个选项。
标签: sql database postgresql sqlite