简体   繁体   中英

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

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.

How can I ensure B does not contain any records that are not in A?

It would be better practice if you use logical replication 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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