Skip to content

Cleanup directory for table data if DatabaseAtomic::commitCreateTable() throws an exception. - #116247

Open
w-i-a-n wants to merge 4 commits into
ClickHouse:masterfrom
w-i-a-n:database-atomic-commit-create-table-failure-cleanup
Open

w-i-a-n wants to merge 4 commits into
ClickHouse:masterfrom
w-i-a-n:database-atomic-commit-create-table-failure-cleanup

Conversation

@w-i-a-n

@w-i-a-n w-i-a-n commented Aug 25, 2026 •

Copy link
Copy Markdown

When DatabaseAtomic::commitCreateTable() throws an exception, such as due to a transient KEEPER_EXCEPTION in case of Replicated database, the catch block is only cleaning up the tmp metadata directory for the table from disk.

We should also cleanup the data directory for the table in case an exception is thrown in this function.

Following test cases are added to demonstrate the fix with the help of a SYSTEM FAILPOINT (database_atomic_commit_create_table_failure):

  1. Functional test for Atomic database.
  2. Integration test for Replicated database.

Patch Disclaimer:

This patch is completely handwritten by the author. As such, it is prone to human-error on part of the author.

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):

Cleans up directory for table data when DatabaseAtomic::commitCreateTable() throws an exception. Closes #116246.


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

@CLAassistant

CLAassistant commented Aug 25, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@w-i-a-n

w-i-a-n commented Aug 25, 2026

Copy link
Copy Markdown
Author

@tavplubix I know you had added the allow_moving_table_directory_to_trash setting to fix the TABLE_ALREADY_EXISTS error for the Replicated database.

I ran into another edge case for TABLE_ALREADY_EXISTS error in Atomic database that this PR fixes. Would you be able to review this PR? Thanks.

@clickhouse-gh

clickhouse-gh Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [142fedf]

Summary: ✅


AI Review

Summary

This PR teaches DatabaseAtomic::commitCreateTable to clean the table UUID directory on the failure path and adds stateless plus replicated tests for the TABLE_ALREADY_EXISTS retry case. The earlier cross-disk cleanup bug looks fixed, but the rollback is still incomplete for engines that create storage-owned external state before publication, so I would not merge this as-is.

PR Metadata
  • Changelog category: Bug Fix matches the change.
  • Changelog entry: required, but the current text is too implementation-oriented and includes Closes #116246, which belongs in the linked-issues block rather than the changelog entry. Suggested replacement: Retrying \CREATE TABLE` no longer fails with `TABLE_ALREADY_EXISTS` when `DatabaseAtomic::commitCreateTable` throws before the table is published.`
  • Add Closes: https://github.com/ClickHouse/ClickHouse/issues/116246 on its own line in the PR body.
Findings

❌ Blockers

  • [src/Databases/DatabaseAtomic.cpp:464] This rollback only removes store/<uuid> and never invokes storage-owned cleanup. Fresh StorageObjectStorage tables can already mutate external state before commitCreateTable runs (configuration->create(...) in src/Storages/ObjectStorage/StorageObjectStorage.cpp, and Iceberg can write metadata files plus register the table in the external catalog in src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp). If commitCreateTable throws after that, the local UUID directory is gone but the remote/catalog table survives, so retries can still fail with already exists and the create is no longer atomic.
  • Suggested fix: run the storage-specific rollback for fresh CREATEs on this path (drop() where safe, or an engine-specific equivalent for ReplicatedMergeTree) and then keep removeTableDataFromDisk as the filesystem cleanup.
Tests
  • The added stateless and integration tests cover local-disk and ReplicatedMergeTree retry paths, but they do not exercise the storage-owned rollback gap above, so this regression would stay invisible.

LLVM Coverage Report

Measured on commit 142fedf.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.50% 81.50% +0.00%

Changed lines: Changed C/C++ lines covered: 37/37 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 25, 2026
@w-i-a-n
w-i-a-n force-pushed the database-atomic-commit-create-table-failure-cleanup branch 2 times, most recently from e84f86d to 5bf5c2d Compare August 27, 2026 04:02
Comment thread src/Databases/DatabaseAtomic.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 142fedfbc with master 0d04f2bc2 (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.

Compile time of recompiled translation units

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

Job report

@w-i-a-n
w-i-a-n force-pushed the database-atomic-commit-create-table-failure-cleanup branch from 5bf5c2d to 094cfe6 Compare September 2, 2026 18:33
@w-i-a-n

w-i-a-n commented Sep 2, 2026

Copy link
Copy Markdown
Author

I have pushed an update, please see my inline comment. I have also expanded the stateless test for scenario when the table is created with custom disk settings (type=local with custom path, and type=object_storage with custom path).

@clickhouse-gh clickhouse-gh Bot added the comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. label Sep 4, 2026
pull Bot pushed a commit to peijunlin2008/ClickHouse that referenced this pull request Sep 5, 2026
`FuzzerLogParser.parse_failure` took the first line of the server log
matching one of its failure patterns, no matter where in the line the
match was. A query the server logs is data, and both a test comment and
a fuzzed query may contain any text, so a line such as

  ... <Error> executeQuery: Code: 394. DB::Exception: Query was cancelled.
  ... (in query: -- Consuming the rowless chunk is `Logical error:
  'max_rows > 0'` in debug and sanitizer builds. Every

was reported as the failure. In the `Stress test (amd_msan)` run of
ClickHouse#115707 that hid the real
failure, `Logical error: 'Can't determine table for parallel replicas'`
(issue ClickHouse#113292), behind
a `max_rows > 0` name that names no failure at all, and the same
mis-titled name was also recorded for
ClickHouse#116247. The name goes to
the CI database and to automatically created fuzzer issues, so such a
match splits one failure into per-quote names and buries the failure
that actually happened.

Skip a match that is preceded on its line by `(in query:` or `(query:`
and keep searching. Only the part of the line before the match is
checked, so a real failure that quotes the query it happened on keeps
its name. The query id of the failed query and the `Format string:`
line are now taken from the failure that was found rather than searched
for again, so they cannot come from a different, quoting line either.

Matching a stack trace string no longer goes through
`echo '<string>' | rg`: a log line contains single quotes, which
terminated the shell quoting, so the quotes were dropped from the
failure name (and the log content reached the shell).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@tavplubix

tavplubix commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Should we just change the setting? #116246 (comment)

@w-i-a-n

w-i-a-n commented Sep 8, 2026

Copy link
Copy Markdown
Author

Refer to my comment: #116246 (comment) , the test tests/queries/0_stateless/04614_database_atomic_commit_create_table_cleanup.sh would still fail without this fix, regardless of whether allow_moving_table_directory_to_trash is enabled or not.

…() throws an exception.

When DatabaseAtomic::commitCreateTable() throws an exception, such as due to a transient
KEEPER_EXCEPTION in case of Replicated database, the catch block is only cleaning up the
tmp metadata directory for the table from disk.

We should also cleanup the data directory for the table in case an exception is thrown in this
function.

Following test cases are added to demonstrate the fix with the help of a SYSTEM FAILPOINT
(database_atomic_commit_create_table_failure):
  1. Functional test for Atomic database.
  2. Integration test for Replicated database.
…itCreateTable()

throws an exception.

Add a new function, DatabaseCatalog::removeTableDataFromDisk, and move the code from
DatabaseCatalog::dropTableFinally that handles table data cleanup from disk into
this new function, and reuse this function in the catch block of
DatabaseAtomic::commitCreateTable. This way, the semantics of cleaning table data
from disk is consistent between DatabaseCatalog::dropTableFinally and
DatabaseAtomic::commitCreateTable.

Expand the stateless test to also add scenarios for custom table disks settings
(type=local with custom path, type=object_storage with custom path).
use the CLICKHOUSE_DISKS_FILES env var instead of hardcoding the local
disk path to /var/lib/clickhouse/disks/. Also add the no-shared-merge-tree
tag to this test.
DatabaseAtomic::commitCreateTable for CREATE TABLE statements and not
for ATTACH TABLE statements. For ATTACH TABLE, this can accidentally
cleanup actual data for a table which was previously detached, for
instance, with "ATTACH TABLE ... UUID <previously detached table uuid> ..."
query.
@w-i-a-n
w-i-a-n force-pushed the database-atomic-commit-create-table-failure-cleanup branch from d948a3b to 142fedf Compare September 14, 2026 03:13
@w-i-a-n

w-i-a-n commented Sep 14, 2026

Copy link
Copy Markdown
Author

Pushing a change as per the comment here: #116246 (comment)

// for instance, with the "ATTACH TABLE ... UUID <previously detached table uuid> ..."
// query).
if (!query.attach)
DatabaseCatalog::instance().removeTableDataFromDisk(table->getStorageID(), table);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rollback is still only the filesystem half of a failed CREATE undo. Fresh StorageObjectStorage tables can already mutate external state before commitCreateTable runs: the constructor calls configuration->create(...) in src/Storages/ObjectStorage/StorageObjectStorage.cpp:198-202, and for Iceberg that path can write metadata files and register the table in the external catalog (src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp:880-916).

If commitCreateTable throws afterwards, this branch removes store/<uuid> but never runs the storage-owned rollback (StorageObjectStorage::drop), so the remote/catalog table survives and a retry can still fail with already exists. Can we keep removeTableDataFromDisk for the local UUID directory but also run a storage-specific rollback for fresh CREATEs here (drop() where that is safe, or an engine-specific equivalent for ReplicatedMergeTree)?

@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026

This branch has not been deployed

No deployments
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. external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Directory for table data is not getting cleaned-up when DatabaseAtomic::commitCreateTable() throws an exception.

5 participants