Repository navigation
Fix read_rows/read_bytes/written_rows/written_bytes in system.query_thread_log - #115596
diegomestre2 merged 3 commits into
Conversation
system.query_thread_log.read_rows, read_bytes, written_rows and written_bytes are read straight off ThreadStatus::progress_in and progress_out, which live for the lifetime of the OS thread. Nothing ever cleared them, so the row of a long-lived pooled thread reported the running total of everything that thread had ever served rather than the values for the logged query. Short-lived worker threads were unaffected because they get a fresh ThreadStatus, so the visible victim is the row with thread_id = master_thread_id, i.e. the query's initiating HTTPHandler thread. Measured before this change, over HTTP on one connection, with four identical SELECTs each reading exactly 200000 rows: query_log.read_rows was 200000 every time and so was the same log row's ProfileEvents['SelectedRows'], while query_thread_log.read_rows climbed 1000000, 1200000, 1400000, 1600000. Four identical INSERTs behaved the same way on written_rows. ThreadStatus::initPerformanceCounters() already exists for this purpose, runs on attach to a thread group from its single call site in attachToGroupImpl, and carries the comment "Clear stats from previous query if a new query is started". It resets performance_counters and memory_tracker but never these two members, which predate the hook. Adding them there completes the existing mechanism instead of introducing a new one. The reset is per attach to a thread group rather than per query, so a thread logged on several rows for one query reports each attach cycle separately. That is what performance_counters, and therefore ProfileEvents on the same row, already does, so the two fields become consistent. Query-level accounting is untouched: system.query_log, system.processes, max_rows_to_read, leaf limits, speed throttling and quotas all read the separate progress object on QueryStatus, and each was verified to still behave correctly. The regression test drives its queries over HTTP through a single keep-alive connection, because over the native protocol the reads happen on a separate pipeline thread and the initiating thread's row would assert nothing. It asserts that the value no longer accumulates across queries, that it agrees with the same row's ProfileEvents for both rows and bytes, and that it never exceeds query_log; it also asserts the precondition that four queries finished on one reused thread, so the oracle cannot pass vacuously. It fails on master.
The previous assertions were satisfiable by a master row whose thread-level values are all zero, which happens whenever the initiating thread performs no reads itself (measured over the native protocol, and reachable over HTTP under parallel replicas). On such a configuration the test passed silently and would keep passing after a regression. Each precondition line now also pins the master row's value to the per-query amount with non-zero bytes, so an all-zero row can no longer satisfy it. On a row set constructed to be all zero the earlier assertions all matched the reference while the new columns print 0 against a reference of 4. Adds the missing written_bytes vs InsertedBytes assertion: written_bytes is one of the four columns this change fixes and nothing covered it. On the unfixed binary it accumulates 400000 to 1600000 across four inserts while InsertedBytes stays correct at 400000, so the column reddens on its own. Tags no-parallel-replicas, since parallel replicas move the reads off the initiating thread. Both consumers of the tag were checked: the settings randomizer stops promoting enable_parallel_replicas, and the feature-tag override forces it off unconditionally. Also fixes two style-check failures in the test itself: the log-table queries now filter on current_database = currentDatabase(), and SYSTEM FLUSH LOGS is wrapped in a bounded retry because the query_log entry is written after the HTTP response returns. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review (2 rounds, 0 open findings)Reviewed by an independent model twice, plus my own cold review of the resulting code. Round 1 bounced the test back for hardening; round 2 returned no findings. ❌ Blockers: none.
💡 Noted, no action
Rounds: 1 approach gate, 2 code-review gates, 1 fix round. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20260820-093900 |
|
Workflow [PR], commit [73284ec] Summary: ✅
AI ReviewSummaryThis PR resets Missing context / blind spots
Final Verdict✅ No blocking or major issues found in the current head. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
|
cc @vitlibar, could you review this? The four progress columns of system.query_thread_log are read straight off two ThreadStatus members that live as long as the OS thread and were never cleared, so the row of the pooled initiating thread reported that thread lifetime running total instead of the logged query; the fix clears them in initPerformanceCounters, which is the existing per-attach reset hook and already clears every sibling piece of per-query state. |
diegomestre2
left a comment
There was a problem hiding this comment.
Properly addressed. No remarks besides the ones already checked.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units8 translation units recompiled, 20 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - 73284ecEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
CI finished clean at this head, so no line needs an owner. 171 unique check-runs, 0 incomplete, Session id: cron:our-pr-ci-monitor:20260820-200000 |
Backport #115596 to 26.6: Fix read_rows/read_bytes/written_rows/written_bytes in system.query_thread_log
Backport #115596 to 25.8: Fix read_rows/read_bytes/written_rows/written_bytes in system.query_thread_log
Backport #115596 to 26.5: Fix read_rows/read_bytes/written_rows/written_bytes in system.query_thread_log
…rows/written_bytes in system.query_thread_log
…rows/written_bytes in system.query_thread_log
Backport #115596 to 26.7: Fix read_rows/read_bytes/written_rows/written_bytes in system.query_thread_log
Backport #115596 to 26.3: Fix read_rows/read_bytes/written_rows/written_bytes in system.query_thread_log
…rows/written_bytes in system.query_thread_log
…rows/written_bytes in system.query_thread_log
…rows/written_bytes in system.query_thread_log
Related: #105246
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
read_rows,read_bytes,written_rowsandwritten_bytesinsystem.query_thread_logreporting a thread lifetime running total instead of the values for the logged query. Rows of long-lived pooled threads, in particular the query's initiatingHTTPHandlerthread, were affected.Description
ThreadStatus::progress_inandprogress_outlive as long as the OS thread, and the four progress columns are read straight off them. Nothing ever cleared them, so for a pooled thread the row reported everything that thread had ever served. Worker threads get a freshThreadStatus, so the victim is the row withthread_id = master_thread_id.ThreadStatus::initPerformanceCounters()already exists for this, runs on attach to a thread group, and says "Clear stats from previous query if a new query is started". It resetsperformance_countersandmemory_trackerbut never these two members, which predate it. The fix adds them.Before the fix, over HTTP on one connection, four identical
SELECTs each reading 200000 rows:query_log.read_rowsquery_thread_log.read_rowsProfileEvents['SelectedRows']After the fix all three agree. Four identical
INSERTs behaved the same way onwritten_rows, and are covered.One semantic point to confirm, since it is a choice: this reset runs per attach to a thread group, not per query, so a thread logged on several rows for one query reports each attach cycle separately.
ProfileEventson the same row already behaves this way, so the two become consistent rather than gaining a new convention. Populating the columns fromSelectedRows/InsertedRowsinstead was rejected: it redefines them and would tie them tolog_profile_events.Query-level accounting is untouched:
query_log,system.processes,max_rows_to_read, leaf limits, throttling and quotas read the separateQueryStatusobject, and I checked each of those.The test drives the queries over HTTP on one keep-alive connection: over the native protocol the reads happen on a separate pipeline thread, so the initiating thread's row would assert nothing. It fails on master.
@PedroTadim
Validation
read_rows1 to 4, distinctwritten_rows1 to 4, and every column comparing a progress value againstProfileEventsmoves on both arms). Reproduced under randomized settings too.query_thread_log, the eight runnable in my sandbox still pass; the remaining two need a listener this sandbox does not provide, and neither reads the four columns.progress_in/progress_outonThreadStatushave two writers (CurrentThread::updateProgressIn/Out), one reader (the log site), and no other reference insrc/,programs/,base/,utils/.Workflow [PR]
Sync PR [sync-upstream/pr/115596]
Version info
26.8.1.1838(included in26.8and later)26.7.6.28,26.6.4.9,26.3.29.9,25.8.34.9