Repository navigation
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Searched open issues for:
"prepared" "REPLACE" "SELECT""REPLACE" "SELECT" "ON DUPLICATE KEY""REPLACE" "unique" "prepared"
Related but not duplicate:
- [Bug] REPLACE has multiple MySQL compatibility gaps #24918 tracks several general
REPLACEcompatibility gaps, but does not cover this preparedREPLACE ... SELECTpath. It also notes that basic unique-key replacement worked in local testing. - [Bug]: REPLACE/UPDATE can commit duplicate primary-key rows on composite-PK table with non-unique secondary indexes #24975 tracks duplicate rows on composite-PK tables with non-unique secondary indexes in a different production/cpkey amplification path.
Branch Name
main
Commit ID
51656deb6003
Other Environment Information
- MatrixOne:
select version()returns8.0.30-MatrixOne-v1.3.0 - MySQL comparison:
8.0.45 - Tested through the MySQL client with server-side prepared statements.
Actual Behavior
After a prepared INSERT ... SELECT ... ON DUPLICATE KEY UPDATE updates an existing row through a composite unique key, a subsequent prepared REPLACE ... SELECT for the same composite unique key leaves the old row behind and inserts the new row.
This creates two rows with the same unique key (tenant, acct) in MatrixOne:
tenant id acct amount status
1 102 a2 20.25 paid
1 103 a2 25.25 paid
2 201 b1 30.75 paid
2 902 b9 9.99 old
MySQL deletes the old unique-key row before inserting the replacement row:
tenant id acct amount status
1 103 a2 25.25 paid
2 201 b1 30.75 paid
2 902 b9 9.99 old
Control check on the same MatrixOne build:
- The same sequence executed as normal non-prepared SQL returns the same result as MySQL.
- A minimal prepared
REPLACE ... SELECTwithout the previous prepared ODKU state did not reproduce.
So the issue appears to be in prepared statement execution / state reuse around INSERT ... SELECT ... ODKU followed by REPLACE ... SELECT on a composite unique key.
Expected Behavior
Same as MySQL: REPLACE should behave like delete + insert when a PRIMARY KEY or UNIQUE conflict is found. The old row that conflicts on (tenant, acct) should be deleted, so the table must not contain duplicate unique-key rows.
Steps to Reproduce
drop database if exists mysql_compat_model14_prepare_replace_state;
create database mysql_compat_model14_prepare_replace_state;
use mysql_compat_model14_prepare_replace_state;
create table src (
tenant int not null,
id int not null,
acct varchar(20) not null,
amount decimal(10,2),
status varchar(20),
primary key (tenant, id),
unique key uk_src_acct (tenant, acct)
);
create table dst (
tenant int not null,
id int not null,
acct varchar(20) not null,
amount decimal(10,2),
status varchar(20),
primary key (tenant, id),
unique key uk_dst_acct (tenant, acct),
key idx_dst_status_amount (status, amount)
);
insert into src values
(1,103,'a2',25.25,'paid'),
(2,201,'b1',30.75,'paid');
insert into dst values
(1,102,'a2',20.25,'old'),
(2,902,'b9',9.99,'old');
prepare s_odku from
'insert into dst (tenant,id,acct,amount,status)
select tenant,id,acct,amount,status from src where tenant = ?
on duplicate key update amount = values(amount), status = values(status)';
set @tenant = 1;
execute s_odku using @tenant;
deallocate prepare s_odku;
prepare s_replace from
'replace into dst (tenant,id,acct,amount,status)
select tenant,id,acct,amount,status from src where tenant = ?';
set @tenant = 2;
execute s_replace using @tenant;
set @tenant = 1;
execute s_replace using @tenant;
deallocate prepare s_replace;
select tenant,id,acct,amount,status from dst order by tenant,id;Additional information
This was found during MySQL compatibility exploration for prepared statements over tables with composite primary keys, composite unique keys, and secondary indexes.
Activity
ck89119 commentedon Jul 3, 2026
无法复现
Ariznawlll commentedon Jul 3, 2026
I re-verified this locally and could not reproduce the final data inconsistency with the SQL-level PREPARE path from the issue description.
Validation environment:
51656deb6003(update go version 1.26.4update go version 1.26.4 #25286), built locally7e70bee98(fix castfix cast #25340), built locallyValidation SQL path:
Result on both
51656deb6003and latest main:The duplicate check also returned no rows:
So with SQL-level
PREPARE ... EXECUTE USING, the issue result is currently normal.One additional note: I also tried a Go driver protocol-level prepared statement (
database/sql+go-sql-driver/mysql, i.e. COM_STMT_PREPARE). That does not reproduce the final data issue either; it fails earlier at prepare time for theREPLACE INTO ... SELECT ... WHERE tenant = ?statement with parser error 1064. That appears to be a different prepared-statement path/issue, not the final duplicate-data behavior described here.Current conclusion: I cannot reproduce this issue as written. If the original failure was from driver/binary-protocol prepared statements or required extra session/transaction/preloaded data conditions, please provide the exact driver/script and connection/session settings.
Ariznawlll commentedon Jul 3, 2026
Regression case added in matrixorigin/motr#39.
Case:
14_issue_regression.issue_25420_prepare_replace_after_odkuCommit:
2c22944Target branch marker:
@target-branch: mainThis motr case covers the SQL-level
PREPARE ... EXECUTE USINGpath verified above: prepared ODKU followed by preparedREPLACE INTO ... SELECT, then checks the finaldstrows and confirmsduplicate_groups = 0.Validation on latest main passed with the issue regression group: