Skip to content

Fix cache key collisions when using table_disk - #96818

Merged
rienath merged 10 commits into
masterfrom
fix-table-disk-plain-rewritable
Feb 21, 2026
Merged

rienath merged 10 commits into
masterfrom
fix-table-disk-plain-rewritable

Conversation

@rienath

@rienath rienath commented Feb 13, 2026 •

Copy link
Copy Markdown
Member

... by including disk name in cache hash keys. With table_disk, two separate web disks can have identical relative part paths. This caused cache collisions in mark cache, primary index cache, text index caches, and vector similarity index cache.

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 cache key collisions between tables with table_disk=1 setting

Version info

  • Merged into: 26.2.1.991

It was created to read external data. table_disk assumes the disk is only used by a single table, with no directory-level isolation between tables. Allowing writes would make it possible to create multiple tables pointing to the same disk and insert into both, corrupting the data
@clickhouse-gh

clickhouse-gh Bot commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [15ca537]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, old analyzer, s3 storage, DatabaseReplicated, sequential) failure
01111_create_drop_replicated_db_stress FAIL cidb, issue ISSUE CREATED
BuzzHouse (arm_asan) failure
Logical error: Digest does not match (STID: 5029-4dec) FAIL cidb, issue ISSUE EXISTS

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label Feb 13, 2026

Copilot AI left a comment

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.

Pull request overview

This PR fixes critical cache key collisions and data corruption issues when using the table_disk=1 setting by (1) including disk names in all cache hash keys to prevent collisions when different disks have identical relative part paths, and (2) restricting table_disk to read-only disks only to prevent data corruption from multiple tables writing to the same writable disk without directory isolation.

Changes:

  • Added disk name prefix (diskname:) to all cache key generation for mark cache, primary index cache, text index caches (header, dictionary block, postings), and vector similarity index cache
  • Modified table_disk validation to reject writable disks and only allow read-only disks
  • Updated existing test to use read-only plain metadata instead of plain_rewritable
  • Added comprehensive tests for cache key collision prevention and writable disk rejection

Reviewed changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Storages/MergeTree/MergeTreeSettings.cpp Changed validation logic to restrict table_disk to read-only disks only; updated documentation comment
src/Storages/MergeTree/MergeTreeMarksLoader.cpp Added disk name prefix to mark cache key generation in loadMarksSync, loadMarksAsync, and addMarksToCache
src/Storages/MergeTree/IMergeTreeDataPart.cpp Added disk name prefix to primary index cache keys in loadIndexToCache, moveIndexToCache, removeIndexFromCache, and vector similarity cache in removeFromVectorIndexCache
src/Storages/MergeTree/MergeTreeIndexText.cpp Added disk name prefix to text index cache keys (header, dictionary block, and postings caches)
src/Storages/MergeTree/MergeTreeIndexReader.cpp Added disk name prefix to vector similarity index cache key generation
src/Storages/MergeTree/MergeTreeDataPartWide.cpp Added disk name prefix to mark cache key in removeMarksFromCache
src/Storages/MergeTree/MergeTreeDataPartCompact.cpp Added disk name prefix to mark cache key in removeMarksFromCache
src/Storages/StorageMergeTree.cpp Removed trailing period from error message
src/Storages/StorageReplicatedMergeTree.cpp Removed trailing period from error message
tests/queries/0_stateless/03824_table_disk_rejects_writable_disks.sql New test verifying writable disks are rejected and read-only disks are accepted for table_disk
tests/queries/0_stateless/03824_table_disk_rejects_writable_disks.reference Empty reference file (test expects error for writable disk, success for readonly)
tests/queries/0_stateless/03823_table_disk_cache_key_collision.sql New test creating two tables on separate web disks to verify no cache collisions
tests/queries/0_stateless/03823_table_disk_cache_key_collision.reference Expected output showing both tables can be queried independently
tests/queries/0_stateless/03362_merge_tree_with_background_refresh.sh Updated to use standard writer table and read-only plain disk for reader table instead of plain_rewritable
tests/queries/0_stateless/03355_mergetree_table_disk.sql Updated comment to reflect that table_disk only supports s3_plain/web (removed s3_plain_rewritable)

Comment thread src/Storages/StorageMergeTree.cpp Resolved
Co-authored-by: Copilot <[email protected]>
@Michicosun Michicosun self-assigned this Feb 13, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Restricting table_disk to read-only disks only.

Please do not. We use it for writable disks.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

.

@alexey-milovidov alexey-milovidov self-assigned this Feb 13, 2026
@rienath

rienath commented Feb 13, 2026

Copy link
Copy Markdown
Member Author

Please do not. We use it for writable disks

@alexey-milovidov are there any alternatives to prevent data corruption? We used it in two tests and I patched them

@alexey-milovidov

Copy link
Copy Markdown
Member

Check that for every disk with "table_disk" there is only one table where it is not "readonly".

@rienath

rienath commented Feb 18, 2026

Copy link
Copy Markdown
Member Author

Check that for every disk with "table_disk" there is only one table where it is not "readonly".

@alexey-milovidov thanks for the suggestion! Now we allow one non-readonly table per disk


for (const auto & existing_disk : existing_merge_tree->getDisks())
{
if (existing_disk->getName() == disk_name && !existing_disk->isReadOnly())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can still register 2 disks with different names but the same settings. In this case, this check will not find them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's use disk->getDataSourceDescription()->sameKind


/// Ensures that for a given disk used with `table_disk`, at most one table is non-readonly. table_disk assumes no directory-level isolation
/// between tables on the same disk, so allowing multiple writable tables would risk data corruption.
static void validateTableDiskUniqueness(const MergeTreeData & merge_tree, ContextPtr context)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's move this code to registerStorageMergeTree.cpp


validateStorage(*res, mode);

/// Validate that for a given disk used with `table_disk`, at most one table is non-readonly

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

also to registerStorageMergeTree.cpp

@rienath
rienath force-pushed the fix-table-disk-plain-rewritable branch from fc5719e to 15ca537 Compare February 20, 2026 15:16
@rienath

rienath commented Feb 20, 2026 •

Copy link
Copy Markdown
Member Author

In discussion with @azat and @Michicosun we realised the current solution to data corruption is not thorough enough. We prevent only the situation where two disks share the same name. However, we can create two disks with different names, but equal endpoints through a config. disk->getDataSourceDescription()->sameKind suggestion can prevent it, but the user can still get a situation where they have a writable and read-only disks looking at the same endpoint. Writing would break the read-only disk. We need to think this through better and not mix it with cache collision fix.

As such, I changed this PR to solve only a single problem.

@rienath rienath changed the title Fix cache key collisions and data corruption when using table_disk Fix cache key collisions when using table_disk Feb 20, 2026
@rienath
rienath added this pull request to the merge queue Feb 21, 2026
Merged via the queue into master with commit d7fa2a2 Feb 21, 2026
142 of 144 checks passed
@rienath
rienath deleted the fix-table-disk-plain-rewritable branch February 21, 2026 00:46
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backward-incompatible Pull request with backwards incompatible changes 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.

5 participants