Skip to content

Fix server startup after an interrupted CREATE OR REPLACE on a plain_rewritable database disk - #123832

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-interrupted-create-or-replace-on-plain-rewritable-database-disk
Oct 11, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-interrupted-create-or-replace-on-plain-rewritable-database-disk

Conversation

@groeneai

@groeneai groeneai commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #87656
Related: #117943

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed the server failing to start with TABLE_ALREADY_EXISTS (UUID collision) after it was killed during CREATE OR REPLACE TABLE, REPLACE TABLE or CREATE TABLE ... AS SELECT in an Atomic database whose metadata is on a plain_rewritable disk.

Description

On 26.8.10.6, a server whose Atomic database keeps its metadata on a plain_rewritable disk (database_disk) cannot start after it is killed during CREATE OR REPLACE TABLE: every start fails with Code: 57 ... UUID collision ... (TABLE_ALREADY_EXISTS). Since #117943 the server runs such statements itself for the system.all_* tables at its first log flush, which is how master CI hit it: Integration tests (amd_asan_ubsan, db disk, 8/8), test_dictionary_allow_read_expired_keys/test_dict_get.py::test_simple_dict_get.

The statement renames its _tmp_replace_* table to the target and, after an exchange, drops it. On plain_rewritable a move copies the object and then removes the source, so a kill in between leaves two metadata files with the same UUID.

DatabaseOnDisk::iterateMetadataFiles now removes a _tmp_replace_* metadata file that is byte-identical to the destination of one of its own moves: the <table>.sql whose name hash the temporary name carries, or its metadata_dropped file. This completes the move, next to the cleanup of this move's other leftovers from #88006. Any other file is left as is.

Not covered: a kill inside the emulated EXCHANGE (DiskObjectStorage::renameExchange, used when the target exists) leaves <file>_tmp_rename_exchange, and startup still fails with INCORRECT_FILE_NAME (measured): between its second and third move the other name is recorded nowhere. A user RENAME or DROP leaves the same kind of copy, but the loader cannot tell which way it was going. @ tuanpach, should the exchange emulation record its counterpart so that it can be recovered at load?

Tests: test_database_disk kills the server at the create_or_replace_before_rename failpoint and replays the copy step on the CI database disk. The publish and drop leftovers fail to start on master and on 26.8.10.6 and pass with the fix; a second publish case uses a target named like a temporary table. A copy under an unrelated name still fails. 10 of 10 module runs are green.


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

Version info

  • Merged into: 26.10.1.2161-master (included in 26.10 and later)

…at database load

`CREATE OR REPLACE TABLE`, `REPLACE TABLE` and `CREATE TABLE ... AS SELECT`
on an `Atomic` database fill a `_tmp_replace_<hash>_<suffix>` table, rename
it to the target and, after an exchange, drop it. On a `plain_rewritable`
database disk a file move is a copy of the object followed by a removal of
the source, so a kill between the two leaves both metadata files. They carry
one table UUID, and every later start of the server fails with
`Mapping for table with UUID=... already exists ... (TABLE_ALREADY_EXISTS)`
until the file is removed by hand. Since the `system.all_*` union tables are
created by default, every fresh server runs several such statements at its
first system log flush.

The temporary table is only ever the source of these moves, so when its
metadata file is a byte-identical copy of the destination of one of them,
the move was interrupted after the copy. The destination is either `<T>.sql`
of the same database, where `T` is the table whose name hash the temporary
name carries, or the `metadata_dropped` file of the temporary table itself.
`DatabaseOnDisk::iterateMetadataFiles` now removes such a source (or only
skips it on a read-only disk) before parsing the metadata, which completes
the move. Any other file is untouched: a temporary table without a copy still
loads, and a copy under an unrelated name is still a UUID collision.

The interrupted emulated `EXCHANGE` (`<file>_tmp_rename_exchange`) and user
`RENAME`/`DROP` are not covered: the loader cannot tell their direction.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 4, 2026
@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding; 1 full pass + 1 fix round + 1 delta recheck with 0 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ A user table legally named _tmp_replace_<w>_<w> was excluded from the destination index, so an interrupted publish to it still left the server unstartable (DatabaseOnDisk::iterateMetadataFiles) AGREE, fixed @ 4b3a852 Every .sql is now a candidate destination, never the file itself; new case with target _tmp_replace_abc_def fails without the change and passes with it

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-10:20261004-060100

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A SIGKILL at the create_or_replace_before_rename failpoint, then a byte-exact replay of the copy step on disk_db_remote: the server never starts again (Code: 57 ... UUID collision), on master and on 26.8.10.6.
b Root cause explained? On plain_rewritable a move copies the object and then removes the source. A kill in between leaves the _tmp_replace_* file and its destination with one UUID, and addUUIDMapping throws on every start.
c Fix matches root cause? Yes. The loader completes the interrupted move for the internal temporary table, which is always the source of its moves, next to the existing cleanup of this move's leftovers. No error is swallowed.
d Test intent preserved / new tests added? Three new integration cases: publish leftover, drop leftover, and a copy under an unrelated name, which must still fail.
e Both directions demonstrated? Publish and drop cases fail on base and pass with the fix. Dropping the name-hash check, the byte comparison or the drop rule each reddens a case.
f Fix is general across code paths? Both moves of the temporary table are covered. The emulated EXCHANGE leftover and user RENAME/DROP are measured and documented as not recoverable at this layer.
g Fix generalizes across inputs (params/datatypes/wrappers)? Target absent or present, a target named like the temporary table (_tmp_replace_abc_def), drop leftover, unrelated copy, read-only disk (skip only), truncated copy (refused by the byte comparison).
h Backward compatible? (maintainer-approved exception only) Yes. No setting and no format change. Only a directory that cannot be loaded today is affected.
i Invariants and contracts preserved? Only a byte-identical copy of the move's own destination is removed, so the only complete copy is never deleted. The pass is sequential and runs at database load, before any DDL.

Session id: cron:clickhouse-impl-slot-5:20261004-033200

@groeneai
groeneai requested review from CheSema and tuanpach October 4, 2026 06:27
@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4b3a852]

Summary: ✅


AI Review

Summary

This PR teaches DatabaseOnDisk::iterateMetadataFiles to recognize _tmp_replace_* metadata files that are byte-identical leftovers of an interrupted move on a plain_rewritable database disk, remove or skip the stale source copy before metadata loading, and leave unrelated duplicates untouched. The added integration coverage exercises the publish path, the dropped-metadata path, a target whose name itself matches _tmp_replace_*, and a negative control for an unrelated copy. I do not see an unresolved correctness, safety, or contract issue in the current diff.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 4b3a852.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 39/41 (95.12%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. labels Oct 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4b3a85285 with master 37a251ffa (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

3 object files changed (+33.58 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Databases/DatabaseOnDisk.cpp.o 548.89 KiB 582.46 KiB +33.57 KiB (+6.12%)

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

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Oct 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 11, 2026
Merged via the queue into ClickHouse:master with commit 34e2636 Oct 11, 2026
149 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
XuJia0210 pushed a commit to XuJia0210/ClickHouse that referenced this pull request Oct 12, 2026
…e#123902

The arm was added by ClickHouse#123832, whose CI ran before ClickHouse#123902 was merged.
ClickHouse#123902 makes `DatabaseOrdinary::loadTablesMetadata` remove a deferred
`_tmp_replace_*` metadata file whose UUID matches any table of the
database, whatever its name, and its test 05325 expects the server to
start in that case. So a copy of the temporary table's metadata under an
unrelated name (`u.sql`) is no longer a UUID collision: the server
starts, and the arm's `start_clickhouse(expected_to_fail=True)` failed on
every master run.

The arm now expects the start with `t` and `u` (which keeps the data),
the temporary file removed, and the message of the ClickHouse#123902 rule for this
file. That message can only be logged if the copy rule of ClickHouse#123832 left
the file alone, so the arm still checks that the copy rule only handles
destinations of the temporary table's own moves.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fm4v added a commit that referenced this pull request Nov 1, 2026
The merge queue is meant to fail fast, so its targeted checks stop at 3
failed tests (`--max-failures 3` for `clickhouse-test`, `--maxfail` for
pytest, with the sequential integration phase getting what remains of the
limit and dropped once it is reached), do not rerun the failed integration
tests, and stop at 20 minutes from their start, the budget of the
merge-queue flaky check, instead of 30. Reaching the time limit stops the
tests without failing the job, as before.

The merge-queue flaky check keeps its 20-minute limit and 5 failures.

The changed stateless tests and those `master` changed after the PR's last
CI run run only in the merge-queue flaky check, which already reruns them
since #126406; the targeted
integration check reruns the integration test modules `master` changed.

Related: #123832
Related: #125292
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-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-bugfix Pull request with bugfix, not backported by default 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