Skip to content

Upgrade check: drop the row policies that cannot be applied to a table they cover - #126169

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:upgrade-check-drop-leftover-row-policies
Oct 19, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:upgrade-check-drop-leftover-row-policies

Conversation

@groeneai

@groeneai groeneai commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #126078
Related: #120978

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

Upgrade check: before the server is upgraded, drop the row policies left by the stress phase whose filter cannot be applied to a table they cover, so they can no longer break a background read on the upgraded server. The other row policies stay and are carried across the upgrade.

Description

Upgrade check (amd_release) failed on #126078 with Error message in clickhouse-server.log: after the upgrade, ExternalDictionariesLoader logged ... Unknown expression or function identifier `x`. In scope x = 1. (UNKNOWN_IDENTIFIER): report.

Root cause. The stress phase runs many tests in one database (--database=test_1). 02703_row_policy_for_database.sh created CREATE ROW POLICY ... ON * USING x=1 TO ALL, and its client stopped before the DROP ROW POLICY. That database-level policy filters every table of test_1, including src of 04365_dictionary_lazy_load, which has no column x. The upgraded server loads that test's eager dictionary at startup, and its source query fails. The old server failed the same way (245 query errors in the stress log): leftover test state, not an upgrade incompatibility.

Change. Before the upgrade, each row policy not from users.xml is checked with DESCRIBE TABLE view(SELECT 1 FROM <table> WHERE <filter>) in the policy's database, for every table it covers (all tables of the database for ON db.*). This only analyzes the filter: apart from those in table function arguments, its subqueries do not run, so another policy cannot fail the check. A policy is dropped only when the server fails to analyze its filter (unknown column, table or function, wrong type or arity, invalid constant), the others stay across the upgrade. Any other check failure and a failed DROP are FAIL rows.

Validation. Locally, the leaked policy reproduces the CI error after a restart. With the step, on 26.9, the policies that cannot be applied (21 error codes; database-level, restrictive, memory) are dropped, the others kept (ALL EXCEPT, quoted names, user-scoped, subqueries over a table with a broken policy); a memory limit in the check keeps them all. After a restart: 0 errors, the dictionary loads, the kept policies still filter. Each FAIL row was forced once.


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

Version info

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

The stress phase runs many tests in one shared database per worker, and a
test that is stopped before its last statement leaves its row policies
behind. A database-level one (`02703_row_policy_for_database.sh`:
`ON * USING x=1 TO ALL` on `test_1`) filters every table of that database,
so after the upgrade the eager dictionary of `04365_dictionary_lazy_load`
over `test_1.src`, which has no column `x`, fails to load in the background
with `UNKNOWN_IDENTIFIER` and the log scan fails the job (seen on ClickHouse#126078).
The old server fails the same way, so it is leftover test state, not an
upgrade incompatibility. 15 row policies were alive when the old server
stopped in that run, two of them created by the AST fuzzer.

Drop every row policy not defined in `users.xml` before the upgrade and
fail the job when one survives, as is done for leftover mutations
(ClickHouse#120978). Neutralizing them instead does not work: the test config sets
`throw_on_unmatched_row_policies`, so a table whose policies apply to
nobody throws for every reader. The statements are generated server-side
from `system.row_policies.name`, so no name passes through the shell.

The drop takes away the policies that happened to cross the upgrade, so
one is created deliberately (`k < 3` on a 10-row table) and the new
server has to apply it.

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

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) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ A row policy on system.row_policies could hide leftovers from the listing and the survivor count DISAGREE Unreachable: the only system-table policy in the suite is 05228 (ON system.one), and the AST fuzzer never changes a policy's ON target (src/Common/QueryFuzzer.cpp:9005-9027).
2 ⚠️ The fixture does not exercise the cleanup DISAGREE The survivor count after the DROP batch is the cleanup's oracle (forced locally); a self-test would test the test infrastructure.
3 💡 A client outliving the stress phase could create a policy after the DROPs are generated noted Same exposure as the mutation kill step; the FAIL row lists the survivors.
4 💡 The fixture is table-level only noted A database-level one would need its own database; one fixture, as for mutations.

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

Session id: cron:clickhouse-review-slot-11:20261016-004100

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @leshikus, could you review this? The upgrade check now drops the row policies the stress phase leaves behind before upgrading the server (a leaked ON * USING x=1 policy from 02703 broke the eager dictionary of 04365 on #126078), and carries one deliberate policy across the upgrade instead.

@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bcff35c]

Summary: ✅


AI Review

Summary

The PR removes leftover row policies whose filters cannot be analyzed against their covered tables before upgrading, while preserving other policies for upgrade coverage. The current DESCRIBE TABLE view(...) probe addresses the previous findings about executing subqueries and applying other policies during validation. No actionable findings remain at bcff35c3507b130dc05e938e117fb41c83d6b62d.

Missing context / blind spots
  • ⚠️ The current CI report contains no executed Upgrade check (amd_release) results, and no local ClickHouse executable was available to rerun the probes. Runtime evidence comes from the author's documented validation on versions 26.3 and 26.9; a completed upgrade run at the current head would provide independent end-to-end confirmation.
Final Verdict

Status: ✅ Approve. No further code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-ci comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). labels Oct 16, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 16, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai

