cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: unexpect 'duplicate key' error while executing 'UPSERT'

Open yznming opened this issue 9 years ago • 11 comments

run UPSERT with 2 concurrence, gets an unexpect duplicate key error.

Step to repo:

  1. 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)
);
  1. 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

yznming avatar Mar 11 '17 17:03 yznming

I'll take a look. Thanks for the data, @yznming

danhhz avatar Mar 13 '17 19:03 danhhz

@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 |
+---------------------------+

yznming avatar Mar 14 '17 00:03 yznming

@danhhz snapshot isolation would be easier to repro.

yznming avatar Mar 14 '17 03:03 yznming

@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 avatar Mar 15 '17 17:03 danhhz

@danhhz Yes, I can repro it reliably. I'm using beta-20170309.

yznming avatar Mar 16 '17 09:03 yznming

@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 &

test.py.zip

yznming avatar Mar 17 '17 03:03 yznming

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!

danhhz avatar Mar 17 '17 20:03 danhhz

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?

danhhz avatar Mar 21 '17 00:03 danhhz

it doesn't occur without SNAPSHOT isolation. In serializable isolation, the UPSERT executed failed because of transaction aborted.

yznming avatar Mar 23 '17 11:03 yznming

cc @spencerkimball this is the issue I mentioned

danhhz avatar Mar 27 '17 20:03 danhhz

@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

danhhz avatar Mar 28 '17 15:03 danhhz