Repository navigation
Don't drop a user table that took a union system log name - #119063
alexey-milovidov wants to merge 22 commits into
Conversation
`SystemLog::prepareUnionTable` issues `CREATE OR REPLACE TABLE system.all_<log>` whenever the definition it finds on that name differs from the one it wants. Users can create tables in the `system` database, and `system.all_query_log` is exactly the name a hand-rolled union over the rotated `query_log_N` tables would take, so with `create_union_system_log_tables` enabled the server dropped such a table with all of its data, leaving only a `LOG_DEBUG` line about an "obsolete or different definition". The union tables this feature generates are proxies over the `merge` / `clusterAllReplicas` table function and hold no data, so replacing one of those is safe. Anything else is now left alone: the server logs an error naming the table and gives up on the union table for that log. Closes: #117945
|
Workflow [PR], commit [4876aca] Summary: ✅
AI ReviewSummaryThis PR closes the data-loss bug where Findings❌ Blockers
Final VerdictRequest changes. The ownership re-check itself is valuable, but the new error path should not disclose the existing object's LLVM Coverage ReportMeasured on commit 4876aca.
Changed lines: Changed C/C++ lines covered: 171/187 (91.44%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
…to go Not marking the union table broken: a flush that still finds a table on the name returns before reaching the refusal, so the message is logged once per check rather than once per flush, and the first flush after the user removes that table creates the union table. `test_recreated_on_rotation` asserted the behaviour this fixes - that a table a user put on the union name is replaced at the next rotation of the log table - so it now asserts that the table survives the rotation and that the union table returns once the user drops it.
…me-collision-117945
…t its table function The check that kept a user table on a union system log name treated every table `AS merge(...)` or `AS clusterAllReplicas(...)` as generated by the feature, so a user's own proxy over those table functions was still replaced with `CREATE OR REPLACE`. Every definition that `getCreateUnionTableQuery` produces has carried a comment ending with "It is safe to drop this table at any time: it will be recreated automatically." since the feature appeared, so that sentence is now the ownership marker, in addition to the table function. A table without it is left alone, whatever it reads from; a table that has it but a stale definition is still brought up to date, as before. The integration test now also covers user proxies `AS merge(...)` and `AS clusterAllReplicas(...)` on union names, a stale generated definition that must still be replaced, and the recreation of the union tables once the user tables are gone. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…his log The comment marker alone let a user's proxy over an unrelated table be replaced as long as it had copied the comment. `prepareUnionTable` now also requires the table function to be one of the shapes `getCreateUnionTableQuery` generates for this very log table - `merge` over the log table and its rotated versions, that `merge` wrapped into `clusterAllReplicas`, or `clusterAllReplicas` over the log table alone. The cluster name and the choice between the shapes come from the server configuration and may have changed since the table was generated, so they are not pinned and the upgrade path is unchanged. The stale fixture in `test_union_system_log_name_collision` is now a definition an older server would really have generated, and a comment-bearing proxy over an unrelated table is added to the tables that must survive. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…me-collision-117945
…me-collision-117945
|
🕵 The only red was Merged |
|
🕵 The only red is @groeneai, please land #121163 (or link whichever fix supersedes it) so this clears fleet-wide. |
|
Confirmed: #121163 is the right fix for this red, and the revert #121144 is not needed for it.
It is green in both worlds, measured rather than argued: the pristine test FAILs on #121163 is ready: its PR workflow finished green (run 35500876152), 05227 is OK in both flavours you cite here, |
|
🕵 Thanks — #121163 is merged ( |
…me-collision-117945
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119063&sha=21379464e0c388f9d1eeff8d3d07c3a0833a1338&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F6%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The only red on this pull request is
I found no open issue tracking this query, so it likely needs one. I merged Search for an existing tracking issue turned up nothing:
|
|
The fix is already in progress: #121090, "Perf CI: fail a Two judges disagree on that shard, and the one that flagged the query is not the job's own.
That #121090 pairs the two: a One caveat on |
… table's owner and replacing it `prepareUnionTable` decided whether `system.all_<log>` holds a table this feature generated, and only then ran `CREATE OR REPLACE`. Nothing tied the replacement to the object that was inspected, so between the two another session could `DROP` the generated proxy and `CREATE` a stateful table of its own on the same name, and the exchange would drop that table with its data - the silent loss this change exists to prevent, just under concurrent DDL. `InterpreterCreateQuery` now takes a `ReplacedTableCheck` callback, invoked from the replace path's pre-swap check - under the target name's `DDLGuard`, immediately before the exchange - with the `StorageID` of the table that is about to be dropped. Throwing from it aborts the replacement atomically: no exchange happens and the filled temporary table is dropped by the existing failure path. `prepareUnionTable` sets it to re-read the definition under the guard and re-apply the same ownership predicate, now factored out as `isGeneratedUnionTable`, so what is checked is what is replaced. When the re-check refuses, the outcome matches the up-front collision: the table is left alone, the reason is logged, and the union table is created on a later flush once the name is free. The regression test drives the race through the existing `create_or_replace_before_rename` failpoint: the union table's creation parks before the exchange, a user table is created on the name and filled, and after resuming the user's rows and `MergeTree` engine survive, no `_tmp_replace_*` table is left behind, and the union table takes the name again once the user's table is dropped. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Pushed @groeneai, thanks for the analysis on The only other red is |
…me-collision-117945
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119063&sha=1388bf2ba84eb51a5d0c2907de747c84abbe99e5&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F6%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The only red on this pull request is again It is unrelated to this pull request, which touches only
A ~15 ms query where a 4 ms shift crosses the bar cannot serve as a gate; it probably needs a longer-running variant or a higher per-query threshold. I found no open issue tracking it ( I merged |
|
The 22.5% is not the learned The row in this shard's The column order is fixed by The job log says why:
The learned bar for this query is 0.65, and CI printed it itself one push earlier, on this same pull request and the same shard. On
So the two reds on this pull request are two different mechanisms, which is why they point at different fixes:
The fix is therefore already in progress, and for this red it is #121189, not #121090. The perf shards on the base you just pushed ( Your design point is separate and I agree with it. Neither PR makes this query a usable gate: at a correct bar of 0.65 it takes a 65% regression to flag, so it stops detecting anything long before it stops being noisy. I posted a write-up of exactly that on #96844 half an hour before your comment, covering this query among six: the flags come from a per-build, per-query persistent code-layout offset the permutation test cannot see, the bar is learned from On the issue, you are right that there is none. I do not open issues unless asked, so tell me and I will file it with the numbers above. |
|
🕵 @groeneai, thanks - you are right, and I misread the row. In Yes, please file the tracking issue with the numbers above (the two mechanisms, the fleet-wide |
|
Filed: #121456 It holds both mechanisms (missing thresholds falling back to Two notes from re-deriving the counts cold:
|
…me-collision-117945
|
🕵 @groeneai, thanks for filing #121456 and for the The other red on that head is Merged |
…me-collision-117945
…ship rule itself The re-check of the table about to be replaced was a callback that `SystemLog::prepareUnionTable` set on its `InterpreterCreateQuery`. That does not hold in a database that replicates its DDL: the initiator only enqueues the query into the replicated log, and the DDL worker replays it on every replica from its text with a fresh interpreter, where the callback is not available, so a user table created between the classification and the replay would still have been dropped. The rule now travels with the query: a definition that declares itself a generated union table of a system log (a proxy over one of the generated table functions carrying the generated comment marker) may only replace another generated union table of the same log, whoever issues the query. That is decided in `doCreateOrReplaceTable` under the `DDLGuard` of the target name, from the query text and the stored definition of the existing table alone, so it holds identically on a replay. The predicate moves out of `SystemLog.cpp` into `SystemLogUnionTable.h` to be shared, and the `ReplacedTableCheck` callback API is removed. New stateless test covers the rule in an `Atomic` and in a `Replicated` database, where the query goes through the DDL worker replay; the failpoint-based integration test keeps covering the race in the system log flush itself. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…me-collision-117945
… spelling the table functions accept The ownership rule that `CREATE OR REPLACE` applies to a definition generated for a union system log (`system.all_query_log` and friends) only recognized string-literal arguments, so the same definition spelled as `merge(system, ...)`, `merge(currentDatabase(), ...)` or `clusterAllReplicas(default, system.query_log)` was not recognized and could replace a user table sitting on the name. For the top-level table function the table functions themselves rewrite such arguments into literals in `parseArguments` before the rule is applied (documented at the call site now), so those spellings were in fact already covered; the arguments of a `merge` nested into `clusterAllReplicas` are the exception, as the outer function keeps its inner table function as written and evaluates it remotely. `getSystemLogOfGeneratedUnionTable` now takes the query context to evaluate those itself, accepts bare identifiers wherever it accepted literals (so the rule does not depend on that order of events), and accepts the log table as one qualified name (`system.query_log` / `'system.query_log'`), which is how `clusterAllReplicas(cluster, db.table)` is stored. A stored definition is still read without a context, as the server wrote it. `05238_create_or_replace_generated_union_system_log_table` covers the non-literal spellings, including the nested one, for both the Atomic and the Replicated database. Co-Authored-By: Claude Fable 5.1 <[email protected]>
… on the union table name of its log `SystemLog::prepareUnionTable` manages only `all_<log>` in the database of the log, but the ownership predicate accepted the generated definition under any name, so `CREATE OR REPLACE TABLE report ... AS merge(currentDatabase(), '^query_log(_[0-9]+)?$') COMMENT '...'` refused to replace an unrelated user table `report`. The predicate now takes the name of the table it classifies and requires it to be the union table name of the log the definition reads. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…me-collision-117945
…d_union_system_log_table` The private CI replaces `ReplicatedMergeTree` with `SharedMergeTree`, which made the check of the kept user table in the `Replicated` database fail there (ClickHouse/clickhouse-private#73717). Check that the engine is of the `MergeTree` family instead. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…union table definition
`merge('^query_log(_[0-9]+)?$')` reads from the current database and keeps
that single argument in its definition, so `getSystemLogOfGeneratedUnionTable`
did not recognize it, and `CREATE OR REPLACE` of that spelling skipped the
pre-swap guard and dropped a user table on the union table name. With a
context, the single-argument form is now read against the current database,
the same way `TableFunctionMerge::parseArguments` resolves it. A stored
definition is still read without a context, where not recognizing it is the
safe side. `05238` covers the top-level and the nested (`clusterAllReplicas`)
single-argument forms.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Review asked whether a mixed-case `MERGE` / `ClusterAllReplicas` bypasses the ownership check of a generated union system log table. It does not: both table functions are registered case-sensitively and without aliases, so such a query fails with `UNKNOWN_FUNCTION` before anything is replaced, including a `Merge` nested into `clusterAllReplicas` (which `parseRemoteFunctionArguments` only treats as a table function through `isTableFunctionName`). Add these spellings to `05238_create_or_replace_generated_union_system_log_table` so the test pins that the user table survives them. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
| { | ||
| if (generated_union_table_log) | ||
| { | ||
| ASTPtr to_drop_create_query = DatabaseCatalog::instance() |
There was a problem hiding this comment.
Fetching to_drop_create_query and embedding formatForErrorMessage() into the TABLE_ALREADY_EXISTS error happens before the type-specific drop privilege check below. CREATE OR REPLACE TABLE only requires DROP_TABLE up front (getRequiredAccess at lines 3795-3797), so this makes a generated-shaped CREATE OR REPLACE TABLE all_query_log ... query into a SHOW CREATE oracle for whatever currently owns that name. In particular, if the name is occupied by a view or a dictionary, a user without DROP_VIEW / DROP_DICTIONARY still gets its full definition in the exception text because the throw here runs before lines 3089-3094. Please do the kind-specific access check first, or drop the existing definition from this message.
|
I would resolve it simply - by not fixing. |
A user table that happens to occupy the name of a union system log table (
system.all_query_logand friends) is no longer dropped whencreate_union_system_log_tablesis enabled.SystemLog::prepareUnionTableissuesCREATE OR REPLACE TABLE system.all_<log>whenever the existing definition on that name differs from the one it wants. The name lives in thesystemdatabase, but users can create tables there, andsystem.all_query_logis exactly the name a hand-rolled union over the rotatedquery_log_Ntables would take. With the setting enabled, the server replaced such a table on startup and the data was gone, with only aLOG_DEBUGline about an "obsolete or different definition" to show for it.The union tables this feature generates are proxies over the
merge/clusterAllReplicastable function and hold no data, so replacing one of those is safe. Anything else is now left alone: the server logs an error naming the table and skips the union table for that log. A generated table is recognised by reading from this very log table through one of the generated shapes and by the comment every one of them carries ("It is safe to drop this table at any time: it will be recreated automatically."), so a user's own proxyAS merge(...)orAS clusterAllReplicas(...)on such a name is kept too, as is a user's table that copied the comment onto a proxy over something else.The classification happens outside any
DDLGuard, so a table created on the name between it and the exchange has to be protected as well. The rule therefore travels with the query:CREATE OR REPLACEof a definition that declares itself a generated union table of a system log, on the union table name of that log (all_<log>; the same definition under any other name is an ordinary user table), may only replace another generated union table of the same log, whoever issues the query. It is decided inInterpreterCreateQuery::doCreateOrReplaceTableunder theDDLGuardof the target name, from the query text and the stored definition of the existing table alone, so it holds identically when a database that replicates its DDL replays the query from its text on the DDL worker. A user who really wants to put such a definition over a table of their own has to drop that table first. The definition is recognized in every spelling the table functions accept (identifiers,currentDatabase(),db.table, amergenested intoclusterAllReplicas), not only the literal one the server writes.Closes: #117945
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed a bug where enabling
create_union_system_log_tablessilently dropped a pre-existing user table occupying the name of a union system log table, such assystem.all_query_log, together with its data. Such a table is now kept, and an error is logged instead.Workflow [PR]
Sync PR [sync-upstream/pr/119063]