We want to test upgrading a server with installed row policies. This is the purpose of the upgrade check. The change now defeats the point.

Can we drop only the incorrect row policies? Or, better, prevent their creation? (Otherwise, it is a usability problem - a user can create an invalid row policy, and the server will not be able to restart.)

@alexey-milovidov alexey-milovidov self-assigned this Oct 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Agreed, the policies should stay. I'll do both:

  1. In this PR, drop only the policies whose filter does not resolve against a table they cover (EXPLAIN QUERY TREE SELECT 1 FROM <table> WHERE <filter> fails). Every other policy left by the stress phase stays across the upgrade.
  2. In a separate PR, make CREATE ROW POLICY and ALTER ROW POLICY analyze the filter against the existing tables it covers (all tables of the database for ON db.*) and fail with the analyzer error, next to the arrayJoin and column matcher checks that already run there. It cannot catch a table created later under an ON db.* policy (dropping a column that a policy uses is Make Row Policies tolerate ALTER COLUMN #121944), and the previous release used by this check will not have it, so 1 is still needed.

On the impact: the server does restart (checked on master). What breaks is every read of a table the policy cannot be applied to, from the moment the policy is created: the old server in this run already failed 245 queries on it, and an eager dictionary over such a table stays FAILED.

Drop only the row policies left by the stress phase whose filter does not
resolve against a table they cover, and keep the rest across the upgrade, so
the upgraded server still has to load and apply them (review on ClickHouse#126169).

A filter is checked with `EXPLAIN QUERY TREE SELECT 1 FROM <table> WHERE
<filter>` for every table it covers (every table of the database for
`ON db.*`). Row policies are applied by the planner, not by the analyzer, so
this checks the one filter without the other policies of the table. The
deliberate fixture policy is removed: it only compensated for dropping all
policies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title Upgrade check: drop the row policies left by the stress phase Upgrade check: drop the row policies that cannot be applied to a table they cover Oct 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Point 1 is done in c4adf1a: only the policies whose filter fails that check are dropped, all the others stay across the upgrade, and the deliberate fixture policy is gone. Locally, of 12 seeded policies the 5 that cannot be applied were dropped and 7 were kept; after a restart there were no errors. The CREATE ROW POLICY check will come as a separate PR.

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Oct 18, 2026
Comment thread tests/docker_scripts/upgrade_runner.sh Resolved
Comment thread tests/docker_scripts/upgrade_runner.sh Resolved
…be failures

The filter of a row policy may name a table without a database, and such a name
resolves in the current database of the reader. The probe ran in `default`, so a
working policy like `k IN (SELECT k FROM allowed)` of a test in `test_1` failed
with `UNKNOWN_TABLE` and was dropped. Select the policy's database before
`EXPLAIN QUERY TREE`.

Any failure of the probe also fell through to `DROP`, including a failure of the
query that only generates it, a timeout and a lost connection. Drop a policy only
when the server rejects its probe (`Received exception from server`); otherwise
keep it and write a FAIL row with the client errors.

Both reported by the AI review of ClickHouse#126169.

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

Copy link
Copy Markdown
Collaborator Author

Both AI review findings are fixed in 3ac1062: the probe now runs in the policy's database, and a policy is dropped only when the server rejects its probe (any other failure keeps it and writes a FAIL row).

Comment thread tests/docker_scripts/upgrade_runner.sh Resolved
@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov, one design question about the CREATE ROW POLICY check (point 2 above) before I open the PR.

When the table is read, the filter is analyzed in each reader's context. CREATE/ALTER can only analyze it in the creator's. Which do you prefer?

  • A. Analyze the whole filter in the creator's session, the way CREATE VIEW and column DEFAULT expressions are analyzed. This catches every analyzer error. A filter whose functions check the grants or settings of whoever analyzes it is then rejected for a creator without them, even if it works for the users the policy is for. For example, getMacro requires SELECT on system.macros.
  • B. Only check that the columns the filter names exist in the table, without building its functions. This catches x = 1 on a table without x, the case of this PR, and never depends on the creator's grants. An unknown function or a type error is still reported only on read.

In both, a filter that reads another table or a dictionary (a subquery, IN t, dictGet) is not checked, because an unqualified name there resolves in the reader's current database.

I would do A.

`Received exception from server` also covers resource limits, server-side
timeouts and cancellation, which say nothing about the filter. With a memory
exception forced in the validation client (26.9.19.9), every checked policy
was dropped, the valid ones included, and no FAIL row was written.

Drop a policy only when the server rejects its probe with one of the codes an
unresolvable filter produces on `EXPLAIN QUERY TREE`, measured on 26.9.19.9:
ILLEGAL_TYPE_OF_ARGUMENT (43), UNKNOWN_FUNCTION (46), UNKNOWN_IDENTIFIER (47),
ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER (59), UNKNOWN_TABLE (60) and NO_COMMON_TYPE
(386, e.g. `has([1, 2], x)` over a `String` column). A failed `USE` gives
UNKNOWN_DATABASE or an operational code, so it never matches. Any other error
keeps the policy and is reported in the existing FAIL row, with the client
output. The `Code: N. DB::Exception: Received from` line is the same in the
26.3, 26.6, 26.8, 26.9 and master clients.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread tests/docker_scripts/upgrade_runner.sh Resolved
Comment thread tests/docker_scripts/upgrade_runner.sh Resolved
A scalar subquery of a filter is evaluated by `EXPLAIN QUERY TREE` and reads
its table with that table's row policies, so a working policy such as
`k > (SELECT max(k) FROM allowed)` failed its check, and was dropped, when
`allowed` carried a broken policy (`x = 1` over a table without `x`). The
filters without a subquery are now checked first, and a broken policy is
dropped as soon as its check fails, so the later checks no longer see it.

The check also recognizes more errors that the analysis of a broken filter
gives on 26.9 (each also fails every read of the table): 53 (`k IN (SELECT
tuple(1, 2))`), 42 (`equals(k)`), 184 (`count() > 0`), 36 (`dictGet` over a
missing dictionary, an unknown time zone), 69 (`toDecimal32(k, 100)`), 6
(`toUInt8('abc')`) and 125 (a scalar subquery returning two rows). These
policies were kept with a FAIL row before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread tests/docker_scripts/upgrade_runner.sh Resolved
`EXPLAIN QUERY TREE` executes the scalar subqueries of the filter, and their
reads apply the row policies of the tables they read as the `default` user.
So a valid policy was dropped when a broken policy covered its subquery's
table, and a valid user-scoped policy failed the check with ACCESS_DENIED
when no policy on that table covered `default`.

`DESCRIBE TABLE view(...)` analyzes the query with `only_analyze`: scalar
subqueries get type-only placeholders and the planner applies no row
policies, so each filter is checked on its own. Subqueries in table function
arguments still run; no row policy in the suite has one. Measured on 26.3.33.24 and
26.9.19.9: every filter it rejects also fails a normal read, and it also
rejects plan-level type errors (`s = 1` on a String column). A scalar
subquery that returns several rows (125) is no longer detected, so the
ordering of filters with subqueries and code 125 are removed.

The stress phase fuzzes `CREATE ROW POLICY` filters (`ast_fuzzer_any_query`),
so add the other analyzer errors a fuzzed filter gives, each measured on
26.9 to fail both the check and a normal read: 20, 38, 41, 44, 81, 153, 190,
407, 675. Reader-dependent errors (ACCESS_DENIED, an unknown custom setting)
still keep the policy and write the FAIL row.

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

Copy link
Copy Markdown
Member

@groeneai, yes, A is ok.

@alexey-milovidov alexey-milovidov 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.

Ok. The check is quite heavy and ad-hoc, but good enough for its place.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks, I'm implementing A and will link the PR here when it opens.

@groeneai

Copy link
Copy Markdown
Collaborator Author

The CREATE/ALTER ROW POLICY check (option A) is in #126643.

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-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). manual approve Manual approve required to run CI pr-ci 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