Repository navigation
Add written_rows quota resource - #118272
Conversation
Quotas could limit the bytes written by INSERT queries (written_bytes) but not the number of rows. Add written_rows, accounted in the counting transform next to written_bytes and pre-checked in InterpreterInsertQuery, with the corresponding columns in system.quota_limits, system.quota_usage and system.quotas_usage.
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [d61ba6e] Summary: ✅
AI ReviewSummaryThis PR adds the new Findings
Final Verdict
LLVM Coverage ReportMeasured on commit d61ba6e.
Changed lines: Changed C/C++ lines covered: 142/144 (98.61%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
`docs/reference/statements/create/quota.mdx` and the `system.quota_limits` / `system.quota_usage` / `system.quotas_usage` pages are autogenerated, so the direct edits there failed the `No direct edits to generated or read-only docs` check. Revert them and add `written_rows` to the real source of truth in `registerStatementQuota` in `src/Parsers/Access/ParserCreateQuotaQuery.cpp` instead (both the description prose and the `.syntax` strings of `CREATE QUOTA` and `ALTER QUOTA`, which were also missing `written_bytes`). The system-table pages regenerate from the `QuotaTypeInfo` descriptions already added in `QuotaDefs.cpp`. Also fix `test_quota/test.py::test_dcl_management`: `SHOW QUOTA` gained one more `used`/`max` column pair for the new resource, so the expected regexes now spell out all four resources that follow `execution_time`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Status update after merging
Locally verified with the merged branch: @groeneai, #118722 is still open — please provide a fix in a separate pull request, or link it here if it is already in progress. |
|
#118722 is still open. The in-progress fix is #118817 (mine, open since 2026-09-08): it removes one producer, the server-wide one-shot Your attribution is right. The row for I am not linking #102308 or #102192 as the fix. Both change the recovery after a failed in-place build rather than this carrier's producer, and I have not measured that they reach it. What keeps the issue open is the producer for the rest of the carrier: 45 of 108 reports over 60 days carry the failing query, and 16 of those put the set in a |
…e checking overflow The multi-counter `EnabledQuota::usedForQuery` applied each usage sequentially and threw on the first overflow, so when a chunk took `written_rows` over the limit its bytes were never added to `written_bytes`: `system.quota_usage` underreported the attempted usage and the two write counters were not independent. Now every usage is accounted with `check_exceeded = false` first, and only then each counter is checked with `checkExceeded`. The regression test `05060_written_rows_quota` now asserts that `written_bytes` grows for the chunk rejected by the `written_rows` limit. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 Status update, 2026-09-18:
Locally, |
`parseQuotaType` accepts every `QuotaType`, but the four resource lists in the `CREATE QUOTA` and `ALTER QUOTA` registrations were written out by hand and had drifted: the `ALTER QUOTA` description omitted `failed_sequential_authentications`, and both `.syntax` blocks omitted it together with `queries_per_normalized_hash`. Since these registrations feed `system.statements`, `system.documentation` and the generated reference pages, users were told that valid quota resources are unsupported. The lists are now built from the enum metadata (`QuotaTypeInfo::get(...).name`) and substituted into the documentation, so adding a resource cannot leave them behind. `written_rows` and the two previously missing resources are now listed everywhere. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`test_exceed_quota` expected `read_bytes` to stay at 0 when a query is rejected by the `read_rows` limit. Since 53c089a, `EnabledQuota::usedForQuery` accounts every counter of one call before checking any of them for overflow, so the 200 bytes of the 50 rows the query reads are recorded as well. That is the intended behaviour - the counters of one chunk of work must not depend on which of them overflows first - so the canonical value is updated. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118272&sha=53c089a7c94f968e6c5797346662b36b885b8c85&name_0=PR&name_1=Integration%20tests%20%28amd_asan_ubsan%2C%20db%20disk%2C%203%2F8%29 Pull request: #118272 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Status update, 2026-09-19 — pushed
The three remaining failures are infrastructure, not this change:
@groeneai, please investigate the two |
|
Both reds are the same failure at the same call site, and #120928 fixes it. That PR merged 2026-09-19 07:25:05Z, about 23 hours after this run started, so the report you linked could not carry it. Test selection reaches the CI database only through ( #120928 sends
|
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…h of counters In the multi-counter `EnabledQuota::usedForQuery`, `Impl::used` advances an ended interval only when the counter it is applying overflows. So when the first insert of a new interval overflows the stale `written_bytes` of the ended interval, the reset wipes the `written_rows` of the same chunk, which was added just before: `system.quota_usage` undercounts `written_rows` and the new interval can accept an extra block. Now every target interval is advanced for `current_time` once, before any counter of the batch is applied. `05239_written_rows_quota_interval_rollover` crosses the end of a 5-second interval within one session (a login starts the new interval by itself) and checks `written_rows = 1` after the rollover; it printed `0` before the fix. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts: # src/Parsers/Access/ParserCreateQuotaQuery.cpp
…re accounting or checking `Impl::used` advanced an ended interval only after the counter overflowed, so the single-counter `used` / `usedForQuery` entrypoints carried the stale usage of the ended interval into the new one and dropped the first part of the new interval on the first overflow (e.g. `max_queries = 10` effectively allowed 14 queries). Now `Impl::used` and `Impl::checkExceeded` call `getEndOfInterval` first, which starts a new interval if needed, so every entrypoint behaves the same; the extra normalization in the batched `usedForQuery` is no longer needed. Also fix `test_quota::test_exceed_quota`: the usage is kept when the quota configuration is reloaded, so the 200 `read_bytes` recorded by the rejected query (all counters of one call are accounted before checking) carry over to the later checks. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118272&sha=4c10820cba916214b0b5914e4ceb3c6debfcc044&name_0=PR&name_1=Integration%20tests%20%28amd_llvm_coverage%2C%207%2F8%29 PR: #118272 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… a new interval The `queries` counter is accounted by itself; with `max_queries = 4` and 3 queries used in the ended interval, the first two queries of the new interval must both be counted (before the fix the second one overflowed the stale value, reset the counters and only re-added itself). Co-Authored-By: Claude Opus 5.5 <[email protected]>
`05320_queries_quota_interval_rollover` failed in the `CH Inc sync` private CI, `Stateless tests (amd_asan_ubsan, flaky check, s3 storage, meta in keeper)`: it read `0` instead of `2`, because the separate client that read `system.quotas_usage` started after the second quota interval had already ended. Both rollover tests now read the usage in the same session from `system.quota_usage`, check that their checkpoints fell into the intended intervals (by `end_time`), and repeat the attempt otherwise. https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/praktika.html?PR=72812&sha=e5d88141153ba3b68c77d6cd0bca6c3255bc604d&name_0=PR Related: #118272 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Status update, 2026-10-06 — pushed
|
The style check matches `system.tables` as a regex, so the phrase "system tables" in a comment was taken for a query without a `database` condition. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118272&sha=5c32229c4a7b5c9cad5ea0533c2f70888ccdbf0d&name_0=PR&name_1=Style%20check #118272 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Status update, 2026-10-07 — pushed
|
…to writers `EnabledQuota::Interval::getEndOfInterval` published the new `end_of_interval` with a CAS and only then zeroed `used`, so a concurrent thread could observe the new interval, account its usage, and have it wiped by the reset. Now the rollover is serialized by a per-interval mutex and the counters are reset before the new end of the interval is published. The fast path (interval not ended) stays lock-free. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| mutable std::atomic<std::chrono::system_clock::duration> end_of_interval; | ||
|
|
||
| /// Serializes the rollover to a new interval in `getEndOfInterval`. | ||
| mutable std::mutex rollover_mutex; |
There was a problem hiding this comment.
Tbh, I'd be okay with imprecise calculation here, and it was not part of this task. Still acceptable.
|
|
||
| if (quota) | ||
| quota->usedForQuery(normalized_query_hash, QuotaType::WRITTEN_BYTES, written_bytes); | ||
| quota->usedForQuery(normalized_query_hash, {{QuotaType::WRITTEN_ROWS, written_rows}, {QuotaType::WRITTEN_BYTES, written_bytes}}); |
There was a problem hiding this comment.
For those who will read - this is the main change.
The test crossed the end of its 5 s quota interval with a fixed `sleep(3)` + `sleep(2.5)`. A fixed sleep adds all the latency before it (client start, three accounted queries, the first usage read) to the time after the boundary. On a loaded MSan runner every trivial query took about 400 ms, so in all 10 attempts the final usage read started 0.10-0.38 s after the end of the second interval and reported the third one; nothing was printed. The latency was the same in every attempt, so the retry loop could not help. Sleep until 0.1 s after the end of the first interval instead, computed on the server from `now64(3)`. Quota intervals are aligned to the epoch, so `END + 5` is a whole second known before the session. One `sleep` is limited to 3 s (`function_sleep_max_microseconds_per_block`), hence two calls clamped to [0, 3]. The sleeps still read only `system.one`, which is not accounted, and no quota state, so they do not start the new interval. The assertion, the reference and the checkpoints are unchanged. Validated with per-query latency injected via `sleep_after_receiving_query_ms`: the old test fails with the CI diff at 600 and 700 ms, the new one passes at 700 and 900 ms on its first attempt and first fails at 1300 ms. 50/50 randomized runs (debug) and 20/20 on ASan pass. On 26.9.25.15, before ClickHouse#118272, the test still prints 1 instead of 2. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #118257
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Quotas can now limit the number of rows written by
INSERTqueries with the newwritten_rowsresource, for exampleCREATE QUOTA q KEYED BY ip_address FOR INTERVAL 1 minute MAX written_rows = 100000, query_inserts = 250 TO user. Until now only the written bytes could be limited (written_bytes), which is awkward when the natural unit of the workload is rows. The current and maximum values are exposed aswritten_rowsandmax_written_rowsinsystem.quota_usage,system.quotas_usageandsystem.quota_limits, and the resource can also be set inusers.xmlas<written_rows>.Details
written_rowsis accounted inCountingTransformnext towritten_bytes(using the sameusedForQuerycall, soNORMALIZED_QUERY_HASHquotas bucket it per query pattern like the other pipeline-fed counters), andInterpreterInsertQuerypre-checks it together withwritten_bytesso that an already exceeded quota rejects the next insert before any data is written. Everything else (SQL parser and formatter,users.xmlparsing, the three system tables) iterates overQuotaType, so no further changes were needed there.The new stateless test
05060_written_rows_quotacovers synchronous inserts, asynchronous inserts,INSERT ... SELECT, the pre-check after exceeding, both spellings (WRITTEN ROWS/written_rows), and that the rows and bytes counters are independent.01297_create_quotaand02117_show_create_table_systemreferences are updated for the new columns. Documentation is updated at the source of truth: theCREATE QUOTAandALTER QUOTAregistrations inregisterStatementQuota, theQuotaTypeInfodescriptions that the three system-table pages are generated from, and the hand-writtenusers.xmlquota configuration page. The resource lists in those two registrations are now generated fromQuotaTypeinstead of being written out by hand, because they had drifted from whatparseQuotaTypeaccepts:failed_sequential_authenticationsandqueries_per_normalized_hashwere missing from some of them, sosystem.statements,system.documentationand the generated reference pages told users that valid quota resources are unsupported.Quota accounting now starts a new interval (resetting its counters) before accounting or checking any counter, in every
EnabledQuotaentrypoint. Previously an ended interval was advanced only after a counter overflowed, so the usage at the beginning of the new interval was added to the stale value of the ended one and partially lost on the first overflow.05239_written_rows_quota_interval_rolloverand05320_queries_quota_interval_rollovercover this for the batched (written_rows/written_bytes) and single-counter (queries) paths.Motivation: I wanted to cap a public demo to N pixels per minute per IP, where a pixel is a row, and found there was no way to express that.
Workflow [PR]
Sync PR [sync-upstream/pr/118272]
Version info
26.10.1.1784-master(included in26.10and later)