Skip to content

HASH indexes apply a key change twice at commit: UNIQUE_HASH lets an UPDATE take a key another record holds, and NOTUNIQUE_HASH keeps a stale entry that later returns a record with a different key #8983

Closed

Description

@tae898

When a transaction changes an indexed property, the commit writes that change to a HASH index twice. There are three visible effects, all in the repro below. On a UNIQUE_HASH index, an UPDATE that sets k to a value another record already holds commits, which leaves two records with k = 2. The same change raises DuplicatedKeyException on a UNIQUE (LSM) index, and inserting a duplicate is refused on both. The Java API (modify().set("k", 2).save()) and openCypher SET commit the duplicate on UNIQUE_HASH too. On a NOTUNIQUE_HASH index, the record is listed twice under its new key. Deleting the record removes one of the two, and the entry that is left points to whichever record takes that RID next: SELECT FROM B_NOTUNIQUE_HASH WHERE k = 1 returns a record whose k is 5 (part 2 of the repro). After a single key change, countEntries() reports 2 entries for 1 record on both hash kinds, and 1 on the LSM kinds.

A JFR method trace of HashIndexBucket.put and HashIndexBucket.remove for one key change shows each method running twice inside TransactionContext.commit1stPhase. The first pass comes from the flush of the deferred update: LocalDatabase.updateRecordNoLock calls DocumentIndexer.updateDocument against the committed buffer, and because the transaction status is already COMMIT_1ST_PHASE, HashIndex.put and remove take their non-transactional branch and write the bucket directly. The second pass is TransactionIndexContext.commit(), which replays the remove and add that save() already queued. The direct write happens before checkUniqueIndexKeys runs, and on a unique hash index a lookup returns only the first entry for the key. After the commit, that entry is the updated record's own, which fits the check finding no duplicate.

Repro

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

lib/ holds the engine jar and its dependencies. On Temurin 21, use the same command without -XX:+UseCompactObjectHeaders. Single thread, no concurrency involved.

import com.arcadedb.database.*;
import com.arcadedb.query.sql.executor.*;
import java.util.*;

public class HashIndexKeyChange {
  public static void main(String[] args) {
    DatabaseFactory factory = new DatabaseFactory(args[0]);
    if (factory.exists())
      factory.open().drop();
    Database db = factory.create();

    System.out.println("1. Change k from 1 to 2 while another record holds k = 2");
    for (String kind : new String[] { "UNIQUE", "UNIQUE_HASH" }) {
      String t = create(db, "A_" + kind, kind);
      db.transaction(() -> {
        db.newDocument(t).set("k", 1).save();
        db.newDocument(t).set("k", 2).save();
      });
      String outcome;
      try {
        db.transaction(() -> db.command("sql", "UPDATE " + t + " SET k = 2 WHERE k = 1"));
        outcome = "committed";
      } catch (Exception e) {
        outcome = e.getClass().getSimpleName();
      }
      System.out.println("   " + kind + ": " + outcome + ", records per k " + rows(db, "SELECT k, count(*) AS records FROM " + t + " GROUP BY k ORDER BY k"));
    }

    System.out.println("2. Change k from 0 to 1, delete that record, insert a new one with k = 5");
    for (String kind : new String[] { "NOTUNIQUE", "NOTUNIQUE_HASH" }) {
      String t = create(db, "B_" + kind, kind);
      RID[] rids = new RID[2];
      db.transaction(() -> rids[0] = db.newDocument(t).set("k", 0).save().getIdentity());
      db.transaction(() -> db.command("sql", "UPDATE " + t + " SET k = 1 WHERE k = 0"));
      db.transaction(() -> db.command("sql", "DELETE FROM " + t + " WHERE k = 1"));
      db.transaction(() -> rids[1] = db.newDocument(t).set("k", 5).save().getIdentity());
      System.out.println("   " + kind + ": deleted " + rids[0] + ", new record " + rids[1] + ", SELECT FROM " + t + " WHERE k = 1 -> "
          + rows(db, "SELECT @rid AS rid, k FROM " + t + " WHERE k = 1"));
    }

    System.out.println("3. Index entries after one record changes k from 0 to 1");
    for (String kind : new String[] { "UNIQUE", "NOTUNIQUE", "UNIQUE_HASH", "NOTUNIQUE_HASH" }) {
      String t = create(db, "C_" + kind, kind);
      db.transaction(() -> db.newDocument(t).set("k", 0).save());
      db.transaction(() -> db.command("sql", "UPDATE " + t + " SET k = 1 WHERE k = 0"));
      System.out.println("   " + kind + ": " + db.getSchema().getType(t).getPolymorphicIndexByProperties("k").countEntries() + " entries for 1 record");
    }
    db.close();
  }

  static String create(Database db, String type, String kind) {
    db.command("sql", "CREATE DOCUMENT TYPE " + type);
    db.command("sql", "CREATE PROPERTY " + type + ".k INTEGER");
    db.command("sql", "CREATE INDEX ON " + type + " (k) " + kind);
    return type;
  }

  static List<String> rows(Database db, String query) {
    List<String> rows = new ArrayList<>();
    try (ResultSet rs = db.query("sql", query)) {
      while (rs.hasNext())
        rows.add(rs.next().toJSON().toString());
    }
    return rows;
  }
}

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

1. Change k from 1 to 2 while another record holds k = 2
   UNIQUE: DuplicatedKeyException, records per k [{"k":1,"records":1}, {"k":2,"records":1}]
   UNIQUE_HASH: committed, records per k [{"k":2,"records":2}]
2. Change k from 0 to 1, delete that record, insert a new one with k = 5
   NOTUNIQUE: deleted #5:0, new record #5:0, SELECT FROM B_NOTUNIQUE WHERE k = 1 -> []
   NOTUNIQUE_HASH: deleted #7:0, new record #7:0, SELECT FROM B_NOTUNIQUE_HASH WHERE k = 1 -> [{"rid":"#7:0","k":5}]
3. Index entries after one record changes k from 0 to 1
   UNIQUE: 1 entries for 1 record
   NOTUNIQUE: 1 entries for 1 record
   UNIQUE_HASH: 2 entries for 1 record
   NOTUNIQUE_HASH: 2 entries for 1 record

Versions affected: main 5e44bc7ab3, main 417314c18 (2026-09-17), and 26.9.1 print the same lines, on both JDKs.

Also on main at 66541f57d9 (2026-10-03): the same output on Temurin 25 and Temurin 21.

Expected: the UPDATE on UNIQUE_HASH fails with DuplicatedKeyException and leaves one record with k = 1 and one with k = 2, as on UNIQUE. SELECT FROM B_NOTUNIQUE_HASH WHERE k = 1 returns no rows, as on NOTUNIQUE. A key change leaves one index entry per record.

Activity

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

    @lvca
    Member

    Fixed in #8989 (26.10.1). A key change on an indexed property was written to a HASH index twice at commit, which skipped the unique check and left stale entries. It is now written once. This also fixed records swapping unique keys in one transaction losing an index entry.

    ❤️ 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 3, 2026
  8. tae898 commented on Oct 3, 2026

    @tae898
    ContributorAuthor

    Verified on main at 3d67d5c5c1 (the #8989 merge, engine built from that commit) against 5e44bc7ab3, the build this was filed on, with the issue's reproduction, on Temurin 25 with -XX:+UseCompactObjectHeaders and on Temurin 21 (both print the same lines):

    line of the output 5e44bc7ab3 3d67d5c5c1
    1. UNIQUE_HASH, change k from 1 to 2 while another record holds 2 committed, records per k [{"k":2,"records":2}] DuplicatedKeyException, records per k [{"k":1,"records":1}, {"k":2,"records":1}], as UNIQUE
    2. NOTUNIQUE_HASH, SELECT ... WHERE k = 1 after the change and a delete [{"rid":"#7:0","k":5}] [], as NOTUNIQUE
    3. index entries after one record changes k from 0 to 1 UNIQUE_HASH: 2 entries for 1 record, NOTUNIQUE_HASH: 2 entries for 1 record 1 entries for 1 record for all four index types

    Thank you, nothing further from my side.

  9. added a commit that references this issue on Oct 3, 2026
  10. added 3 commits that reference this issue on Oct 6, 2026
  11. added a commit that references this issue on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengineseverity:critical

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions