简体   繁体   中英

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:

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 .

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.

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