Skip to content

MergeTreeSink: honor query cancellation while writing many parts - #107929

Merged
alexey-milovidov merged 1 commit into
masterfrom
insert-path-cancellation-check
Jun 28, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
insert-path-cancellation-check

Conversation

@al13n321

@al13n321 al13n321 commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

A single INSERT can split into a very large number of parts (e.g. a high-cardinality partition key together with max_partitions_per_insert_block), and writing/finalizing each part can be slow on object storage with the filesystem cache. The per-part loops in MergeTreeSink::consume and MergeTreeSink::finishDelayedChunk had no cancellation point, so a killed or timed-out INSERT kept grinding through all remaining parts — which could keep the query (and server shutdown) stuck for a long time, surfacing as "Hung check failed" in stress tests.

Call process_list_element->checkTimeLimit() between parts in both loops, the same idiom ReplicatedMergeTreeSink already uses. It throws QUERY_WAS_CANCELLED on a kill or TIMEOUT_EXCEEDED on a 'throw'-mode max_execution_time timeout. onFinish already skips finishDelayedChunk when cancelled, so a normal finish never throws.

Related: #107477

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

INSERT into a MergeTree table now honors query cancellation and max_execution_time while writing many parts, instead of potentially running long after being killed.

Version info

  • Merged into: 26.7.1.187 (included in 26.7 and later)

The per-part loops in `consume` and `finishDelayedChunk` had no cancellation
point, so a killed or timed-out `INSERT` that splits into many parts (e.g. a
high-cardinality partition key with `max_partitions_per_insert_block`) kept
running for a long time. Call `process_list_element->checkTimeLimit()` between
parts, as `ReplicatedMergeTreeSink` already does.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [03d9834]

Summary: ✅


AI Review

Summary

This PR adds per-part time-limit checks to non-replicated MergeTreeSink so killed or throw-mode timed-out inserts do not continue writing many parts indefinitely. The direction is right, but the current implementation still ignores break-mode max_execution_time inside those same long loops, so the promised timeout behavior is incomplete.

Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeSink.cpp:126] checkTimeLimit returns false for max_execution_time with timeout_overflow_mode = 'break', and this loop ignores that return value. A many-part insert can therefore keep writing every remaining part until consume returns, while the executor has no chance to observe and cancel the pipeline. The same handling is needed in finishDelayedChunk. Treat the false return as insert cancellation, consistent with existing break-mode insert timeout behavior.
Tests
  • ⚠️ Add a focused regression test for a non-replicated MergeTree insert split into many parts, covering at least break-mode max_execution_time cancellation; default throw-mode timeout or KILL QUERY coverage would also make the changed contract much stronger.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: handle checkTimeLimit returning false in both new checks, and add a focused many-part MergeTree insert regression test.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 19, 2026
/// A single INSERT can split into very many parts (e.g. high-cardinality partition key with
/// max_partitions_per_insert_block); honor cancellation/timeout between them.
if (process_list_element)
process_list_element->checkTimeLimit();

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.

checkTimeLimit returns false instead of throwing when max_execution_time is exceeded with timeout_overflow_mode = 'break'. Since this call ignores the return value, that mode still keeps writing every remaining part in this long consume loop; the executor cannot observe and cancel until consume returns. Existing insert timeout behavior treats break-mode insert timeouts as cancellation, so this should handle the false return here and in finishDelayedChunk, and add a focused regression test for a many-part MergeTree insert.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems ok.

@tiandiwonder tiandiwonder self-assigned this Jun 19, 2026
/// A single INSERT can split into very many parts (e.g. high-cardinality partition key with
/// max_partitions_per_insert_block); honor cancellation/timeout between them.
if (process_list_element)
process_list_element->checkTimeLimit();

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.

optional: worth a regression test for the kill/timeout-during-many-parts path.

an INSERT with a high-cardinality partition key and a small throw-mode max_execution_time should come back with TIMEOUT_EXCEEDED promptly instead of running to completion, which would guard against a future refactor dropping this check.

@clickhouse-gh

clickhouse-gh Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.30% +0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.50% 77.60% +0.10%

Changed lines: Changed C/C++ lines covered by tests: 16/16 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this Jun 25, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 28, 2026
Merged via the queue into master with commit cb34b20 Jun 28, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the insert-path-cancellation-check branch June 28, 2026 02:48
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 28, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ellation-check

MergeTreeSink: honor query cancellation while writing many parts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

4 participants