Skip to content

UNIQUE KEY: insert - #123003

Merged
alesapin merged 9 commits into
masterfrom
unique-key/stack-test/insert
Oct 16, 2026
Merged

alesapin merged 9 commits into
masterfrom
unique-key/stack-test/insert

Conversation

@murphy-4o

@murphy-4o murphy-4o commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Part of #103486

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/123003]

Version info

  • Merged into: 26.11.1.77-master (included in 26.11 and later)

…nder their final name

- MergeTreeSink commits a UNIQUE KEY part through UniqueKeyTxnCommit::insert under a per-partition
  transaction; the commit takes the MergeTreeSink, core's IUniqueKeyInsertSink and its header go,
  and the UniqueKeyInsertOutcome the commit returns lives in UniqueKeyTxnCommit.h.
  MergeTreeDataWriter and MergeTreeSinkPatch take the transaction that stamps creation_tid.
- DeleteBitmapFileOps writes a bitmap straight to its final name; isAnyDeleteBitmapFile and the
  two tmp + rename gtests go.
- SSTIndexWriter's duplicate-key message no longer says INSERT-time dedup is unimplemented.
- Tests: 04161_insert_duplicate, 04168 cases 1 and 1b (without case 1's merge lines), 04174
  case 2, 04189; the Keeper feature flags in test_unique_key_sst. The Insert cases that read an
  overwrite back through SELECT land with Read.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@murphy-4o
murphy-4o added this pull request to stack #123004 September 30, 2026 03:17
@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3389770]

Summary: ✅


AI Review

Summary

This PR rewires UNIQUE KEY inserts so block-number allocation and dedup-log registration happen under the partition guard, ignore keeps the new rows in-place via a self delete bitmap instead of rewriting the part, and lost commit replies are waited out before the next writer can enter. The remaining correctness issue is the unknown-status replay path: if the first insert has already published its part but returns UNKNOWN_STATUS_OF_TRANSACTION, the dedup-log entry is dropped too early, so retrying the same insert can stop deduplicating and turn into a fresh UNIQUE KEY conflict.

Findings
  • ❌ Blockers
    • [src/Storages/MergeTree/UniqueKey/UniqueKeyTxn.cpp:162] The lost-reply path still breaks replay semantics. commitTransaction(..., false) can publish the part and then return CommittingCSN; if waitForLostCommitReply later throws UNKNOWN_STATUS_OF_TRANSACTION, InsertCommit::run unwinds before allocation->commit() at src/Storages/MergeTree/UniqueKey/UniqueKeyTxnCommit.cpp:461, so BlockAllocation::~BlockAllocation at src/Storages/MergeTree/UniqueKey/BlockAllocation.cpp:39 drops the dedup-log entry even though the published part may still resolve committed. A client retry of the same block/token then stops deduplicating the first insert and can flip into VIOLATED_CONSTRAINT / extra overwrite work depending on unique_key_conflict_action.
      Suggested fix: keep the dedup-log reservation alive until the transaction is definitively COMMITTED or ROLLED_BACK instead of tying dropPart(part_info) to stack unwinding. One workable shape is to make BlockAllocation release its entry only on an explicit rollback signal from UniqueKeyTxnManager::commitTransaction, while the unknown-status path leaves it registered until the updater thread resolves the transaction.
Tests
  • ⚠️ Add a focused regression test that forces the lost-reply path (commitTransaction(..., false) returning CommittingCSN and waitForLostCommitReply throwing UNKNOWN_STATUS_OF_TRANSACTION), then retries the same insert and verifies the replay still deduplicates if the first transaction later commits, and is cleaned up correctly if it later rolls back.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Keep the dedup-log entry alive across the UNKNOWN_STATUS_OF_TRANSACTION path until the transaction definitively resolves.
    • Add a regression test for the ambiguous-replay case.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 30, 2026
@murphy-4o murphy-4o changed the title [stack experiment] UNIQUE KEY: commit INSERTs through UniqueKeyTxnCommit Commit UNIQUE KEY inserts through UniqueKeyTxnCommit Sep 30, 2026
@murphy-4o murphy-4o changed the title Commit UNIQUE KEY inserts through UniqueKeyTxnCommit UNIQUE KEY: insert Sep 30, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 30, 2026
murphy-4o and others added 2 commits September 30, 2026 14:21
The layer rebased onto 8ed4339, with the review fixes since the first push.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…rupt-bitmap CHECK case into test_unique_key_sst

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3389770aa with master 900576b4c (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

22 object files changed (-17.93 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/UniqueKey/UniqueKeyTxnCommit.cpp.o 430.31 KiB 389.20 KiB -41.12 KiB (-9.56%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

284 translation units recompiled, 2945 s compile time in total, 284 of them have a recent master baseline.

Job report

@murphy-4o
murphy-4o marked this pull request as ready for review September 30, 2026 14:48
A lost reply is waited out holding the guard, so the next writer of the partition sees the
outcome; a failed write rolls back before the guard is released, and the guard is released
before waiting for the csn to load. A write waits for its partition's unresolved parts (a
given-up wait's) before resolving conflicts, instead of the server-wide refusal. A dedup retry of
the rows left after a conflict begins a fresh transaction (04174 case 3).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… flag for the commit waits, shorter commit protocol comments

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/Storages/MergeTree/MergeTreeSink.h Resolved
Base automatically changed from unique-key/stack-test/og-guards to master October 3, 2026 02:52
murphy-4o and others added 2 commits October 4, 2026 09:18
…tStateChange, parts-lock guard

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 32 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: b3a760f7-89b8-409e-93fa-a7d38d657b93
  • MIRAI run: d7c88bca-d8a6-4cba-a7af-e3cd9871c0dd
  • PR check IDs:
    • clickbench_3294316_1791173389
    • clickbench_3294329_1791173389
    • clickbench_3294335_1791173389
    • tpch_adapted_1_official_3294340_1791173389
    • tpch_adapted_1_official_3294353_1791173389
    • tpch_adapted_1_official_3294387_1791173389

@alesapin
alesapin self-requested a review October 16, 2026 12:09
@alesapin alesapin self-assigned this Oct 16, 2026
@alesapin
alesapin added this pull request to the merge queue Oct 16, 2026
Merged via the queue into master with commit 56b4a52 Oct 16, 2026
178 checks passed
@alesapin
alesapin deleted the unique-key/stack-test/insert branch October 16, 2026 13:04
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 2026
@clickhouse-gh clickhouse-gh Bot added comp-unique-key Unique key in MergeTree: uniqueness enforced on insert through a key index (SST / dense index), d... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-unique-key Unique key in MergeTree: uniqueness enforced on insert through a key index (SST / dense index), d... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants