Skip to content

Do not charge pooled connection memory to the queries that use it - #125573

Merged
Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:memory-drift-untracked-batching
Oct 14, 2026
Merged

Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:memory-drift-untracked-batching

Conversation

@Algunenano

@Algunenano Algunenano commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

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:

  • The compressed buffers and native readers and writers that a Connection creates for a query were only freed by the next sendQuery, 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::connect allocated 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.
  • Every multi-threaded query could end "holding" up to the threads' untracked memory: a free can reach the query tracker before the allocation it matches, which another thread still holds in its untracked memory. The query root passes the excess to the server total, and the allocation arrives later. The end state was already correct thanks to the settle added in Do not leave memory charged to a user after the query that used it #113647, so debug builds now only stop reporting this as drift.

Measured on an INSERT into a Distributed table: 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-threaded INSERT; all of them are now 0.

Related: #113647

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Queries that send data over pooled connections, such as INSERT into a Distributed table, are no longer charged for connection buffers that stay in the pool after they finish, which could count against max_memory_usage_for_user while another query of the same user was running.


Workflow [PR]
Sync PR [sync-upstream/pr/125573]

Version info

  • Merged into: 26.10.1.2397-master (included in 26.10 and later)

Algunenano and others added 4 commits October 9, 2026 18:29
…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]>
@Algunenano Algunenano added the memory When memory usage is higher than expected label Oct 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c171e1d]

Summary: ✅


AI Review

Summary

This PR moves long-lived pooled-connection state off query memory tracking, frees per-query Connection buffers when a pooled connection is returned, and narrows debug-only drift warnings for batched untracked memory. The current revision also fixes the earlier PoolBase overreach by making the "pool outlives the query" behavior explicit per pool, so query-scoped pools like WasmCompartmentPool remain charged to the query. I did not find a correctness, memory-accounting, or concurrency issue in the current diff.

Final Verdict

✅ No review findings in the current code. The PR looks ready to merge.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... labels Oct 13, 2026
Algunenano and others added 2 commits October 13, 2026 11:33
…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]>
Comment thread src/Common/PoolBase.h Resolved
Algunenano and others added 2 commits October 13, 2026 14:59
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 Algunenano left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. Helps moving the needle towards less drift in the allocations

@Algunenano
Algunenano added this pull request to the merge queue Oct 14, 2026
Merged via the queue into ClickHouse:master with commit a48b12b Oct 14, 2026
176 checks passed
@Algunenano
Algunenano deleted the memory-drift-untracked-batching branch October 14, 2026 18:29
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... memory When memory usage is higher than expected pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants