Repository navigation
TTL CLEAR INDEX for MergeTree parts - #108069
EmeraldShift wants to merge 6 commits into
Conversation
|
Workflow [PR], commit [0b19b0c] Summary: ❌
AI ReviewSummaryPR #108069 adds Findings
Tests
Final VerdictStatus: Defer early index omission when TTL actions can change the deadline inputs, and add the focused column-TTL regression. Keep the patch-update issue tracked in PR #116175. |
3537298 to
42ec9f6
Compare
42ec9f6 to
f39aa26
Compare
f39aa26 to
4932a29
Compare
4932a29 to
62ae599
Compare
2b12024 to
1adee2e
Compare
1adee2e to
d660f02
Compare
d660f02 to
70f883a
Compare
e386731 to
1cba4cc
Compare
|
Replies to the AI review of 55a3575:
|
|
@EmeraldShift Could you solve the conflicts with master please? |
55a3575 to
250ae10
Compare
|
CI says:
|
250ae10 to
e48171c
Compare
e48171c to
efcb13e
Compare
efcb13e to
1c04c0a
Compare
| if (!ctx->force_ttl && !global_ctx->ttl_merges_blocker->isCancelled()) | ||
| for (const auto & [index_name, _] : getIndexesWithExpiredClearTTL( | ||
| *global_ctx->metadata_snapshot, global_ctx->new_data_part->ttl_infos, global_ctx->time_of_merge)) | ||
| exclude_index_names.insert(index_name); |
There was a problem hiding this comment.
[P2] Recalculate index-clear TTL bounds when applying patches
force_ttl only detects missing TTL entries, so a lightweight update to a column used by CLEAR INDEX leaves this decision using the original parts' bounds. Index-clear TTLs do not contribute to part_min_ttl, so an index-only TTL does not cause TTLTransform to recalculate them when the merge applies patches.
Reproduced with this PR's ARM CI binary: create a table with TTL d + INTERVAL 1 DAY CLEAR INDEX idx and apply_patches_on_merge = 1, insert two parts dated 2100-01-01, run UPDATE ... SET d = '2000-01-01' WHERE 1, then OPTIMIZE ... FINAL. Reading with apply_patch_parts = 0 confirms that every stored row is now dated 2000-01-01, but the merged part still reports index_clear_ttl_info.max = ['2100-01-02 00:00:00'] and retains the index. Subsequent dedicated clears also trust this bound, so the expired index is retained until the old deadline. The inverse update removes the index despite every output row having a future deadline.
Please recalculate affected index_clear_ttl bounds from patched rows and defer index omission/pruning until those bounds are available. Cover both update directions, including dedicated cleanup while TTL-changing patches are pending.
There was a problem hiding this comment.
Master has the same problem. A merge that applies patches keeps the source parts' TTL bounds, so a lightweight update to d leaves stale bounds for DELETE and RECOMPRESS too. I'd rather fix that for every TTL kind in a separate PR. @groeneai could you take it? I think the fix is to set force_ttl when the patches update a column that a TTL expression reads. This PR already handles force_ttl.
There was a problem hiding this comment.
This is already fixed by #116175 (open, by @ valerypetrov): a merge that applies patch parts recalculates the TTL infos from the patched rows. I ran your scenario on master (1df150bae26c) and on that PR's CI build (80e591e425fb): TTL d + INTERVAL 1 DAY, two parts at 2100-01-01, UPDATE ... SET d = '2000-01-01' WHERE 1, then OPTIMIZE ... FINAL.
- DELETE: on master both rows survive two merges with
delete_ttl_info_max = 2100-01-02. With Recalculate TTL infos on merges that apply patch parts #116175 the first merge drops them. - RECOMPRESS: on master the part stays
LZ4withrecompression_ttl_info.max = 2100-01-02. With Recalculate TTL infos on merges that apply patch parts #116175 the first merge corrects the bound to2000-01-02(stillLZ4, because the codec is chosen before the rows are read), and the next merge recompresses toZSTD(3).
So I won't open a duplicate PR.
…alization `ParserTTLElement` accepts `WHERE` only after `DELETE`, and `TTLDescription` applies it only for `DELETE`, but `ASTTTLElement::readJSON` restored `where_expr` for every mode. A `clickhouse_json` `CREATE TABLE ... TTL d TO DISK 'default'` carrying a `where_expr` was therefore accepted, the `WHERE` was ignored, and `formatImpl` stored `TTL d TO DISK 'default' WHERE x`, which fails to parse at the next start (`Code: 62`), so the database does not load. Same for `RECOMPRESS` and `GROUP BY`; `formatQueryFromJSON` returned the same unparsable SQL. Reject `where_expr` unless the mode is `DELETE`, like the existing per-mode checks for the codec, destination and `GROUP BY` fields in the same function. The check sits at the read site so it does not textually overlap ClickHouse#108069 (which adds the same check for its new `CLEAR INDEX` mode) or ClickHouse#121585. Requested in ClickHouse#108069 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
1c04c0a to
bddbbca
Compare
A `TTL expr CLEAR INDEX idx` rule names a skip index whose files a part doesn't need once every row in it has expired. This commit only parses and validates the rule and records its min and max TTL in each part's `ttl.txt`, which `system.parts` shows as `index_clear_ttl_info`. Nothing deletes index files yet.
A merge leaves out an index whose `CLEAR INDEX` rule has expired for all of its source rows. A merge that recalculates TTL info, like a TTL delete, only learns which indexes expired after writing the part, so it deletes their files then, rewriting `skp_idx.packed` if needed.
A `TTLClearIndex` merge takes one part and hardlinks every file except the expired index files into the new part, copying where it can't hardlink. It loads the new part from disk without reading any rows, and the new part keeps the source part's data version. Nothing selects these merges yet.
A part that never merges again, like one at the maximum size, keeps its expired indexes. Merge selection now picks a `TTLClearIndex` merge for any part with an expired index whose files it can hardlink. These merges count as TTL merges, ignore the merge size limits, and are picked after the other TTL merges. The MergeTree setting `ttl_clear_index_merges` turns these merges off. It's on by default, and setting `compatibility` to an older version turns it off during a rolling upgrade.
A `TTLClearIndex` merge is cheaper than a fetch, so every replica of a `ReplicatedMergeTree` runs the merge on its own copy of the part, copying the files if the replica can't hardlink them. A replica fetches the result only if its part has a different format or it can't reserve space. The size limit on merges doesn't apply to these entries.
bddbbca to
0b19b0c
Compare
| if (!ctx->force_ttl && !global_ctx->ttl_merges_blocker->isCancelled()) | ||
| for (const auto & [index_name, _] : getIndexesWithExpiredClearTTL( | ||
| *global_ctx->metadata_snapshot, global_ctx->new_data_part->ttl_infos, global_ctx->time_of_merge)) | ||
| exclude_index_names.insert(index_name); |
There was a problem hiding this comment.
[Major] Preserve the index when a column TTL changes its deadline
force_ttl stays false for an ordinary merge applying an already-calculated column TTL. That merge can change a CLEAR INDEX input, so the source bounds used here do not establish that the output rows have expired. The final pruning step cannot restore an index already excluded here.
Reproduced with this head's Fast test binary:
CREATE TABLE t
(
k UInt64, expiry Date,
d Date DEFAULT toDate('2100-01-01') TTL expiry,
v UInt64, INDEX idx v TYPE minmax
)
ENGINE = MergeTree ORDER BY k
TTL d + INTERVAL 1 DAY CLEAR INDEX idx
SETTINGS ttl_clear_index_merges = 0,
max_number_of_merges_with_ttl_in_pool = 0,
min_bytes_for_wide_part = 0, min_bytes_for_full_part_storage = 0;
INSERT INTO t VALUES (1, '2000-01-01', '2000-01-01', 1);
OPTIMIZE TABLE t FINAL;The output rows have d = '2100-01-01' and system.parts.index_clear_ttl_info.max = ['2100-01-02 00:00:00'], but secondary_indices_compressed_bytes = 0. A lone resulting part can keep scanning without its index indefinitely despite its future deadline. No patch parts are involved.
Please defer omission whenever the merge can change the TTL inputs, build the index in that case, and let the final bounds decide whether to prune it. Add a stateless regression for this column-TTL case.
|
Dear @Ergus, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Closes: #107804
Closes: #78714
Add a
CLEAR INDEXTTL action that deletes a skip index's files from parts once all their rows are past the TTL.The index stays in the table definition and new parts still build it. We want this for log tables, where the text index can be many times larger than the data, and almost every search is over the last few days.
Regular merges already rewrite parts, so they just leave out any index whose TTL has expired for all of their source rows. That covers most parts and doesn't need a setting. TTL delete merges recalculate TTL info as they go, so they only find out which indexes expired after writing the part, and delete those files then.
Parts at the merge size limit never merge again though, so they'd keep their index forever. For those we add a
TTLClearIndexmerge type, behind the MergeTree settingttl_clear_index_merges(on by default). It hardlinks everything except the expired index files, rewritesskp_idx.packedif needed, writeschecksums.txt, and loads the new part from disk. It never reads rows. The new part keeps the source part's data version, so pending mutations likeRENAME COLUMNstill apply to it.These merges only take parts whose files we can hardlink, and we let them skip the merge size limits since hardlinking costs about the same for any size part. Some incompatible cases: packed part storage, parts with UUIDs, level-0 parts in engines that merge rows like
ReplacingMergeTree, and tables withalways_use_copy_instead_of_hardlinksor zero-copy replication. Those still get cleared by their next regular merge or anOPTIMIZE ... FINAL. Otherwise they're just TTL merges: they count against the TTL merge limits,SYSTEM STOP TTL MERGESstops them, and they're picked after the other TTL merges. LikeTTLDropmerges, they don't wait formerge_with_ttl_timeout.On
ReplicatedMergeTreeevery replica runs the merge on its own copy of the part (copying instead of hardlinking if it has to), so an entry never waits on one particular replica. A replica whose part has a different format, or that can't reserve space, just fetches.For rollout, wait until every replica is upgraded before adding a
CLEAR INDEXrule. Older versions can't parse the rule, and they stop pulling the replication log when they see aTTLClearIndexmerge. The usualcompatibilitysetting during an upgrade also turnsttl_clear_index_mergesoff.Mutations like
MATERIALIZE INDEXandMATERIALIZE TTLdon't clear indexes yet, because clearing them in mutations is tricky enough to warrant its own PR. Mutations write parts withMutateTask, so none of the merge code here applies to them. Until then, a mutation can write an expired index again, and the nextTTLClearIndexmerge will cheaply delete it.system.partsgetsindex_clear_ttl_info.{expression,min,max}like the other TTL info columns, andsystem.part_loglogs these merges asTTLClearIndexMerge.Since the last review I simplified things a lot, so many of the earlier threads are about code that's gone now:
TTLClearIndexmerges now only select parts they can hardlink. The old behavior, to select and fall back to rewriting data, is gone.TTLClearIndexmerges are cheap.ttl_clear_index_mergesreplaces the query settingenable_ttl_clear_index_merge_type_generation.About two thirds of the diff is new since then, so I rewrote the history into commits that build on each other. They're meant to be read in order.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
MergeTree tables support
TTL ... CLEAR INDEX index_name, which deletes a skip index's files from parts once all their rows are past the TTL.Workflow [PR]
Sync PR [sync-upstream/pr/108069]