Skip to main content
Quotas in ClickHouse CloudQuotas are supported in ClickHouse Cloud but must be created using the DDL syntax. The XML configuration approach documented below is not supported.
Quotas allow you to limit resource usage over a period of time or track the use of resources. Quotas are set up in the user config, which is usually ‘users.xml’. The system also has a feature for limiting the complexity of a single query. See the section Restrictions on query complexity. In contrast to query complexity restrictions, quotas:
  • Place restrictions on a set of queries that can be run over a period of time, instead of limiting a single query.
  • Account for resources spent on all remote servers for distributed query processing.
Let’s look at the section of the ‘users.xml’ file that defines quotas.
By default, the quota tracks resource consumption for each hour, without limiting usage. The resource consumption calculated for each interval is output to the server log after each request.
For the ‘statbox’ quota, restrictions are set for every hour and for every 24 hours (86,400 seconds). The time interval is counted, starting from an implementation-defined fixed moment in time. In other words, the 24-hour interval does not necessarily begin at midnight. When the interval ends, all collected values are cleared. For the next hour, the quota calculation starts over. Here are the amounts that can be restricted: queries – The total number of requests. query_selects – The total number of select requests. query_inserts – The total number of insert requests. errors – The number of queries that threw an exception. result_rows – The total number of rows given as a result. result_bytes - The total size of rows given as a result. read_rows – The total number of source rows read from tables for running the query on all remote servers. read_bytes - The total size read from tables for running the query on all remote servers. written_bytes - The total size of a writing operation. written_rows - The total number of rows written by INSERT queries. execution_time – The total query execution time, in seconds (wall time). failed_sequential_authentications - The total number of sequential authentication errors. queries_per_normalized_hash – The maximum number of executions of any single normalized query. Normalized queries are queries with literals replaced by placeholders, so SELECT 1 and SELECT 2 are considered the same normalized query. This limit is tracked per distinct normalized query pattern independently. In addition to the predefined amounts above, a limit can be set on any profile event (see the system.events table) by using the event name as the tag, for example:
The same can be expressed with the DDL syntax:
Unlike the predefined amounts, profile events are accounted once per query, at the moment the query finishes (successfully or with an error): a query that crosses the limit is still allowed to finish, and subsequent queries are rejected until the interval ends. The consumption is reported in the profile_events column of the system.quotas_usage table. Profile events accumulated outside of queries (for example, by background merges) are not charged to quotas. For a distributed query, the profile events that the remote servers report back to the initiator are charged to the quota of the initiating user together with the local ones. The remote servers always send these reports for a distributed query, regardless of the send_profile_events setting in the query, in its SETTINGS clause, or in the settings constraints of the user on the remote server, so a user cannot turn them off. This includes a synchronous INSERT into a Distributed table or the remote table function, for which a remote server reports its profile events once it has received all the data; the background sends of Distributed tables with distributed_foreground_insert = 0 are not part of any user query and are not charged. The deferred flush of asynchronous inserts is charged to the quota of the user who submitted the inserts, in the same bucket as the inserts were checked against (for a quota keyed by ip_address or forwarded_ip_address, the bucket of the client address; for a quota keyed by normalized_query_hash, the bucket of the submitted INSERT), and not to the query that forces the flush, such as SYSTEM FLUSH ASYNC INSERT QUEUE. If a remote server receiving a distributed INSERT queues it as an asynchronous insert, the deferred flush on that server is charged there, to the quota of the user the initiator connects as, and is not reported back to the initiator. Nothing is counted twice: the rows that the initiator of a distributed INSERT sends to a remote server are charged once in InsertedRows and InsertedBytes, while what the remote server writes beyond them, for example into the target tables of materialized views, is charged in addition. If the limit is exceeded for at least one time interval, an exception is thrown with a text about which restriction was exceeded, for which interval, and when the new interval begins (when queries can be sent again).
Queries excluded from quota accountingTo allow quota configuration and usage to be inspected even when a quota is exhausted, SELECT queries that exclusively read from one of the following system tables are not charged to quotas and are not subject to query-complexity limits:
  • system.quotas
  • system.quota_limits
  • system.quota_usage
  • system.quotas_usage
  • system.one
Quotas can use the “quota key” feature to report on resources for multiple keys independently. Here is an example of this:
You can also key quotas by normalized query hash, so that each distinct query pattern gets its own independent quota bucket. In the XML configuration this is written as <keyed_by_normalized_query_hash />:
The same can be expressed with the DDL syntax:
In this example, the user can execute up to 100 instances of each distinct normalized query per hour. SELECT number FROM numbers(1) and SELECT number FROM numbers(2) share the same bucket (because they have the same normalized form), but SELECT number, number FROM numbers(1) uses a separate bucket. The quota is assigned to users in the ‘users’ section of the config. See the section “Access rights”. For distributed query processing, the accumulated amounts are stored on the requestor server. So if the user goes to another server, the quota there will “start over”. When the server is restarted, quotas are reset.