Skip to content

feat: support system.connections table for client connections metadata - #99702

Merged
kavirajk merged 34 commits into
ClickHouse:masterfrom
kavirajk:kavirajk/system-connections-table
Oct 20, 2026
Merged

kavirajk merged 34 commits into
ClickHouse:masterfrom
kavirajk:kavirajk/system-connections-table

Conversation

@kavirajk

@kavirajk kavirajk commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

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

new system.connections to have visibility on active/idle clients connections to ClickHouse server

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Primarily driven by ClickHouse cloud use cases (but I can see it's worth having it for other OSS use cases as well). Currently there is no clean way of knowing existing "client" connections to ClickHouse server.

The hardway is to parse query_logs, proxy_logs and metrics to get the idea. but that's fragile.

Plus particularly in clickhouse cloud, people often wonder why their instance is not going idle even though they think they are not actively using the ClickHouse instance. Now we can have clear picture.

This tries to address the open issue #60670

I also made sure it works with proxies in-between. So that system.connections shows "real" client info as much as possible instead of just random proxy info in-between (we have run the clickhouse server with tcp_with_proxy_port and auth_use_forwarded_address=1 configs. And proxy_protocol has to be enabled in the proxy (to forward the to proxy_port of clickhouse server)

clickbox :) select * from system.connections;

SELECT *
FROM system.connections

Query id: 845dcadd-7249-4ce4-8c3d-c08032594155

   ┌─connection_id─┬─protocol─┬─client_address─┬─client_port─┬─server_port─┬─user────┬─status─┬─query_id─────────────────────────────┬─client_name───────┬─client_version_major─┬─client_version_minor─┬─client_version_patch─┬──────connected_time─┬─────last_query_time─┐
1. │             2 │ TCP      │ ::1            │       38792 │        9000 │ default │ active │ 845dcadd-7249-4ce4-8c3d-c08032594155 │ ClickHouse client │                   26 │                    2 │                    0 │ 2026-03-17 09:18:18 │ 2026-03-17 09:18:27 │
2. │             1 │ HTTP     │ ::1            │       47424 │        8123 │ default │ active │ bafe306d-3089-4ebc-874a-8119c006047c │                   │                    0 │                    0 │                    0 │ 2026-03-17 09:18:14 │                ᴺᵁᴸᴸ │
   └───────────────┴──────────┴────────────────┴─────────────┴─────────────┴─────────┴────────┴──────────────────────────────────────┴───────────────────┴──────────────────────┴──────────────────────┴──────────────────────┴─────────────────────┴─────────────────────┘

This the whole table columns and it's types currently supported.

clickbox :) show create table system.connections

SHOW CREATE TABLE system.connections

Query id: 2b3fee01-2870-461b-8dca-9b50c1687b39

   ┌─statement────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
1. │ CREATE TABLE system.connections                                                                                                                                     ↴│
   │↳(                                                                                                                                                                   ↴│
   │↳    `connection_id` UInt64,                                                                                                                                         ↴│
   │↳    `protocol` LowCardinality(String),                                                                                                                              ↴│
   │↳    `client_address` IPv6,                                                                                                                                          ↴│
   │↳    `client_port` UInt16,                                                                                                                                           ↴│
   │↳    `server_port` UInt16,                                                                                                                                           ↴│
   │↳    `user` String,                                                                                                                                                  ↴│
   │↳    `status` LowCardinality(String),                                                                                                                                ↴│
   │↳    `query_id` String,                                                                                                                                              ↴│
   │↳    `client_name` String,                                                                                                                                           ↴│
   │↳    `client_version_major` UInt64,                                                                                                                                  ↴│
   │↳    `client_version_minor` UInt64,                                                                                                                                  ↴│
   │↳    `client_version_patch` UInt64,                                                                                                                                  ↴│
   │↳    `connected_time` DateTime,                                                                                                                                      ↴│
   │↳    `last_query_time` Nullable(DateTime)                                                                                                                            ↴│
   │↳)                                                                                                                                                                   ↴│
   │↳ENGINE = SystemConnections                                                                                                                                          ↴│
   │↳COMMENT 'Contains a list of currently active client connections to the server (TCP and HTTP). Requires the collect_connection_metrics server setting to be enabled.' │
   └──────────────────────────────────────────────────────

It also works with


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

Version info

  • Merged into: 26.11.1.360-master (included in 26.11 and later)

