Repository navigation
Do not charge pooled connection memory to the queries that use it - #125573
Merged
Algunenano merged 8 commits intoOct 14, 2026
Merged
Conversation
…o the pool The compressed buffers and native readers and writers that `Connection` creates for a query were only reset by the next `sendQuery`. A query that sent data over a pooled connection, for example a synchronous `INSERT` into a `Distributed` table, ended still holding them, and the next query that took the connection freed memory it never held. `PoolBase` now calls `releaseObject` when an entry is given back, on the thread that used it, and the connection pool uses it to drop those buffers there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nects it `Connection::connect` allocates the socket, its read and write buffers and the handshake state, about 2 MiB per connection, in whichever query first needs the connection. The connection then stays in the pool, so that query ended still holding the memory, 12 MiB for an `INSERT` into a `Distributed` table over 2 shards with 3 replicas each. Allocate them, and free them in `disconnect`, outside of query accounting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tes them `PoolBase::get` creates a new object, for example a `Connection`, in whichever query needs one more, and the object then stays in the pool. That query ended still holding it, about 5 KB per connection, 31.7 KB for an `INSERT` into a `Distributed` table over 2 shards with 3 replicas each. Create the pooled objects, and destroy them with the pool, outside of query accounting. A pool that lives only for one query, as `remote` creates, stays balanced since both sides are excluded. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Threads batch what they allocate and free, up to `max_untracked_memory`, before passing it to the query tracker. When one thread frees a block that another thread allocated and has not flushed yet, the query root holds less than the free, and `free` passes the rest straight to the server total. The allocation, flushed later, then stays charged to the query, and the query ends "still holding" memory that was already freed. Every multi-threaded `INSERT` logged about 2 MiB of such drift. Debug builds now count what a query root frees beyond what it holds, and `unexpectedDrift` subtracts it from a positive drift, so only drift that this does not explain is reported. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
|
Workflow [PR], commit [c171e1d] Summary: ✅
AI ReviewSummaryThis PR moves long-lived pooled-connection state off query memory tracking, frees per-query Final Verdict✅ No review findings in the current code. The PR looks ready to merge. |
…onnect`
With `async_socket_for_remote`, `connect` runs in a stackful coroutine that suspends during the socket
I/O, and the level of `MemoryTrackerBlockerInThread` is not swapped by coroutines. When several
coroutines connect on the same thread, as `remote('127.{1..10}', ...)` does, their blockers end in a
different order than they started, and the last one restores the blocked level: the thread then
stops charging every later query, which passed its memory limits in tests.
Block only the creation and destruction of the connection's buffers, which do no I/O. Also initialize
the pooled object pointer in `PoolBase::get` for clang-tidy.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Not charging the pooled objects to the query also applied to `WasmCompartmentPool`, which creates compartments for the current query, so they escaped `max_memory_usage` and user overcommit. Make every `PoolBase` subclass state whether the pool outlives the query: connection pools and the Hive metastore client pool do, `WasmCompartmentPool` does not. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…no/ClickHouse into memory-drift-untracked-batching
Algunenano
commented
Oct 14, 2026
Algunenano
left a comment
Member
Author
There was a problem hiding this comment.
Changes look good. Helps moving the needle towards less drift in the allocations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Memory that outlives a query was charged to whichever query happened to allocate it, so that query ended "still holding" it and, in debug builds, logged a drift warning without a query id (the warning is emitted from
~ThreadGroup, after the thread has detached). Most of these warnings came from distributed queries:Connectioncreates for a query were only freed by the nextsendQuery, so the next query that took the connection from the pool freed memory it never held. They are now freed when the query gives the connection back to the pool.Connection::connectallocated the socket and its buffers (about 2 MiB per connection) in the query that first needed the connection, and the pool kept the object itself the same way. Both are now outside of query accounting, as they live as long as the pool.Measured on an
INSERTinto aDistributedtable: 10 MiB left on the first query and handed over to the next one, 12 MiB per first use of 6 new connections, and about 2 MiB of reported drift per multi-threadedINSERT; all of them are now 0.Related: #113647
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Queries that send data over pooled connections, such as
INSERTinto aDistributedtable, are no longer charged for connection buffers that stay in the pool after they finish, which could count againstmax_memory_usage_for_userwhile another query of the same user was running.Workflow [PR]
Sync PR [sync-upstream/pr/125573]
Version info
26.10.1.2397-master(included in26.10and later)