Skip to content

Do not count a hidden destination in a Buffer table's total_rows - #129801

Merged
azat merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/buffer-total-rows-hidden-destination
Nov 8, 2026
Merged

azat merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/buffer-total-rows-hidden-destination

Conversation

@groeneai

@groeneai groeneai commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #114596

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

A Buffer table no longer counts its destination's rows in system.tables.total_rows for a user who has no privilege on the destination, so that table's row count is no longer disclosed. Such a user sees only the rows held in the buffer, and DROP TABLE IF EMPTY judges the buffer by those rows.

Description

On 26.3.33.24 and 26.10.2.103, a user granted SELECT on a Buffer table and nothing on its destination gets the destination's exact row count from SELECT total_rows FROM system.tables, although the destination is not listed for that user and every read of the Buffer fails with ACCESS_DENIED (it needs SELECT on the destination).

StorageBuffer::totalRows added destination->totalRows(context) without an access check. It now counts the destination only when the user has SHOW TABLES on the declared destination, the privilege under which system.tables already shows the destination's own total_rows and which every grant on it implies. Otherwise the destination contributes nothing, as it already does when it is missing. This is the gate Alias uses for the same metadata (#114596).

Users with any privilege on the destination see no change. For the others, DROP TABLE IF EMPTY of an empty buffer over a non-empty hidden destination now succeeds; a buffer with rows is still refused, and dropping a Buffer flushes its rows, so nothing is lost.

Validation: the new test fails on master (1007 instead of 7, and the empty buffer is refused) and passes 50/50 with randomized settings. Gating on SELECT instead of SHOW TABLES, or returning NULL for a hidden destination, also fails it.


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

Version info

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

groeneai and others added 2 commits November 6, 2026 16:40
StorageBuffer::totalRows added destination->totalRows(context) with no
access check. system.tables lists a Buffer on SHOW TABLES of the Buffer
alone and reads totalRows with the user's context, so a user granted
SELECT on the Buffer and nothing on its destination got the destination's
exact row count, while every read of the Buffer requires SELECT on the
destination. DROP TABLE IF EMPTY used the same number.

Count the destination only when SHOW TABLES is granted on the declared
destination, checked before the table is resolved so that a hidden and a
missing destination give the same answer. Otherwise only the buffer's
own rows are returned, which is what the function already returns when
the destination is missing (value_or(0), ClickHouse#40643). SHOW TABLES is the
privilege under which system.tables shows the destination's own
total_rows, and every grant on the destination implies it, so users with
any privilege on it see no change. StorageAlias::totalRows uses the same
gate (ClickHouse#114596).

NULL (as Alias returns) was rejected: a Buffer has rows of its own, and
Buffer -> Alias -> hidden table already yields the buffer's rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A user with DROP TABLE on two Buffers and no privilege on their
destination: the Buffer holding rows is still refused with
TABLE_NOT_EMPTY, the empty one is now dropped (before the fix it was
refused because the destination's rows were counted). Both DROPs pin
ignore_drop_queries_probability = 0 so the stress job cannot skip them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings; 1 full pass + 1 fix round + 1 delta recheck).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test did not pin the claimed DROP TABLE IF EMPTY outcomes (empty buffer over a hidden destination drops; a buffer with rows is refused) AGREE, fixed @ 40d8294 Added both arms. On master the empty buffer is refused and still listed; returning NULL instead drops the buffer that holds rows.
2 💡 Use Critical Bug Fix instead of Bug Fix DISAGREE I do not pick Critical Bug Fix on my own; that is the reviewer's call.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261106-174900

@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new test on an unfixed master build fails 1/1 without and 10/10 with randomized settings: the no-privilege line prints 1007 instead of 7, and DROP TABLE IF EMPTY of the empty buffer is refused with TABLE_NOT_EMPTY.
b Root cause explained? StorageBuffer::totalRows added the destination's totalRows with no access check; system.tables and DROP TABLE IF EMPTY read it with the user's context, while reads of the Buffer require SELECT on the destination.
c Fix matches root cause? Yes. The destination is counted only with SHOW TABLES on the declared destination, checked in totalRows itself before the table is resolved; same gate as Alias (#114596). No caller is special-cased.
d Test intent preserved / new tests added? New stateless test: total_rows for the default user, with no privilege and with SHOW TABLES on the destination, plus DROP TABLE IF EMPTY of a buffer holding rows (refused) and of an empty one (dropped). Each arm is proven load-bearing below. No existing test changed.
e Both directions demonstrated? Master: FAIL (1007, empty buffer not dropped). Fix: OK, 50/50 with randomized settings. Gating on SELECT fails the SHOW TABLES line (7); returning NULL fails the no-privilege line (\N) and drops the buffer that holds rows.
f Fix is general across code paths? Fixed in the storage, so both system.tables branches, DROP TABLE IF EMPTY and wrappers summing a Buffer's totalRows are covered. totalBytes and the lifetime counters are the Buffer's own; a Buffer over a Buffer checks each hop.
g Fix generalizes across inputs (params/datatypes/wrappers)? Column types N/A (metadata only). Checked: table, column, db.*, role-granted and INSERT-only privileges (all count the destination), revoke, destination in another database, chained Buffer, empty and non-empty buffer for DROP TABLE IF EMPTY.
h Backward compatible? (maintainer-approved exception only) Unchanged for any user with a privilege on the destination. Only users without one see the buffer's own rows. No setting, as for Alias in #114596.
i Invariants and contracts preserved? Return value stays an engaged optional (value_or(0) as before). No lock is held around the access check. A null context does not count the destination.

Session id: cron:clickhouse-impl-self-slot-57:20261106-174000

@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

cc @azat @vitlibar, could you review this and add the can be tested label so CI runs? StorageBuffer::totalRows added the destination's row count with no access check, so system.tables disclosed the row count of a destination the user cannot see; it now counts the destination only with SHOW TABLES on it, the gate Alias uses for the same metadata.

@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [40d8294]

Summary: ✅


AI Review

Summary

StorageBuffer::totalRows now checks SHOW TABLES on the destination before resolving or counting it, while retaining the buffer's own row count and the documented DROP TABLE IF EMPTY behavior. No actionable findings. The new regression test passed in Fast test at 40d8294f39decfdcf26a9b5d4241c7e5158dda76.

Missing context / blind spots
  • ⚠️ The workflow completion report reports that dedicated bugfix-validation jobs were dropped after a configuration failure. Regression evidence consists of the passing head test and the author's documented fail-before/pass-after runs; rerunning bugfix validation would independently confirm the base-revision failure.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-simple-engines Lightweight single-node table engines: Log/StripeLog (append-only logs), Buffer (async batching),... labels Nov 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 40d8294f3 with master 93b4bc101 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 14 s compile time in total, 7 of them have a recent master baseline.

Job report

@azat azat self-assigned this Nov 8, 2026
@azat
azat added this pull request to the merge queue Nov 8, 2026
Merged via the queue into ClickHouse:master with commit 324b7a5 Nov 8, 2026
546 of 550 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 8, 2026
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-simple-engines Lightweight single-node table engines: Log/StripeLog (append-only logs), Buffer (async batching),... pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants