Answer the question
In order to leave comments, you need to log in
Adding and updating records via Insert Into?
Hello. Question to connoisseurs, both in terms of performance and correctness. For example, there is a table
ROLLBACK;
BEGIN;
CREATE TEMP TABLE test( id integer, number varchar(12), other varchar(10)) ON COMMIT DROP;
CREATE UNIQUE INDEX ON test( number );
insert into test values ( 1, '1', '');
select * from test;
commit;
select * from test where number = '1';
insert into test values ( '1', '');
update test set other = 'ttt' where number = '1';
insert into test values ( '1', 'tttt') ON CONFLICT (number) DO update SET other = EXCLUDED.other;
Answer the question
In order to leave comments, you need to log in
With insert or update on the result of select (by the way, a completely unnecessary request), you get into a race condition
on conflict. It is specially made for the normal serialization of what is happening and should be used. Other ways to serialize a race condition from times before on conflict squander performance for obvious reasons.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question