Skip to content

Add max_rows setting for databases - #111166

Open
zeev1079 wants to merge 97 commits into
ClickHouse:masterfrom
zeev1079:database-max-rows
Open

zeev1079 wants to merge 97 commits into
ClickHouse:masterfrom
zeev1079:database-max-rows

Conversation

@zeev1079

@zeev1079 zeev1079 commented Jul 21, 2026 •

Copy link
Copy Markdown

Closes: #109355

Adds a max_rows setting for Atomic and Ordinary databases: a ceiling on active MergeTree rows counted toward the database limit. It is enforced on INSERT, ATTACH, and partition-import/move operations. A batch INSERT may overshoot, and the next INSERT throws TOO_MANY_ROWS.

Enforcement is a best-effort snapshot check, not a reservation spanning the commit: concurrent operations may each observe free headroom and transiently overshoot the limit together; the next checked operation then fails. On replicated attach paths the limit is charged only for parts that ZooKeeper actually accepts: an ATTACH deduplicated by its block id stays a no-op even when the database is over the limit, and a partially duplicated ATTACH PARTITION FROM pays only for the accepted remainder. A multi-part ATTACH PARTITION is all-or-nothing: the rows of the parts ZooKeeper will accept are validated together before the first part is committed, so the command never leaves a partially attached partition behind.

The counted total is exposed through the nullable system.databases.rows column. It intentionally excludes engines that do not participate in max_rows accounting. The setting can be changed at runtime with ALTER DATABASE ... MODIFY SETTING max_rows = N; its default value, 0, is unlimited.

Changelog category (leave one):

  • New Feature

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

Add the max_rows setting for Atomic and Ordinary databases, limiting active MergeTree rows counted across a database. It is enforced on INSERT, ATTACH, and partition imports as a best-effort snapshot check, is reconfigurable via ALTER DATABASE ... MODIFY SETTING, and is exposed through system.databases.rows.


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

Adds a `max_rows` setting for `Atomic` and `Ordinary` databases (issue
ClickHouse#109355): a ceiling on the total active rows across the database's tables,
enforced on INSERT and ATTACH. Also exposes the current count via the new
`system.databases.rows` column. Default 0 keeps current (unlimited) behavior.

The count is summed on demand from each table's active rows (MergeTree
reports its rows, other engines contribute 0), mirroring how the sibling
`max_tables` setting reads `tables.size()`. This avoids a maintained counter
and its trade-offs (see PR description).
@zeev1079 zeev1079 changed the title Add max_rows setting for Atomic/Ordinary databases Add max_rows database setting for Atomic/Ordinary databases Jul 21, 2026
@zeev1079 zeev1079 changed the title Add max_rows database setting for Atomic/Ordinary databases Add max_rows database setting for Atomic/Ordinary Jul 21, 2026
@zeev1079 zeev1079 changed the title Add max_rows database setting for Atomic/Ordinary Add max_rows setting for databases Jul 21, 2026
Two gaps in the `max_rows` check (issue ClickHouse#109355):

- `DatabaseOnDisk::createTable` counted rows before `waitDatabaseStarted`,
  so with `async_load_databases` still-loading tables were undercounted.
  Count after the wait.
- `RENAME`/`EXCHANGE TABLES` across databases go through
  `DatabaseAtomic::renameTable`, not `createTable`, bypassing the limit.
  Enforce the destination's `max_rows` there, under the mutexes already
  held, via a new `getCurrentRowCountUnlocked`.
zeev1079 added 4 commits July 21, 2026 10:51
- Fix a self-deadlock on the cross-database RENAME/EXCHANGE limit check:
  the throw path called `getDatabaseName`, which locks the database mutex
  already held by `renameTable`. Read the `database_name` member directly,
  as the surrounding code does. Only the throwing path was affected.
- Drop `issue ClickHouse#109355` from comments and condense or remove the obvious ones.
- Dedup the repeated `getStorageID().getDatabaseName()` in the INSERT check.
- Extend `04612_database_max_rows` with cross-database RENAME/EXCHANGE that
  exceed the destination's `max_rows` and are rejected.
# Conflicts:
#	src/Databases/DatabaseOrdinary.cpp
Throw `NOT_IMPLEMENTED` instead of `SUPPORT_IS_DISABLED` when
`ALTER DATABASE ... MODIFY SETTING` targets an engine that does not
support these settings. This matches the base `IDatabase` behavior and
the semantics of the code: the operation is not implemented for that
engine, rather than a feature disabled at build or configuration time.

Also condense the `max_rows` setting description to match the concise
style of the surrounding database metadata settings.
`DatabaseOnDisk::renameTable` moved a table into the destination database
and only enforced `max_rows` inside the final `createTable` call, which
runs after the data has been physically moved and the source table has
been detached. A rejection there left the table orphaned. Check the
destination's row limit up front, next to `checkMetadataFilenameAvailability`
and before the move, so a rejection is rolled back by the existing catch.

This covers the `Ordinary` and engine-conversion rename paths that go
through `DatabaseOnDisk::renameTable`; the same-engine `Atomic` path
already checked before moving. Word the limit message for both attach and
rename, and add an `Ordinary` rename regression test.
@zeev1079
zeev1079 marked this pull request as ready for review July 21, 2026 20:06
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Aug 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2c0ed6b]

