简体   繁体   English

postgres:在插入之前删除需要锁定?

[英]postgres: delete before insert requires lock?

I want to replace a list of records for a client.我想替换客户的记录列表。 It seems that postgres does not have a merge statement that handles my case I decided to do it as follows:似乎 postgres 没有处理我的情况的merge语句我决定按如下方式进行:

I delete all records of a client like so我像这样删除客户的所有记录

DELETE FROM SOME_TABLE WHERE SOME_TABLE.CLIENT_ID=?

and then insert some new records:然后插入一些新记录:

INSERT INTO SOME_TABLE (CLIENT_ID, ...) VALUES (1, ...), (2, ...)

I'm doing that in a transaction.我在交易中这样做。

My question: I know about write-after-read problems that require a form of synchronization (locks, or a certain level of transaction isolation).我的问题:我知道需要某种形式的同步(锁定或一定级别的事务隔离)的先读后写问题。

Is these one of these cases?这些是其中一种情况吗?

If you don't have too many clients, you could partition the table by client_id .如果您没有太多客户端,则可以按client_id对表进行分区。

Then replacing the data for a client could be as fast as it gets:然后为客户端替换数据可能会尽可能快:

BEGIN;
TRUNCATE some_table_partition_1;
COPY some_table_partition_1 (client_id, ...) FROM STDIN
   (FORMAT 'csv', FREEZE);
COMMIT;

The partition will automatically be locked for the duration of the operation.分区将在操作期间自动锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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