INSERTINTO t VALUES ('Hrecvx_0004ln-00',1), ('Hrecvx_0004mm-00',1);
INSERTINTO t VALUES ('Hrecvx_0004mm-00',2) ON DUPLICATE KEYUPDATE n = VALUES (n);
SELECT * FROM t;
produce this output:
s n
Hrecvx_0004ln-00 2
Hrecvx_0004mm-00 1
So the latter "INSERT" updates the wrong row.
This happens whether the first column is "BLOB" or "TEXT", but only
with specific values. (In my actual use case with ~1 million rows,
it happened a few dozen times, which might be consistent e.g. with
collisions of a 32 bit hash or so.)
Likewise, these statements:
DROPTABLEIF EXISTS t;
CREATETABLE t (s BLOB, n INT, UNIQUE (s));
INSERTINTO t VALUES ('Hrecvx_0004ln-00',1), ('Hrecvx_0004mm-00',1);
REPLACEINTO t VALUES ('Hrecvx_0004mm-00',2);
SELECT * FROM t;
give the error:
ERROR 1062 (23000) at line 4: Duplicate entry 'Hrecvx_0004mm-00' for key 's'
In my understanding, this error should actually be impossible with
"REPLACE INTO".
It might be the same issue, i.e. it tries to delete the wrong row
before inserting the new one, so it's still duplicate.
Attachments
Issue Links
is duplicated by
MDEV-30588Failed to update duplicate data when running insert... on duplicate key update.
Thanks for the report! I repeated as described on 10.4-10.11, InnoDB. Probably caused by MDEV-371
--source include/have_innodb.inc
CREATETABLE t (s BLOB, n INT, UNIQUE (s)) engine=innodb;
INSERTINTO t VALUES ('Hrecvx_0004ln-00',1), ('Hrecvx_0004mm-00',1);
REPLACEINTO t VALUES ('Hrecvx_0004mm-00',2);
10.4 0d586d62e5326053383
CURRENT_TEST: main.1_my
mysqltest: At line 9: query 'REPLACE INTO t VALUES ('Hrecvx_0004mm-00',2)' failed: 1062: Duplicate entry 'Hrecvx_0004mm-00' for key 's'
CREATETABLE t (s BLOB, n INT, UNIQUE (s)) engine=innodb;
INSERTINTO t VALUES ('Hrecvx_0004ln-00',1), ('Hrecvx_0004mm-00',1);
INSERTINTO t VALUES ('Hrecvx_0004mm-00',2) ON DUPLICATE KEYUPDATE n = VALUES(n) ;
SELECT * FROM t;
SELECT * FROM t;
s n
Hrecvx_0004ln-00 2
Hrecvx_0004mm-00 1
Alice Sherepa
added a comment - - edited Thanks for the report! I repeated as described on 10.4-10.11, InnoDB. Probably caused by MDEV-371
--source include/have_innodb.inc
CREATE TABLE t (s BLOB, n INT , UNIQUE (s)) engine=innodb;
INSERT INTO t VALUES ( 'Hrecvx_0004ln-00' ,1), ( 'Hrecvx_0004mm-00' ,1);
REPLACE INTO t VALUES ( 'Hrecvx_0004mm-00' ,2);
10.4 0d586d62e5326053383
CURRENT_TEST: main.1_my
mysqltest: At line 9: query 'REPLACE INTO t VALUES ('Hrecvx_0004mm-00',2)' failed: 1062: Duplicate entry 'Hrecvx_0004mm-00' for key 's'
CREATE TABLE t (s BLOB, n INT , UNIQUE (s)) engine=innodb;
INSERT INTO t VALUES ( 'Hrecvx_0004ln-00' ,1), ( 'Hrecvx_0004mm-00' ,1);
INSERT INTO t VALUES ( 'Hrecvx_0004mm-00' ,2) ON DUPLICATE KEY UPDATE n = VALUES (n) ;
SELECT * FROM t;
SELECT * FROM t;
s n
Hrecvx_0004ln-00 2
Hrecvx_0004mm-00 1
INSERTINTO t1 VALUES ('Hrecvx_0004mm-00',2) ON DUPLICATE KEYUPDATE n = VALUE(n) ;
SELECT * FROM t1;
+------------------+------+
| s | n |
+------------------+------+
| Hrecvx_0004ln-00 | 1 |
| Hrecvx_0004mm-00 | 2 |
+------------------+------+
Alexander Barkov
added a comment - Works fine with MyISAM:
CREATE OR REPLACE TABLE t1 (s BLOB, n INT , UNIQUE (s)) engine=MyISAM;
INSERT INTO t1 VALUES ( 'Hrecvx_0004ln-00' ,1),
( 'Hrecvx_0004mm-00' ,1);
REPLACE INTO t1 VALUES ( 'Hrecvx_0004mm-00' ,2);
SELECT * FROM t1;
+------------------+------+
| s | n |
+------------------+------+
| Hrecvx_0004ln-00 | 1 |
| Hrecvx_0004mm-00 | 2 |
+------------------+------+
CREATE OR REPLACE TABLE t1 (s BLOB, n INT , UNIQUE (s)) engine=MyISAM;
INSERT INTO t1 VALUES ( 'Hrecvx_0004ln-00' ,1), ( 'Hrecvx_0004mm-00' ,1);
INSERT INTO t1 VALUES ( 'Hrecvx_0004mm-00' ,2) ON DUPLICATE KEY UPDATE n = VALUE(n) ;
SELECT * FROM t1;
+------------------+------+
| s | n |
+------------------+------+
| Hrecvx_0004ln-00 | 1 |
| Hrecvx_0004mm-00 | 2 |
+------------------+------+
Thanks bar for the pointers, the bug with IDEMPOTENT replication was found with long uniques, which uses the same logic as REPLACE, and even contains the copy-paste from there. I guess it was made so to allocate and reuse the key buffer memory on the stack, saving from extra malloc. I think our priority now is to minimize stack usage vs extra mallocs, so it's not the point anymore.
I made some refactoring and extracted common code. Also moved handler's RND search initialization to Write_rows_log_event::do_before_row_operations. This fixed many long unique bugs in relplication and optimized the use a little bit.
One more refactoring should be done to generalize handler initialization (and de-initialization) across REPLACE, LOAD DATA, IDEMPOTENT replication, and improve memory usage.
Nikita Malyavin
added a comment - Thanks bar for the pointers, the bug with IDEMPOTENT replication was found with long uniques, which uses the same logic as REPLACE, and even contains the copy-paste from there. I guess it was made so to allocate and reuse the key buffer memory on the stack, saving from extra malloc. I think our priority now is to minimize stack usage vs extra mallocs, so it's not the point anymore.
I made some refactoring and extracted common code. Also moved handler's RND search initialization to Write_rows_log_event::do_before_row_operations . This fixed many long unique bugs in relplication and optimized the use a little bit.
https://github.com/MariaDB/server/commit/703e73e221a42638f2f05379124b35c57482da93
One more refactoring should be done to generalize handler initialization (and de-initialization) across REPLACE, LOAD DATA, IDEMPOTENT replication, and improve memory usage.
Otto Kekäläinen
added a comment - The commit https://github.com/MariaDB/server/commit/703e73e221a42638f2f05379124b35c57482da93 was never in a pull request nor included on any branch, and `git log -S MDEV-30046 ` does not yield any results from 10.5 branch, so I assume this issue is still open (and thus will also continue to keep https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1015293 open).
The refactoring took a significant amount of time, but I think it will be very beneficial.
We had two implementations of the replace behavior, one is usual, and another one is an idempotent insert. The latter was many fixes behind the primary one.
First, the implementations should have been joined. I didn't stop only on that, though.
I also reworked the implementation to a better readability, as well as, as I believe, maintainability.
Also I have optimized it in several places.
013fbe19 MDEV-30046 Refactor write_record and fix idempotent replication
aae554b1 MDEV-30046 wrong row targeted with "insert ... on duplicate" and "replace"
Or take a look at the same-titled commits at branch bb-10.5-nikita-MDEV-30046.
They are best to view with commit
9f2ef5d4 MDEV-15990 REPLACE on a precise-versioned table returns ER_DUP_ENTRY
as it further simplifies the REPLACE implementation, however it goes for review to midenok. Anyway, comments are welcome.
Nikita Malyavin
added a comment - The refactoring took a significant amount of time, but I think it will be very beneficial.
We had two implementations of the replace behavior, one is usual, and another one is an idempotent insert. The latter was many fixes behind the primary one.
First, the implementations should have been joined. I didn't stop only on that, though.
I also reworked the implementation to a better readability, as well as, as I believe, maintainability.
Also I have optimized it in several places.
The commit message will bring the better detail.
serg , please review commits the following commits
013fbe19 MDEV-30046 Refactor write_record and fix idempotent replication
aae554b1 MDEV-30046 wrong row targeted with "insert ... on duplicate" and "replace"
Or take a look at the same-titled commits at branch bb-10.5-nikita- MDEV-30046 .
They are best to view with commit
9f2ef5d4 MDEV-15990 REPLACE on a precise-versioned table returns ER_DUP_ENTRY
as it further simplifies the REPLACE implementation, however it goes for review to midenok . Anyway, comments are welcome.
The refactoring commit is generally moving in the right direction, but it won't be ready before the release, so I don't think the actual bug fix should wait for it.
Sergei Golubchik
added a comment - https://github.com/MariaDB/server/commit/972f6941df8b09b523f7f14d2465cee39a98fecb is ok to push, preferably with a clearer comment in sql_insert.cc .
The refactoring commit is generally moving in the right direction, but it won't be ready before the release, so I don't think the actual bug fix should wait for it.
Otto Kekäläinen
added a comment - For the record, this was merged in as https://github.com/MariaDB/server/commit/72429cad7f5448ad680d8df7b55aa055462d5964 (not visible in this Jira)
People
Nikita Malyavin
Frank Heckenbach
Votes:
0Vote for this issue
Watchers:
6Start watching this issue
Dates
Created:
Updated:
Resolved:
Git Integration
Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.
{"report":{"fcp":1387,"ttfb":274.7000002861023,"pageVisibility":"visible","entityId":117028,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"6dd35617-5bf9-42c4-b11e-40c9778a47e2","navigationType":0,"readyForUser":1499.0999999046326,"redirectCount":0,"resourceLoadedEnd":1067.3000001907349,"resourceLoadedStart":308.80000019073486,"resourceTiming":[{"duration":62.299999713897705,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":308.80000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":308.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":371.09999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":62.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":309.2000002861023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":309.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":371.30000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":520.7999997138977,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":309.30000019073486,"connectEnd":309.30000019073486,"connectStart":309.30000019073486,"domainLookupEnd":309.30000019073486,"domainLookupStart":309.30000019073486,"fetchStart":309.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":381,"responseEnd":830.0999999046326,"responseStart":459.40000009536743,"secureConnectionStart":309.30000019073486},{"duration":757.7000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":309.59999990463257,"connectEnd":309.59999990463257,"connectStart":309.59999990463257,"domainLookupEnd":309.59999990463257,"domainLookupStart":309.59999990463257,"fetchStart":309.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":381.30000019073486,"responseEnd":1067.3000001907349,"responseStart":461,"secureConnectionStart":309.59999990463257},{"duration":175.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":309.80000019073486,"connectEnd":309.80000019073486,"connectStart":309.80000019073486,"domainLookupEnd":309.80000019073486,"domainLookupStart":309.80000019073486,"fetchStart":309.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":390.90000009536743,"responseEnd":485.40000009536743,"responseStart":476.2000002861023,"secureConnectionStart":309.80000019073486},{"duration":177.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":309.90000009536743,"connectEnd":309.90000009536743,"connectStart":309.90000009536743,"domainLookupEnd":309.90000009536743,"domainLookupStart":309.90000009536743,"fetchStart":309.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":405.40000009536743,"responseEnd":487.7000002861023,"responseStart":478,"secureConnectionStart":309.90000009536743},{"duration":177.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":310.09999990463257,"connectEnd":310.09999990463257,"connectStart":310.09999990463257,"domainLookupEnd":310.09999990463257,"domainLookupStart":310.09999990463257,"fetchStart":310.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":406.40000009536743,"responseEnd":487.5,"responseStart":477.30000019073486,"secureConnectionStart":310.09999990463257},{"duration":64.39999961853027,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":310.2000002861023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":310.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":374.59999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":178.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":310.40000009536743,"connectEnd":310.40000009536743,"connectStart":310.40000009536743,"domainLookupEnd":310.40000009536743,"domainLookupStart":310.40000009536743,"fetchStart":310.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":406.90000009536743,"responseEnd":488.59999990463257,"responseStart":480.5,"secureConnectionStart":310.40000009536743},{"duration":64.7000002861023,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":310.59999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":310.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":375.30000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":206,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":310.7000002861023,"connectEnd":310.7000002861023,"connectStart":310.7000002861023,"domainLookupEnd":310.7000002861023,"domainLookupStart":310.7000002861023,"fetchStart":310.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":409.09999990463257,"responseEnd":516.7000002861023,"responseStart":481.59999990463257,"secureConnectionStart":310.7000002861023},{"duration":714.7000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":352.09999990463257,"connectEnd":352.09999990463257,"connectStart":352.09999990463257,"domainLookupEnd":352.09999990463257,"domainLookupStart":352.09999990463257,"fetchStart":352.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":437.90000009536743,"responseEnd":1066.8000001907349,"responseStart":1059.5999999046326,"secureConnectionStart":352.09999990463257},{"duration":683.9000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":383,"connectEnd":383,"connectStart":383,"domainLookupEnd":383,"domainLookupStart":383,"fetchStart":383,"redirectEnd":0,"redirectStart":0,"requestStart":484.2000002861023,"responseEnd":1066.9000000953674,"responseStart":1060.2000002861023,"secureConnectionStart":383},{"duration":248.7999997138977,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1016.7000002861023,"connectEnd":1016.7000002861023,"connectStart":1016.7000002861023,"domainLookupEnd":1016.7000002861023,"domainLookupStart":1016.7000002861023,"fetchStart":1016.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":1231.5999999046326,"responseEnd":1265.5,"responseStart":1265,"secureConnectionStart":1016.7000002861023},{"duration":259.8999996185303,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1326.7000002861023,"connectEnd":1326.7000002861023,"connectStart":1326.7000002861023,"domainLookupEnd":1326.7000002861023,"domainLookupStart":1326.7000002861023,"fetchStart":1326.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":1551.2000002861023,"responseEnd":1586.5999999046326,"responseStart":1584.8000001907349,"secureConnectionStart":1326.7000002861023}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":77,"responseStart":274,"responseEnd":409,"domLoading":281,"domInteractive":1627,"domContentLoadedEventStart":1627,"domContentLoadedEventEnd":1698,"domComplete":2004,"loadEventStart":2004,"loadEventEnd":2004,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1574.8000001907349},{"name":"bigPipe.sidebar-id.end","time":1576.5},{"name":"bigPipe.activity-panel-pipe-id.start","time":1576.5999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":1585.5999999046326},{"name":"activityTabFullyLoaded","time":1721.9000000953674}],"measures":[],"correlationId":"c848366e7fda01","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":129,"dbReadsTimeInMs":15,"dbConnsTimeInMs":24,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Thanks for the report! I repeated as described on 10.4-10.11, InnoDB. Probably caused by
MDEV-371--source include/have_innodb.inc
10.4 0d586d62e5326053383
CURRENT_TEST: main.1_my
mysqltest: At line 9: query 'REPLACE INTO t VALUES ('Hrecvx_0004mm-00',2)' failed: 1062: Duplicate entry 'Hrecvx_0004mm-00' for key 's'
SELECT * FROM t;
s n
Hrecvx_0004ln-00 2
Hrecvx_0004mm-00 1