Skip to content

UNIQUE index: delete a record, insert its key again, delete that new record, and insert the key a third time in one transaction, and the commit fails with DuplicatedKeyException naming the record the transaction deleted #8961

Closed

Description

@tae898

Inside one transaction, on a UNIQUE index: delete committed record A with key x, insert B with x, delete B, insert C with x. The commit fails with DuplicatedKeyException ... already assigned to record A, although A is deleted in that same transaction and C is the only record with x at the end. Without the last insert (delete A, insert B, delete B; key y in the repro) the commit succeeds, but on main the index keeps its entry for A: get("y") still returns A's RID after A is gone.

In TransactionIndexContext.addIndexKeyLock the entries of a unique key collapse into one: REMOVE A then ADD B becomes REPLACE B with oldRid = A. The next REMOVE B replaces that entry with a plain REMOVE B and drops oldRid = A, so nothing left in the transaction removes A's committed entry. A following ADD C turns into REPLACE C with oldRid = B, and the uniqueness check at commit (TransactionIndexContext.checkUniqueIndexKeys, from commit()) then finds A. #7881 lost oldRid for array keys through a different path; this one is a plain STRING key. I found it while testing vector indexes (a delete-and-reinsert of the same id twice in one transaction), but nothing here involves a vector index.

Repro

javac -cp 'lib/*' -d cls UniqueKeyDeleteTwice.java
java -Xmx8g -XX:+UseCompactObjectHeaders --add-modules=jdk.incubator.vector --enable-native-access=ALL-UNNAMED -cp 'lib/*:cls' UniqueKeyDeleteTwice /tmp/unique-db

lib/ holds the engine jar and its dependencies. On Temurin 21 the same command without -XX:+UseCompactObjectHeaders.

import com.arcadedb.database.*;
import com.arcadedb.index.*;

public class UniqueKeyDeleteTwice {
  public static void main(String[] args) {
    DatabaseFactory factory = new DatabaseFactory(args[0]);
    if (factory.exists())
      factory.open().drop();
    Database db = factory.create();
    db.command("sql", "CREATE DOCUMENT TYPE Item");
    db.command("sql", "CREATE PROPERTY Item.code STRING");
    db.command("sql", "CREATE INDEX ON Item (code) UNIQUE");
    db.transaction(() -> {
      db.newDocument("Item").set("code", "x").save();
      db.newDocument("Item").set("code", "y").save();
    });

    // 1) delete the committed record, insert B, delete B, insert C: one record with code 'x' at the end
    try {
      db.transaction(() -> {
        db.query("sql", "SELECT FROM Item WHERE code = 'x'").next().getRecord().get().asDocument().delete();
        MutableDocument b = db.newDocument("Item").set("code", "x").save();
        b.delete();
        db.newDocument("Item").set("code", "x").save();
      });
      System.out.println("1) committed");
    } catch (Exception e) {
      System.out.println("1) " + e);
    }

    // 2) delete the committed record, insert B, delete B: no record with code 'y' at the end
    db.transaction(() -> {
      db.query("sql", "SELECT FROM Item WHERE code = 'y'").next().getRecord().get().asDocument().delete();
      MutableDocument b = db.newDocument("Item").set("code", "y").save();
      b.delete();
    });
    System.out.println("2) committed; records with code 'y': " + db.query("sql", "SELECT FROM Item WHERE code + '' = 'y'").stream().count()
        + ", index entries for 'y': " + db.getSchema().getIndexByName("Item[code]").get(new Object[] { "y" }).estimateSize());
    IndexCursor cursor = db.getSchema().getIndexByName("Item[code]").get(new Object[] { "y" });
    while (cursor.hasNext()) {
      RID rid = cursor.next().getIdentity();
      System.out.println("   'y' -> " + rid + " exists: " + db.existsRecord(rid));
    }
    db.close();
  }
}

Output on main at bda510bca0, embedded, identical on Temurin 25 (with compact object headers) and Temurin 21:

1) com.arcadedb.exception.DuplicatedKeyException: Duplicated key [x] found on index 'Item[code]' already assigned to record #1:0
2) committed; records with code 'y': 0, index entries for 'y': 1
   'y' -> #1:1 exists: false

Versions affected: main 417314c18 (2026-09-17) and 26.9.1 throw the same DuplicatedKeyException in case 1. In case 2 they report no index entry for y:

1) com.arcadedb.exception.DuplicatedKeyException: Duplicated key [x] found on index 'Item[code]' already assigned to record #1:0
2) committed; records with code 'y': 0, index entries for 'y': 0

Also on main at 9319980f60 (2026-10-02, after #8947): the same output on Temurin 25 and Temurin 21.

Expected: case 1 commits and leaves one record with code x (C) and one index entry for it; case 2 commits and leaves no index entry for y. A REMOVE that displaces a REPLACE would need to keep the replaced entry's oldRid, so the committed RID is still removed at commit.

Activity

  1. added
    engine
    severity:minor
    on Oct 2, 2026
  2. self-assigned this
    on Oct 2, 2026
  3. added
    in progress
    on Oct 2, 2026
  4. added this to the 26.10.1 milestone on Oct 2, 2026
  5. added a commit that references this issue on Oct 2, 2026
  6. lvca commented on Oct 2, 2026

    @lvca
    Member

    Confirmed and fixed in #8969 (26.10.1). In one transaction on a UNIQUE index, deleting a record, inserting its key, deleting that insert and inserting the key again failed with DuplicatedKeyException, or left a stale index entry. It now commits and the index holds exactly one entry for the key.

    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. removed
    in progress
    on Oct 2, 2026
  8. tae898 commented on Oct 3, 2026

    @tae898
    ContributorAuthor

    Verified on main at 66541f57d9 (which carries the #8969 merge a556c94456 and the #8971 merge 66541f57d9) against bda510bca0, the build this was filed on, with the issue's reproduction, on Temurin 25 with -XX:+UseCompactObjectHeaders and on Temurin 21, same results on both:

    UNIQUE index, one transaction bda510bca0 66541f57d9
    1) delete A, insert B, delete B, insert C (key x) DuplicatedKeyException ... already assigned to record #1:0 committed
    2) delete A, insert B, delete B (key y) committed, 1 index entry left for y (pointing at a deleted record) committed, 0 entries

    The Python bindings suite is green on a wheel built with that engine. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengineseverity:minor

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions