sql: unexpect 'duplicate key' error while executing 'UPSERT'
run UPSERT with 2 concurrence, gets an unexpect duplicate key error.
Step to repo:
- Create schema as follow:
CREATE TABLE tweet(
id STRING PRIMARY KEY NOT NULL,
url STRING,
author STRING,
author_level INT DEFAULT 0,
content BLOB,
source BLOB,
retweet INT DEFAULT 0,
comment INT DEFAULT 0,
approvers INT DEFAULT 0,
create_time TIMESTAMP,
delete_time TIMESTAMP,
update_time TIMESTAMP,
article_id STRING,
INDEX idx_article (article_id),
INDEX idx_update_time (update_time)
);
- Run the commands:
cat dat_1.txt | ./cockroach sql --database=kg_ugc_weibo > 1.log 2>&1 &
cat dat_2.txt | ./cockroach sql --database=kg_ugc_weibo > 2.log 2>&1 &
Expect: commands run success without any errors.
Actual Result:
command 1 exits because of unexpect duplicate key.
pq: duplicate key value (update_time)=('2017-03-07 06:30:02+00:00') violates unique constraint "idx_update_time"
Error: pq: duplicate key value (update_time)=('2017-03-07 06:30:02+00:00') violates unique constraint "idx_update_time"
Test data dat.tar.gz
Jira issue: CRDB-70179
Jira issue: CRDB-70275
I'll take a look. Thanks for the data, @yznming
@danhhz It looks like there's data inconsistent between TABLE and INDEX.
root@:25899/kg_ugc_weibo> SELECT id ,update_time FROM tweet WHERE update_time='2017-03-06T23:45:00'::timestamp;
+------------------+---------------------------+
| id | update_time |
+------------------+---------------------------+
| 3757184707635744 | 2017-03-06 23:45:00+00:00 |
| 4082422788689336 | 2017-03-06 23:45:00+00:00 |
+------------------+---------------------------+
root@:25899/kg_ugc_weibo> SELECT update_time FROM tweet WHERE id = e'3757184707635744';
+---------------------------+
| update_time |
+---------------------------+
| 2017-03-07 23:37:07+00:00 |
+---------------------------+
@danhhz snapshot isolation would be easier to repro.
@yznming I'm having trouble reproducing this using your data. Are you able to reliably get this error from an empty table using the data you attached? Which version of cockroach are you using?
@danhhz Yes, I can repro it reliably. I'm using beta-20170309.
@danhhz you can try this test.py code.
cat dat_1.txt | python ./test.py > 1_1.log 2>&1 &
cat dat_2.txt | python ./test.py > 2_1.log 2>&1 &
Okay that worked! I was able to reproduce at master. I'm traveling until monday, but i'll take a look at it then. thanks @yznming!
I looked into this today and it looks like we may be seeing write skew in the impl of UPSERT. Did you ever see this without SNAPSHOT isolation?
it doesn't occur without SNAPSHOT isolation. In serializable isolation, the UPSERT executed failed because of transaction aborted.
cc @spencerkimball this is the issue I mentioned
@yznming I tested the test.py script again with spencer's fix and it seems to work now. this should make the beta we release this week