Skip to content

[Bug]: Prepared REPLACE SELECT can leave duplicate composite unique-key rows after prepared ODKU #25420

Closed

Description

@Ariznawlll

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:

Branch Name

main

Commit ID

51656deb6003

Other Environment Information

  • MatrixOne: select version() returns 8.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 ... SELECT without 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

  1. added this to the MO-202607 milestone on Jul 3, 2026
  2. added
    kind/bug
    on Jul 3, 2026
  3. added theissue type on Jul 3, 2026
  4. ck89119 commented on Jul 3, 2026

    @ck89119
    Contributor

    无法复现

  5. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    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:

    Validation SQL path:

    prepare s_odku from
      "insert into dst ... select ... where tenant = ? on duplicate key update ...";
    execute s_odku using @tenant;
    
    prepare s_replace from
      "replace into dst ... select ... where tenant = ?";
    execute s_replace using @tenant;

    Result on both 51656deb6003 and latest main:

    final  tenant=1  id=103  acct=a2  amount=25.25  status=paid
    final  tenant=2  id=201  acct=b1  amount=30.75  status=paid
    final  tenant=2  id=902  acct=b9  amount=9.99   status=old
    

    The duplicate check also returned no rows:

    select tenant, acct, count(*) as cnt
    from dst
    group by tenant, acct
    having count(*) > 1;

    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 the REPLACE 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.

  6. Ariznawlll commented on Jul 3, 2026

    @Ariznawlll
    CollaboratorAuthor

    Regression case added in matrixorigin/motr#39.

    Case: 14_issue_regression.issue_25420_prepare_replace_after_odku
    Commit: 2c22944
    Target branch marker: @target-branch: main

    This motr case covers the SQL-level PREPARE ... EXECUTE USING path verified above: prepared ODKU followed by prepared REPLACE INTO ... SELECT, then checks the final dst rows and confirms duplicate_groups = 0.

    Validation on latest main passed with the issue regression group:

    SUMMARY | total=15 success=15 failed=0 skipped=0 | stmts=179 ok=179 fail=0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s-1

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions