Skip to content

Fix ALTER atomicity when combining UPDATE/DELETE with RENAME COLUMN - #96022

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-alter-update-rename-atomicity
Feb 6, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-alter-update-rename-atomicity

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 5, 2026 •

Copy link
Copy Markdown
Member

Fixes #70678

When an ALTER statement combined UPDATE or DELETE with RENAME COLUMN on the same column, the RENAME would succeed even if the mutation validation failed, leaving the table in an inconsistent state.

For example:

CREATE TABLE t0 (c0 Int) ENGINE = Memory();
ALTER TABLE t0 UPDATE c0 = 1 WHERE TRUE, RENAME COLUMN c0 TO c1;
-- Error occurred, but column was renamed anyway!
INSERT INTO TABLE t0 (c0) VALUES (1);  -- Fails: No such column c0

Root cause: In InterpreterAlterQuery::executeToTable, alter commands (including RENAME) are executed BEFORE mutation commands (UPDATE/DELETE) are validated. If mutation validation fails after the rename is committed, the table schema is already changed.

The fix adds early validation that detects conflicts between RENAME COLUMN and UPDATE/DELETE operations before any changes are made:

  1. If an UPDATE references a column that is being renamed, reject early
  2. If a DELETE/UPDATE predicate references a column being renamed, reject early
  3. Operations on different columns continue to work normally

Error messages are clear and actionable:

  • "Cannot UPDATE column c0 and RENAME it in the same ALTER query. Please split into separate ALTER statements."
  • "Cannot use column c0 in DELETE predicate and RENAME it in the same ALTER query. Please split into separate ALTER statements."

Changelog category (leave one):

  • Improvement

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

In previous versions, when combining conflicting ALTER expressions with UPDATE and RENAME COLUMN, a logical error was thrown instead of a proper exception. Closes #70678.

Version info

  • Merged into: 26.2.1.336

Fixes #70678

When an ALTER statement combined UPDATE or DELETE with RENAME COLUMN
on the same column, the RENAME would succeed even if the mutation
validation failed, leaving the table in an inconsistent state.

For example:
```sql
CREATE TABLE t0 (c0 Int) ENGINE = Memory();
ALTER TABLE t0 UPDATE c0 = 1 WHERE TRUE, RENAME COLUMN c0 TO c1;
-- Error occurred, but column was renamed anyway!
INSERT INTO TABLE t0 (c0) VALUES (1);  -- Fails: No such column c0
```

Root cause: In `InterpreterAlterQuery::executeToTable`, alter commands
(including RENAME) are executed BEFORE mutation commands (UPDATE/DELETE)
are validated. If mutation validation fails after the rename is committed,
the table schema is already changed.

The fix adds early validation that detects conflicts between RENAME COLUMN
and UPDATE/DELETE operations before any changes are made:

1. If an UPDATE references a column that is being renamed, reject early
2. If a DELETE/UPDATE predicate references a column being renamed, reject early
3. Operations on different columns continue to work normally

Error messages are clear and actionable:
- "Cannot UPDATE column `c0` and RENAME it in the same ALTER query.
   Please split into separate ALTER statements."
- "Cannot use column `c0` in DELETE predicate and RENAME it in the same
   ALTER query. Please split into separate ALTER statements."

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [028c7be]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_msan, sequential, 2/2) failure
03032_dynamically_resize_filesystem_cache_2 FAIL cidb, issue
Stress test (arm_asan) failure
AddressSanitizer: stack-use-after-scope (STID: 0250-04e2) FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 5, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@alexey-milovidov alexey-milovidov self-assigned this Feb 6, 2026
@alexey-milovidov
alexey-milovidov merged commit 2505bb1 into master Feb 6, 2026
253 of 258 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-alter-update-rename-atomicity branch February 6, 2026 19:19
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 6, 2026
ALTER TABLE test_alter_atomic UPDATE c0 = 100 WHERE true, RENAME COLUMN c1 TO c2;

-- Wait for mutation to complete
SELECT sleepEachRow(0.1) FROM numbers(30) SETTINGS function_sleep_max_microseconds_per_block = 10000000 FORMAT Null;

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.

I don't understand this. Why is it using sleep to wait for a mutation instead of mutations_sync?

/// If a column is both renamed and used in UPDATE/DELETE in the same ALTER, we must fail early
/// to ensure atomicity - otherwise RENAME would succeed but UPDATE/DELETE would fail,
/// leaving the table in an unexpected state. See issue #70678.
if (!alter_commands.empty() && mutation_commands.hasNonEmptyMutationCommands())

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.

The fix is hacky and it is not fixing all possible combinations, for example this is stil an issue https://fiddle.clickhouse.com/86d0c5df-932f-4a65-acbd-c08e57e80cda

I would suggest forbidding the combination of the rename column command with anything else; anyway, we have barrier logic that will prevent it from merging with other commands during mutation execution.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi @alexey-milovidov — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-rejected Should've been rejected, but got merged. pr-improvement Pull request with some product improvements 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.

Alter update with rename unexpected behavior

5 participants