简体   繁体   English

Postgres如何从“插入”的收件人表中删除记录

[英]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.

相关问题 如何从表的每个组中删除90%的记录(postgres) - How to delete 90% of records from each group of a table (postgres) 如何从 Postgres 中的 CTE(公用表表达式)中删除记录 - How to delete records from CTE (Common Table Expression) in Postgres 将记录从一个表插入到另一个表,然后删除插入的记录 - Insert records from one table to another and then delete inserted records Postgres 从表中删除记录,保持最小值和最大值 - Postgres Delete records from table keeping a minimum and maximum value 我如何从 postgres 的表中删除超过 90 天的记录 - how do i delete records older than 90 days from a table in postgres 如何将数据从表插入到 postgres 列 - How to insert data from a table to postgres column 如何在postgres中将唯一行从一个表插入到另一个表? - How to insert unique rows from one table to another table in postgres? 如何基于Postgres中的日期从表中检索记录 - How to retrieve records from a table based on date in postgres 如何从Postgres中的另一个表更新记录 - How to update records based from another table in Postgres 在 postgres 的多个订单表中,无法从表(订单)中删除其 id 列作为外键的记录 - Not able to delete records from table(order) having its id column as foreign key in multiple order tables in postgres
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM