Skip to content

log internal queries - #83277

Merged
mstetsyuk merged 136 commits into
masterfrom
log-internal-queries
Nov 15, 2025
Merged

mstetsyuk merged 136 commits into
masterfrom
log-internal-queries

Conversation

@mstetsyuk

@mstetsyuk mstetsyuk commented Jul 4, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Log internal queries (those executed internally by dictionaries, refreshable materialized views, etc) and add the new is_internal column to system.query_log.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Jul 4, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [38a41d9]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan, old analyzer, 2/6) failure
test_external_http_authenticator/test.py::test_user_from_config_basic_auth_pass FAIL cidb
test_external_http_authenticator/test.py::test_user_create_basic_auth_pass FAIL cidb
test_external_http_authenticator/test.py::test_basic_auth_failed FAIL cidb
test_external_http_authenticator/test.py::test_header_failed FAIL cidb
test_external_http_authenticator/test.py::test_session_settings_from_auth_response FAIL cidb
BuzzHouse (amd_debug) failure
Buzzing result failure cidb

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 4, 2025
@mstetsyuk
mstetsyuk force-pushed the log-internal-queries branch from 4d2e191 to 1c060e8 Compare July 18, 2025 09:08
@mstetsyuk
mstetsyuk marked this pull request as ready for review July 18, 2025 17:16
@mstetsyuk
mstetsyuk requested a review from Algunenano July 18, 2025 17:16
@Algunenano Algunenano self-assigned this Jul 21, 2025

@Algunenano Algunenano 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.

Please review the test and style failures

Comment thread src/Dictionaries/FlatDictionary.h Resolved
Comment thread src/Interpreters/executeQuery.cpp Resolved
@mstetsyuk
mstetsyuk marked this pull request as draft August 14, 2025 20:04
@mstetsyuk
mstetsyuk requested a review from Algunenano November 11, 2025 12:52

@Algunenano Algunenano 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.

Minor comments

Comment thread src/Core/SettingsChangesHistory.cpp Resolved
/// We should not include internal queries for limiting the number of simultaneous queries.
if (!is_internal)
{
++non_internal_processes;

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.

It's unclear to me which mutex protects this variable (or query_kind_amounts). I don't see a mutex here, and they are not atomic

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.

All the accesses of ProcessList::non_internal_processes are guarded by ProcessList::mutex. And all the accesses of ProcessListForUser::non_internal_queries are also guarded by ProcessList::mutex.

/// limit for waiting queries. 0 means no limit. Otherwise, when limit exceeded, an exception is thrown.
std::atomic<UInt64> max_waiting_queries_amount{0};

/// amounts of waiting queries

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.

What happened with all the code around waiting queries?

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.

dead code

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.

I don't think it's dead code. We have server settings max_waiting_queries, max_concurrent_select_queries, max_concurrent_insert_queries, etc. How will they work now?

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.

Neither incrementWaiters nor decrementWaiters was ever invoked, so increaseWaitingQueryAmount and decreaseWaitingQueryAmount were never invoked either. Consequently, all of waiting_queries_amount, waiting_insert_queries_amount, and waiting_select_queries_amount were always zero and all the checks worked without taking the number of waiting queries into account.

But now that you've raised this, I think I should've kept all this logic and started calling incrementWaiters and decrementWaiters instead.

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.

Let's ping @serxa as the author

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.

It seems to be not fully implemented. AsyncLoader callbacks on_waiters_increment and on_waiters_decrement should be passed to all places where makeLodJob is called and they should call corresponding function of ProcessList. And it requires some tests, which I obviously skipped, so now it does not work at all. Let me fix it separately.

@mstetsyuk
mstetsyuk added this pull request to the merge queue Nov 15, 2025
Merged via the queue into master with commit fb94712 Nov 15, 2025
@mstetsyuk
mstetsyuk deleted the log-internal-queries branch November 15, 2025 22:25
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 15, 2025
query_scope = std::make_unique<CurrentThread::QueryScope>(context_copy);
}

context_copy->setCurrentQueryId({});

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.

I believe this is not enough, it does not set query_id for current_thread - and this means that you cannot find logs for this queries in text_log/trace_log/..., we need this as well

current_thread->setQueryId(...);

And this

current_thread->clearQueryId();

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.

good point, will do! (I was just debugging an incident where this information would have been quite handy...)

@mstetsyuk mstetsyuk Feb 18, 2026 •

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.

feiyunwill pushed a commit to feiyunwill/ClickHouse that referenced this pull request Oct 20, 2026
…ing_queries

Restores the accounting added in ClickHouse#61053 and removed as dead code in ClickHouse#83277, with
the two defects that kept it from working in the first place fixed.

`limit == 0` is documented as "no limit", but the original check was
`if (value >= limit) throw`, which refuses the very first waiter on a
default-configured server. The limit is now consulted only when it is set.

The per-query election used `waiting_threads.fetch_add(1) == 0` to decide which
thread registers the query. When the limit check then threw, `waiting_threads`
was left at 1 with the query never registered, so that query stayed exempt from
the limit forever; rolling the counter back instead would race a second thread
of the same query into a follower with no leader, which underflows the global
counter. A per-query mutex makes the check run strictly before the counter
moves, so a refused query is left exactly as it was.

A query that the concurrency limits exempt (`KILL QUERY`, `SELECT FROM
system.processes`, the introspection port) is counted but never refused: it is
counted by the limits that the waiting amounts are subtracted from, and the
admin needs it to work on a server that has hit the limit. Internal queries are
outside both, matching `non_internal_processes` and `query_kind_amounts`.

Nothing calls this yet; the load jobs are wired up in the next commit.

Co-Authored-By: Claude Opus 5 <[email protected]>
feiyunwill pushed a commit to feiyunwill/ClickHouse that referenced this pull request Oct 20, 2026
`max_waiting_queries` documents that a query waiting for an asynchronously
loading table is not counted when `max_concurrent_queries`,
`max_concurrent_insert_queries`, `max_concurrent_select_queries`,
`max_concurrent_queries_for_user` and `max_concurrent_queries_for_all_users` are
checked, "to avoid hitting these limits just after server startup". That
correction was added in ClickHouse#61053 and removed together with the rest of the waiting
accounting in ClickHouse#83277, so the second half of the paragraph defining this setting
has been false since then.

It also makes the two limits compose. A waiting query holds a
`max_concurrent_queries` slot without it, so with `max_concurrent_queries = 100`
and `max_waiting_queries = 200` the 101st waiter is refused by the wrong limit and
the configured 200 is unreachable.

The discount is applied to all five checks including the `have_space` predicate,
otherwise a query would be parked for `queue_max_wait_ms` and then refused despite
it. The reported ", waiting: N" is emitted only when the count is non-zero, so an
operator can tell why a limit did or did not fire; the existing tests that match
these messages all match a prefix.

In steady state all four waiting counters are zero and the behaviour is unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants