system.user_query_logOne issue labelled a warmup task, three implementations, and the review comments that sent each of them back to the drawing board.
system.query_log is what a UI needs for autocomplete and for server-side history in the client, but it is often closed to every user except default, and opening it to everyone would be unsafe.
A new table, system.user_query_log, which is a view over system.query_log with filtering by currentUser() and definer as the default user.
It should be possible to enable or disable this table in the server's configuration.
Additional context
Check if there could be attacks using throwIf and condition pushdown, leading to the speculative reading. Out of caution, we can put the condition to PREWHERE.
SystemUserQueryLog2026-07-12currentUser()”SQL SECURITY NONE, then an internal context with no user at all2026-06-19, 2026-07-12WHERE: a query log with ENGINE = Null rejects PREWHERE2026-06-20query_log.enable_user_query_logsurvivedthrowIf and condition pushdown — is the reason for every other row.7 pull requests by 5 authors. 3 were merged. Each of the 4 closed ones was replaced by a different design, not by a fix.
| What | Author | Opened | Closed | Outcome | Days | |
|---|---|---|---|---|---|---|
| #82539 | Issue: a new system table | alexey-milovidov | 2025-06-25 | 2026-08-02 | closed by the merge | 403 |
| #98863 | Attempt 1: a view inside a system log | svilen-mihaylov-db | 2026-03-05 | 2026-04-03 | closed, not merged | 29 |
| #104462 | Attempt 2: a view with a security barrier | niyue | 2026-05-09 | 2026-08-04 | closed, not merged | 87 |
| #110156 | Attempt 3: a table engine | alexey-milovidov | 2026-07-12 | 2026-08-02 | merged, 26.8 | 21 |
| #112847 | A barrier flag on plan steps | alexey-milovidov | 2026-08-01 | 2026-09-30 | closed, not merged | 60 |
| #122330 | A sealed plan step | alexey-milovidov | 2026-09-25 | 2026-10-01 | merged, 26.10 | 6 |
| #126334 | A marker column in the query log | skuznetsov-clickhouse | 2026-10-16 | 2026-11-18 | closed, not merged | 33 |
| #130654 | Child rows in the query log | groeneai | 2026-11-10 | 2026-11-18 | merged, 26.11 | 8 |
The table was built inside the machinery that writes system.query_log, system.trace_log and the other logs: the nearest thing with “query log” in its name.
create view system.user_query_log as
select * from system.query_log
prewhere user = currentUser()
UserQueryLog class derived from SystemLog, with its own <user_query_log> section in config.xml: partitioning, flush interval, buffer sizes.getStorage parses the statement above, creates a StorageView, starts it and attaches it to the system database. A new SystemLog::isView skips prepareTable.PREWHERE. The code carries its own doubt: // TODO: Is this correct?We really only need a view on top of the existing system.query_log table. Maybe I can recommend an alternative way to implement this: check how the views in INFORMATION_SCHEMA are defined […] do something similar for system.user_query_log […] add hooks in programs/server/Server.cpp […] and programs/local/LocalServer.cpp […] so that the view is created at program startup
Attempt #2: an ATTACH VIEW user_query_log next to the INFORMATION_SCHEMA views.hostname, current_database and query: “enough for testing”.Yue Ni's pull request had the feature working, tested and documented in its first commit. Three hours later, in the second commit, it needed a new kind of view.
CREATE VIEW system.user_query_log
DEFINER = default SQL SECURITY DEFINER AS
SELECT * FROM system.query_log
PREWHERE user = currentUser()
INFORMATION_SCHEMA views. Enabled by default; query_log.enable_user_query_log turns it off.SELECT on it implicitly, next to system.one and system.functions.system.query_log.user is LowCardinality(String). After the view applies PREWHERE user = currentUser(), the resulting rows are filtered correctly, but the LowCardinality dictionary may still contain values from the original block, including other users. If an outer predicate such as throwIf(user != currentUser()) is evaluated or partially evaluated against that dictionary representation, it can observe those filtered-out dictionary values and throw.
24 findings of the automated review, by theme. Almost every one was answered with one more guard; 5 only asked to repair documentation that an earlier fix had left stale.
| Date | Theme | Finding of the automated review | What was done |
|---|---|---|---|
| 2026-05-09 | The source table | The view hardcodes system.: a custom query_log database is ignored | Source built from the configured table |
| 2026-05-09 | The name | With query_log.table = user_query_log and the feature off, the raw log is readable by every user | Name reserved for the feature |
| 2026-05-10 | Stale docs | currentUser written without parentheses | Docs fixed |
| 2026-05-10 | The source table | Eager prepareTable can break startup when the log is in another database | Eager table creation removed |
| 2026-05-13 | The name | A pre-existing system.user_query_log is silently kept and becomes world-readable | Existing object validated, startup fails otherwise |
| 2026-05-13 | The name | The validation is skipped when query_log is not configured | Validated before the early return |
| 2026-05-13 | Stale docs | Docs still say “a view over system.query_log” | Docs updated |
| 2026-06-15 | The name | The name is still free for DDL when the feature is disabled | CREATE and RENAME to the name rejected |
| 2026-06-15 | Usability | SHOW CREATE TABLE system.user_query_log throws | View marked as system storage; CREATE VIEW rebuilt from memory |
| 2026-06-15 | The name | Asynchronous metadata loading can attach a legacy object after the check | Rejected in the common attachTable path |
| 2026-06-17 | Identity | DEFINER = default breaks if default is removed or locked down | SQL SECURITY NONE |
| 2026-06-19 | Stale docs | Docs still describe DEFINER = default | Docs updated |
| 2026-06-20 | Identity | On a shard, rows are logged as user = default: the caller cannot see them, and default sees everyone's | Filter by the initiating user |
| 2026-06-20 | Usability | PREWHERE fails on an ENGINE = Null query log | WHERE |
| 2026-06-20 | The source table | The table comment names system.query_log even for a custom table | Comment built from the configured name |
| 2026-06-20 | Stale docs | Docs still say PREWHERE | Sentence removed |
| 2026-06-21 | Side channels | additional_table_filters keyed on system.query_log injects throwIf inside the barrier | Setting rejected for barrier views |
| 2026-06-22 | Side channels | additional_result_filter does the same | Rejected too, including in Planner.cpp |
| 2026-07-03 | Side channels | The guard rejects every additional_table_filters, even for unrelated tables | Setting stripped from the inner context instead (2026-07-10) |
| 2026-07-03 | Stale docs | The setting description is stale again | Docs updated |
| 2026-07-10 | The name | The reserved name is an upgrade dead end: a legacy table can be neither read nor renamed | Documented; behaviour unchanged |
| 2026-07-12 | The source table | The first SELECT on a fresh server fails with UNKNOWN_TABLE: the log table is created lazily | prepareTable again, guarded (2026-07-27) |
| 2026-07-19 | Stale docs | The comment in config.xml is stale | Comment updated |
| 2026-07-27 | The source table | query_log.database is not honoured at all: SystemLog forces the system database | Still open when the pull request was closed |
query_log database. The last one pointed out that the server does not honour that setting at all. And the eager table creation removed on 2026-05-10 came back on 2026-07-27 as the fix for UNKNOWN_TABLE.After two months and 35 commits the pull request touched 16 source files, 11 of them in the shared engine: views, the planner, the analyzer, the interpreters, the database layer. Six comments from Alexey Milovidov, in seven minutes:
LowCardinality is removed from every exposed column typeWhy?
SELECT for every user; SQL SECURITY NONEWhy don't we require an explicit grant? […] At least an explicit grant will allow us not to give access to this table, as the queries can be resource-intensive.
Also, why do we use SQL_SECURITY NONE instead of DEFINER? As the view is created by the server, it should be equivalent, shouldn't it?
isSecurityBarrier, allowsViewInlining on every StorageViewWhat/why?
bool security_barrier;Why do we have all those complications?
readImpl rejects additional_result_filter for barrier viewsWhy?
additional_table_filters is stripped from the inner context of barrier viewsI'm sure we should approach it differently. For example, all SQL SECURITY (other than INVOKER) views can't use additional_table_filters.
After analysis nothing is left of a view but its subquery, so every optimization and every expression-injecting setting has to be told, one by one, not to cross its filter.
throwIf(query LIKE '%secret%')user = currentUser()system.query_logand(inner, outer)and runs first depends on short_circuit_function_evaluation, a setting of the callerPREWHERE and index analysis on the sourceadditional_table_filters, additional_result_filterLowCardinality dictionaries of the source blocksthrowIf(query LIKE '%secret%')LowCardinalityStorageSystemUserQueryLog resolves the configured log table at query time, runs one internal SELECT over it, and gives the result to the outer plan as a finished source.
| Concern | Attempt 2 · a view with a barrier | Attempt 3 · a table engine |
|---|---|---|
Outer WHERE, view inlining | banned in both analyzers | nothing to ban: the read is opaque |
additional_table_filters | stripped from the inner context | never applied to the inner query |
additional_result_filter | rejected, in three places | allowed: it runs above the user filter |
LowCardinality dictionaries | column types rewritten in StorageView | a converting cast ends the inner pipeline |
The name system.user_query_log | guards in CREATE, RENAME, attach, catalog | a system table: the generic rules apply |
| Reading before the first flush | UNKNOWN_TABLE | an empty result |
| Whose rights the read uses | DEFINER = default, then NONE | an internal context without a user |
StorageView, Planner, QueryAnalyzer, the interpreters, the database layerThis makes the barrier structural instead of defending each channel separately.
Alexey Milovidov's review of the pull request, 2026-07-31. The simple version took 27 commits and 12 review threads of its own. The barrier held from the first commit; the review found everything around it.
0, max_threads limits are bypassedclickhouse-local never sets the switch: ACCESS_DENIEDLocalServerProfileEvents.Names, …)query_log.database = foo slips past the name-collision checkDistributed query log: full access exists on this server onlylessOrEqual in the allow-list; the function is called lessOrEquals<= and >= are pushed at last--only-system-tables: no running logger does not mean no table<query_log> is removedEXPLAIN ANALYZE trips chassert(clock), inner processors belong to no stepread_rowsStorageSystemUserQueryLog.cppThe first version put nothing but the user filter into the internal query. Looking up one query_id from today read the whole retained log.
Because SELECT on this table is implicit for every user, this turns routine self-service queries into an unprivileged full-scan path over system.query_log. We should preserve the opaque barrier for unsafe expressions, but still push a vetted subset of safe predicates […] into the inner query
throwIf, arithmetic, a LIKE pattern taken from a cell can.today() becomes a date; a subquery or dictGet is never executed with full access.LIMIT goes inside only when there is no WHERE.event_dateevent_timeevent_time_microsecondsquery_start_timequery_start_time_microsecondsquery_idinitial_query_idtypequery_kindis_initial_queryuserinitial_usercurrent_databaseandornot=!=<<=>>=LIKENOT LIKEILIKENOT ILIKESourceStepWithFilter: the optimizer offers it the outer filter, and the step picks the conjuncts it trusts.event_date > today() + 1 prunes every part.query, exception, Settings, ProfileEvents are not on the list: their values never take part in the inner query.lessOrEqual; the function is lessOrEquals, so <= and >= were silently not pushed. An allow-list fails closed: the typo cost speed, not secrecy.Every pushed predicate narrows the scan before the user filter, so the number of rows a query reads depends on rows the caller cannot see — and read_rows of that query is in the caller's own log.
A user can run a probe such as SELECT count() FROM system.user_query_log WHERE query_id = 'victim-id', then inspect that probe query's own read_rows / runtime via their next system.user_query_log lookup […] The safest fix is to stop pushing outer predicates into the inner query and keep them above the opaque ReadFromPreparedSource barrier.
I don't think the current allow-list can make this safe: even “harmless” columns like query_id, user, initial_user, current_database, or event_date leak whether matching foreign rows exist once their effect is observable through read_rows / runtime.
This is expected but unavoidable.
read_rows or timing, a caller can learn whether rows matching a predicate over the 13 columns exist: that a query id is real, that a user was active on a date.The whole feature is this statement, built as text for every read, parsed, and run under a context that the caller cannot influence.
SELECT <the columns the caller asked for>
FROM system.query_log -- the configured query log table
WHERE if(initial_user != '', initial_user, user) = '<caller>'
AND (<vetted conjuncts of the caller's WHERE>)
LIMIT n -- only if the caller has no WHERE
-- then: cast to the declared columns, LowCardinality removed
-- then: handed to the outer plan as one source step
initial_user filter and the LowCardinality finding.SELECT is implicit while query_log.enable_user_query_log is on, which is the default.system.query_log, and gets none.SHOW CREATE TABLE shows ENGINE = SystemUserQueryLog; CREATE, RENAME and DROP are refused as for any system table.Distributed, is refused with BAD_ARGUMENTS.clickhouse-local: the table always exists; it is empty unless a query log is configured.The merged version has more code than the one it replaced. The difference is where the code lives, and who has to know about the feature afterwards.
| Pull request | Where | Lines added | Files |
|---|---|---|---|
| #104462 | wiring: attach, access control, config | 193 | 5 |
| #104462 | shared engine code | 161 | 11 |
| #110156 | the table's own storage class | 475 | 2 |
| #110156 | wiring: attach, access control, config | 60 | 6 |
StorageView, Planner, QueryAnalyzer, InterpreterCreateQuery, InterpreterRenameQuery, DatabaseCatalog, DatabaseOnDisk, …The comment of 2026-07-12 — “all SQL SECURITY (other than INVOKER) views” — became a pull request of its own, the day before the table was merged.
CREATE VIEW user_query_log
DEFINER = default SQL SECURITY DEFINER
AS SELECT * FROM system.query_log
WHERE user = currentUser();
GRANT SELECT ON user_query_log TO alice;
-- as alice, who has no grant on system.query_log
SELECT event_time FROM user_query_log
WHERE throwIf(query LIKE '%some probe%', 'DISCLOSED');
-- DB::Exception: DISCLOSED ... While executing MergeTreeSelect
-- an exception message carries the value itself
SELECT * FROM v_secrets
WHERE if(owner = 'alice', 1, toUInt8(secret)) = 1;
-- DB::Exception: Cannot parse string 'TOP-SECRET-BOB-42' as UInt8
I wonder which other cases we should cover with a security barrier beyond rows being filtered
IQueryPlanStep gets security_barrier; every step of a view that can drop rows is marked; every optimization that moves something down the plan has to look for the mark. The passes that had to learn it:filter push-downfilter mergingexpression mergingfilter into JOIN conditionPREWHEREprimary key analysisread in ordertop-Ktop-K through JOINJOIN runtime filtersLIMIT push-downprojectionsvector searchvolume-reducing functionsread_rows tells the caller whether such a row exists: ten more walks to fence.A flag is allow-by-default: each pass must remember to check it. During one merge of master, a reworked walk in updateQueryConditionCache dropped the check, and it had to be put back by hand.
I believe there could be a better fix. If we just wrap a view with SQL SECURITY into a special read step, it would not be transparent to all optimizations, which would resolve the issue. We could explicitly list these steps among the optimizations that are safe to perform (for example, limit pushdown), and this should be better and safer than explicitly checking in all other optimizations
Checks look fragile, especially the AST one, since the query may be more complex. Why not just consider all views with sql security sealed?
No, it is not for backport. A security hardening for a scenario that ought to be secure, but was not guaranteed.
| Flag on every plan step (#112847, closed) | Sealed step (#122330, merged) | |
|---|---|---|
| Lines of code added | 1,800 | 230 |
| Code files touched | 52 | 22 |
| Lines of tests added | 4,943 | 498 |
| Commits | 164 | 29 |
| Days open | 60 | 6 |
ReadFromSealedViewStep was merged on 2026-10-01, in 26.10. A view that is a plain projection of a table hides no rows and stays transparent, so it keeps its push-down.Once the views were sealed, the query log itself became the next channel: what a DEFINER view reads on behalf of the caller must be logged somewhere, and the caller reads the log.
user = currentUser()
The obvious reading of “my queries”.
if(initial_user != '',
initial_user, user)
= currentUser()
On a shard a distributed query is logged under the user of the cluster, often default. The caller could not see those rows, and default could see everyone's.
if(initial_user != '',
initial_user, user)
= '<caller>'
The same predicate, but the name is a literal taken from the client info, and the query runs without a user.
… = '<caller>'
AND is_initial_query = 1
Bodies of DEFINER views are now logged as child rows that carry the caller's initial_user and list what the view reads. A forwarded name is trusted only if it is the authenticated one. Shard rows are hidden again: backward incompatible.
The option and the query_access_info_is_hidden column exist only because the body's access info is merged into the caller's query_log row. Once merged, something must mark the row, and the marker drags in the reader filters, the NULL handling and the option that ships the fix disabled.
Instead, log the body […] as a child row, like distributed execution already does
2 is the right path to me
SQL SECURITY, asked to choose between the marker and the child rowsEach time, a design that defended the boundary check by check was replaced by one where the boundary is a structure. And each time the push came from a person asking why.
SystemLog subclass with a view inside“We really only need a view on top of the existing system.query_log table.”
security_barrier flag and a reserved name“Why do we have all those complications?”
Alexey Milovidov · 2026-07-12security_barrier flag on every plan step“If we just wrap a view with SQL SECURITY into a special read step, it would not be transparent to all optimizations”
system.query_log“The option and the […] column exist only because the body's access info is merged into the caller's query_log row.”
A list of checks has to be re-verified on every merge of master. A step the optimizer cannot see into does not.
The pushdown: 13 columns, 13 functions, constants only. The cost has a name too: read_rows.
34 threads of the automated review on the two feature pull requests found holes and got them fixed. The changes of direction above came from four human comments.
The warmup task shipped as a table that every user can read without a grant, and left behind a rule for every view that hides rows.
user_query_logsystem.query_logSQL SECURITY DEFINER and NONE views that can hide rows are read through one opaque stepSELECT event_time, query_duration_ms, read_rows, query
FROM system.user_query_log
WHERE event_date >= yesterday() -- pushed down: prunes partitions
AND type = 'QueryFinish' -- pushed down
AND query ILIKE '%join%' -- stays above the barrier
ORDER BY event_time DESC
LIMIT 10