Repository navigation
Delay background merges after the server startup (delay_merges_after_startup_seconds) - #133508
Conversation
After a server restart every MergeTree table starts background merges at
once: the merges interrupted by the shutdown are selected again, and a
replica executes the merges of its replication queue. They compete with
the first queries, which already run with cold caches.
The new setting (default 60) delays background merges of a table the
server loads from its metadata at startup (`FORCE_ATTACH` /
`FORCE_RESTORE`, server only), counted from the table's own `startup()`,
so it also works with `async_load_databases`, where table startups run
after all tables are loaded. `CREATE TABLE`, a runtime `ATTACH TABLE`,
`SYSTEM RESTART REPLICA` and `clickhouse-local` are not delayed.
- MergeTree: `scheduleDataProcessingJob` skips merge selection while the
delay lasts. Mutations and `OPTIMIZE` (a separate path) are not delayed.
- ReplicatedMergeTree: `shouldExecuteLogEntry` postpones `MERGE_PARTS`
entries with a `postpone_reason`, at the same layer as `SYSTEM STOP
MERGES`. `GET_PART`, `MUTATE_PART` and the merge assignment are not
delayed, so other replicas keep merging. `OPTIMIZE` and `SYSTEM SYNC
REPLICA` on the restarted replica can wait until the delay ends.
- The delay ends earlier once a partition has `parts_to_delay_insert`
active parts, i.e. when inserts start to be slowed down.
- The setting is read on each check, so `ALTER TABLE ... MODIFY SETTING`
applies at once. History `{"26.12", 0, 60}` keeps `compatibility` < 26.12
on the old behaviour.
`merges_blocker` was not reused: it makes `OPTIMIZE` of MergeTree throw
`ABORTED`, blocks mutations, and `SYSTEM START MERGES` could not lift it.
The integration harness pins the setting to 0 for every instance running
the binary built from these sources (incl. `restart_with_latest_version`,
removed again by `restart_with_original_version`), because tests expect
merges right after a restart and older versions reject the unknown key.
Closes ClickHouse#133472
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The description of `delay_merges_after_startup_seconds` now says that a replica also postpones the merges it would fetch as a merged part, and that merges are not delayed while a partition has at least `parts_to_delay_insert` active parts (the guard is checked every time, it does not end the delay once). The integration test now checks that the scheduler of the delayed table itself reports the delay (a log line keyed by the table UUID), that a mutation runs during the delay, that a 10 s delay ends on its own and not before 10 s after the server start, and that the parts threshold is per partition (two partitions of 2 parts, table total 4, threshold 3). Two comments in `MergeTreeData.h` are corrected: `UNDROP TABLE` also loads a table from its metadata, and the startup time is 0 only when the delay is not armed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
On a `ReplicatedMergeTree` replica a postponed `MERGE_PARTS` entry keeps its source parts covered, so no `MUTATE_PART` is assigned to them and a mutation of those parts stays not done until the delay ends (measured: `system.mutations` still not done 30 s into a 3600 s delay, done right after the setting is set to 0). The setting description now says so, and that only mutations and `OPTIMIZE` of a `MergeTree` table are not delayed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove a comment on delayMergesAfterStartup that only named its caller. 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; 3 rounds, 4 findings) plus my own review.
Severity: ❌ blocker / |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261122-060200 |
|
Workflow [PR], commit [7e7c3f6] Summary: ✅
AI ReviewSummaryThe PR adds Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units260 translation units recompiled, 2237 s compile time in total, 260 of them have a recent master baseline. |
Let inserts and optimized just drop the timer. |
Requested in review: inserts and OPTIMIZE drop the timer of `delay_merges_after_startup_seconds`, so parts inserted after a restart are merged as usual and OPTIMIZE on a restarted replica does not wait for the postponed merges. Mutations end it too: on a replica they otherwise waited for the postponed merges of the parts they cover. The first such query wakes the background task, so the postponed queue entries run at once. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Done in 8b467e6: the first |
AI review of a later commit does not approve the PR, see the AI Review comment.
An `ALTER` that changes data, e.g. `MODIFY COLUMN` with a new type, creates its mutation in `alter`, not in `mutate`, so it did not end the delay. On a restarted replica the postponed `MERGE_PARTS` entry covers the source parts, so the mutation was not assigned and the `ALTER` waited out the remaining delay with `alter_sync = 1`. Lightweight updates (`UPDATE`, `ALTER UPDATE` with `alter_update_mode = 'lightweight'`) write a patch part without `write`, so they did not end it either. Both engines now end the delay in `alter` where it checks the mutation throttle, as `mutate` does, and in `updateLightweight`. The restart test gets a column type change and a lightweight update on a MergeTree and on a ReplicatedMergeTree table. `UNDROP TABLE` does not delay merges: `InterpreterCreateQuery` creates the table with `ATTACH`, not `FORCE_RESTORE`. The setting description said it did; it now lists `UNDROP TABLE` with `CREATE` and `ATTACH`, and the test checks it. The expiry check measured from the server start, so it passed without any delay when the table started up late (14 s after the process start in one local run), and had no upper bound. It now measures from the first "merges are delayed" log line of the table and expects 8 to 30 s. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #133472
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added the MergeTree setting
delay_merges_after_startup_seconds(default 60). A table the server loads at startup does not start background merges for this many seconds, so the first queries after a restart do not compete with them. The firstINSERT,OPTIMIZE, mutation or lightweight update of the table ends the delay, and merges are not delayed while a partition has at leastparts_to_delay_insertactive parts. Tables fromCREATE TABLE,ATTACH TABLEorUNDROP TABLEare not delayed. Closes #133472.Description
After a restart every MergeTree table starts merging at once, competing with the first queries.
The delay is armed only for tables the server loads from metadata at startup (
FORCE_ATTACH/FORCE_RESTORE, server only, also lazily loaded tables) and counts from the table's ownstartup(), so it works withasync_load_databases. Not forCREATE, runtimeATTACH,UNDROP,SYSTEM RESTART REPLICAorclickhouse-local.MergeTree: merge selection is skipped while delayed.ReplicatedMergeTree:MERGE_PARTSqueue entries are postponed (visible aspostpone_reason), at the same place asSYSTEM STOP MERGES.GET_PARTfetches,MUTATE_PARTentries and merge assignment are not delayed, so other replicas keep merging; a merged part this replica would fetch for a postponed entry is fetched after the delay.INSERT,OPTIMIZE, mutation (also one anALTERcreates) or lightweight update on the table ends its delay and wakes its background task, so a table that is written to merges as usual.ALTER TABLE ... MODIFY SETTINGapplies from the next one;compatibility< 26.12 gives 0.Where it loses: on the restarted replica
SYSTEM SYNC REPLICAand mutations started on another replica can wait up to the remaining delay;SYSTEM SCHEDULE MERGEmerges are delayed too. SharedMergeTree is not covered.The integration harness pins the setting to 0 for instances running the binary under test, since tests expect merges after a restart and older versions reject the key.
Validation: new integration test
test_delay_merges_after_startup, green 5/5; its INSERT, OPTIMIZE, mutation,MODIFY COLUMNand lightweight update assertions (MergeTree and replica) fail without the timer drop, and the delay, expiry (measured from the table startup), threshold, replica, ATTACH,UNDROPand CREATE ones on a mutated build or setting.test_shutdown_cancels_merges,test_index_filename_upgradeand the settings stateless tests pass.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/133508]
Version info
26.12.1.234-master(included in26.12and later)