Skip to content

Do not bound the parse of a table's metadata in a Replicated database by the reader's parser limits - #119220

Closed
alexey-milovidov wants to merge 5 commits into
masterfrom
replicated-db-recovery-metadata-size-117695
Closed

alexey-milovidov wants to merge 5 commits into
masterfrom
replicated-db-recovery-metadata-size-117695

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

A Replicated database whose table metadata in ZooKeeper is longer than max_query_size, or nested deeper than max_parser_depth, could not be recovered by a new or lost replica: recoverLostReplica re-parses that metadata for every table it finds, and the parses in DatabaseReplicated used the recovering server's own limits (getTableUUIDIfReplicated even its max_query_size, 262144 by default). The parse threw Max query size exceeded or AST is too deep before the table was created, the recovery retried forever, and the database stayed one replica short - while the replicas that had loaded the very same text from their disk kept serving it.

The metadata is the server's own canonical CREATE text, already parsed and validated when the table was created, and it is as long and as deep as the table's definition needs, while max_query_size, max_parser_depth and max_parser_backtracks bound what a client may send. A recovering replica has no way to learn the values the creating server used, so all three parses of the stored metadata in DatabaseReplicated (getTableUUIDIfReplicated, parseQueryFromMetadata, getTablesForBackup) now go through one parseStoredMetadata helper that passes 0 for every limit, as SQLDefinedHandlersMetadataStorage and StorageSystemHandlers already do for the statements they store. parseQuery still guards the stack via checkStackSize.

Metadata over 256 KiB is reachable with a wide schema, long DEFAULT/CODEC/TTL expressions or a large COMMENT, from any session that raised max_query_size for its CREATE. Metadata deeper than 1000 levels needs the creating server's default profile to raise the parser limits, because the Replicated DDL worker re-parses every entry with that profile; the integration test models exactly that with two differently configured nodes.

Closes: #117695

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a new or recovering replica of a Replicated database being unable to recover a table whose metadata in ZooKeeper is longer than max_query_size or nested deeper than max_parser_depth: the recovery refused the server-generated metadata for its size or depth and retried forever.


Workflow [PR]
Sync PR [sync-upstream/pr/119220]

`recoverLostReplica` re-parses the metadata of every replicated table it
finds in ZooKeeper, and it did so with the recovering replica's own
`max_query_size`. A table whose metadata is longer than that - a wide
schema, long expressions, a large comment - was therefore out of reach:
the parse threw `Max query size exceeded` before the table was created
and the recovery retried forever, while the replicas that had loaded the
very same text from their disk kept serving it.

Every other place that re-parses this metadata passes no limit, because
the text is generated by a server and is as long as the definition needs,
while `max_query_size` bounds what a client may send - and a recovering
replica cannot learn the value the creating session raised it to.

Closes: #117695
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [46549fb]

Summary: ✅


AI Review

Summary

This PR removes the recovering replica's max_query_size / max_parser_* limits from the DatabaseReplicated ZooKeeper metadata reparse paths and adds focused regressions for the large-metadata and deep-metadata recovery cases. The ZooKeeper-side recovery loop looks fixed, but the broader "do not bound stored metadata by the reader's parser limits" contract is still only half met: the recovering replica can recover the table, yet its later on-disk metadata reads are still bounded by that replica's own parser limits.

Findings

⚠️ Majors

  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/119220#discussion_r3979133255] [src/Databases/DatabaseOnDisk.cpp:893-907, src/Interpreters/loadMetadata.cpp:70-79] The new parseStoredMetadata helper removes reader-side limits only while the definition is read from ZooKeeper. After recovery, the replica writes the same deep CREATE text to metadata/*.sql, and every later local metadata load still re-parses it under that replica's default max_parser_depth / max_parser_backtracks. A replica recovered by this patch can therefore still fail on SHOW CREATE TABLE, ATTACH DATABASE, or server restart for the same table. The new integration test proves queryability after SYSTEM SYNC DATABASE REPLICA (tests/integration/test_replicated_database_recovery_parser_limits/test.py:68-82), but it never exercises a metadata reread on the recovering node. Suggested fix: make the on-disk metadata readers for Replicated databases treat stored metadata the same way as the new ZooKeeper helper, or reject persisting metadata that the local loader cannot parse back.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: close the remaining local-metadata reread hole for Replicated databases, or narrow the PR so it no longer claims a full fix for reader-side parser limits and does not close the issue on that basis.

