[英]Postgres How to Delete Records From the Recipient Table of "Insert Into"
I have table A and table B. Every five minutes I sync table A into Table B using我有表 A 和表 B。每五分钟我将表 A 同步到表 B 中
insert into B (col_1) select col_1 from A on conflict do update A.col_1 = B.col_1
This works great to keep B up to date with A. Except it's possible that A has a record, then it gets inserted into B, and then deleted from A.这可以很好地使 B 与 A 保持同步。除非 A 有记录,然后将其插入 B,然后从 A 中删除。
How can I ensure B does not contain any records that are not in A?如何确保 B 不包含任何不在 A 中的记录?
It would be better practice if you use logical replication https://www.postgresql.org/docs/current/logical-replication.html .如果您使用逻辑复制https://www.postgresql.org/docs/current/logical-replication.html会更好。 In your current state you could run:
在您当前的状态下,您可以运行:
delete from b where not exists (select col_1 from a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.