Skip to content

Reject UNIQUE KEY on disks that do not support writing with append - #128898

Merged
murphy-4o merged 6 commits into
ClickHouse:masterfrom
groeneai:unique-key-reject-disks-without-transactions
Nov 5, 2026
Merged

murphy-4o merged 6 commits into
ClickHouse:masterfrom
groeneai:unique-key-reject-disks-without-transactions

Conversation

@groeneai

@groeneai groeneai commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Description

A UNIQUE KEY insert commits its part under a transaction, but StorageMergeTree supports transactions only if every disk supports writing with append (plain, plain_rewritable do not). On such a disk the part is not stamped with the transaction and the commit throws LOGICAL_ERROR (CreationTID ... is set to unexpected value), seen in La Casa Del Dolor.

UNIQUE KEY tables now require supportsTransactions, else SUPPORT_IS_DISABLED:

  • at CREATE;
  • on INSERT, for tables attached without the CREATE check;
  • on an ALTER that switches to a storage policy with such a disk.

The insert layer (#123003) is not in any release, hence not for changelog.


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

Version info

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

A `UNIQUE KEY` insert opens an internal `MergeTreeTransaction` and commits its part
under it, but `MergeTreeDataWriter` stamps the transaction on the part only when the
table supports transactions (`StorageMergeTree::support_transaction`, false when a disk
of the policy cannot append, e.g. `plain`/`plain_rewritable` metadata). The part then
carries `NonTransactionalTID` and `IMergeTreeDataPart::assertHasVersionMetadata` throws
`LOGICAL_ERROR` "CreationTID of part ... is set to unexpected value".

Refuse such tables with `SUPPORT_IS_DISABLED`, keyed on `support_transaction` itself:
at `CREATE` in the `StorageMergeTree` constructor (attach and server startup still load
existing tables), and in `StorageMergeTree::write` for tables that exist without passing
that check (created by an older version, full `ATTACH ... UUID`). Object storage with
local metadata (e.g. `s3_disk`) keeps working. The same refusal applies to an
`ALTER ... MODIFY SETTING storage_policy` that adds such a disk to a `UNIQUE KEY` table
(`MergeTreeData::checkAlterEligibility`).
`supportWritingWithAppend` now unwraps every delegate layer, so stacked `encrypted` disks
are classified like single ones. `write` checks the table's current disks, so a
storage-configuration reload that adds such a disk does not bypass the refusal.

Found by `La Casa Del Dolor (amd_debug)` on master:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=d8d24e15f18c1584567da000d2c2f75b4df33fca&name_0=MasterCI&name_1=La%20Casa%20Del%20Dolor%20%28amd_debug%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Internal review (3 rounds)
  • ⚠️ Gate B stale-policy-capability (r1): AGREE (major). ALTER ... MODIFY SETTING storage_policy could add a disk without append support to a UNIQUE KEY table; refused at ALTER since r1.
  • ⚠️ Gate B nested-wrapper-capability (r2): AGREE (major). supportWritingWithAppend unwrapped one layer only, so a stacked encrypted disk over plain_rewritable passed every refusal; it unwraps every layer since r2.
  • ⚠️ Gate B stale-policy-reload (r2): AGREE (major). A config reload that adds such a disk to the table's policy was not refused at INSERT; write checks the current disks since r2.
  • Gate B r3: no findings.
  • 💡 own nits, left as is: three walks of the same predicate; the ALTER check also runs on a Replicated-database replay, like the table_disk check above it; an INSERT already running when a reload extends the policy is not re-checked.

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CREATE TABLE t (c0 Int32) ENGINE = MergeTree ORDER BY c0 UNIQUE KEY (c0) SETTINGS disk = 'local_plain_rewritable'; INSERT INTO t VALUES (1) aborts a debug master server 1/1 with the CI message and frames.
b Root cause explained? The UNIQUE KEY insert commits under an internal transaction, but the writer stamps NonTransactionalTID when supportsTransactions is false (non-appendable disk metadata), so assertHasVersionMetadata throws.
c Fix matches root cause? Yes. StorageMergeTree refuses UNIQUE KEY with the disk check behind support_transaction (the value the writer reads): at CREATE, and in write against the table's current disks, before any part exists; an ALTER that changes the storage policy is refused when the new policy has such a disk. The check unwraps every wrapper layer.
d Test intent preserved / new tests added? New test 05493_unique_key_disk_without_transactions: CREATE, wrapper and stacked-wrapper disks, MV, refreshable MV, full ATTACH + INSERT, s3_disk positive arm. New test_unique_key_sst cases: a storage-policy ALTER adding a plain_rewritable disk is refused, and so is an INSERT after a config reload adds one.
e Both directions demonstrated? Unfixed: expected-error mismatches, then the INSERT aborts the server. Fixed: 50/50 randomized, 20/20 parallel, 10/10 async insert. Removing any check, keying on the old local-only predicate, unwrapping one layer only, or checking write against the construction-time flag turns a test red; the ALTER and reload cases fail on master because the ALTER and the INSERT succeed.
f Fix is general across code paths? write is the only constructor of MergeTreeSink, the only UNIQUE KEY commit path on master; covered for MV inner tables, refreshable MVs, async inserts, ATTACH, tables loaded at startup, a storage policy ALTER (test_unique_key_switch_policy_to_s3 still passes) and a config reload that extends the policy.
g Fix generalizes across inputs (params/datatypes/wrappers)? Disk kinds: plain_rewritable, encrypted/cache wrappers and stacked encrypted wrappers refused; local-metadata object storage and the default disk accepted. Column types do not affect the check.
h Backward compatible? (maintainer-approved exception only) No setting or format change. Existing tables on such disks still load and read; their INSERT now fails SUPPORT_IS_DISABLED instead of LOGICAL_ERROR. A table on stacked encrypted disks over such a disk is now treated like one on a single encrypted disk (no transactions, deduplication log rewritten instead of appended).
i Invariants and contracts preserved? Refused CREATE writes nothing to the disk; ATTACH, secondary create and startup keep loading (same strictness rule as the UNIQUE KEY TTL/projection rejects).

Session id: cron:clickhouse-impl-slot-5:20261103-053600

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @murphy-4o @CheSema, could you review this and add the can be tested label so CI runs? A UNIQUE KEY insert commits its part under a transaction, but on a disk whose metadata cannot append (plain_rewritable, also behind encrypted or cache wrappers) the part keeps NonTransactionalTID and the commit throws a logical error; such tables are now refused at CREATE, at a storage-policy ALTER and at INSERT, and supportWritingWithAppend unwraps every wrapper layer.

Comment thread src/Storages/StorageMergeTree.cpp Resolved
Comment thread src/Storages/StorageMergeTree.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [56702ba]

Summary: ✅


AI Review

Summary

The PR rejects UNIQUE KEY creation and inserts when the table lacks transaction support, and validates destination storage policies for ALTER, including RESET SETTING. ⚠️ Request changes: the existing configuration-reload finding remains valid at head 56702ba97b075d5a8e1091b3784289fff5e81a49.

Findings

⚠️ Majors

  • src/Storages/StorageMergeTree.cpp:527 — A live policy reload bypasses the transaction-capability guard. support_transaction is constant after construction, while MergeTreeData::getStoragePolicy resolves the current policy. Adding a plain_rewritable disk to the same policy through a configuration reload bypasses checkStoragePolicy; subsequent inserts still pass this guard and can write transactional parts onto that disk. On reattach or restart, the flag becomes false and VersionMetadataOnDisk disables metadata writes for all parts, including previously valid transactional parts on the original disk. CSN reconciliation or TRUNCATE can then throw LOGICAL_ERROR (was involved in transaction but cannot write metadata). Validate the live policy and prevent reloads from invalidating recovery/removal of existing transactional parts; checking inserts alone does not protect those parts. This is the existing unresolved inline finding; no duplicate comment was posted.
Tests
  • ⚠️ Add a focused case in the existing integration cluster: create and populate a supported UNIQUE KEY table, reload its policy with an added disk lacking append support, and verify both subsequent insert handling and safe reattach/recovery/removal of the existing parts. The current added tests cover CREATE, attached-table inserts, and SQL policy changes, but omit configuration reloads. This review traced the failure through source; no local build was available to rerun it.
Final Verdict

⚠️ Request changes. Close the configuration-reload gap while preserving existing transactional metadata, and add the focused regression coverage above.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 3, 2026
Comment thread src/Disks/supportWritingWithAppend.cpp Resolved
Disks know nothing about transactions: what the check tests is
`supportWritingWithAppend`, and a table supports transactions only when
all its disks do (`supportTransaction` in `StorageMergeTree.cpp`). Rename
the helper to `checkDisksSupportWritingWithAppend`, and say "does not
support writing with append" in its comment, in the `ALTER` check's
comment and in both error messages. The test is renamed to match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Reject UNIQUE KEY on disks that do not support transactions Reject UNIQUE KEY on disks that do not support writing with append Nov 3, 2026
… UNIQUE KEY

Unwrapping every delegate layer in `supportWritingWithAppend` also turned
off `supportsTransactions` for existing tables without `UNIQUE KEY` on a
stacked `encrypted` disk. A transactional part written there by the
previous version then fails to load: `VersionMetadataOnDisk` gets
`can_write_metadata = false`, and `loadAndUpdateMetadata` throws
`LOGICAL_ERROR` "was involved in transaction but cannot write metadata"
(measured: master writes one part with `implicit_transaction`, restart
into the previous head aborts at startup 3/3, master and this commit
load it and `TRUNCATE` 3/3 and 4/4).

Restore the function as on master, so tables without `UNIQUE KEY` are not
affected, and make the two `UNIQUE KEY` checks test the innermost disk.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 56702ba97 with master a8e6d0bf4 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 7f11488e0; 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.

Compile time of recompiled translation units

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

Job report

@murphy-4o murphy-4o self-assigned this Nov 4, 2026
Comment thread src/Storages/StorageMergeTree.cpp Resolved
@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 56702ba

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Upgrade check (amd_release) / Error message in clickhouse-server.log (see upgrade_error_messages.txt) not from this PR (it does not touch TTL): the log scan catches ILLEGAL_DIVISION intDiv(100, x) While executing TTL from a background merge, which is the intentionally failing case of 05073_column_ttl_recompute_materialized. Same signature on 42 other PRs since 2026-10-24 #127731 (mine, open)
Mergeable Check, PR praktika rollups of the row above #127731 (the owner of the row above)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261104-083003

@murphy-4o
murphy-4o added this pull request to the merge queue Nov 5, 2026
Merged via the queue into ClickHouse:master with commit b02fbee Nov 5, 2026
366 of 367 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. 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.

4 participants