LLVM Coverage Report

Measured on commit 46549fb.

Metric Baseline Current Δ
Lines 88.20% 88.10% -0.10%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.50% +0.10%

Changed lines: Changed C/C++ lines covered: 28/28 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 10, 2026
Comment thread src/Databases/DatabaseReplicated.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. label Sep 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 46549fb78 with master 96757f50c (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 8a0adcace; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 15 s compile time in total, 7 of them have a recent master baseline.

Job report

alexey-milovidov and others added 2 commits September 17, 2026 17:58
…'s parser limits

Address review: the recovery of a `Replicated` database still re-parsed the table metadata stored in ZooKeeper with the recovering server's `max_parser_depth` and `max_parser_backtracks`, so a table created on a server whose default profile raises them (the `Replicated` DDL worker re-parses every entry with the default profile, so a session-level raise is not enough) left a new or lost replica retrying forever with `AST is too deep`, exactly as the `max_query_size` case did.

All three parses of the stored metadata in `DatabaseReplicated` (`getTableUUIDIfReplicated`, `parseQueryFromMetadata`, `getTablesForBackup`) now go through one `parseStoredMetadata` helper that passes `0` for the size, depth and backtracks limits, as `SQLDefinedHandlersMetadataStorage` and `StorageSystemHandlers` already do for the statements they store: the text is the server's own canonical output, already validated when the table was created, and `parseQuery` still guards the stack via `checkStackSize`.

The regression is covered by an integration test with two nodes whose default profiles differ in the parser limits, which is the only way such metadata comes to be. The stateless test for the size case is renumbered after the merge of master.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@alexey-milovidov alexey-milovidov changed the title Do not bound the parse of a table's metadata by max_query_size Do not bound the parse of a table's metadata in a Replicated database by the reader's parser limits Sep 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

This PR changes only how a Replicated database re-parses stored CREATE metadata: it drops the parser size, depth and backtrack limits during replica recovery, backup and metadata loading, and adds tests. None of that runs during SELECT execution in clickbench or tpch_adapted_1_official. Only Q33 (+8.7%) and Q34 (+5.3%) on clickbench were flagged. Both are heavy GROUP BY URL aggregations, and the automated checks already marked them inconclusive because their medians (1237.5 ms and 1208 ms) fall inside master's current run-to-run range. That call is consistent with the diff, so no performance impact is attributed to this PR.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 33 not_sure 1138 1238 +8.7% <0.0001 aggregation: This large GROUP BY over URLs never reaches the replicated-database metadata parsing this PR changes. Its median sits inside master's current band.
⚠️ 34 not_sure 1147 1208 +5.3% <0.0001 aggregation: This is the twin of Q33, a GROUP BY over URLs, and this PR does not touch its execution path. The +5.3% median sits inside master's current band.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 6d2442d4-5048-4a75-ad1b-e00ad643a28c
  • MIRAI run: 1e0a4ef8-ae64-43b2-bc38-eb041698e8bc
  • PR check IDs:
    • clickbench_2650266_1790829386
    • clickbench_2650382_1790829401
    • clickbench_2650494_1790829411
    • tpch_adapted_1_official_2650525_1790829413
    • tpch_adapted_1_official_2650668_1790829416
    • tpch_adapted_1_official_2650727_1790829418

alexey-milovidov and others added 2 commits September 24, 2026 15:00
…tizer builds

The table definition nested 600 levels deep made the analyzer throw `Stack size too large`
(`TOO_DEEP_RECURSION`) on `CREATE TABLE` in the ASan, MSan and TSan integration jobs (TSan allows
only 5% of the stack in `checkStackSize`). Nest the default expression 30 levels deep instead and
lower `max_parser_depth` / `max_ast_depth` of the recovering node to 50, so the stored metadata still
exceeds its limits (about two levels of parser depth per nesting).

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=119220&sha=7ac909d4a46756ef541d4cf2f3af6cbced2bf2d6&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%203%2F6%29
PR: #119220

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

I don't like the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replicated database: a new/recovering replica wedges on tables whose ZooKeeper metadata exceeds max_query_size

1 participant