Skip to content

fix: reject PK-changing UPDATEs onto an existing key and enforce UNIQUE constraints - #517

Merged
CastleSoft merged 1 commit into
fix/linq2db-adapter-and-scalar-functionsfrom
fix/pk-update-and-unique-constraints
Nov 16, 2026
Merged

CastleSoft merged 1 commit into
fix/linq2db-adapter-and-scalar-functionsfrom
fix/pk-update-and-unique-constraints

Conversation

@CastleSoft

Copy link
Copy Markdown
Collaborator

Stacked on #516. This PR targets fix/linq2db-adapter-and-scalar-functions so its diff shows only this change. Retarget to master once #516 is merged.

Summary

Fixes known issues §8 and §9 from docs/sharpcoredb-known-issues.md on directory storage, plus three defects found on the way. Every fix has a test that was run red before it and green after.

§8 — an UPDATE that moved a PRIMARY KEY onto an existing key lost a row

UPDATE t SET id = 1 WHERE id = 3 with a row 1 present was accepted: the primary-key index was re-pointed at the moved row and the original row 1 became unreachable (silent data loss).

Every UPDATE path now validates new key values before any row is written:

  • Table.UpdateAffectedCount (SQL UPDATE)
  • UpdateMultipleCore (ExecuteBatchSQL, structured batch API), tracking what earlier operations in the batch claim and release
  • UpdateByPrimaryKey

A SET of the key that matches more than one row is rejected too. Pinned by PrimaryKeyUpdateTests (columnar and page-based, in session and after reopen).

§9 — a UNIQUE constraint was never enforced

email TEXT UNIQUE (or UNIQUE (email)) accepted duplicates; the INSERT-time check was a stub. A single-column UNIQUE now gets an automatic unique hash index, as SQLite's sqlite_autoindex_…, registered at CREATE TABLE and on every open (existing tables are covered; registration builds nothing, so rows stored earlier never block an open). One validator covers single-row INSERT, both batch critical sections and every UPDATE path. NULLs never conflict. Pinned by UniqueConstraintTests (columnar and page-based).

Found on the way

Defect Fix
A UNIQUE index was checked only by a single-row INSERT into a columnar table — not page-based, not multi-row INSERT, not UPDATE Same validator on every path
All three HashIndex add paths rejected a second NULL as a duplicate DBNull keys skip the uniqueness check
A table with a unique index gave up the atomic batched multi-row INSERT for a per-row loop that left earlier rows behind The batch core enforces unique indexes now, so CanUseBatchedInsert no longer excludes them

Changed test: MultiRowInsertBatchingTests.CanUseBatchedInsert_IsFalse_WithUniqueSecondaryIndex pinned the old fallback, which existed only because the batch core did not enforce unique indexes. It is replaced by CanUseBatchedInsert_IsTrue_WithUniqueSecondaryIndex_BecauseTheBatchCoreEnforcesIt, which asserts the batch path is used and still rejects a duplicate keeping no row of the statement.

Performance

Every check starts with an allocation-free test that returns at once when a table or statement has no key to protect (the capturing lambdas live in separate *Core helpers so no closure is allocated on the fast path).

Write-path gate (--gate, Release, alone on the box): GATE PASSED, every metric ok, nothing slower than baseline × 1.50. The box was NOISY — WSearch and the Defender exclusion need elevation; build servers were shut down with quiet-machine.ps1 -Apply.

metric median ratio best-of-reps ratio
raw INSERT 1.07× 1.02×
raw UPDATE 0.60× 1.22×
raw DELETE 1.16× 1.15×
default INSERT 1.05× 1.02×
default UPDATE — 1.08×

(ratio = baseline ÷ current; > 1 is slower.) The added work for an UPDATE that touches no key is one dictionary miss plus a scan of a one- or two-entry SET list per operation; the untouched DELETE path moved by a similar amount, so the spread reads as machine noise.

Not covered (recorded in the known-issues document)

  • Single-file storage — its persisted ColumnFlags.Unique is also written for every inline PRIMARY KEY column (DatabaseExtensions: "PRIMARY KEY implies UNIQUE"), so a reopened file cannot tell a declared UNIQUE from an EF Core composite key's column. Enforcing it was implemented and backed out when CompleteExampleIntegrationTests.CompositeKey_* failed. Same column-entry format decision as §10/§11; §8's single-file half (a duplicated key, not a lost row) is tracked in §11.
  • A composite UNIQUE (a, b) on INSERT.

Tests

  • core 2392 / 0 failed (6 skipped: documented single-file cases), VectorSearch 255 / 0, EF Core 116 / 0, linq2DB 96 / 0
  • regression canaries (AGENTS.md list) 51 / 0
  • SharpCoreDB.CI.slnf builds with 0 errors

…UE constraints (directory storage)

Known issue 8: an UPDATE that moved a primary key onto an existing key was
accepted; the PK index was re-pointed at the moved row and the original row became
unreachable (silent data loss). Every UPDATE path - UpdateAffectedCount (SQL),
UpdateMultipleCore (ExecuteBatchSQL, structured batch API) and UpdateByPrimaryKey -
now validates new key values before any row is written, tracking what earlier
operations of a batch claim and release; a SET of the key matching several rows is
rejected too.

Known issue 9: a UNIQUE column constraint was stored but never checked (the
INSERT-time check was a stub). A single-column UNIQUE now gets an automatic unique
hash index (as SQLite's sqlite_autoindex_*), registered at CREATE TABLE and on
every open. One validator covers single-row INSERT, both batch critical sections
and every UPDATE path; NULLs never conflict.

On the way: a UNIQUE index was enforced only by a single-row INSERT into a columnar
table (not page-based, not multi-row, not UPDATE), all three HashIndex add paths
rejected a second NULL, and a table with a unique index gave up the atomic batched
multi-row INSERT for a per-row loop. The batch core enforces unique indexes now, so
CanUseBatchedInsert no longer excludes them; the test that pinned the old fallback
is replaced by one asserting the batch path is used and still rejects a duplicate.

Every check starts with an allocation-free test that returns at once when a table
or statement has no key to protect.

Not covered: single-file storage (its persisted Unique flag is also written for
every inline PRIMARY KEY column, so it cannot tell a declared UNIQUE from an EF Core
composite key's column - tried and backed out when the EF CompositeKey tests failed;
known issues 10/11) and composite UNIQUE (a, b) on INSERT.

Tests: PrimaryKeyUpdateTests, UniqueConstraintTests (columnar, page-based), red
before. Suites: core 2392/0 (6 skipped), VectorSearch 255/0, EF Core 116/0,
linq2DB 96/0, canaries 51/0. Write-path gate: PASSED, every metric ok.
@sonarqubecloud

Copy link
Copy Markdown

@CastleSoft
CastleSoft merged commit 8c43fe6 into fix/linq2db-adapter-and-scalar-functions Nov 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant