max_memory_usage
Limits the request's combined live SQL heap allocations in bytes. The type is UInt64 and the default is 0, meaning no added database memory ceiling.
Scope and changes
Use SET after selecting a server database. The change is persisted for that database and observed by other clients before later requests. It requires autocommit and no active transactions in the database. Resource limits are sampled once per request; a SET within a batch changes defaults for later requests.
A nonzero XML ceiling can only be tightened by the saved database value. Zero removes the database's additional ceiling while retaining any server ceiling. Inspect system.settings for effective values and main.settings for stored values.
Accounting
The budget covers SQL processing allocations across the query's threads, including parallel workers. Existing connection caches, transport/log ownership and unrelated threads are excluded.
Accounting and process metrics remain active with a zero ceiling. Checks are cooperative; an allocation can finish before the next check reports a violation. This counter describes accounted query memory rather than the server's entire resident memory.
Example
SET max_memory_usage = 67108864;SELECT name, value
FROM system.settings
WHERE name = 'max_memory_usage'
FORMAT csv;"max_memory_usage","67108864"Terminal query-log memory_usage and peak_memory_usage both report the peak accounted value. See query_log and processes.