This table manages the active and idle (in case of TCP) connections
from the client

Signed-off-by: Kaviraj <[email protected]>
This add connection registry and it's implementation. Also get invoked
in TCP and HTTP handlers for every new connections.

Signed-off-by: Kaviraj <[email protected]>
This is part of in-memory storage similar to system.process tables.

Signed-off-by: Kaviraj <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [da6bd13]

Summary: ✅


AI Review

Summary

PR #99702 adds opt-in system.connections reporting for native TCP and HTTP connections, including idle keep-alive sockets, per-user visibility, and runtime configuration reloads. No unresolved findings remain at head da6bd1318726b4ec5ba61e4339e6d30a8715dbd0. The four relevant fast-test cases passed in CI; the three TCP lifecycle and access-control tests skipped by fast test passed locally against that head’s CI binary.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Mar 17, 2026
Comment thread src/Server/TCPHandler.cpp Resolved
Also added tests to lock the behavior from regression

Signed-off-by: Kaviraj <[email protected]>
@kavirajk
kavirajk marked this pull request as ready for review March 18, 2026 12:37
use `getCurrenetQueryID()` to avoid setting empty query_id

Signed-off-by: Kaviraj <[email protected]>
Changes:
1. when used with HTTP, it was not extracting the client_name properly.
   Now it does
2. connected_time makes no sense with http (unlike TCP). Only
   last_query_time matters for HTTP

Signed-off-by: Kaviraj <[email protected]>
@jkartseva jkartseva self-assigned this Jun 9, 2026
@kavirajk kavirajk added the can be tested Allows running workflows for external contributors label Jun 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.80% 84.70% -0.10%
Functions 92.40% 92.30% -0.10%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 133/141 (94.33%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Dear @jkartseva, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@jkartseva jkartseva self-assigned this Jul 11, 2026

@jkartseva jkartseva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Most importantly, please fix the HTTP semantics to support keep-alive sockets and idle intervals.

Comment thread src/Core/ServerSettings.cpp Resolved
Comment thread src/Server/HTTPHandler.cpp Resolved
Comment thread src/Server/ConnectionRegistry.h Resolved
Comment thread src/Server/HTTPHandler.cpp Resolved
Comment thread src/Server/ConnectionRegistry.cpp Resolved
Comment thread src/Server/TCPHandler.cpp Resolved
Conflict in `src/Core/ServerSettings.cpp`: both sides appended a new
setting declaration at the same position in the settings list. Kept both
`collect_connection_metrics` and `user_profile_events_per_cpu`.
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Dear @jkartseva, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@kavirajk

Copy link
Copy Markdown
Contributor Author

@jkartseva Thanks for the review. Addressed the feedback and remarks. Ready for another look :)

@jkartseva jkartseva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mostly LGTM, I have a couple of suggestions, PTAL at the comments.

Comment thread src/Core/ServerSettings.cpp Outdated
)", 0) \
DECLARE(UInt64, total_memory_profiler_sample_min_allocation_size, 0, R"(Collect random allocations of size greater or equal than specified value with probability equal to `total_memory_profiler_sample_probability`. 0 means disabled. You may want to set 'max_untracked_memory' to 0 to make this threshold to work as expected.)", 0) \
DECLARE(UInt64, total_memory_profiler_sample_max_allocation_size, 0, R"(Collect random allocations of size less or equal than specified value with probability equal to `total_memory_profiler_sample_probability`. 0 means disabled. You may want to set 'max_untracked_memory' to 0 to make this threshold to work as expected.)", 0) \
DECLARE(Bool, collect_connection_metrics, false, R"(Adds the `system.connections` table. This table shows the connections of the native TCP and HTTP query interfaces. It does not show connections of MySQL, PostgreSQL, gRPC, or other protocols.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Currently, SYSTEM RELOAD CONFIG does not reload this setting: it keeps the value of the previous <collect_connection_metrics>true</collect_connection_metrics> until the server is restarted. Let's fix this.
Could you please also add a test:

  SYSTEM RELOAD CONFIG;
  SELECT count() > 0 FROM system.connections WHERE query_id = currentQueryID();
``

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Addressed 👍


return ColumnsDescription
{
{"connection_id", std::make_shared<DataTypeUInt64>(), "Unique identifier of the connection. A TCP connection gets the identifier after authentication. An HTTP connection gets the identifier on its first query."},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wdyt about switching from sequential IDs to a UUID, which can be derived
{"connection_id", std::make_shared(), "..."},

A pseudo-random UUID can be easily assigned through the call

const UUID id = UUIDHelpers::generateV4();

It follows an established pattern in CH.
In the Cloud, we often use the clusterAllReplicas query, and connections from different pods and the same id will collide.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree. This makes is super simple in cluster mode 👍 addressed

{"client_version_major", std::make_shared<DataTypeUInt64>(), "Major version of the client from the TCP handshake. 0 for HTTP connections."},
{"client_version_minor", std::make_shared<DataTypeUInt64>(), "Minor version of the client from the TCP handshake. 0 for HTTP connections."},
{"client_version_patch", std::make_shared<DataTypeUInt64>(), "Patch version of the client from the TCP handshake. 0 for HTTP connections."},
{"connected_time", std::make_shared<DataTypeDateTime>(), "Time when the connection started. For a TCP connection, this is the time after authentication."},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please expand the documentation part explaining that connected_time means different things per protocol.
HTTP records it at socket accept (HTTPServerConnection.cpp:50, before any request), while TCP records std::time(nullptr) at registration in runImpl, i.e. after authentication, so accept, handshake and auth time are excluded.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. I actually thought about a bit after adding runtime reload compatibility. And may be we can make it consistent across both HTTP and TCP instead of documenting the difference.

Before:
Http recorded the time when the server started to handle the socket. TCP recorded the time after the handshake and authentication. So same column connected_time measured two different things.

Now:
TCP also records the time when the handler is created for the socket. This is same moment as for HTTP. Why? the lazy TCP registration before made the old registration time even less meaningful. A connection registered after a switch at runtime would have shown that later time as when it "connected"?

WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Okay, sounds good.

Basically two minor changes
1. Make the setting `collect_connection_metrics` reloadable at the
   runtime (via SYSTEM RELOAD CONFIG for example). To make this
   possible, now we check for registeration before each query during
   HTTP. Common how it's done in other places
2. Make connection_Id UUID instead of Uint64. To avoid complexities and
   conflict during cluster mode.

Signed-off-by: Kaviraj <[email protected]>
@kavirajk
kavirajk requested a review from jkartseva October 16, 2026 15:38
set log level to fatal similar to other tests that does system settings
reload

Signed-off-by: Kaviraj <[email protected]>
If user's username is used for checking the permission, then there is
subtle bug when user is renamed. And it can be security violation. Bot
review identified that.

This patch addressed it by using user_id (that doesn't change even when
renaming)

Signed-off-by: Kaviraj <[email protected]>
@kavirajk

Copy link
Copy Markdown
Contributor Author

FYI: All the failed test cases are related to 05339_string_filters_during_scan_parquet_short_dictionary_values tests which looks irrelevant to the PR changes. I also see this particular tests are flaky even in master branch ?

@jkartseva jkartseva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me, with one minor comment (see below). Thanks for following up, let's merge.

I also see this particular tests are flaky even in master branch ?

Yes, we have tests that flake on master 😞

{"client_version_major", std::make_shared<DataTypeUInt64>(), "Major version of the client from the TCP handshake. 0 for HTTP connections."},
{"client_version_minor", std::make_shared<DataTypeUInt64>(), "Minor version of the client from the TCP handshake. 0 for HTTP connections."},
{"client_version_patch", std::make_shared<DataTypeUInt64>(), "Patch version of the client from the TCP handshake. 0 for HTTP connections."},
{"connected_time", std::make_shared<DataTypeDateTime>(), "Time when the connection started. For a TCP connection, this is the time after authentication."},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Okay, sounds good.

@kavirajk
kavirajk enabled auto-merge October 20, 2026 08:38
Comment thread src/Server/HTTPHandler.cpp Resolved
This is needed as there can be following case where one user may see the
connections of other users (HTTP case)

one HTTP keep-alive socket, execute a query as alice with `User-Agent: alice-agent`, disable `collect_connection_metrics` via SYSTEM RELOAD CONFIG,
execute a query as bob with User-Agent: bob-agent, then enable collection again.

The row still reports alice / alice-agent; Alice can see it without SHOW CONNECTIONS, while Bob cannot.
It stays stale until another query on that socket. Re-enabling during a running request similarly exposes the retained idle state

Signed-off-by: Kaviraj <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-system-table system.* tables (introspection, system metadata, performance visibility). pr-feature Pull request with new product feature 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.

4 participants