Repository navigation
Fix wrong results and a server abort from the query condition cache after a MergeTree table is attached again - #132457
Conversation
StorageMergeTree sets its block number counter from the parts on disk when the table is loaded. After TRUNCATE or ALTER TABLE ... DROP PARTITION, once the removed parts are cleaned up, a table attached again (ATTACH TABLE, ATTACH DATABASE) gives its next part the name of a removed one, for example all_1_1_0. The query condition cache keys its entries by table UUID, part name and condition hash, never evicts the entries of a removed part, and the attach keeps the UUID. So the new part gets the verdicts of the removed one: granules with matching rows are skipped, and when the new part has more marks, filterPartsByQueryConditionCache reads past the cached bitmap and the server aborts on the libc++ hardening check (also on 26.10.5.57). ClickHouse#128677 clears the caches when a table is created with an explicit UUID, but a short ATTACH TABLE does not reach that code, and ATTACH DATABASE loads its tables through DatabaseOrdinary without InterpreterCreateQuery at all. Clear the query condition cache in the StorageMergeTree constructor for a runtime attach (LoadingStrictnessLevel::ATTACH), the only place where block numbers restart. Server startup loads tables with FORCE_ATTACH, when the cache has no entries for them yet. A table without a UUID never has entries, so its attach leaves the cache alone. StorageReplicatedMergeTree takes block numbers from Keeper, which never restart. Only the query condition cache is cleared: the mark, primary index, statistics and columns caches are evicted per part, and the caches keyed by file path (uncompressed, index uncompressed, text index) are handled by ClickHouse#128715, which is why the test pins use_uncompressed_cache = 0. Related: ClickHouse#128715 Related: ClickHouse#128677 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding; 1 full pass).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261117-185600 |
|
cc @hanfei1991, could you review this? Clear the query condition cache when a MergeTree table is attached at runtime, because its next part can reuse the name of a removed part. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261117-170700 |
|
Workflow [PR], commit [19ce319] Summary: ✅
AI ReviewSummaryClears the query condition cache before a UUID-bearing, non-replicated Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 31 s compile time in total, 7 of them have a recent master baseline. |
c25f02d
Backport #132457 to 26.10: Fix wrong results and a server abort from the query condition cache after a MergeTree table is attached again
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results and a possible server abort of queries using the query condition cache when a
MergeTreetable was attached again (DETACH/ATTACH TABLEorATTACH DATABASE) afterTRUNCATEorALTER TABLE ... DROP PARTITION, so that a new part got the name of a removed one.Description
On 26.10.5.57, with default settings, this returns 0 instead of 2:
If the new part has more granules, the read indexes past the cached bitmap and the server aborts (
vector<bool>::operator[] index out of boundsinfilterPartsByQueryConditionCache). The same happens afterDROP PARTITION.StorageMergeTreetakes its block numbers from the parts on disk when the table is loaded, so a table attached again gives its next part the name of a removed one (all_1_1_0). The query condition cache keys entries by table UUID, part name and condition, and keeps them across the attach. #128677 clears the caches onCREATEwith an explicit UUID, which a shortATTACH TABLEandATTACH DATABASEdo not reach.The
StorageMergeTreeconstructor now clears the query condition cache when a table with a UUID is attached at runtime. Server startup is unaffected;ReplicatedMergeTreetakes block numbers from Keeper. The call is in the constructor becauseATTACH DATABASEcreates its tables withoutInterpreterCreateQuery::doCreateTable. This is not the same-UUID case of #116202: it is the same table and the user writes no UUID.The uncompressed and text index caches, keyed by file path, have the same problem on this trigger and are fixed in #128715, so the test pins
use_uncompressed_cache = 0. CallingclearCaches()here instead would cover both.The new test fails on master (wrong result, then the abort) and passes with the fix, 50/50 with randomized settings.
Related: #128715
Related: #128677
Workflow [PR]
Sync PR [sync-upstream/pr/132457]
Version info
26.11.1.2707-master(included in26.11and later)26.10.8.8