Summary: ✅


AI Review

Summary

Adds the best-effort max_rows setting for Atomic and Ordinary databases, with runtime reconfiguration, admission checks for inserts and data imports, and access-controlled reporting through system.databases.rows. Reviewed head 2c0ed6b4f0d5ab544a94c4cb49ff3aee54e6df26, the current implementation, regression tests, and all prior discussions. No remaining actionable findings.

Missing context / blind spots
  • ⚠️ Full current-head CI is still running. Fast test passes 15 database quota regressions, including 05768_database_max_rows_attach_partition_all; it skips 05406_database_max_rows_unique_key_dedup and 05224_database_max_rows_table_function_not_resolved, whose execution remains for the normal stateless jobs. No local binary was available for independent execution.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Aug 15, 2026
@alexey-milovidov alexey-milovidov self-assigned this Aug 15, 2026
Comment thread src/Storages/IStorage.h Resolved
Comment thread src/Databases/DatabaseOnDisk.cpp Resolved
Comment thread src/Databases/DatabasesCommon.cpp Resolved
Comment thread src/Databases/DatabaseOnDisk.cpp Resolved
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Oct 17, 2026
In the ParallelReplicas lanes the table's single mark goes to whichever
replica asks the in-order coordinator first. When a remote replica wins,
the FileOpen/S3GetObject counts land in its own query_log row
(is_initial_query = 0, current_database = 'default'), which the
`current_database = currentDatabase()` filter drops, so the check read 0
(seen on PR ClickHouse#111166: FileOpen 0 instead of 6).

Sum the counter over all rows whose initial_query_id is one of the
test's queries, as done for the prewarm mark cache tests in ClickHouse#124942.
Replicas without a range open nothing, so the sums are exact and the
references are unchanged; without parallel replicas each group is the
initiator row alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 3 commits October 18, 2026 22:56
…EPLACE PARTITION FROM`

Add a regression for `MergeTree` and `ReplicatedMergeTree`: with `max_rows = 100`,
60 filler rows and a 30-row destination partition, replacing it from a 50-row
source must throw `TOO_MANY_ROWS` and leave the destination intact, while
replacing it from a 40-row source must succeed and leave 100 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged the latest master (clean merge, ninja clickhouse builds) and pushed 1643e3cfa835..fba4b67ee558. On the merged binary, 04910, 05223, 05224, 05339, 05340, 05353, 05394, 05395 and 05401 pass locally. The ZooKeeper-dependent tests (05403, 05406, 05407 and others) were not run locally. All review threads are resolved.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Public CI on 6ba305b2236a is green; the only red check was CH Inc sync (the sync PR conflicted with private master in src/Interpreters/DatabaseCatalog.cpp, the UNDROP limit preflight next to the new cross-database UNDROP check). Resolved it by keeping both and pushed the merge to the sync branch; the sync PR is now MERGEABLE and its per-file diff against private master equals this PR's diff. No changes to this PR were needed. All review threads are resolved.

@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged the latest master (92672218fe75; clean merge, ninja clickhouse builds) and pushed 6ba305b2236a..257c71639799. On the merged binary against a local server without Keeper, 04910, 05223, 05224, 05339, 05340, 05353, 05394, 05395 and 05401 pass. The MergeTree parts of 05258 and 05407 also match the reference. The other tests need Keeper or the backups disk, so I am leaving them to CI. There are no unresolved review threads.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111166&sha=e9c24a6f9cb490c1e516c948207114369ec359bd&name_0=PR&name_1=La%20Casa%20Del%20Dolor%20%28arm_asan_ubsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

The abort is an out-of-bounds vector::operator[] in DataFileStatistics::getNullCounts (Iceberg writer), not related to this PR. It looks like #125361, with an open fix in #126768.

@groeneai

Copy link
Copy Markdown
Collaborator

This is the abort that #126768 fixes (my PR, open, waiting for review).

DataFileStatistics::getNullCounts indexes field_ids by the position of the written block's column. On master, when a written column has no field id in the latest Iceberg schema, fieldIdsOfWrittenColumns returns an empty list and DataFileStatistics falls back to the schema's own ids, so a block wider than the latest schema reads past the end of field_ids. This is the stale-structure INSERT from #125361. In this run, earlier INSERTs into the same table d15.t463 had failed with No such column c1 and No such column c2 before the crashing INSERT INTO d15.t463 (c1).

#126768 removes that fallback: every written column must have a field id in the latest schema (the check Parquet already did, now for every format), otherwise the INSERT fails with INCORRECT_DATA. So field_ids always has one entry per block column, and this read cannot go out of bounds. Its integration test aborts the server on master for ORC and Avro after DROP COLUMN, and passes with the fix.

@zeev1079

Copy link
Copy Markdown
Author

@alexey-milovidov can you please review? It's max_rows from your issue, similar approach as your max_tables PR. CI is green, threads resolved.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Automated check on head 925c19aec3a3 (merged with master on 2026-11-10): all CI checks pass, the PR is MERGEABLE, and there are no unresolved review threads. No further changes are needed from the automation side. The final review and merge decision is left to a human maintainer.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged the latest master (clean merge; ninja clickhouse builds) and pushed b7177cff5cae..4ad301092ac8. On the merged binary, against a local server without Keeper, 04910, 05223, 05224, 05339, 05340, 05353, 05394, 05395 and 05401 pass. The tests that need Keeper were skipped locally.

The red checks on the previous head were all six Performance Comparison (arm_release, master_head, N/6) shards. They did not run any queries: performance test selection failed with GitHub API 403 Client Error: rate limit exceeded (e.g. https://s3.amazonaws.com/clickhouse-test-reports/PRs/111166/b7177cff5cae59a72f4ba4a6ae708b033843ae34/pr/performance_comparison_arm_release_master_head_1_6/job.log). This is an infrastructure failure unrelated to this PR. The fix is in progress in #131332.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 The only red check was CH Inc sync: the private sync PR conflicted with private master in src/Interpreters/DatabaseCatalog.cpp (the UNDROP limit preflight comment block, next to the cross-database check that public master has since removed). I resolved it by taking this PR's current version of the block, merged the current PR head 2c0ed6b4f0d5 on top, and pushed the result to the sync branch. Its diff against private master matches this PR's diff file for file. The sync PR is MERGEABLE again. No changes to this PR's branch were needed: all other CI checks pass and there are no unresolved review threads.

This branch has not been deployed

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

Labels

can be tested Allows running workflows for external contributors comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. external Author is not a member of the ClickHouse organization pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add database settings to limit the number of tables and their sizes in bytes or rows.

4 participants