- Query Queue v1: Triggers queuing based on query concurrency, BE memory usage, and BE CPU usage. The original query queue configurations and behaviors in this topic belong to v1. From v3.1.4 onwards, v1 supports setting query queues on the resource group level.
- Query Queue v2: Supported from v3.3 onwards. v2 estimates the BE resources consumed by each query, represents BE resources as logical slots, and queues and schedules queries based on the number of slots each query needs.
Query Queue v1
Query Queue v1 supports setting thresholds on CPU usage, memory usage, and query concurrency to trigger query queues. Roadmap:Enable Query Queue v1
Query queues are disabled by default. You can enable global or resource group-level query queues for INSERT loading, SELECT queries, and statistics queries by setting corresponding global session variables.Enable global query queues
- Enable query queues for loading tasks:
- Enable query queues for SELECT queries:
- Enable query queues for statistics queries:
Enable resource group-level query queues
From v3.1.4 onwards, the database supports setting query queues on the resource group level. To enable the resource group-level query queues, you also need to setenable_group_level_query_queue in addition to the global session variables mentioned above.
Enable query queues in shared-data clusters
In a shared-data cluster, query queues are enabled per warehouse instead of through the global session variables above. To enable the query queue for a warehouse:enable_query_queue_v2 decides whether the enabled queue is v1 or v2 (v2 by default; changing it requires restarting FE nodes). When it is set to false, each warehouse with the query queue enabled uses its own v1 queue:
- The concurrency limit is taken from the warehouse property
query_queue_concurrency_limit. - The
concurrency_limitof a resource group is counted separately within each warehouse (enable_group_level_query_queuemust also be set). - Whether the CPU and memory usage thresholds are reached, and whether a resource group exceeds its own resource limits, is judged only against the nodes of that warehouse.
Specify resource thresholds
Specify resource thresholds for global query queues
You can set the thresholds that trigger query queues via the following global session variables:
Note
- After Query Queue v2 is enabled,
query_queue_cpu_used_permille_limitis no longer supported for queue triggering, andquery_queue_mem_used_pct_limitstill applies. See Memory admission. - By default, BE reports resource usage to FE at one-second intervals. You can change this interval by setting the BE configuration item
report_resource_usage_interval_ms.
Specify resource thresholds for resource group-level query queues
From v3.1.4 onwards, you can set individual concurrency limits (concurrency_limit) and CPU core limits (max_cpu_cores) when creating a resource group. You can also set a memory usage threshold (mem_used_pct_limit) for the resource group. When a query is initiated, if any of the resource consumptions exceed the resource threshold at either the global or resource group level, the query will be placed in queue until all resource consumptions are within the threshold.
Resource group-level query queues are implemented only by Query Queue v1. After Query Queue v2 is enabled (
enable_query_queue_v2 is set to true, which is the default from v4.1 onwards), concurrency_limit, max_cpu_cores, and mem_used_pct_limit of a resource group no longer cause queries to be queued. In this case, max_cpu_cores and mem_used_pct_limit have no effect. concurrency_limit is not enforced for a query only when enable_group_level_query_queue is set to true and the query itself is subject to the query queue (that is, the query queue is enabled for its query type). Otherwise, for example when enable_group_level_query_queue is false (default) or the query queue is disabled for the query type, a query that exceeds concurrency_limit on a BE node fails with the error Exceed concurrency limit instead of being queued.
You can use SHOW USAGE RESOURCE GROUPS to view the resource usage information for each resource group on each BE node, as described in View Resource Group Usage Information.
Manage query concurrency
When the number of running queries (num_running_queries) exceeds the global or resource group’s concurrency_limit, incoming queries are placed in the queue. The way to obtain num_running_queries differs between versions < v3.1.4 and >= v3.1.4.
-
In versions
<v3.1.4,num_running_queriesis reported by BEs at the interval specified inreport_resource_usage_interval_ms. Therefore, there might be some delay in the identification of changes innum_running_queries. For example, if thenum_running_queriesreported by BEs at the moment does not exceed the global or resource group’sconcurrency_limit, but incoming queries arrive and exceed theconcurrency_limitbefore the next report, these incoming queries will be executed without waiting in the queue. -
In versions
>=v3.1.4, all running queries are collectively managed by the Leader FE. Each Follower FE notifies the Leader FE when initiating or finishing a query, allowing the database to handle scenarios where there is a sudden increase in queries exceeding theconcurrency_limit.
Configure Query Queue v1
You can set the capacity of a query queue and the maximum timeout of queries in queues via the following global session variables:Configure dynamic adjustment of query concurrency
Starting from version v3.1.4, for queries managed by the query queue and run by the Pipeline Engine, the database can dynamically adjust the query concurrencypipeline_dop for incoming queries based on the current number of running queries num_running_queries, the number of fragments num_fragments, and the query concurrency pipeline_dop. This allows you to dynamically control query concurrency while minimizing scheduling overhead, ensuring optimal BE resource utilization. For more information about fragments and query concurrency pipeline_dop, see Query Management - Adjusting Query Concurrency.
For each query under a query queue, the database maintains a concept of drivers, which represent the concurrent fragments of a query on a single BE. Its logical value num_drivers, which represents the total concurrency of all fragments of that query on a single BE, is equal to num_fragments * pipeline_dop. When a new query arrives, the database adjusts the query concurrency pipeline_dop based on the following rules:
- The more the number of running drivers
num_driversexceeds the low water limit of concurrent driversquery_queue_driver_low_water, the lower the query concurrencypipeline_dopis adjusted to. - The database restrains the number of running drivers
num_driversbelow the high water limit of concurrent drivers for queriesquery_queue_driver_high_water.
pipeline_dop using the following global session variables:
Query Queue v2
From v3.3 onwards, the database supports Query Queue v2. In Query Queue v2, query queues are no longer triggered based on fixed thresholds for query concurrency, BE memory usage, or BE CPU usage. Instead, it estimates the BE resources required by each query and queues and schedules queries based on logical slots. If there are not enough available slots, the query waits in the queue until enough slots are released.Configure Query Queue v2
Query Queue v2 is enabled and tuned through FE configuration items. Changes toenable_query_queue_v2 require restarting FE nodes to take effect. In a shared-data cluster, this item also decides whether the query queues of warehouses use v1 or v2, while the queue itself is enabled by the warehouse property enable_query_queue.
Note
query_queue_cpu_used_permille_limit applies only to Query Queue v1. For how Query Queue v2 applies query_queue_mem_used_pct_limit, see Memory admission.
Resource slots
Query Queue v2 represents BE resources as logical slots:- Total cluster slots: the database sets a logical total number of slots for the entire cluster. This total is positively correlated with the number of BEs and BE CPU cores, and is also affected by
query_queue_v2_concurrency_level. - Slots required by a query: the database estimates the number of slots required by each query. The estimation is based on factors such as statistics, query complexity, the number of fragments, estimated input and output data volumes of complex operators, and DOP.
Queuing logic
When the number of slots required by a query exceeds the current number of remaining slots, the query waits in the queue. By default (query_queue_v2_schedule_strategy is SWRR), Query Queue v2 divides pending queries into categories by the number of slots they require, and selects queries from these categories by using the smooth weighted round-robin algorithm. Categories of queries that require fewer slots have higher weights, allowing small queries to obtain resources first and avoiding head-of-line blocking where a large query at the head of the queue blocks later small queries, while queries that require more slots are not starved. In versions earlier than v4.1.4, queries that require only one slot can also be admitted from an extra pool of small slots. If query_queue_v2_schedule_strategy is set to SJF (supported from v3.5.0 onwards), Query Queue v2 schedules short jobs first and uses aging to avoid starvation.
The entire queuing logic is completed on FE, including setting the total number of cluster slots, estimating the number of slots required by a query, and deciding which query’s slot requirement to satisfy first. Slot scheduling does not depend on the actual resource usage of BEs. The only input taken from actual usage is the optional memory usage limit query_queue_mem_used_pct_limit, which makes new queries wait while a node uses too much memory. See Memory admission.
Choose an estimation strategy
PBE
Parallel-based estimation (PBE) strategy is best for:- Normal reporting queries
- Mixed point lookups and large queries
- Users who do not want to understand cost model details
- DBAs who want stable, simple, and explainable queueing behavior first
- Point lookups or queries that scan little data after pruning use fewer slots
- Queries that scan larger ranges use more slots
- Small queries are more likely to get execution resources during peak hours
MBE
Memory cost-based estimation (MBE) strategy is suitable for dealing with memory pressure, such as large joins, large aggregations, or high-cardinality aggregations. The following example sets MBE as the strategy, and allocates 2 GB of memory to each slot:- Adjustment: Decrease
query_queue_v2_concurrency_level - Effect: Directly lowers the MBE total memory budget
- Adjustment: Increase
query_queue_v2_concurrency_level - Effect: Directly raises the MBE total memory budget
max_slots is very small and integer rounding is visible
- Adjustment: Decrease
query_queue_v2_mem_bytes_per_slot - Effect: Uses a finer memory slot granularity and reduces coarse rounding error
CBE
CPU cost-based estimation (CBE) strategy is suitable for dealing with memory pressure, such as compute-heavy SQL, complex expressions, or heavy CPU work after scanning. The following example sets CBE as the strategy, and set the CPU cost threshold to1000000000 for each slot:
- Adjustment: Decrease
query_queue_v2_cpu_costs_per_slot - Effect: The same CPU cost for more slots, making concurrency more conservative
- Adjustment: Increase
query_queue_v2_cpu_costs_per_slot - Effect: The same CPU cost for fewer slots, making concurrency looser
Tune concurrency capacity
If you only want to increase or decrease overall concurrency, do not switch between PBE, MBE, and CBE first. Tune total slot capacity first:- Start with default value
4. - Observe
remain_slots,max_slots,query_pending_length, CPU, memory, and query latency. - If there is resource headroom but queries queue noticeably, gradually increase
query_queue_v2_concurrency_level. - If resources are often saturated or queries interfere with each other heavily, gradually decrease
query_queue_v2_concurrency_level. - Make small changes each time, such as 10% to 25%, and observe one business peak period before making another change.
query_queue_v2_concurrency_level to tune overall capacity first. Consider switching to MBE or PBE only after that. Do not change multiple parameters at the same time at the beginning, because it becomes hard to tell which parameter caused the effect.
Fallback Concurrency Cap
query_queue_concurrency_limit is a fallback concurrency cap and applies to PBE, MBE, and CBE. Query Queue V2 first estimates the slots required by a query with the current estimator and checks whether total slots are available. After that, it checks whether the current number of running queries has reached query_queue_concurrency_limit.
The default value 0 means unlimited. Set it only when you need an absolute cap on the number of concurrently running queries:
default_warehouse and does not support warehouse DDL statements such as ALTER WAREHOUSE (they fail with the error Multi-Warehouse is not implemented). Use the global variable instead.
Use query_queue_v2_concurrency_level to tune resource capacity first. Use query_queue_concurrency_limit only when you need to explicitly limit the number of queries run at the same time.
Memory admission
Slots do not measure the memory that queries use. Memory admission controls memory with two settings:query_queue_mem_used_pct_limit: the memory usage limit of a node. While the process memory usage of any alive node reaches this fraction of the node’s memory limit, new queries wait in the queue. It is the same global session variable that Query Queue v1 uses.query_mem_limit: the existing session variable that sets the memory limit of a query on each BE node.
query_queue_mem_used_pct_limit defaults to 0, which means that no query waits because of memory. Set it with SET GLOBAL.
Memory usage limit
query_queue_mem_used_pct_limit is a fraction in [0, 1], such as 0.8 for 80%. It is compared with the memory usage of each node: the process memory that the node uses, divided by the memory limit of the node. The process memory includes not only queries but also loads, compaction, metadata, and the in-memory data cache. A query fails when either its query pool or the BE process runs out of memory, so comparing the process memory keeps a buffer against both limits, and new queries wait before a node runs out of memory.
The memory usage limit works as follows:
- Each BE and CN node reports its memory usage every second, as set by the BE configuration item
report_resource_usage_interval_ms. Only the leader FE receives these reports, and the leader FE makes the admission decisions. - While the memory usage of any alive node is at or above the limit, new queries wait in the queue. Running queries are not affected. Once every node is below the limit, the queued queries are admitted again. The decision is binary: there is no gradual throttling between the two states.
- The memory usage limit is checked together with the slot pool and
query_queue_concurrency_limit. A query is admitted only when all three allow it. - When no query that was admitted by the queue is running, one query is still admitted even if a node is over the limit. The memory may be held by loads, caches, or queries that bypass the queue, and keeping queued queries waiting cannot release it.
- A node whose latest report is older than the global variable
query_queue_fresh_resource_usage_interval_ms(5000milliseconds by default) does not take part in the decision, so that a node that stops reporting cannot keep new queries waiting. A node that is not alive does not take part either. - The memory usage limit does not read or depend on resource groups.
Choose a limit
A large part of the process memory is usually the in-memory data cache, which holds the decompressed pages that queries read from segment files. Its size is limited by the BE configuration itemdatacache_mem_size (20% of the process memory limit by default), and it grows as queries read data. BE gives this memory back when the process memory is high: above the BE configuration item memory_high_level (75 percent of the process memory limit by default), BE evicts the cache gradually, and above memory_urgent_level (85 by default), BE shrinks the cache immediately.
We recommend a limit between 0.75 and 0.85:
- Within this range, when queries need more memory, the cache gives memory back as the process memory usage rises above 75%, and new queries wait only until the cache has shrunk.
- Below
0.75, BE does not release the cache, so a node with a large cache can keep new queries waiting. The cluster then admits one query at a time, when nothing is running. - A lower limit leaves more room for the queries that are already running to grow. On a node that holds little besides queries, the room left before the query pool limit (90% of the process memory limit by default, as set by the BE configuration item
query_max_memory_limit_percent) is 0.9 minus the limit.
Per-query memory limit on each node
The session variablequery_mem_limit sets the memory limit of a query on each BE node, in bytes. The default value 0 means no limit. To set it for the current session, run SET query_mem_limit = <bytes>;. To set it for all future sessions, run SET GLOBAL query_mem_limit = <bytes>;. For more information, see query_mem_limit.
On each node, BE applies the smallest of query_mem_limit, the resource group property big_query_mem_limit, and the limits of the parent memory trackers. When a query exceeds query_mem_limit, the error message contains Mem usage has exceed the limit of single query, You can change the limit by set session variable query_mem_limit.
Configure memory admission
The following example sets the memory usage limit:query_queue_mem_used_pct_limit, also set query_mem_limit. See Limitations for the reason.
Monitor memory admission
To see the memory usage that is compared with the limit, run SHOW BACKENDS in a shared-nothing cluster or SHOW COMPUTE NODES in a shared-data cluster. TheMemUsedPct column is the memory usage of each node, as a percentage of its memory limit. Only the leader FE receives the memory usage that the nodes report, so run the statement on the leader FE.
Limitations
- Admission reserves no memory. A newly admitted query adds to the usage only after it allocates memory and the next report arrives, so the queries admitted in one batch can together overshoot the limit. The slot pool and
query_queue_concurrency_limitbound how many queries a batch admits, but onlyquery_mem_limitbounds how much memory each of them can use on a node. This is why the FE log records a warning, once for each limit value, when the memory usage limit is set but the global default ofquery_mem_limitis0. - One node at or above the limit makes all new queries wait, even if the other nodes have free memory, because queries usually run on all nodes.
Monitor query queues
You can view information related to query queues using the following methods.SHOW PROC
You can check the number of running queries, and memory and CPU usages in BE nodes using SHOW PROC:SHOW PROCESSLIST
You can check if a query is in a queue (whenIsPending is true) using SHOW PROCESSLIST:
FE audit log
You can check the FE audit log file fe.audit.log. The fieldPendingTimeMs indicates the time a query spent waiting in a queue, and its unit is milliseconds.
Monitoring metrics
You can obtain metrics of query queues in the database using the Monitor and Alert feature. The following FE metrics are derived from the statistical data of each FE node.SHOW RUNNING QUERIES
From v3.1.4 onwards, the database supports the SQL statementSHOW RUNNING QUERIES, which is used to display queue information for each query. The meanings of each field are as follows:
QueryId: The ID of the query.ResourceGroupId: The ID of the resource group that the query hit. When there is no hit on a user-defined resource group, it will be displayed as ”-”.StartTime: The start time of the query.PendingTimeout: The time when the PENDING query will time out in the queue.QueryTimeout: The time when the query times out.State: The queue state of the query, where “PENDING” indicates it is in the queue, and “RUNNING” indicates it is currently executing.Slots: The logical resource quantity requested by the query. In Query Queue v1, this value is usually1. In Query Queue v2, this value is the estimated number of slots for the query.Frontend: The FE node that initiated the query.FeStartTime: The start time of the FE node that initiated the query.