Repository navigation
Check max_query_size when altering table metadata - #108283
Conversation
|
Workflow [PR], commit [f96a293] Summary: ❌
AI ReviewSummaryThis PR adds early Findings❌ Blockers
Final VerdictStatus: |
|
📊 Cloud Performance Report ✅ AI verdict: This change only adds a max_query_size guard to ALTER/DDL code paths (rejecting oversized table metadata before it commits); it does not touch the SELECT execution hot path that ClickBench exercises. The two flagged ClickBench queries (Q4 -10.0%, Q15 -19.8%) were deterministically suppressed as within master's current variance band, and that call is correct here: the deltas are improvements the diff cannot plausibly cause, and per-query CPU time is essentially flat, so they are run-to-run variance. No real performance regressions or improvements are attributable to this PR. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
| } | ||
| } | ||
|
|
||
| void checkMetadataDoesNotExceedMaxQuerySize(const StorageID & table_id, const StorageInMemoryMetadata & metadata, ContextPtr context) |
There was a problem hiding this comment.
This helper still is not applied before every storage-specific ALTER path that can mutate external or inner state before DatabaseOrdinary::alterTable reaches the new size guard. Two concrete paths are still uncovered:
StorageObjectStorage::altercallsconfiguration->alterbefore the databasealterTable; for non-catalogIceberg,Iceberg::alterwrites a new metadata file/version hint beforeQUERY_IS_TOO_LARGEcan be thrown from the local metadata update.StorageWindowView::altercallsshutdown, drops and recreates the inner table, and only then callsalterTable.
With a table whose persisted CREATE is already near this session's max_query_size, a short ALTER TABLE ... ADD COLUMN on Iceberg or ALTER TABLE ... MODIFY COMMENT on WindowView can now fail after those side effects, leaving the external schema or inner table changed even though the ALTER was rejected. Please run this check on the final new_metadata in those paths before any storage-specific mutation, or make those side effects transactional.
|
@groeneai help sort out the test failures. |
|
@nikitamikhaylov triaged the two failing Stress checks on the current head (7b4a650). Both are pre-existing crashes unrelated to the 1. Stress test (amd_debug) -- the three rows ("Unexpected exception in refresh scheduling" STID 2508-34af, "Cannot start clickhouse-server", "Check failed") are one crash. The actual caught exception in This is a refreshable-MV scheduling bug, nothing to do with this PR (which does not touch 2. Stress test (arm_asan_ubsan) -- A pre-existing trunk bug in the The PR's own earlier crash ( |
ALTER TABLE operations that grow the table metadata (e.g. ADD COLUMN) were not bounded by max_query_size, so they could produce metadata exceeding the limit. This made the table unloadable by components that enforce max_query_size on metadata parsing, such as DDL distribution and replica recovery. The check is placed in DatabaseOrdinary::alterTable after serializing the resulting CREATE TABLE statement but before writing it to disk. It is gated on validate_new_create_query so that only the initiator validates, not replicas re-applying committed ALTERs. Closes: ClickHouse#79887
ReplicatedMergeTree schema ALTERs (e.g. ADD COLUMN) wrote metadata directly to ZooKeeper without checking max_query_size, bypassing the check added for non-replicated tables. Added the same check after validating the resulting CREATE query and before committing to ZooKeeper.
The max_query_size check in DatabaseOrdinary::alterTable could throw after callers had already mutated in-memory metadata (e.g. via changeSettings or setInMemoryMetadata), leaving the storage in an inconsistent state. Extracted a shared helper checkMetadataDoesNotExceedMaxQuerySize and call it early in StorageMergeTree::alter and StorageReplicatedMergeTree::alter, before any in-memory mutations. The inline check in DatabaseOrdinary::alterTable is kept as defense-in-depth since it uses the already-serialized statement. Updated stale comments that claimed alterTable does not validate settings/comment alters.
StorageMemory::alter mutated data (trimmed rows for max_rows_to_keep / max_bytes_to_keep) and updated memory_settings before calling alterTable, so the max_query_size check in alterTable could throw after data was already deleted. Added an early checkMetadataDoesNotExceedMaxQuerySize call before any data mutations.
…rite StorageObjectStorageQueue::alter wrote settings to Keeper and updated runtime state before calling alterTable, so the max_query_size check could throw after Keeper metadata was already changed. Added an early checkMetadataDoesNotExceedMaxQuerySize call before any state mutations.
Temporary tables have no on-disk metadata, so the max_query_size check does not apply. getObjectDefinitionFromCreateQuery asserts that the CREATE query is not temporary, so calling checkMetadataDoesNotExceedMaxQuerySize on a temporary table (e.g. ALTER TEMPORARY TABLE ... MODIFY COLUMN) caused a crash.
The 03393 test copies system.metric_log, which has 1200+ columns whose serialized metadata naturally exceeds the default max_query_size (262144 bytes). With random MergeTree settings applied in CI, the metadata grows past the limit, so the new max_query_size check on ALTER TABLE ... MODIFY SETTING rejects a legitimate operation. Set max_query_size = 0 for the test session to disable the check.
…e change In StorageReplicatedMergeTree::alter, a mixed structural + setting/comment ALTER (e.g. DROP COLUMN big_col, MODIFY COMMENT '<long comment>') builds metadata_copy from current_metadata and applies only the local setting/comment change, then calls changeSettings/setInMemoryMetadata and alterTable. The pre-check at the top of alter only validates future_metadata, which can be below max_query_size because the dropped column shrinks it, while metadata_copy keeps the column and stays above the limit. The alterTable check would then throw QUERY_IS_TOO_LARGE after the local state was already mutated, leaving a failed mixed replicated ALTER partially visible. Apply the setting/comment change to metadata_copy first, check its size before mutating any in-memory state, and drop the now-false comment claiming alterTable does not validate settings/comment alters.
…d tests The 04408 and 04412 tests use explicit ReplicatedMergeTree engines and assert on exact serialized metadata sizes. Under the private CI's SharedMergeTree and replicated-database replacements, the engine arguments are rewritten and the comment-replication semantics differ, so the byte-size assertions do not hold. Tag the tests to skip those configurations, matching the convention used by similar replicated metadata tests.
RESET SETTING first removes a setting from the metadata, then re-adds it with its default value. The precheck ran before this expansion, so a table whose final metadata exceeds max_query_size could pass the check while the reset setting was temporarily absent, mutate Keeper state via alterSettings, and only then throw QUERY_IS_TOO_LARGE from alterTable - leaving Keeper changed despite the rejected ALTER. Move the check to after the reset-default expansion, before any Keeper or runtime mutation. ClickHouse#79887
… ALTER StorageObjectStorage::alter called configuration->alter (which writes new metadata to object storage for non-catalog Iceberg) before the max_query_size check in alterTable could reject the operation. A table whose resulting CREATE statement exceeds max_query_size would have its Iceberg metadata file written, only for the ALTER to be rejected afterwards - leaving the lake schema inconsistent with ClickHouse. Move the check before configuration->alter. ClickHouse#79887
7b4a650 to
b46f312
Compare
| if (table_id.database_name == DatabaseCatalog::TEMPORARY_DATABASE) | ||
| return; | ||
|
|
||
| size_t max_query_size = context->getSettingsRef()[Setting::max_query_size]; |
There was a problem hiding this comment.
This still keys the guard off the initiating query context, not the metadata-reading context that restart / replica-recovery uses. DatabaseReplicated::recoverLostReplica parses Keeper metadata through getTableUUIDIfReplicated with getContext()->getSettingsRef()[Setting::max_query_size] in src/Databases/DatabaseReplicated.cpp:1517-1520, so a user can run ALTER with a larger session max_query_size than the server/global setting, pass this check, and commit metadata that the same server later cannot parse during recovery.
Please compare against the non-session limit used by metadata loading/recovery (and make the inline copy in DatabaseOrdinary::alterTable use the same bound), then add a regression where the ALTER runs under a raised session max_query_size while the server context remains lower.
StorageBuffer::alter flushed buffered rows into the destination table via optimize() before checking max_query_size, so a rejected oversized ALTER still mutated user-visible data placement.
|
|
||
| void checkMetadataDoesNotExceedMaxQuerySize(const StorageID & table_id, const StorageInMemoryMetadata & metadata, ContextPtr context) | ||
| { | ||
| /// Temporary tables have no on-disk metadata, so the max_query_size check does not apply. |
There was a problem hiding this comment.
This helper now also rejects oversized ALTERs for ENGINE = Memory databases, but those tables do not go through the unloadability path this PR is trying to protect. DatabaseMemory::getCreateTableQueryImpl only clones the AST kept in create_queries (src/Databases/DatabaseMemory.cpp:113-123), and DatabaseMemory::alterTable updates that AST directly, so there is no metadata file / Keeper parse on restart or recovery that can fail on max_query_size.
As written, a wide table in a Memory database can now get QUERY_IS_TOO_LARGE from the new early precheck even though the same metadata would remain perfectly usable. Please skip non-persistent metadata databases here (at least DatabaseMemory), or gate the helper on a database capability rather than only TEMPORARY_DATABASE.
The Buffer table used `max_time=1`, so the background flush thread could flush buffered rows into the destination between the `count()` checks, independent of the ALTER under test. On a loaded CI runner more than a second elapses between the slow `clickhouse-client` spawns, so the periodic flush raced the checks and produced `dest_before=1` / `dest_after=1`. Raised the time thresholds so the buffer only flushes via the code path being tested (the rejected ALTER), never via the timer. Verified with 30 consecutive local runs on a debug build (30/30 passed). 🤖 Generated with [ECA](https://eca.dev) (anthropic/claude-opus-4-8) Co-Authored-By: eca-agent <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 87/95 (91.58%) · Uncovered code |
|
@groeneai help sort out the test failures. |
|
@lockie only one red check remains on Stress test (arm_tsan) -
All other checks are green, so the PR itself has no CI failures attributable to it. |
f70abba
…-size Check max_query_size when altering table metadata
ALTER TABLEoperations that grow the table metadata (e.g.ADD COLUMN) were not bounded bymax_query_size, so they could produce metadata exceeding the limit. This made the table unloadable by components that enforcemax_query_sizeon metadata parsing, such as DDL distribution and replica recovery.The check is placed in
DatabaseOrdinary::alterTableafter serializing the resultingCREATE TABLEstatement but before writing it to disk. It is gated onvalidate_new_create_queryso that only the initiator validates, not replicas re-applying committedALTERs.Closes: #79887
Changelog category
Changelog entry
ALTER TABLEoperations that would produce table metadata exceedingmax_query_sizeare now rejected upfront, preventing tables from becoming unloadable by components such as DDL distribution and replica recovery.Version info
26.7.1.1275(included in26.7and later)